private static bool IsCast(ScriptParser parser) { var token = parser.Peek(); if (token.Type != ScriptTokenType.Identifier) { return(false); } if (token.Contents == "resizeable") { return(true); } var i = 1; while (parser.Match(ScriptTokenType.Dot, i) && parser.Match(ScriptTokenType.Identifier, i + 1)) { i += 2; } var next1Type = parser.Peek(i).Type; var next2Type = parser.Peek(i + 1).Type; return((next1Type == ScriptTokenType.RightParen && IsCastable(next2Type)) || (next1Type == ScriptTokenType.LeftSquare && next2Type == ScriptTokenType.RightSquare)); }
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)); }
private static BlockStatement ParseBlock(ScriptParser parser) { var statements = new List <Statement>(); var start = parser.Peek(); while (!parser.Match(ScriptTokenType.Case) && !parser.Match(ScriptTokenType.Default) && !parser.Match(ScriptTokenType.RightBrace)) { statements.Add(parser.ParseStatement()); } return(new BlockStatement(start, parser.Previous, statements, true)); }
public Declaration Parse(ScriptParser parser, ScriptToken token) { var done = false; var tokens = parser.ParseSeparatedBy(ScriptTokenType.Dot, (_, first) => { if (done) { return(null); } if (parser.Match(ScriptTokenType.Multiply)) { done = true; return(parser.Take(ScriptTokenType.Multiply)); } return(parser.Take(ScriptTokenType.Identifier)); }); var name = string.Join(".", tokens.Select(t => t.Contents)); if (!parser.MatchAndTake(ScriptTokenType.Semicolon)) { Console.WriteLine("JFjngkjasnholjhnaskl"); } return(new IncludeDeclaration(token, parser.Previous, name)); }
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 = 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 = 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 Statement Parse(ScriptParser parser, ScriptToken token, out bool trailingSemicolon) { trailingSemicolon = false; var statements = new List <Statement>(); while (!parser.Match(ScriptTokenType.RightBrace)) { statements.Add(parser.ParseStatement()); } parser.Take(ScriptTokenType.RightBrace); return(new BlockStatement(token, parser.Previous, statements)); }
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; var mainBlock = parser.ParseBlock(false); var catches = new List <TryStatement.Branch>(2); do { parser.Take(ScriptTokenType.Catch); parser.Take(ScriptTokenType.LeftParen); ScriptType type; string name; parser.ParseNamedType(out type, out name); parser.Take(ScriptTokenType.RightParen); var block = parser.ParseBlock(false); catches.Add(new TryStatement.Branch(type, name, block)); } while (parser.Match(ScriptTokenType.Catch)); return(new TryStatement(token, parser.Previous, mainBlock, catches)); }
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)); }