Exemplo n.º 1
0
        public Node ProcedureDeclaration()
        {
            Expect(TokenCategory.PROCEDURE);
            var procedure_node = new Procedure_Declaration()
            {
                AnchorToken = Expect(TokenCategory.IDENTIFIER)
            };
            Expect(TokenCategory.PARENTHESIS_OPEN);

            procedure_node.Add(new Parameter_Declaration()
            {
                ZeroOrMore(TokenCategory.IDENTIFIER, ParameterDeclaration)
            });

            Expect(TokenCategory.PARENTHESIS_CLOSE);

            var typeNode = Optional(TokenCategory.COLON, Type, true);
            if (typeNode == null)
            {
                typeNode = new VoidTypeNode();
            }
            procedure_node.Add(typeNode);

            Expect(TokenCategory.SEMICOLON);
            procedure_node.Add(Optional(TokenCategory.CONST, () =>
            {
                var node = new Constant_List()
                {
                    AnchorToken = Expect(TokenCategory.CONST)
                };
                node.Add(OneOrMore(TokenCategory.IDENTIFIER, ConstantDeclaration));
                return node;
            }));

            procedure_node.Add(Optional(TokenCategory.VAR, () =>
            {
                var node = new Variable_Declaration()
                {
                    AnchorToken = Expect(TokenCategory.VAR)
                };
                node.Add(OneOrMore(TokenCategory.IDENTIFIER, VariableDeclaration));
                return node;
            }));

            Expect(TokenCategory.BEGIN);

            procedure_node.Add(new Statement_List()
            {
                ZeroOrMore(firstOfStatement, Statement)
            });

            Expect(TokenCategory.END);
            Expect(TokenCategory.SEMICOLON);

            return procedure_node;
        }
Exemplo n.º 2
0
        public Node ProcedureDeclaration()
        {
            var procedureNode = new Procedure_Declaration()
            {
                AnchorToken = Expect(TokenCategory.PROCEDURE)
            };

            var procedureName = new Identifier()
            {
                AnchorToken = Expect(TokenCategory.IDENTIFIER)
            };

            var declList = new Declaration_List();
            var stmtList = new Statement_List();

            Expect(TokenCategory.PARENTHESIS_OPEN);
            while (CurrentToken == TokenCategory.IDENTIFIER)
            {
                declList.Add(ParameterDeclaration());
            }
            Expect(TokenCategory.PARENTHESIS_CLOSE);

            if (CurrentToken == TokenCategory.DECL)
            {
                var declNode = new Identifier()
                {
                    AnchorToken = Expect(TokenCategory.DECL)
                };
                declNode.Add(procedureName);
                declNode.Add(Type());

                procedureNode.Add(declNode);
            }
            else
            {
                procedureNode.Add(procedureName);
            }

            Expect(TokenCategory.EOL);


            while (CurrentToken == TokenCategory.CONST)
            {
                declList.Add(ConstantDeclaration());
            }
            while (CurrentToken == TokenCategory.VAR)
            {
                declList.Add(VariableDeclaration());
            }


            stmtList.AnchorToken = (Expect(TokenCategory.BEGIN));
            while (firstOfStatement.Contains(CurrentToken))
            {
                stmtList.Add(Statement());
            }
            Expect(TokenCategory.END);
            Expect(TokenCategory.EOL);

            procedureName.Add(declList);
            procedureName.Add(stmtList);

            return(procedureNode);
        }