我们从Python开源项目中,提取了以下27个代码示例,用于说明如何使用token.COMMA。
def print_stmt(self, nodelist): # print ([ test (',' test)* [','] ] | '>>' test [ (',' test)+ [','] ]) items = [] if len(nodelist) == 1: start = 1 dest = None elif nodelist[1][0] == token.RIGHTSHIFT: assert len(nodelist) == 3 \ or nodelist[3][0] == token.COMMA dest = self.com_node(nodelist[2]) start = 4 else: dest = None start = 1 for i in range(start, len(nodelist), 2): items.append(self.com_node(nodelist[i])) if nodelist[-1][0] == token.COMMA: return Print(items, dest, lineno=nodelist[0][2]) return Printnl(items, dest, lineno=nodelist[0][2])
def parse_grouped_expr(self): self.next() if self.cur_is(token.RPAREN): expr = ast.Tuple(self.cur_tok, []) return expr expr = self.parse_expr(LOWEST) if self.peek_is(token.COMMA): self.next() self.next() expr = ast.Tuple(expr.token, [expr] + self.parse_expr_list(token.RPAREN)) if type(expr) != ast.Tuple and not self.expect(token.RPAREN): return None return expr
def parse_expr_list(self, end): exprs = [] if self.cur_is(end): return exprs exprs.append(self.parse_expr(LOWEST)) while self.peek_is(token.COMMA): self.next() if self.peek_is(end): self.next() return exprs self.next() exprs.append(self.parse_expr(LOWEST)) if not self.expect(end): return None return exprs
def parse_expr_pairs(self, end): pairs = {} if self.cur_is(token.COLON): self.next() return pairs key, val = self.parse_pair() pairs[key] = val while self.peek_is(token.COMMA): self.next() if self.peek_is(end): self.next() return pairs self.next() key, val = self.parse_pair() pairs[key] = val if not self.expect(end): return None return pairs
def parse_params(self, end): params = [] if self.peek_is(end): self.next() return params self.next() params.append(self.parse_id(False)) while self.peek_is(token.COMMA): self.next() self.next() params.append(self.parse_id(False)) if not self.expect(end): return None return params
def com_assign_list(self, node, assigning): assigns = [] for i in range(1, len(node), 2): if i + 1 < len(node): if node[i + 1][0] == symbol.list_for: raise SyntaxError, "can't assign to list comprehension" assert node[i + 1][0] == token.COMMA, node[i + 1] assigns.append(self.com_assign(node[i], assigning)) return AssList(assigns, lineno=extractLineNo(node))
def com_list_constructor(self, nodelist): # listmaker: test ( list_for | (',' test)* [','] ) values = [] for i in range(1, len(nodelist)): if nodelist[i][0] == symbol.list_for: assert len(nodelist[i:]) == 1 return self.com_list_comprehension(values[0], nodelist[i]) elif nodelist[i][0] == token.COMMA: continue values.append(self.com_node(nodelist[i])) return List(values, lineno=values[0].lineno)
def com_dictorsetmaker(self, nodelist): # dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) | # (test (comp_for | (',' test)* [','])) ) assert nodelist[0] == symbol.dictorsetmaker nodelist = nodelist[1:] if len(nodelist) == 1 or nodelist[1][0] == token.COMMA: # set literal items = [] for i in range(0, len(nodelist), 2): items.append(self.com_node(nodelist[i])) return Set(items, lineno=items[0].lineno) elif nodelist[1][0] == symbol.comp_for: # set comprehension expr = self.com_node(nodelist[0]) return self.com_comprehension(expr, None, nodelist[1], 'set') elif len(nodelist) > 3 and nodelist[3][0] == symbol.comp_for: # dict comprehension assert nodelist[1][0] == token.COLON key = self.com_node(nodelist[0]) value = self.com_node(nodelist[2]) return self.com_comprehension(key, value, nodelist[3], 'dict') else: # dict literal items = [] for i in range(0, len(nodelist), 4): items.append((self.com_node(nodelist[i]), self.com_node(nodelist[i+2]))) return Dict(items, lineno=items[0][0].lineno)
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)