def LeftIncDec(p, token, left, rbp): # type: (tdop.Parser, tdop.Token, arith_expr_t, int) -> arith_expr_t """ For i++ and i-- """ if not isinstance(left, (arith_expr__Var, arith_expr__Index)): raise tdop.ParseError("Can't assign to %r" % left) token.type = 'post' + token.type return arith_expr__Unary(token.val, left)
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])
def NullIncDec(p, token, bp): # type: (tdop.Parser, tdop.Token, int) -> arith_expr_t """ ++x or ++x[1] """ right = p.ParseUntil(bp) if not isinstance(right, (arith_expr__Var, arith_expr__Index)): raise tdop.ParseError("Can't assign to %r" % right) return arith_expr__Unary(token.val, right)
def LeftAssign(p, token, left, rbp): # type: (tdop.Parser, tdop.Token, arith_expr_t, int) -> arith_expr_t """ Normal binary operator like 1+2 or 2*3, etc. """ # x += 1, or a[i] += 1 if not isinstance(left, (arith_expr__Var, arith_expr__Index)): raise tdop.ParseError("Can't assign to %r" % left) node = arith_expr__Binary(token.val, left, p.ParseUntil(rbp)) # For TESTING node.spids.append(42) node.spids.append(43) return node
def LeftFuncCall(p, token, left, unused_bp): """ Function call f(a, b). """ args = [] # f(x) or f[i](x) if not isinstance(left, arith_ast.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 arith_ast.FuncCall(func_name, args)
def LeftIndex(p, token, left, unused_bp): """ index f[x+1] """ # f[x] or f[x][y] if not isinstance(left, arith_ast.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 arith_ast.Slice(left, index, end, None) else: return arith_ast.Index(left, index)
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])
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)])