예제 #1
0
def LeftIncDec(p, token, left, rbp):
    """ For i++ and i--
  """
    if left.token.type not in ('name', 'get'):
        raise tdop.ParseError("Can't assign to %r (%s)" % (left, left.token))
    token.type = 'post' + token.type
    return CompositeNode(token, [left])
예제 #2
0
def LeftIndex(p, token, left, unused_bp):
    """ index f[x+1] """
    # f[x] or f[x][y]
    if left.token.type not in ('name', 'get'):
        raise tdop.ParseError("%s can't be indexed" % left)
    index = p.ParseUntil(0)
    p.Eat("]")

    token.type = 'get'
    return CompositeNode(token, [left, index])
예제 #3
0
def LeftFuncCall(p, token, left, unused_bp):
    """ Function call f(a, b). """
    children = [left]
    # f(x) or f[i](x)
    if left.token.type not in ('name', 'get'):
        raise tdop.ParseError("%s can't be called" % left)
    while not p.AtToken(')'):
        # We don't want to grab the comma, e.g. it is NOT a sequence operator.  So
        # set the precedence to 5.
        children.append(p.ParseUntil(COMMA_PREC))
        if p.AtToken(','):
            p.Next()
    p.Eat(")")
    token.type = 'call'
    return CompositeNode(token, children)
예제 #4
0
def LeftFuncCall(p, token, left, unused_bp):
    """ Function call f(a, b). """
    args = []
    # f(x) or f[i](x)
    if not isinstance(left, demo_asdl.ArithVar):
        raise tdop.ParseError("%s can't be called" % left)
    func_name = left.name  # get a string

    while not p.AtToken(')'):
        # We don't want to grab the comma, e.g. it is NOT a sequence operator.  So
        # set the precedence to 5.
        args.append(p.ParseUntil(COMMA_PREC))
        if p.AtToken(','):
            p.Next()
    p.Eat(")")
    return demo_asdl.FuncCall(func_name, args)
예제 #5
0
def LeftIndex(p, token, left, unused_bp):
    """ index f[x+1] """
    # f[x] or f[x][y]
    if not isinstance(left, demo_asdl.ArithVar):
        raise tdop.ParseError("%s can't be indexed" % left)
    index = p.ParseUntil(0)
    if p.AtToken(':'):
        p.Next()
        end = p.ParseUntil(0)
    else:
        end = None

    p.Eat(']')

    # TODO: If you see ], then
    # 1:4
    # 1:4:2
    # Both end and step are optional

    if end:
        return demo_asdl.Slice(left, index, end, None)
    else:
        return demo_asdl.Index(left, index)
예제 #6
0
def LeftAssign(p, token, left, rbp):
    """ Normal binary operator like 1+2 or 2*3, etc. """
    # x += 1, or a[i] += 1
    if left.token.type not in ('name', 'get'):
        raise tdop.ParseError("Can't assign to %r (%s)" % (left, left.token))
    return CompositeNode(token, [left, p.ParseUntil(rbp)])
예제 #7
0
def NullIncDec(p, token, bp):
    """ ++x or ++x[1] """
    right = p.ParseUntil(bp)
    if right.token.type not in ('name', 'get'):
        raise tdop.ParseError("Can't assign to %r (%s)" % (right, right.token))
    return CompositeNode(token, [right])