예제 #1
0
 // Returns NExprPrimary or any of subproduction nodes
 public Node ExprPrimary()
 {
     if (CurrentToken == TokenCategory.IDENTIFIER)
     {
         Token identifier = Expect(TokenCategory.IDENTIFIER);
         if (CurrentToken == TokenCategory.PARENTHESIS_LEFT)
         {
             NFunCall nFunCall = new NFunCall()
             {
                 AnchorToken = identifier
             };
             nFunCall.Add(FunCall());
             return(nFunCall);
         }
         else
         {
             NExprPrimary nExprPrimary = new NExprPrimary();
             nExprPrimary.AnchorToken = identifier;
             return(nExprPrimary);
         }
     }
     else if (firstOfLitAlt.Contains(CurrentToken))
     {
         return(LitAlt());
     }
     else if (CurrentToken == TokenCategory.PARENTHESIS_LEFT)
     {
         Expect(TokenCategory.PARENTHESIS_LEFT);
         Node resultingNode = Expr();
         Expect(TokenCategory.PARENTHESIS_RIGHT);
         return(resultingNode);
     }
     return(new Node());
 }
예제 #2
0
        // Appends NStmt to nStmtList
        public void Stmt(NStmtList nStmtList)
        {
            switch (CurrentToken)
            {
            case TokenCategory.IDENTIFIER: {
                Token tokenForAnchor = Expect(TokenCategory.IDENTIFIER);
                if (CurrentToken == TokenCategory.ASSIGN)
                {
                    NAssign nAssign = new NAssign()
                    {
                        AnchorToken = tokenForAnchor
                    };
                    nAssign.Add(Assign());
                    nStmtList.Add(nAssign);
                }
                else if (CurrentToken == TokenCategory.PARENTHESIS_LEFT)
                {
                    NFunCall nFunCall = new NFunCall()
                    {
                        AnchorToken = tokenForAnchor
                    };
                    nFunCall.Add(FunCall());
                    nStmtList.Add(nFunCall);
                }
                else
                {
                    throw new SyntaxError(new HashSet <TokenCategory>()
                        {
                            TokenCategory.ASSIGN,
                            TokenCategory.PARENTHESIS_LEFT
                        }
                                          , tokenStream.Current);
                }
                break;
            }

            case TokenCategory.IF: {
                Expect(TokenCategory.IF);
                nStmtList.Add(IfStmt());
                break;
            }

            case TokenCategory.SWITCH: {
                nStmtList.Add(SwitchStmt());
                break;
            }

            case TokenCategory.WHILE: {
                nStmtList.Add(WhileStmt());
                break;
            }

            case TokenCategory.DO: {
                nStmtList.Add(DoWhileStmt());
                break;
            }

            case TokenCategory.FOR: {
                nStmtList.Add(ForStmt());
                break;
            }

            case TokenCategory.BREAK: {
                nStmtList.Add(new NBreak()
                    {
                        AnchorToken = Expect(TokenCategory.BREAK)
                    });
                Expect(TokenCategory.SEMICOLON);
                break;
            }

            case TokenCategory.CONTINUE: {
                nStmtList.Add(new NContinue()
                    {
                        AnchorToken = Expect(TokenCategory.CONTINUE)
                    });
                Expect(TokenCategory.SEMICOLON);
                break;
            }

            case TokenCategory.RETURN: {
                Expect(TokenCategory.RETURN);
                NReturn nReturn          = new NReturn();
                Node    returnExpression = Expr();
                if (returnExpression != null)
                {
                    nReturn.Add(returnExpression);
                }
                nStmtList.Add(nReturn);
                Expect(TokenCategory.SEMICOLON);
                break;
            }

            case TokenCategory.SEMICOLON: {
                Expect(TokenCategory.SEMICOLON);
                break;
            }

            default: {
                throw new SyntaxError(firstOfStatement, tokenStream.Current);
            }
            }
        }