def p_object_literal(self, p): """object_literal : LBRACE RBRACE | LBRACE property_list RBRACE | LBRACE property_list COMMA RBRACE """ if len(p) == 3: p[0] = ast.Object() else: p[0] = ast.Object(properties=p[2])
def parse_array_or_object(self): self.next() if self.peek_is(token.COLON) or self.cur_is(token.COLON): pairs = self.parse_expr_pairs(token.RSQUARE) return ast.Object(self.cur_tok, pairs) else: return ast.Array(self.cur_tok, self.parse_expr_list(token.RSQUARE))
def p_expression_assignment(self, parse): ''' expression : ID ASSIGN expression ''' parse[0] = AST.Assignment(AST.Object(name=parse[1]), expression=parse[3])
def p_expression_object_identifier(self, parse): ''' expression : ID ''' parse[0] = AST.Object(name=parse[1])
import formatter import ast arr = ast.Array( ast.Literal('1'), ast.Literal('"foo"'), ast.Object(ast.Member('"foo"', ast.Literal('3'))), ast.Array( ast.Literal('"33"'), ast.Object( ast.Member('"foo"', ast.Object(ast.Member('"foo"', ast.Literal('3'))))))) formatter.JsonFormatter(arr).print_formatted() print('') print('') obj = ast.Object( ast.Member( '"foo"', ast.Array( ast.Literal('1'), ast.Literal('"foo"'), ast.Object(ast.Member('"foo"', ast.Literal('3'))), ast.Array( ast.Literal('"33"'), ast.Object( ast.Member( '"foo"', ast.Object(ast.Member('"foo"', ast.Literal('3'))))))))) formatter.JsonFormatter(obj).print_formatted()
def p_expression_assignment(self, parse): """ expression : ID ASSIGN expression """ parse[0] = AST.Assignment(AST.Object(name=parse[1]), expr=parse[3])
def p_expression_object_identifier(self, parse): """ expression : ID """ parse[0] = AST.Object(name=parse[1])