def _Atom(self, children): # type: (List[PNode]) -> expr_t """Handles alternatives of 'atom' where there is more than one child.""" tok = children[0].tok id_ = tok.id n = len(children) if id_ == Id.Op_LParen: # atom: '(' [yield_expr|testlist_comp] ')' | ... if n == 2: # () is a tuple assert children[1].tok.id == Id.Op_RParen, children[1] return expr.Tuple([], expr_context_e.Store) return self._TestlistComp(children[1], id_) if id_ == Id.Op_LBracket: # atom: ... | '[' [testlist_comp] ']' | ... if n == 2: # [] assert children[1].tok.id == Id.Op_RBracket, children[1] return expr.List([], expr_context_e.Store) # unused expr_context_e return self._TestlistComp(children[1], id_) if id_ == Id.Op_LBrace: return self._Dict(children[1]) if id_ == Id.Arith_Slash: r = self._Regex(children[1]) flags = [] # type: List[token] return expr.RegexLiteral(children[0].tok, r, flags) raise NotImplementedError(id_)
def _Tuple(self, children): # type: (List[PNode]) -> expr_t # NOTE: We haven't solved the 1, issue. Gah! Or () # 1, 2, 3 n = len(children) if n == 1: return self.Expr(children[0]) elts = [] for i in xrange(0, n, 2): # skip commas p_node = children[i] elts.append(self.Expr(p_node)) return expr.Tuple(elts, expr_context_e.Store) # unused expr_context_e
def _Atom(self, children): # type: (List[PNode]) -> expr_t """Handles alternatives of 'atom' where there is more than one child.""" tok = children[0].tok id_ = tok.id n = len(children) if id_ == Id.Op_LParen: # atom: '(' [yield_expr|testlist_comp] ')' | ... if n == 2: # () is a tuple assert children[1].tok.id == Id.Op_RParen, children[1] return expr.Tuple([], expr_context_e.Store) return self._TestlistComp(children[1], id_) if id_ == Id.Op_LBracket: # atom: ... | '[' [testlist_comp] ']' | ... if n == 2: # [] assert children[1].tok.id == Id.Op_RBracket, children[1] return expr.List([], expr_context_e.Store) # unused expr_context_e return self._TestlistComp(children[1], id_) if id_ == Id.Op_LBrace: # atom: ... | '{' [Op_Newline] [dict] '}' i = 1 if children[i].tok.id == Id.Op_Newline: i += 1 return self._Dict(children[i]) if id_ == Id.Arith_Slash: r = self._Regex(children[1]) flags = [] # type: List[Token] # TODO: Parse translation preference. trans_pref = None # type: Token return expr.RegexLiteral(children[0].tok, r, flags, trans_pref) if id_ == Id.Expr_Func: # STUB. This should really be a Func, not Lambda. return expr.Lambda([], expr.Implicit()) raise NotImplementedError(Id_str(id_))
def _Atom(self, children): # type: (List[PNode]) -> expr_t """Handles alternatives of 'atom' where there is more than one child.""" tok = children[0].tok id_ = tok.id if id_ == Id.Op_LParen: # atom: '(' [yield_expr|testlist_comp] ')' | ... if children[1].tok.id == Id.Op_RParen: # () is a tuple return expr.Tuple([], expr_context_e.Store) else: return self.Expr(children[1]) if id_ == Id.Op_LBracket: # atom: ... | '[' [testlist_comp] ']' | ... if len(children) == 2: # [] return expr.List([], expr_context_e.Store) # unused expr_context_e p_list = children[1].children # what's between [ and ] # [x for x in y] if children[1].typ == grammar_nt.testlist_comp: return self.Expr(children[1]) # [1, 2, 3] n = len(p_list) elts = [] for i in xrange(0, n, 2): # skip commas p_node = p_list[i] elts.append(self.Expr(p_node)) return expr.List(elts, expr_context_e.Store) # unused expr_context_e if id_ == Id.Op_LBrace: return self._Dict(children[1]) if id_ == Id.Arith_Slash: r = self._Regex(children[1]) flags = [] # type: List[token] return expr.RegexLiteral(children[0].tok, r, flags) raise NotImplementedError(id_)
def _Tuple(self, children): # type: (List[PNode]) -> expr_t n = len(children) # (x) -- not a tuple if n == 1: return self.Expr(children[0]) # x, and (x,) aren't allowed if n == 2: p_die('Write singleton tuples with tup(), not a trailing comma', token=children[1].tok) elts = [] # type: List[expr_t] for i in xrange(0, n, 2): # skip commas p_node = children[i] elts.append(self.Expr(p_node)) return expr.Tuple(elts, expr_context_e.Store) # unused expr_context_e