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 LeftTernary(p, token, left, bp): """ e.g. a > 1 ? x : y """ true_expr = p.ParseUntil(bp) p.Eat(':') false_expr = p.ParseUntil(bp) children = [left, true_expr, false_expr] return CompositeNode(token, children)
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])
def NullPrefixOp(p, token, bp): """Prefix operator. Low precedence: return, raise, etc. return x+y is return (x+y), not (return x) + y High precedence: logical negation, bitwise complement, etc. !x && y is (!x) && y, not !(x && y) """ r = p.ParseUntil(bp) return CompositeNode(token, [r])
def LeftComma(p, token, left, rbp): """ foo, bar, baz Could be sequencing operator, or tuple without parens """ r = p.ParseUntil(rbp) if left.token.type == ',': # Keep adding more children left.children.append(r) return left children = [left, r] return CompositeNode(token, children)
def LeftTernary(p, token, left, bp): """ e.g. a > 1 ? x : y """ # 0 binding power since any operators allowed until ':'. See: # # http://en.cppreference.com/w/c/language/operator_precedence#cite_note-2 # # "The expression in the middle of the conditional operator (between ? and # :) is parsed as if parenthesized: its precedence relative to ?: is # ignored." true_expr = p.ParseUntil(0) p.Eat(':') false_expr = p.ParseUntil(bp) children = [left, true_expr, false_expr] return CompositeNode(token, children)
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)
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)])
def LeftBinaryOp(p, token, left, rbp): """ Normal binary operator like 1+2 or 2*3, etc. """ return CompositeNode(token, [left, p.ParseUntil(rbp)])
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])