public Node ExpressionIdentifierOrFunctionCall() { var identifier = Expect(TokenType.IDENTIFIER); if (CurrentToken == TokenType.PARENTHESIS_OPEN) { var n1 = new FunctionCall(); n1.AnchorToken = identifier; Expect(TokenType.PARENTHESIS_OPEN); while (firstOfSimpleExpression.Contains(CurrentToken)) { n1.Add(Expression()); while (CurrentToken == TokenType.LIST) { Expect(TokenType.LIST); n1.Add(Expression()); } } Expect(TokenType.PARENTHESIS_CLOSE); return(n1); } else { return(new Identifier() { AnchorToken = identifier }); } }
public override Expression Parse(Token[] tokens, int index) { if (index < tokens.Length - 2 && tokens[index].Type == TokenType.Identifier && tokens[index + 1].Type == TokenType.RoundOpen) { var function = new FunctionCall { Identifier = tokens[index], ArgumentList = ParseSubExpression(tokens, index + 1, 1, ArgumentListParsers), StringIndex = tokens[index].Index }; function.Add(new SingleToken() { Token = function.Identifier}); if (function.ArgumentList != null) function.Add(function.ArgumentList); function.TokenLength = 1 + function.ArgumentList.TokenLength; var semiColonIndex = index + 1 + function.ArgumentList.TokenLength; if (semiColonIndex < tokens.Length) { var semiColon = tokens[semiColonIndex]; if (semiColon.Type == TokenType.SemiColon) { function.SemiColon = semiColon; function.TokenLength++; function.Add(new SingleToken() { Token = semiColon}); } } var lastToken = tokens[index + function.TokenLength - 1]; function.StringLength = lastToken.Index + lastToken.Value.Length - function.StringIndex; return function; } return null; }
public Node ExprPrimary() { switch (CurrentToken) { case TokenCategory.IDENTIFIER: var idToken = Expect(TokenCategory.IDENTIFIER); if (CurrentToken == TokenCategory.OPENEDPAR) { Expect(TokenCategory.OPENEDPAR); var node_func = new FunctionCall() { AnchorToken = idToken }; if (CurrentToken != TokenCategory.CLOSEDPAR) { node_func.Add(ExprList()); } Expect(TokenCategory.CLOSEDPAR); return(node_func); } else { var node_id = new Identifier() { AnchorToken = idToken }; return(node_id); } case TokenCategory.OPENEDBRACKET: return(Arr()); case TokenCategory.OPENEDPAR: Expect(TokenCategory.OPENEDPAR); var node_expr = Expr(); Expect(TokenCategory.CLOSEDPAR); return(node_expr); case TokenCategory.STRING: return(new StringLiteral() { AnchorToken = Expect(TokenCategory.STRING) }); case TokenCategory.CHAR: return(new CharLiteral() { AnchorToken = Expect(TokenCategory.CHAR) }); case TokenCategory.INTLITERAL: return(new IntLiteral() { AnchorToken = Expect(TokenCategory.INTLITERAL) }); default: throw new SyntaxError(firstOfExprPrimary, tokenStream.Current); } }
public Node StatementFunCall() { var result = new FunctionCall(); result.Add(FunctionCall()); Expect(TokenCategory.SEMICOL); return(result); }
public Node ExpressionPrimary() { switch (CurrentToken) { case TokenCategory.IDENTIFIER: var idToken = Expect(TokenCategory.IDENTIFIER); if (CurrentToken == TokenCategory.PARENTHESIS_OPEN) { var funCall = new FunctionCall() { AnchorToken = idToken }; funCall.Add(FunctionCall()); return(funCall); } else { return(new Identifier() { AnchorToken = idToken }); } case TokenCategory.BRACKET_OPEN: return(Array()); case TokenCategory.LIT_INT: return(new IntLiteral() { AnchorToken = Expect(TokenCategory.LIT_INT) }); case TokenCategory.LIT_CHAR: return(new CharLiteral() { AnchorToken = Expect(TokenCategory.LIT_CHAR) }); case TokenCategory.LIT_STR: return(new StringLiteral() { AnchorToken = Expect(TokenCategory.LIT_STR) }); case TokenCategory.PARENTHESIS_OPEN: Expect(TokenCategory.PARENTHESIS_OPEN); var result = Expression(); Expect(TokenCategory.PARENTHESIS_CLOSE); return(result); default: throw new SyntaxError(firstOfStatement, tokenStream.Current); } }
public Node StmtFunCall(Token id) { var funCall = new FunctionCall() { AnchorToken = id }; funCall.Add(FunCall()); Expect(TokenCategory.SEMICOLON); return(funCall); }
public Node StatementList() { var exprList = new FunctionCall(); while (FirstOfExprUnary.Contains(CurrentToken) || CurrentToken == TokenType.LIST) { if (CurrentToken == TokenType.LIST) { Expect(TokenType.LIST); } exprList.Add(Expression()); } return(exprList); }
public Node FunCall(Token identifierToken) { var functionCall = new FunctionCall() { AnchorToken = identifierToken }; Expect(TokenCategory.PARENTHESIS_OPEN); if (CurrentToken != TokenCategory.PARENTHESIS_CLOSE) { functionCall.Add(ExprList()); } Expect(TokenCategory.PARENTHESIS_CLOSE); return(functionCall); }
public Node ExprPrimary() { switch (Current) { case TokenCategory.IDENTIFIER: var id = Expect(TokenCategory.IDENTIFIER); if (Current == TokenCategory.OPEN_PARENTHESIS) { var funCall = new FunctionCall() { AnchorToken = id }; funCall.Add(FunCall()); return(funCall); } else { return(new Identifier() { AnchorToken = id }); } case TokenCategory.OPEN_SQUARE_BRACK: return(Array()); case TokenCategory.OPEN_PARENTHESIS: Expect(TokenCategory.OPEN_PARENTHESIS); var expr = Expr(); Expect(TokenCategory.CLOSE_PARENTHESIS); return(expr); case TokenCategory.INTEGER_LITERAL: case TokenCategory.STRING_LITERAL: case TokenCategory.CHARACTER_LITERAL: return(Lit()); default: throw new SyntaxError(String.Format("Syntax Error: Expected expression, given '{0}' at ({1}, {2})", stream.Current.Value, previous.Row, previous.LastIndex())); } }
public Node Stmt() { switch (CurrentToken) { case TokenCategory.IDENTIFIER: var idToken = Expect(TokenCategory.IDENTIFIER); switch (CurrentToken) { case TokenCategory.ASSIGN: Expect(TokenCategory.ASSIGN); var node = new Assignment() { AnchorToken = idToken }; node.Add(Expr()); Expect(TokenCategory.SEMICOLON); return(node); case TokenCategory.INCREMENT: Expect(TokenCategory.INCREMENT); var node_inc = new Increment() { AnchorToken = idToken }; Expect(TokenCategory.SEMICOLON); return(node_inc); case TokenCategory.DECREMENT: Expect(TokenCategory.DECREMENT); var node_dec = new Decrement() { AnchorToken = idToken }; Expect(TokenCategory.SEMICOLON); return(node_dec); case TokenCategory.OPENEDPAR: Expect(TokenCategory.OPENEDPAR); var node_func = new FunctionCall() { AnchorToken = idToken }; if (CurrentToken != TokenCategory.CLOSEDPAR) { node_func.Add(ExprList()); } Expect(TokenCategory.CLOSEDPAR); Expect(TokenCategory.SEMICOLON); return(node_func); } break; case TokenCategory.IF: return(If()); case TokenCategory.LOOP: return(Loop()); case TokenCategory.BREAK: var node_break = new Break() { AnchorToken = Expect(TokenCategory.BREAK) }; Expect(TokenCategory.SEMICOLON); return(node_break); case TokenCategory.RETURN: return(Return()); case TokenCategory.SEMICOLON: return(new StatementList() { AnchorToken = Expect(TokenCategory.SEMICOLON) }); default: throw new SyntaxError(firstOfStmt, tokenStream.Current); } throw new SyntaxError(firstOfStmt, tokenStream.Current); }