我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用token.LBRACE。
def parse_while_loop(self): expr = ast.WhileLoop(self.cur_tok, None, None) if not self.expect(token.LPAREN): return None self.next() expr.condition = self.parse_expr(LOWEST) if not self.expect(token.RPAREN): return None if not self.expect(token.LBRACE): return None expr.body = self.parse_block_statement() return expr
def parse_for_loop(self): expr = ast.ForLoop(self.cur_tok, None, None, None) if not self.expect(token.LPAREN): return None self.next() expr.var = self.parse_id(True) if not self.expect(token.COLON): return None self.next() expr.collection = self.parse_expr(LOWEST) if not self.expect(token.RPAREN): return None if not self.expect(token.LBRACE): return None expr.body = self.parse_block_statement() return expr
def parse_match_expr(self): expr = ast.MatchExpression(self.cur_tok, None, None) if not self.expect(token.LPAREN): return None self.next() expr.expr = self.parse_expr(LOWEST) if not self.expect(token.RPAREN): return None if not self.expect(token.LBRACE): return None expr.arms = self.parse_match_arms() if not expr.arms: return None return expr
def parse_def_stmt(self): stmt = ast.FunctionDefinition(self.cur_tok, [], None) self.next() stmt.pattern = self.parse_pattern_call(token.LBRACE) if stmt.pattern == None: return None stmt.body = self.parse_block_statement() if len(stmt.pattern) == 0: self.err("expected at least one item in a pattern") return None return stmt
def __init__(self): self._dispatch = {} for value, name in symbol.sym_name.items(): if hasattr(self, name): self._dispatch[value] = getattr(self, name) self._dispatch[token.NEWLINE] = self.com_NEWLINE self._atom_dispatch = {token.LPAR: self.atom_lpar, token.LSQB: self.atom_lsqb, token.LBRACE: self.atom_lbrace, token.BACKQUOTE: self.atom_backquote, token.NUMBER: self.atom_number, token.STRING: self.atom_string, token.NAME: self.atom_name, } self.encoding = None
def parse_if_expr(self): expr = ast.IfExpression(self.cur_tok, None, None, None) if not self.expect(token.LPAREN): return None self.next() expr.condition = self.parse_expr(LOWEST) if not self.expect(token.RPAREN): return None if not self.expect(token.LBRACE): return None expr.consequence = self.parse_block_statement() if self.peek_is(token.ELSE): self.next() if not self.expect(token.LBRACE): return None expr.alternative = self.parse_block_statement() elif self.peek_is(token.ELIF): self.next() expr.alternative = ast.BlockStatement( self.cur_tok, [ast.ExpressionStatement(self.cur_tok, self.parse_if_expr())] ) return expr
def parse_method_call(self, left): expr = ast.MethodCall(self.cur_tok, left, []) while self.peek_in(self.arg_tokens) or self.peek_in(token.keywords.values()): self.next() if self.cur_in(token.keywords.values()): expr.pattern.append(ast.Identifier(self.cur_tok)) continue arg = lambda val: ast.Argument(self.cur_tok, val) handlers = { token.ID: lambda: ast.Identifier(self.cur_tok), token.LPAREN: lambda: arg(self.parse_grouped_expr()), token.NUM: lambda: arg(self.parse_num()), token.NULL: lambda: arg(self.parse_null()), token.TRUE: lambda: arg(self.parse_bool()), token.FALSE: lambda: arg(self.parse_bool()), token.STR: lambda: arg(self.parse_string()), token.PARAM: lambda: arg(ast.Identifier(self.cur_tok)), token.LSQUARE: lambda: arg(self.parse_array_or_map()), token.LBRACE: lambda: arg(self.parse_block_literal()), } handler = handlers[self.cur_tok.type] expr.pattern.append(handler()) if len(expr.pattern) == 0: self.err("expected at least one item in a pattern") return None return expr
def parse_try_expr(self): expr = ast.TryExpression(self.cur_tok, None, None, None) if not self.expect(token.LBRACE): return None expr.body = self.parse_block_statement() if not self.expect(token.CATCH): return None if not self.expect(token.LPAREN): return None self.next() expr.err_name = self.parse_id(False) if not self.expect(token.RPAREN): return None if not self.expect(token.LBRACE): return None expr.arms = self.parse_match_arms() return expr
def parse_class_declaration(self): stmt = ast.ClassStatement(self.cur_tok, None, [], None) if not self.expect(token.ID): return None stmt.name = self.parse_id(False) if self.peek_is(token.EXTENDS): self.next() self.next() stmt.parent = self.parse_id(False) if not self.expect(token.LBRACE): return None self.next() if self.cur_is(token.RBRACE): return stmt while self.cur_in([token.INIT, token.DEF]): if self.cur_is(token.INIT): stmt.methods.append(self.parse_init_stmt()) elif self.cur_is(token.DEF): stmt.methods.append(self.parse_def_stmt()) if not self.expect(token.SEMI): return None if self.peek_in([token.INIT, token.DEF]): self.next() if not self.expect(token.RBRACE): return None return stmt
def parse_init_stmt(self): stmt = ast.InitDefinition(self.cur_tok, [], None) self.next() stmt.pattern = self.parse_pattern_call(token.LBRACE) stmt.body = self.parse_block_statement() if len(stmt.pattern) == 0: self.err("expected at least one item in a pattern") return None return stmt
def test_exact_type(self): self.assertExactTypeEqual('()', token.LPAR, token.RPAR) self.assertExactTypeEqual('[]', token.LSQB, token.RSQB) self.assertExactTypeEqual(':', token.COLON) self.assertExactTypeEqual(',', token.COMMA) self.assertExactTypeEqual(';', token.SEMI) self.assertExactTypeEqual('+', token.PLUS) self.assertExactTypeEqual('-', token.MINUS) self.assertExactTypeEqual('*', token.STAR) self.assertExactTypeEqual('/', token.SLASH) self.assertExactTypeEqual('|', token.VBAR) self.assertExactTypeEqual('&', token.AMPER) self.assertExactTypeEqual('<', token.LESS) self.assertExactTypeEqual('>', token.GREATER) self.assertExactTypeEqual('=', token.EQUAL) self.assertExactTypeEqual('.', token.DOT) self.assertExactTypeEqual('%', token.PERCENT) self.assertExactTypeEqual('{}', token.LBRACE, token.RBRACE) self.assertExactTypeEqual('==', token.EQEQUAL) self.assertExactTypeEqual('!=', token.NOTEQUAL) self.assertExactTypeEqual('<=', token.LESSEQUAL) self.assertExactTypeEqual('>=', token.GREATEREQUAL) self.assertExactTypeEqual('~', token.TILDE) self.assertExactTypeEqual('^', token.CIRCUMFLEX) self.assertExactTypeEqual('<<', token.LEFTSHIFT) self.assertExactTypeEqual('>>', token.RIGHTSHIFT) self.assertExactTypeEqual('**', token.DOUBLESTAR) self.assertExactTypeEqual('+=', token.PLUSEQUAL) self.assertExactTypeEqual('-=', token.MINEQUAL) self.assertExactTypeEqual('*=', token.STAREQUAL) self.assertExactTypeEqual('/=', token.SLASHEQUAL) self.assertExactTypeEqual('%=', token.PERCENTEQUAL) self.assertExactTypeEqual('&=', token.AMPEREQUAL) self.assertExactTypeEqual('|=', token.VBAREQUAL) self.assertExactTypeEqual('^=', token.CIRCUMFLEXEQUAL) self.assertExactTypeEqual('^=', token.CIRCUMFLEXEQUAL) self.assertExactTypeEqual('<<=', token.LEFTSHIFTEQUAL) self.assertExactTypeEqual('>>=', token.RIGHTSHIFTEQUAL) self.assertExactTypeEqual('**=', token.DOUBLESTAREQUAL) self.assertExactTypeEqual('//', token.DOUBLESLASH) self.assertExactTypeEqual('//=', token.DOUBLESLASHEQUAL) self.assertExactTypeEqual('@', token.AT) self.assertExactTypeEqual('a**2+b**2==c**2', NAME, token.DOUBLESTAR, NUMBER, token.PLUS, NAME, token.DOUBLESTAR, NUMBER, token.EQEQUAL, NAME, token.DOUBLESTAR, NUMBER) self.assertExactTypeEqual('{1, 2, 3}', token.LBRACE, token.NUMBER, token.COMMA, token.NUMBER, token.COMMA, token.NUMBER, token.RBRACE) self.assertExactTypeEqual('^(x & 0x1)', token.CIRCUMFLEX, token.LPAR, token.NAME, token.AMPER, token.NUMBER, token.RPAR)