public Declaration Parse(LoonyParser parser, LoonyToken token) { var start = token; var name = parser.Take(LoonyTokenType.Identifier); var parameters = new List <FuncDeclaration.Parameter>(); TypeBase returnType = null; parser.Take(LoonyTokenType.LeftParen); if (!parser.MatchAndTake(LoonyTokenType.RightParen)) { do { var paramName = parser.Take(LoonyTokenType.Identifier); parser.Take(LoonyTokenType.Colon); var paramType = parser.ParseType(); parameters.Add(new FuncDeclaration.Parameter(paramName, paramType)); } while (parser.MatchAndTake(LoonyTokenType.Comma)); parser.Take(LoonyTokenType.RightParen); } if (parser.MatchAndTake(LoonyTokenType.Colon)) { returnType = parser.ParseType(); } var body = parser.ParseBlock(false); var end = parser.Previous; return(new FuncDeclaration(start, end, name, parameters, returnType, body)); }
public Declaration Parse(LoonyParser parser, LoonyToken token) { var start = token; var name = parser.Take(LoonyTokenType.Identifier); var parameters = new List<FuncDeclaration.Parameter>(); TypeBase returnType = null; parser.Take(LoonyTokenType.LeftParen); if (!parser.MatchAndTake(LoonyTokenType.RightParen)) { do { var paramName = parser.Take(LoonyTokenType.Identifier); parser.Take(LoonyTokenType.Colon); var paramType = parser.ParseType(); parameters.Add(new FuncDeclaration.Parameter(paramName, paramType)); } while (parser.MatchAndTake(LoonyTokenType.Comma)); parser.Take(LoonyTokenType.RightParen); } if (parser.MatchAndTake(LoonyTokenType.Colon)) returnType = parser.ParseType(); var body = parser.ParseBlock(false); var end = parser.Previous; return new FuncDeclaration(start, end, name, parameters, returnType, body); }
private static TypeBase Parse(string type) { var lexer = new LoonyLexer(type); var parser = new LoonyParser(lexer); return(parser.ParseType()); }
public Statement Parse(LoonyParser parser, LoonyToken token, out bool trailingSemicolon) { trailingSemicolon = true; var name = parser.Take(LoonyTokenType.Identifier).Contents; TypeBase type = null; Expression initializer = null; if (parser.MatchAndTake(LoonyTokenType.Colon)) { type = parser.ParseType(); } if (type == null) { parser.Take(LoonyTokenType.Assign); initializer = parser.ParseExpression(); } else if (parser.MatchAndTake(LoonyTokenType.Assign)) { initializer = parser.ParseExpression(); } return(new VariableStatement(token, parser.Previous, name, type, initializer)); }
public Statement Parse(LoonyParser parser, LoonyToken token, out bool trailingSemicolon) { trailingSemicolon = true; var name = parser.Take(LoonyTokenType.Identifier).Contents; TypeBase type = null; Expression initializer = null; if (parser.MatchAndTake(LoonyTokenType.Colon)) type = parser.ParseType(); if (type == null) { parser.Take(LoonyTokenType.Assign); initializer = parser.ParseExpression(); } else if (parser.MatchAndTake(LoonyTokenType.Assign)) { initializer = parser.ParseExpression(); } return new VariableStatement(token, parser.Previous, name, type, initializer); }