Esempio n. 1
0
  def atom(self, children):
    # type: (List[PNode]) -> expr_t
    """Handles alternatives of 'atom' where there is more than one child."""

    id_ = children[0].tok.id

    if id_ == Id.Op_LParen:
      # atom: '(' [yield_expr|testlist_comp] ')' | ...
      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 len(p_list) == 2 and p_list[1].typ == grammar_nt.sync_comp_for:
        elt = self.Expr(p_list[0])

        # TODO: transform 'for', 'if', etc.
        return expr.ListComp(elt, [])

      # [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

    raise NotImplementedError
Esempio n. 2
0
  def _TestlistComp(self, p_node, id0):
    # type: (PNode, Id_t) -> expr_t
    """
    testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
    """
    assert p_node.typ == grammar_nt.testlist_comp
    children = p_node.children
    n = len(children)
    if n > 1 and children[1].typ == grammar_nt.comp_for:
      elt = self.Expr(children[0])
      comp = self._CompFor(children[1])
      if id0 == Id.Op_LParen:
        return expr.GeneratorExp(elt, [comp])
      if id0 == Id.Op_LBracket:
        return expr.ListComp(elt, [comp])
      raise AssertionError()

    if id0 == Id.Op_LParen:
      if len(children) == 1:  # parenthesized expression like (x+1) or (x)
        return self.Expr(children[0])

      # (1,)  (1, 2)  etc.
      if children[1].tok.id == Id.Arith_Comma:
        return self._Tuple(children)

      raise NotImplementedError('testlist_comp')

    if id0 == Id.Op_LBracket:
      elts = []  # type: List[expr_t]
      for i in xrange(0, n, 2):  # skip commas
        elts.append(self.Expr(children[i]))

      return expr.List(elts, expr_context_e.Store)  # unused expr_context_e

    raise AssertionError(Id_str(id0))
Esempio n. 3
0
    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_)
Esempio n. 4
0
  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_)
Esempio n. 5
0
    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_))