Пример #1
0
        protected virtual StatementSequenceNode ParseStatementSequence(IStatementParentNode parent, Token token)
        {
            // PARSE: (<expression> ['.' [<statements>]])
            StatementSequenceNode result = new StatementSequenceNode(parent);

            ExpressionNode expression = this.ParseExpression(result, token);

            if (expression == null)
            {
                this.ReportParserError(result, SemanticErrors.MissingExpression, token);
            }

            SpecialCharacterToken period = null;

            token = this.GetNextTokenxx(Preference.Default);
            if (Parser.IsStatementDelimiter(token))
            {
                period = (SpecialCharacterToken)token;
            }
            else
            {
                result.SetContents(expression, null, null);
                this.ResidueToken = token;
                return(result);
            }

            // ['.' [<statements>]])
            token = this.GetNextTokenxx(Preference.NegativeSign);
            if (token is EofToken)
            {
                result.SetContents(expression, period, null);
                this.ResidueToken = token;
                return(result);
            }
            if (Parser.IsBlockEndDelimiter(token))
            {
                result.SetContents(expression, period, null);
                this.ResidueToken = token;
                return(result);
            }
            else if (Parser.IsStatementDelimiter(token))
            {
                this.ReportParserError(result, SemanticErrors.MissingStatement, token);
                result.SetContents(expression, period, null);
                this.ResidueToken = token;
                return(result);
            }

            StatementNode nextStatement = this.ParseStatement(result, token);

            result.SetContents(expression, period, nextStatement);
            return(result);
        }
Пример #2
0
        protected virtual ReturnStatementNode ParseReturnStatement(IStatementParentNode parent, ReturnOperatorToken returnOperator)
        {
            // PARSE: <return statement> ::= returnOperator <expression>
            // Also: <statements> ::= <return statement> [’.’]
            ReturnStatementNode result = new ReturnStatementNode(parent, returnOperator);

            Token          token      = this.GetNextTokenxx(Preference.NegativeSign);
            ExpressionNode expression = this.ParseExpression(result, token);

            if (expression == null)
            {
                this.ReportParserError(result, SemanticErrors.MissingExpression, token);
            }

            SpecialCharacterToken period = null;

            token = this.GetNextTokenxx(Preference.Default);
            if (Parser.IsStatementDelimiter(token))
            {
                period = (SpecialCharacterToken)token;
                token  = this.GetNextTokenxx(Preference.Default);
            }

            result.SetContents(expression, period);

            // Returns statement ... should have reached the end of the statement.
            this.ResidueToken = token;
            if (token is EofToken)
            {
                return(result); // OK
            }
            else if (Parser.IsBlockEndDelimiter(token))
            {
                return(result); // OK;
            }
            else if (Parser.IsStatementDelimiter(token))
            {
                this.ReportParserError(result, SemanticErrors.MissingStatement, token);
            }
            else
            {
                this.ReportParserError(result, SemanticErrors.CodeAfterReturnStatement, token);
            }
            return(result);
        }
Пример #3
0
        protected virtual StatementNode ParseStatement(IStatementParentNode parent, Token token)
        {
            // PARSE: <statements> ::=
            //      (<return statement> ['.'] ) |
            //      (<expression> ['.' [<statements>]])

            if ((token is EofToken) || Parser.IsBlockEndDelimiter(token))
            {
                this.ResidueToken = token;
                return(null);
            }

            if (token is ReturnOperatorToken)
            {
                return(this.ParseReturnStatement(parent, (ReturnOperatorToken)token));
            }
            else
            {
                return(this.ParseStatementSequence(parent, token));
            }
        }
Пример #4
0
        protected virtual BlockNode ParseBlock(IPrimaryParentNode parent, SpecialCharacterToken leftBracket)
        {
            // PARSE: <block constructor> ::= '[' <block body> ']'
            //      <block body> ::= [<block argument>* '|'] [<temporaries>] [<statements>]
            //      <block argument> ::= ':' identifier
            BlockNode result = new BlockNode(parent, leftBracket);

            // PARSE: [<block argument>* '|']
            // ISSUE: It this a bug in X3J20. Shouldn't it be: [<block argument>+ '|']
            // The current definition allows blocks like: [ | ] ... or ... [ | self doStuff ]  ... or ... [ | | temp | temp := self something ]
            // We assume X3J20 bug and expect: [<block argument>+ '|']
            VerticalBarToken         argumentsBar = null;
            List <BlockArgumentNode> arguments    = new List <BlockArgumentNode>();

            Token token = this.GetNextTokenxx(Preference.VerticalBar | Preference.NegativeSign);

            // Check if the block has arguments
            if (Parser.IsBlockArgumentPrefix(token))
            {
                // ... yes arguments ... parse them ...
                while (true)
                {
                    if (Parser.IsBlockArgumentPrefix(token))
                    {
                        // <block argument>
                        arguments.Add(this.ParseBlockArgument(result, (SpecialCharacterToken)token));
                    }
                    else if (token is VerticalBarToken)
                    {
                        // The '|' after the arguments.
                        argumentsBar = (VerticalBarToken)token;
                        token        = this.GetNextTokenxx(Preference.NegativeSign | Preference.VerticalBar);
                        break; // Done with block arguments
                    }
                    else
                    {
                        this.ReportParserError(result, SemanticErrors.MissingBlockClosingArgsBar, token);
                        break;
                    }
                    // Get the next token
                    token = this.GetNextTokenxx(Preference.VerticalBar | Preference.NegativeSign);
                }
            }

            // PARSE: [<temporaries>]
            ParseTemporariesResult ptr = this.ParseTemporaries(result, token);

            // PARSE: <statements>
            token = this.GetNextTokenxx(Preference.NegativeSign);
            StatementNode statements = this.ParseStatement(result, token);

            // Ensure the block is properly closed.
            SpecialCharacterToken rightBracket = null;

            token = this.GetNextTokenxx(Preference.Default);
            if (!Parser.IsBlockEndDelimiter(token))
            {
                this.ReportParserError(result, SemanticErrors.MissingBlockClosingBracket, token);
                this.ResidueToken = token;
            }
            else
            {
                rightBracket = (SpecialCharacterToken)token;
            }

            result.SetContents(arguments, argumentsBar, ptr.LeftBar, ptr.Temporaries, ptr.RightBar, statements, rightBracket);
            return(result);
        }