Inheritance: AstNode, IScope
Beispiel #1
0
        public static IScope ParseScope(ParseContext context, IAstNode parent, bool allowsSimpleScope, string terminatingKeyword) {
            TokenStream<RToken> tokens = context.Tokens;
            IScope scope;

            if (tokens.CurrentToken.TokenType == RTokenType.OpenCurlyBrace) {
                scope = new Scope(string.Empty);
                if (scope.Parse(context, parent)) {
                    return scope;
                }
            } else if (allowsSimpleScope) {
                // Try simple on-line scope as in 'for(...) statement # comment'
                scope = new SimpleScope(terminatingKeyword);
                if (scope.Parse(context, parent)) {
                    return scope;
                }
            } else {
                context.AddError(new MissingItemParseError(ParseErrorType.OpenCurlyBraceExpected, tokens.PreviousToken));
            }

            return null;
        }
Beispiel #2
0
        public override bool Parse(ParseContext context, IAstNode parent) {
            TokenStream<RToken> tokens = context.Tokens;
            RToken currentToken = tokens.CurrentToken;

            context.Scopes.Push(this);

            if (!(this is GlobalScope) && currentToken.TokenType == RTokenType.OpenCurlyBrace) {
                this.OpenCurlyBrace = RParser.ParseToken(context, this);
            }

            while (!tokens.IsEndOfStream()) {
                currentToken = context.Tokens.CurrentToken;

                switch (currentToken.TokenType) {
                    case RTokenType.CloseCurlyBrace:
                        if (this.OpenCurlyBrace != null) {
                            this.CloseCurlyBrace = RParser.ParseToken(context, this);
                        } else {
                            context.AddError(new ParseError(ParseErrorType.UnexpectedToken, ErrorLocation.Token, currentToken));
                            context.Tokens.MoveToNextToken();
                        }
                        break;

                    case RTokenType.OpenCurlyBrace:
                        IScope scope = new Scope(string.Empty);
                        scope.Parse(context, this);
                        break;

                    default:
                        IStatement statement = Statement.Create(context, this, null);
                        if (statement != null) {
                            if (statement.Parse(context, this)) {
                                this.statements.Add(statement);
                            } else {
                                statement = null;
                            }
                        }

                        if (statement == null) {
                            if (!context.TextProvider.IsNewLineBeforePosition(context.Tokens.CurrentToken.Start)) {
                                // try recovering at the next line or past nearest 
                                // semicolon or closing curly brace
                                tokens.MoveToNextLine(context.TextProvider,
                                    (TokenStream<RToken> ts) => {
                                        return ts.CurrentToken.TokenType == RTokenType.Semicolon ||
                                               ts.NextToken.TokenType == RTokenType.CloseCurlyBrace;
                                    });
                            } else {
                                tokens.MoveToNextToken();
                            }
                        }
                        break;
                }

                if (this.CloseCurlyBrace != null) {
                    break;
                }
            }

            context.Scopes.Pop();

            if (this.OpenCurlyBrace != null && this.CloseCurlyBrace == null) {
                context.AddError(new MissingItemParseError(ParseErrorType.CloseCurlyBraceExpected, context.Tokens.PreviousToken));
            }

            // TODO: process content and fill out declared variables 
            // and functions and get data to the classifier for colorization.
            return base.Parse(context, parent);
        }