def p_array_literal_2(self, p): """array_literal : LBRACKET element_list RBRACKET | LBRACKET element_list COMMA elision_opt RBRACKET """ items = p[2] if len(p) == 6: items.extend(p[4]) p[0] = ast.Array(items=items)
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_multi_expr(p): ''' multi_expr : expr COMMA multi_expr | expr COMMA | expr ''' if DEBUG: print("\nmulti_expr: ", end="") for i in range(len(p)): print(i, " ", p[i], " ", end="") l = len(p) if l == 4: p[0] = ast.Array(p[1], p[3]) elif l == 3: p[0] = ast.Array(p[1], None) else: p[0] = ast.Array(p[1], None)
def p_expr_array(p): ''' expr_array : LBRACKET RBRACKET | LBRACKET expr RBRACKET | LBRACKET expr COMMA RBRACKET | LBRACKET expr COMMA expr RBRACKET | LBRACKET multi_expr RBRACKET | LBRACKET COMMA multi_expr RBRACKET ''' if DEBUG: print("\nexpr_array: ", end="") if len(p) == 6: # LBRACKET expr COMMA expr RBRACKET p[0] = ast.Array(p[2], p[4]) elif len(p) == 5 and p[2] == ',': # LBRACKET COMMA multi_expr RBRACKET p[0] = ast.Array(p[3], None) # Do we want to force this ordering? elif len(p) == 5 and p[3] == ',': # LBRACKET expr COMMA RBRACKET p[0] = ast.Array(p[3], None) elif len(p) == 4: # LBRACKET expr RBRACKET p[0] = ast.Array(p[3], None) else: pass
def parse_array_expression(self): self.expect_punctuation('[') elements = [] if self.at_punctuation(']'): elements = [] else: first = self.parse_expression(False) elements.append(first) while self.at_punctuation(','): self.expect_punctuation(',') next = self.parse_expression(False) elements.append(next) self.expect_punctuation(']') return ast.Array(elements)
def parse_declr_expr(self, typ, e): ''' parse an expression as declaration; e.g. "int *x[8]" when treated as expression will be parsed as `PrefixExpr(op='*', expr=Index(array='x', index=8))`, which we want to transform into this: `Array(Pointer(typ='int'), cap=8)` ''' while is_type_modifier(e): if type(e) == ast.Index: cap = int(e.index.val) if e.index is not None else None typ = ast.Array(typ=typ, cap=cap) e = e.array else: typ = ast.Pointer(typ=typ) e = e.expr if type(e) == ast.CallExpr: return self.parse_declr_expr( ast.Function(ret=typ or 'void', args=e.args, body=None), e.func) else: return ast.Declaration(typ=typ, name=e)
def p_array_literal_1(self, p): """array_literal : LBRACKET elision_opt RBRACKET""" p[0] = ast.Array(items=p[2])
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_expr_arr(p): ''' expr : arrOpen_tok arguments arrClose_tok ''' p[0] = ast.Array(p[2])
if tok := self.match(token.STRING): return ast.String([tok], str(tok.lexeme)) if tok := self.match(token.BOOL_VALUE): return ast.Bool([tok], True if self.peek(-1).lexeme=="true" else False) toks = TokenList() # parentheses ( ... ) if toks.add(self.match(token.LPAREN)): exp = self.expression() if not toks.add(self.match(token.RPAREN)): raise ParseException("Missing closing parenthesis ).") exp.tokens.extend(toks) return exp # array values [ ... ] if toks.add(self.match(token.LBRACKET)): if toks.add(self.match(token.RBRACKET)): return ast.Array(toks, ast.ExpressionList([], [])) elements = self.parse_commata_expressions() if not toks.add(self.match(token.RBRACKET)): raise ParseException("Expected ].") return ast.Array(toks, elements) # variable name, function call, struct value, ... here, we only # parse the corresponding identifier or ... program call fallback if toks.add(self.match(token.IDENTIFIER)): identifier = self.peek(-1).lexeme if (identifier in self.symbol_tree or identifier in self.symbols_global or self.following_access()): return ast.Identifier(toks, identifier) # Program Call fallback! name = self.peek(-1).lexeme args = self.syscall_arguments(name)
def parse(self, parser, tokens): tokens.consume_expected('LBRACK') items = ListOfExpressions().parse(parser, tokens) tokens.consume_expected('RBRACK') return ast.Array(items)