Inheritance: AstNode, IStatement
Esempio n. 1
0
        /// <summary>
        /// Abstract factory creating statements depending on current
        /// token and the following token sequence
        /// </summary>
        /// <returns></returns>
        public static IStatement Create(ParseContext context, IAstNode parent, string terminatingKeyword) {
            TokenStream<RToken> tokens = context.Tokens;
            RToken currentToken = tokens.CurrentToken;

            IStatement statement = null;

            switch (currentToken.TokenType) {
                case RTokenType.Keyword:
                    // If statement starts with a keyword, it is not an assignment
                    // hence we should always try keyword based statements first.
                    // Some of the statements may be R-values like typeof() but
                    // in case of the statement appearing on its own return value
                    // will be simply ignored. IDE may choose to show a warning.
                    if (currentToken.SubType == RTokenSubType.BuiltinFunction && tokens.NextToken.TokenType != RTokenType.OpenBrace) {
                        // 'return <- x + y' is allowed
                        statement = new ExpressionStatement(terminatingKeyword);
                    } else {
                        statement = KeywordStatement.CreateStatement(context, parent);
                    }
                    break;

                case RTokenType.Semicolon:
                    statement = new EmptyStatement();
                    break;

                default:
                    // Possible L-value in a left-hand assignment, 
                    // a function call or R-value in a right hand assignment.
                    statement = new ExpressionStatement(terminatingKeyword);
                    break;
            }

            return statement;
        }
Esempio n. 2
0
        /// <summary>
        /// Abstract factory creating statements depending on current
        /// token and the following token sequence
        /// </summary>
        /// <returns></returns>
        public static IStatement Create(ParseContext context, IAstNode parent, string terminatingKeyword)
        {
            TokenStream <RToken> tokens = context.Tokens;
            RToken currentToken         = tokens.CurrentToken;

            IStatement statement = null;

            switch (currentToken.TokenType)
            {
            case RTokenType.Keyword:
                // If statement starts with a keyword, it is not an assignment
                // hence we should always try keyword based statements first.
                // Some of the statements may be R-values like typeof() but
                // in case of the statement appearing on its own return value
                // will be simply ignored. IDE may choose to show a warning.
                if (currentToken.SubType == RTokenSubType.BuiltinFunction && tokens.NextToken.TokenType != RTokenType.OpenBrace)
                {
                    // 'return <- x + y' is allowed
                    statement = new ExpressionStatement(terminatingKeyword);
                }
                else
                {
                    statement = KeywordStatement.CreateStatement(context, parent);
                }
                break;

            case RTokenType.Semicolon:
                statement = new EmptyStatement();
                break;

            default:
                // Possible L-value in a left-hand assignment,
                // a function call or R-value in a right hand assignment.
                statement = new ExpressionStatement(terminatingKeyword);
                break;
            }

            return(statement);
        }