コード例 #1
0
ファイル: Parser.cs プロジェクト: alirazavi/nase-compiler
        /*
         * statementSequence = statement { statement }.
         */
        SyntaxTreeNode IsStatementSequence()
        {
            SyntaxTreeSequenceNode statementSequenceNode = null;
            SyntaxTreeNode statementNode;

            FilePosition position = GetInputFilePosition();
            if (null != (statementNode = IsStatement()))
            {
                statementSequenceNode = new SyntaxTreeSequenceNode(position, statementNode, statementSequenceNode);
                statementSequenceNode.AppendToLast();
            }

            SyntaxTreeNode firstStatementNode = statementSequenceNode;

            while (null != (statementNode = IsStatement()))
            {
                position = GetInputFilePosition();
                statementSequenceNode = new SyntaxTreeSequenceNode(position, statementNode, statementSequenceNode);
                statementSequenceNode.AppendToLast();
            }
            return firstStatementNode;
        }
コード例 #2
0
ファイル: Parser.cs プロジェクト: alirazavi/nase-compiler
        /*
         * declaration = typeName identifier { COMMA_SYMBOL identifier }.
         */
        SyntaxTreeNode IsDeclaration()
        {
            bool commaOccurred = false;
            SyntaxTreeNode firstDeclSequeceNode = null;
            SyntaxTreeSequenceNode declSequenceNode = null;
            SyntaxTreeNode declNode = null;
            SyntaxTreeNode typeNode;

            FilePosition position = GetInputFilePosition();
            if (null != (typeNode = IsTypeName()))
            {
                do
                {
                    Symbol currentSymbol = this._scanner.PeekSymbol();
                    if (this._symbolTable.IsIdentifierSymbol(currentSymbol))
                    {
                        declNode = new SyntaxTreeDeclarationNode(position, typeNode, currentSymbol);
                        this._symbolTable.SetDeclarationNodeLinkToSymbol(currentSymbol, declNode);
                        this._scanner.NextSymbol();
                        if (this._scanner.PeekSymbol() == Symbol.EOF)
                        {
                            return CreateErrorNode("Unexpected EOF", false);
                        }
                    }
                    else
                    {
                        return CreateErrorNode("Identifier expected after type name");
                    }

                    if (this._scanner.PeekSymbol() == Symbol.COMMA_SYMBOL)
                    {
                        position = GetInputFilePosition();
                        declSequenceNode = new SyntaxTreeSequenceNode(position, declNode, declSequenceNode);
                        declSequenceNode.AppendToLast();
                        if (!commaOccurred)
                        {
                            commaOccurred = true;
                            firstDeclSequeceNode = declSequenceNode;
                        }
                        this._scanner.NextSymbol();
                        if (this._scanner.PeekSymbol() == Symbol.EOF)
                        {
                            return CreateErrorNode("Unexpected EOF", false);
                        }
                    }
                    else if (this._scanner.PeekSymbol() == Symbol.DELIMITER_SYMBOL)
                    {
                        position = GetInputFilePosition();
                        declSequenceNode = new SyntaxTreeSequenceNode(position, declNode, declSequenceNode);
                        declSequenceNode.AppendToLast();
                        if (!commaOccurred)
                        {
                            firstDeclSequeceNode = declSequenceNode;
                        }
                    }
                    else
                    {
                        return CreateErrorNode("\',\' or \';\' expected after an identifier in a declaration");
                    }
                } while (Symbol.DELIMITER_SYMBOL != this._scanner.PeekSymbol());
            }

            return firstDeclSequeceNode;
        }