public bool Parse(TextReader reader)
        {
            if (egtDataManager == null)
            {
                OnNotLoadedError();
                return false;
            }

            TokenQueueStack inputTokens = new TokenQueueStack();
            Stack<Token> tokenStack = new Stack<Token>();
            tokenStack.Push(new Token());
            Stack<Token> groupStack = new Stack<Token>();
            LookAheadBuffer lookAheadBuffer = new LookAheadBuffer(reader);
            ushort lalrState = 0;

            while (true)
            {
                if (inputTokens.Count == 0)
                {
                    inputTokens.Push(ProduceToken(groupStack, lookAheadBuffer));
                    OnTokenRead(inputTokens.Peek());
                }
                //Runaway group
                else if (groupStack.Count != 0)
                {
                    OnGroupError();
                    return false; //Error; abort
                }
                else
                {
                    Token read = inputTokens.Peek();

                    switch (read.Type)
                    {
                        case SymbolType.Noise:
                            inputTokens.Pop();
                            break;

                        case SymbolType.Error:
                            OnLexicalError(read.EndPosition, read.Data);
                            return false; //Error; abort

                        default:
                            LRActionList actionList = egtDataManager.GetLRActionList(lalrState);
                            LRAction parseAction = actionList[read.Symbol];
                            if (parseAction == null)
                            {
                                HandleSyntaxError(actionList, read);
                                return false;
                            }

                            switch (parseAction.Type)
                            {
                                case LRActionType.Accept:
                                    OnCompleted(tokenStack.Peek().Tag);
                                    return true;

                                case LRActionType.Shift:
                                    lalrState = parseAction.Value;
                                    read.State = lalrState;
                                    tokenStack.Push(read);
                                    //It now exists on the Token-Stack and must be eliminated from the queue.
                                    inputTokens.Dequeue();
                                    break;

                                case LRActionType.Reduce:
                                    if (!DoReduction(tokenStack, lookAheadBuffer, ref lalrState, parseAction))
                                    {
                                        return false;
                                    }
                                    break;
                            }
                            break;
                    }
                }
            }
        }
Exemple #2
0
        public bool Parse(TextReader reader)
        {
            if (egtDataManager == null)
            {
                OnNotLoadedError();
                return(false);
            }

            TokenQueueStack inputTokens = new TokenQueueStack();
            Stack <Token>   tokenStack  = new Stack <Token>();

            tokenStack.Push(new Token());
            Stack <Token>   groupStack      = new Stack <Token>();
            LookAheadBuffer lookAheadBuffer = new LookAheadBuffer(reader);
            ushort          lalrState       = 0;

            while (true)
            {
                if (inputTokens.Count == 0)
                {
                    inputTokens.Push(ProduceToken(groupStack, lookAheadBuffer));
                    OnTokenRead(inputTokens.Peek());
                }
                //Runaway group
                else if (groupStack.Count != 0)
                {
                    OnGroupError();
                    return(false); //Error; abort
                }
                else
                {
                    Token read = inputTokens.Peek();

                    switch (read.Type)
                    {
                    case SymbolType.Noise:
                        inputTokens.Pop();
                        break;

                    case SymbolType.Error:
                        OnLexicalError(read.EndPosition, read.Data);
                        return(false);    //Error; abort

                    default:
                        LRActionList actionList  = egtDataManager.GetLRActionList(lalrState);
                        LRAction     parseAction = actionList[read.Symbol];
                        if (parseAction == null)
                        {
                            HandleSyntaxError(actionList, read);
                            return(false);
                        }

                        switch (parseAction.Type)
                        {
                        case LRActionType.Accept:
                            OnCompleted(tokenStack.Peek().Tag);
                            return(true);

                        case LRActionType.Shift:
                            lalrState  = parseAction.Value;
                            read.State = lalrState;
                            tokenStack.Push(read);
                            //It now exists on the Token-Stack and must be eliminated from the queue.
                            inputTokens.Dequeue();
                            break;

                        case LRActionType.Reduce:
                            if (!DoReduction(tokenStack, lookAheadBuffer, ref lalrState, parseAction))
                            {
                                return(false);
                            }
                            break;
                        }
                        break;
                    }
                }
            }
        }