Exemple #1
0
    def _Trailer(self, base, p_trailer):
        # type: (expr_t, PNode) -> expr_t
        children = p_trailer.children
        op_tok = children[0].tok

        if op_tok.id == Id.Op_LParen:
            p_args = children[1]

            # NOTE: This doesn't take into account kwargs and so forth.
            if p_args.children is not None:
                # a, b, c -- every other one is a comma
                arglist = children[1].children[::2]
            else:
                arg = children[1]
                arglist = [arg]
            return expr.FuncCall(base, [self.Expr(a) for a in arglist])

        if op_tok.id == Id.Op_LBracket:
            p_args = children[1]

            # NOTE: This doens't take into account slices
            if p_args.children is not None:
                # a, b, c -- every other one is a comma
                arglist = children[1].children[::2]
            else:
                arg = children[1]
                arglist = [arg]
            return expr.Subscript(base, [self.Expr(a) for a in arglist])

        if op_tok.id == Id.Expr_Dot:
            #return self._GetAttr(base, nodelist[2])
            raise NotImplementedError

        raise AssertionError(op_tok)
Exemple #2
0
    def trailer(self, base, p_trailer):
        # type: (expr_t, PNode) -> expr_t
        children = p_trailer.children
        op_tok = children[0].tok

        # TODO: Need to process ALL the trailers, e.g. f(x, y)[1, 2](x, y)

        if op_tok.id == Id.Op_LParen:
            args = []  # type: List[expr_t]
            if len(children) == 2:  # ()
                return expr.FuncCall(base, args)

            p = children[1]  # the X in ( X )
            # NOTE: The "no singleton" rule causes some complication here.
            if p.typ == grammar_nt.arglist:  # f(x, y)
                self._Arglist(p.children, args)
            else:  # f(1+2)
                args.append(self.Expr(p))
            return expr.FuncCall(base, args)

        if op_tok.id == Id.Op_LBracket:
            p_args = children[1]

            # NOTE:
            # - This doesn't take into account slices
            # - Similar to _Arglist.
            if p_args.children is not None:
                # a, b, c -- every other one is a comma
                arglist = children[1].children[::2]
            else:
                arg = children[1]
                arglist = [arg]
            return expr.Subscript(base, [self.Expr(a) for a in arglist])

        if op_tok.id == Id.Expr_Dot:
            #return self._GetAttr(base, nodelist[2])
            raise NotImplementedError

        raise AssertionError(op_tok)
Exemple #3
0
  def _Trailer(self, base, p_trailer):
    # type: (expr_t, PNode) -> expr_t
    """
    trailer: (
      '(' [arglist] ')' | '[' subscriptlist ']'
    | '.' NAME | '->' NAME | '::' NAME
    )
    """
    children = p_trailer.children
    op_tok = children[0].tok

    # TODO: Need to process ALL the trailers, e.g. f(x, y)[1, 2](x, y)

    if op_tok.id == Id.Op_LParen:
      arglist = arg_list()
      if len(children) == 2:  # ()
        return expr.FuncCall(base, arglist)

      p = children[1]  # the X in ( X )
      assert p.typ == grammar_nt.arglist  # f(x, y)
      self._Arglist(p.children, arglist)
      return expr.FuncCall(base, arglist)

    if op_tok.id == Id.Op_LBracket:
      p_args = children[1]
      assert p_args.typ == grammar_nt.subscriptlist
      indices = []  # type: List[expr_t]
      n = len(p_args.children)
      for i in xrange(0, n, 2):  # was children[::2]
        a = p_args.children[i]
        indices.append(self._Subscript(a.children))
      return subscript(base, indices)

    if op_tok.id in (Id.Expr_Dot, Id.Expr_RArrow, Id.Expr_DColon):
      attr = children[1].tok  # will be Id.Expr_Name
      return attribute(base, op_tok, attr, expr_context_e.Store)

    raise AssertionError(Id_str(op_tok.id))
Exemple #4
0
  def _Trailer(self, base, p_trailer):
    # type: (expr_t, PNode) -> expr_t
    """
    trailer: (
      '(' [arglist] ')' | '[' subscriptlist ']'
    | '.' NAME | '->' NAME | '::' NAME
    )
    """
    children = p_trailer.children
    op_tok = children[0].tok

    # TODO: Need to process ALL the trailers, e.g. f(x, y)[1, 2](x, y)

    if op_tok.id == Id.Op_LParen:
      args = []  # type: List[expr_t]
      if len(children) == 2:  # ()
        return expr.FuncCall(base, args)

      p = children[1]  # the X in ( X )
      assert p.typ == grammar_nt.arglist  # f(x, y)
      self._Arglist(p.children, args)
      return expr.FuncCall(base, args)

    if op_tok.id == Id.Op_LBracket:
      p_args = children[1]

      if p_args.typ == grammar_nt.subscriptlist:
        # a, b, c -- every other one is a comma
        arglist = p_args.children[::2]
      else:
        arglist = [p_args]
      return expr.Subscript(base, [self.Expr(a) for a in arglist])

    if op_tok.id in (Id.Expr_Dot, Id.Expr_RArrow, Id.Expr_DColon):
      attr = children[1].tok  # will be Id.Expr_Name
      return expr.Attribute(base, op_tok, attr, expr_context_e.Store)

    raise AssertionError(op_tok)