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 _DictPair(self, p_node): # type: (PNode) -> Tuple[expr_t, expr_t] """ dict_pair: ( Expr_Name [':' test] | '[' testlist ']' ':' test ) """ assert p_node.typ == grammar_nt.dict_pair children = p_node.children typ = children[0].typ if ISNONTERMINAL(typ): # for sq_string # Note: Could inline these cases instead of going through self.Expr. if typ == grammar_nt.sq_string: key = self.Expr(children[0]) # type: expr_t elif typ == grammar_nt.dq_string: key = self.Expr(children[0]) value = self.Expr(children[2]) return key, value tok0 = children[0].tok id_ = tok0.id if id_ == Id.Expr_Name: key = expr.Const(tok0) if len(children) >= 3: value = self.Expr(children[2]) else: value = expr.Implicit() if id_ == Id.Op_LBracket: # {[x+y]: 'val'} key = self.Expr(children[1]) value = self.Expr(children[4]) return key, value return key, value