Syntax ParseFactor() { var token = Read(); switch (token.Type) { case TokenType.Identifier: if (Match(TokenType.OpenParenthese)) { Read(); List <Syntax> arguments = new List <Syntax>(); while (!Match(TokenType.CloseParenthese)) { var argument = ParseBinaryExpression(0); arguments.Add(argument); if (!Match(TokenType.CloseParenthese)) { Expect(TokenType.Comma); } } Expect(TokenType.CloseParenthese); return(new FunctionExpression(token.Contents, arguments, token)); } else { return(new IdentifierExpression(token.Contents, token)); } case TokenType.Field: return(new FieldExpression(token.Contents, token)); case TokenType.OpenParenthese: Syntax parenthesizedExpression = ParseBinaryExpression(0); Expect(TokenType.CloseParenthese); return(parenthesizedExpression); case TokenType.DecimalLiteral: case TokenType.BooleanLiteral: case TokenType.StringLiteral: case TokenType.DateTimeLiteral: case TokenType.Null: return(new LiteralExpression(token.Contents, token)); default: throw ScriptError.UnexpectedToken(token, token.Contents); } }
public Token Expect(params TokenType[] types) { if (Index >= Tokens.Count) { throw ScriptError.UnexpectedEOF(null); } foreach (var type in types) { if (Tokens[Index].Type == type) { var token = Tokens[Index]; Index++; return(token); } } throw ScriptError.UnexpectedToken(Tokens[Index], Tokens[Index].Contents); }