public Expression Parse(ScriptParser parser, ScriptToken token) { var type = string.Join(".", parser.ParseSeparatedBy(ScriptTokenType.Dot, (_, first) => parser.Take(ScriptTokenType.Identifier).Contents)); if (type == "string") { type = "String"; } var isArray = parser.Match(ScriptTokenType.LeftSquare); parser.Take(isArray ? ScriptTokenType.LeftSquare : ScriptTokenType.LeftParen); var parameters = parser.ParseSeparatedBy(ScriptTokenType.Comma, (_, first) => { if (first && parser.Match(isArray ? ScriptTokenType.RightSquare : ScriptTokenType.RightParen)) { return(null); } return(parser.ParseExpression()); }).ToList(); parser.Take(isArray ? ScriptTokenType.RightSquare : ScriptTokenType.RightParen); List <Expression> values = null; if (isArray && parser.MatchAndTake(ScriptTokenType.LeftBrace)) { values = parser.ParseSeparatedBy(ScriptTokenType.Comma, (_, first) => { if (parser.Match(ScriptTokenType.RightBrace)) { return(null); } return(parser.ParseExpression()); }).ToList(); parser.MatchAndTake(ScriptTokenType.RightBrace); } int arrayDimensions = isArray ? 1 : 0; if (isArray) { while (parser.Match(ScriptTokenType.LeftSquare) && parser.Match(ScriptTokenType.RightSquare, 1)) { parser.Take(ScriptTokenType.LeftSquare); parser.Take(ScriptTokenType.RightSquare); arrayDimensions++; } } return(new NewExpression(token, parser.Previous, type, arrayDimensions, parameters, values)); }
public Expression Parse(ScriptParser parser, ScriptToken token) { if (IsCast(parser)) { var type = parser.ParseType(); parser.Take(ScriptTokenType.RightParen); var value = parser.ParseExpression((int)PrecedenceValue.Cast - 1); return(new CastExpression(token, type, value)); } var expression = parser.ParseExpression(); parser.Take(ScriptTokenType.RightParen); return(expression); }
public Declaration Parse(ScriptParser parser, ScriptToken token) { var returnType = parser.ParseType(token); var name = parser.Take(ScriptTokenType.Identifier).Contents; if (parser.MatchAndTake(ScriptTokenType.Assign)) { var value = parser.ParseExpression(); parser.Take(ScriptTokenType.Semicolon); return(new FieldDeclaration(token, parser.Previous, returnType, name, value, false, false)); } parser.Take(ScriptTokenType.LeftParen); var parameters = parser.ParseSeparatedBy(ScriptTokenType.Comma, (_, first) => { if (parser.Match(ScriptTokenType.RightParen)) { return(null); } ScriptType paramType; string paramName; parser.ParseNamedType(out paramType, out paramName); return(new MethodDeclaration.Parameter(paramType, paramName)); }).ToList(); parser.Take(ScriptTokenType.RightParen); var block = parser.ParseBlock(false); return(new MethodDeclaration(token, parser.Previous, returnType, name, parameters, block)); }
public Statement Parse(ScriptParser parser, ScriptToken token, out bool trailingSemicolon) { trailingSemicolon = true; var baseType = parser.ParseType(token); var definitions = parser.ParseSeparatedBy(ScriptTokenType.Comma, (_, first) => { var name = parser.Take(ScriptTokenType.Identifier).Contents; var arrayDims = 0; while (parser.MatchAndTake(ScriptTokenType.LeftSquare)) { parser.Take(ScriptTokenType.RightSquare); arrayDims++; } var type = baseType; if (arrayDims > 0) { type = new ScriptType(baseType.Name, baseType.ArrayDimensions + arrayDims, baseType.IsResizable); } Expression value = null; if (parser.MatchAndTake(ScriptTokenType.Assign)) { value = parser.ParseExpression(); } return(new VariableStatement.Definition(type, name, value)); }).ToList(); return(new VariableStatement(token, parser.Previous, baseType, false, definitions)); }
public Expression Parse(ScriptParser parser, Expression left, ScriptToken token) { var index = parser.ParseExpression(); parser.Take(ScriptTokenType.RightSquare); return(new IndexerExpression(parser.Previous, left, index)); }
public Statement Parse(ScriptParser parser, ScriptToken token, out bool trailingSemicolon) { trailingSemicolon = true; var value = parser.ParseExpression(); return(new ThrowStatement(token, value)); }
public Statement Parse(ScriptParser parser, ScriptToken token, out bool trailingSemicolon) { trailingSemicolon = false; parser.Take(ScriptTokenType.LeftParen); var condition = parser.ParseExpression(); parser.Take(ScriptTokenType.RightParen); var block = parser.ParseBlock(); return(new WhileStatement(token, parser.Previous, condition, block)); }
public Statement Parse(ScriptParser parser, ScriptToken token, out bool trailingSemicolon) { trailingSemicolon = true; if (parser.Match(ScriptTokenType.Semicolon)) { return(new ReturnStatement(token, parser.Previous, null)); } var value = parser.ParseExpression(); return(new ReturnStatement(token, parser.Previous, value)); }
public Declaration Parse(ScriptParser parser, ScriptToken token) { ScriptType type; string name; parser.ParseNamedType(out type, out name); parser.Take(ScriptTokenType.Assign); var value = parser.ParseExpression(); parser.Take(ScriptTokenType.Semicolon); return(new FieldDeclaration(token, parser.Previous, type, name, value, true, true)); }
public Expression Parse(ScriptParser parser, ScriptToken token) { var values = parser.ParseSeparatedBy(ScriptTokenType.Comma, (_, first) => { if (parser.Match(ScriptTokenType.RightBrace)) { return(null); } return(parser.ParseExpression()); }).ToList(); parser.Take(ScriptTokenType.RightBrace); return(new ArrayInitializerExpression(token, parser.Previous, values)); }
public Expression Parse(ScriptParser parser, Expression left, ScriptToken token) { var parameters = parser.ParseSeparatedBy(ScriptTokenType.Comma, (_, first) => { if (first && parser.Match(ScriptTokenType.RightParen)) { return(null); } return(parser.ParseExpression()); }).ToList(); parser.Take(ScriptTokenType.RightParen); return(new CallExpression(parser.Previous, left, parameters)); }
public Statement Parse(ScriptParser parser, ScriptToken token, out bool trailingSemicolon) { trailingSemicolon = false; parser.Take(ScriptTokenType.LeftParen); List <Statement> initializers = null; if (!parser.Match(ScriptTokenType.Semicolon)) { initializers = parser.ParseSeparatedBy(ScriptTokenType.Comma, (_, first) => { var stmt = parser.ParseStatement(false); if (!(stmt is VariableStatement) && !(stmt is NakedStatement)) { throw new CompilerException(token, "bad for loop initializer"); } return(stmt); }).ToList(); } parser.Take(ScriptTokenType.Semicolon); Expression condition = null; if (!parser.Match(ScriptTokenType.Semicolon)) { condition = parser.ParseExpression(); } parser.Take(ScriptTokenType.Semicolon); List <Expression> increment = null; if (!parser.Match(ScriptTokenType.RightParen)) { increment = parser.ParseSeparatedBy(ScriptTokenType.Comma, (_, first) => parser.ParseExpression()).ToList(); } parser.Take(ScriptTokenType.RightParen); var block = parser.ParseBlock(); return(new ForStatement(token, parser.Previous, initializers, condition, increment, block)); }
public Statement Parse(ScriptParser parser, ScriptToken token, out bool trailingSemicolon) { trailingSemicolon = false; var first = true; var branches = new List <IfStatement.Branch>(); IfStatement.Branch defaultBranch = null; do { var isDefaultBranch = !first && !parser.MatchAndTake(ScriptTokenType.If); first = false; Expression condition = null; if (!isDefaultBranch) { parser.Take(ScriptTokenType.LeftParen); condition = parser.ParseExpression(); parser.Take(ScriptTokenType.RightParen); } var block = parser.ParseBlock(); var branch = new IfStatement.Branch(condition, block); if (isDefaultBranch) { defaultBranch = branch; } else { branches.Add(branch); } if (isDefaultBranch) { break; } } while (parser.MatchAndTake(ScriptTokenType.Else)); return(new IfStatement(token, parser.Previous, branches, defaultBranch)); }
public Statement Parse(ScriptParser parser, ScriptToken token, out bool trailingSemicolon) { trailingSemicolon = true; ScriptType type; string name; parser.ParseNamedType(out type, out name); parser.Take(ScriptTokenType.Assign); var value = parser.ParseExpression(); var definitions = new List <VariableStatement.Definition>() { new VariableStatement.Definition(type, name, value) }; return(new VariableStatement(token, parser.Previous, type, true, definitions)); }
public static void Main(string[] args) { try { var str = "(1 + 1) * 2 = 4 or null + 1 is null and 1 + 1 in (1, 2, 3)"; var expected = "((((1 + 1) * 2) = 4) or (((null + 1) is null) and ((1 + 1) in (1, 2, 3))))"; var actual = ScriptParser.ParseExpression(str).ToString(); Console.WriteLine(actual); if (expected != actual) { throw new Exception( string.Format( "Expected not equal actual:{1}{0}{1}!={1}{2}", expected, Environment.NewLine, actual)); } } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e.ToString()); } }
public Statement Parse(ScriptParser parser, ScriptToken token, out bool trailingSemicolon) { trailingSemicolon = false; parser.Take(ScriptTokenType.LeftParen); var expression = parser.ParseExpression(); parser.Take(ScriptTokenType.RightParen); parser.Take(ScriptTokenType.LeftBrace); var hasDefault = false; var branches = new List <SwitchStatement.Branch>(); while (!parser.Match(ScriptTokenType.RightBrace)) { var conditions = new List <Expression>(); while (true) { if (parser.MatchAndTake(ScriptTokenType.Case)) { var condition = parser.ParseExpression(); conditions.Add(condition); parser.Take(ScriptTokenType.Colon); continue; } if (!parser.Match(ScriptTokenType.Default)) { break; } var defaultToken = parser.Take(ScriptTokenType.Default); if (hasDefault) { throw new CompilerException(defaultToken, "Multiple default labels"); } conditions.Add(null); // special default condition hasDefault = true; parser.Take(ScriptTokenType.Colon); } if (conditions.Count > 0) { var block = ParseBlock(parser); var branch = new SwitchStatement.Branch(conditions, block); branches.Add(branch); continue; } var errorToken = parser.Peek(); throw new CompilerException(errorToken, "Expected Case or Default but found {0}", errorToken); } parser.Take(ScriptTokenType.RightBrace); return(new SwitchStatement(token, parser.Previous, expression, branches)); }
public Expression Parse(ScriptParser parser, Expression left, ScriptToken token) { var right = parser.ParseExpression(Precedence - (_isRight ? 1 : 0)); return(new BinaryOperatorExpression(token.Type, left, right)); }
public Expression Parse(ScriptParser parser, ScriptToken token) { var right = parser.ParseExpression(_precedence); return(new PrefixOperatorExpression(token, right)); }