// power: atom trailer* ['**' factor] private Expression ParsePower() { Expression ret = ParsePrimary(); ret = AddTrailers(ret); if (MaybeEat(TokenKind.Power)) { var start = ret.StartIndex; ret = new BinaryExpression(PythonOperator.Power, ret, ParseFactor()); ret.SetLoc(_globalParent, start, GetEnd()); } return ret; }
private Expression ParseExpr(int precedence) { Expression ret = ParseFactor(); while (true) { Token t = PeekToken(); OperatorToken ot = t as OperatorToken; if (ot == null) return ret; int prec = ot.Precedence; if (prec >= precedence) { NextToken(); Expression right = ParseExpr(prec + 1); var start = ret.StartIndex; ret = new BinaryExpression(GetBinaryOperator(ot), ret, right); ret.SetLoc(_globalParent, start, GetEnd()); } else { return ret; } } }
// power: atom trailer* ['**' factor] private Expression ParsePower() { Expression ret = ParsePrimary(); ret = AddTrailers(ret); if (MaybeEat(TokenKind.Power)) { SourceLocation start = ret.Start; ret = new BinaryExpression(PythonOperator.Power, ret, ParseFactor()); ret.SetLoc(start, GetEnd()); } return ret; }
//comparison: expr (comp_op expr)* //comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' private Expression ParseComparison() { Expression ret = ParseExpr(); while (true) { PythonOperator op; switch (PeekToken().Kind) { case TokenKind.LessThan: NextToken(); op = PythonOperator.LessThan; break; case TokenKind.LessThanOrEqual: NextToken(); op = PythonOperator.LessThanOrEqual; break; case TokenKind.GreaterThan: NextToken(); op = PythonOperator.GreaterThan; break; case TokenKind.GreaterThanOrEqual: NextToken(); op = PythonOperator.GreaterThanOrEqual; break; case TokenKind.Equals: NextToken(); op = PythonOperator.Equal; break; case TokenKind.NotEquals: NextToken(); op = PythonOperator.NotEqual; break; case TokenKind.LessThanGreaterThan: NextToken(); op = PythonOperator.NotEqual; break; case TokenKind.KeywordIn: NextToken(); op = PythonOperator.In; break; case TokenKind.KeywordNot: NextToken(); Eat(TokenKind.KeywordIn); op = PythonOperator.NotIn; break; case TokenKind.KeywordIs: NextToken(); if (MaybeEat(TokenKind.KeywordNot)) { op = PythonOperator.IsNot; } else { op = PythonOperator.Is; } break; default: return ret; } Expression rhs = ParseComparison(); BinaryExpression be = new BinaryExpression(op, ret, rhs); be.SetLoc(_globalParent, ret.StartIndex, GetEnd()); ret = be; } }
private Expression ParseExpr(int precedence) { Expression ret = ParseFactor(); while (true) { Token t = PeekToken(); OperatorToken ot = t as OperatorToken; if (ot == null) return ret; int prec = ot.Operator.Precedence; if (prec >= precedence) { NextToken(); Expression right = ParseExpr(prec + 1); Location start = ret.Start; ret = new BinaryExpression((BinaryOperator)ot.Operator, ret, right); ret.SetLoc(GetExternal(), start, GetEnd()); } else { return ret; } } }