示例#1
0
        /// <summary>
        /// Parses a token stream creating the largest statement possible
        /// </summary>
        /// <param name="stream">Stream of tokens to parse</param>
        /// <returns>Statement</returns>
        private StatementList ParseStatement(Stream stream)
        {
            var tokenType = stream.Peek().Type;
            Token identifier = null;
            switch (tokenType)
            {
                case TokenType.Identifier:
                    //standard identifier
                    identifier = stream.Next();
                    break;
                case TokenType.OpenBracket:
                case TokenType.OpenParenthesis:
                    //ignore these
                    break;
                case TokenType.StringLiteral:
                case TokenType.MultiLineStringLiteral:
                case TokenType.StringLiteralPipe:
                case TokenType.QuotedStringLiteral:
                    //string statement
                    identifier = stream.Next();
                    break;
            }

            Statement statement = null;
            StatementTail tail = null;

            while (stream.Peek() != null)
            {
                var token = stream.Peek();
                if (token == null)
                {
                    break;
                }

                switch (token.Type)
                {
                    case TokenType.OpenParenthesis:
                    case TokenType.OpenBrace:
                    case TokenType.OpenBracket:
                        tail = ParseStatementTail(stream);
                        break;
                    case TokenType.GreaterThan:
                        //consume greater than
                        stream.NextNoReturn();

                        //might be a single child or a statement list of siblings
                        tail = ParseSingleStatementTail(stream);
                        break;
                    case TokenType.Colon:
                        //next thing must be an identifier
                        var colon = stream.Next();
                        identifier = stream.Next();
                        statement = new EncodedOutput(_host, identifier.Content);
                        goto checkForSiblings;
                    case TokenType.Equal:
                        //next thing must be an identifier
                        var equal = stream.Next();
                        identifier = stream.Next();
                        statement = new RawOutput(_host, identifier.Content);
                        goto checkForSiblings;

                    default:
                        statement = GetStatementFromToken(identifier, tail);
                        goto checkForSiblings;
                }
            }

            statement = GetStatementFromToken(identifier, tail);

            checkForSiblings:

            var list = new StatementList(_host);
            list.Add(statement);

            while (stream.Peek() != null)
            {
                //Parrot.Debugger.Debug.WriteLine("Looking for siblings");
                if (stream.Peek().Type == TokenType.Plus)
                {
                    //it's now a statementlist not a statement
                    stream.NextNoReturn();
                    var siblings = ParseStatement(stream);
                    foreach (var sibling in siblings)
                    {
                        list.Add(sibling);
                    }
                }
                else
                {
                    break;
                }
            }

            return list;
        }
示例#2
0
        /// <summary>
        /// Parses a token stream creating the largest statement possible
        /// </summary>
        /// <param name="stream">Stream of tokens to parse</param>
        /// <returns>Statement</returns>
        private StatementList ParseStatement(Stream stream)
        {
            var   tokenType  = stream.Peek().Type;
            Token identifier = null;

            switch (tokenType)
            {
            case TokenType.Identifier:
                //standard identifier
                identifier = stream.Next();
                break;

            case TokenType.OpenBracket:
            case TokenType.OpenParenthesis:
                //ignore these
                break;

            case TokenType.StringLiteral:
            case TokenType.MultiLineStringLiteral:
            case TokenType.StringLiteralPipe:
            case TokenType.QuotedStringLiteral:
                //string statement
                identifier = stream.Next();
                break;
            }


            Statement     statement = null;
            StatementTail tail      = null;

            while (stream.Peek() != null)
            {
                var token = stream.Peek();
                if (token == null)
                {
                    break;
                }

                switch (token.Type)
                {
                case TokenType.OpenParenthesis:
                case TokenType.OpenBrace:
                case TokenType.OpenBracket:
                    tail = ParseStatementTail(stream);
                    break;

                case TokenType.GreaterThan:
                    //consume greater than
                    stream.NextNoReturn();

                    //might be a single child or a statement list of siblings
                    tail = ParseSingleStatementTail(stream);
                    break;

                case TokenType.Colon:
                    //next thing must be an identifier
                    var colon = stream.Next();
                    identifier = stream.Next();
                    statement  = new EncodedOutput(_host, identifier.Content);
                    goto checkForSiblings;

                case TokenType.Equal:
                    //next thing must be an identifier
                    var equal = stream.Next();
                    identifier = stream.Next();
                    statement  = new RawOutput(_host, identifier.Content);
                    goto checkForSiblings;

                default:
                    statement = GetStatementFromToken(identifier, tail);
                    goto checkForSiblings;
                }
            }

            statement = GetStatementFromToken(identifier, tail);

checkForSiblings:


            var list = new StatementList(_host);

            list.Add(statement);

            while (stream.Peek() != null)
            {
                //Parrot.Debugger.Debug.WriteLine("Looking for siblings");
                if (stream.Peek().Type == TokenType.Plus)
                {
                    //it's now a statementlist not a statement
                    stream.NextNoReturn();
                    var siblings = ParseStatement(stream);
                    foreach (var sibling in siblings)
                    {
                        list.Add(sibling);
                    }
                }
                else
                {
                    break;
                }
            }

            return(list);
        }