Exemplo n.º 1
0
        public void Shift(Lexeme z, int targetStateIndex)
        {
#if HISTORY
            var from = m_topStack.StateIndex;
#endif

            StackNode shiftNode = new StackNode(targetStateIndex, m_topStack, z);

            m_topStack = shiftNode;

#if HISTORY
            var to = m_topStack.StateIndex;
            History.Add(String.Format("S{0}:{1}", from, to));
#endif
        }
Exemplo n.º 2
0
        public IProduction PanicRecover(TransitionTable transitions, SourceSpan lastLocation, bool isEos)
        {
            while (true)
            {
                int currentStateIndex = m_topStack.StateIndex;
                for (int i = 0; i < transitions.ProductionCount; i++)
                {
                    int gotoState = transitions.GetGoto(currentStateIndex, i);

                    if (gotoState > 0)
                    {
                        var recoverNT = transitions.NonTerminals[i];

                        if (isEos)
                        {
                            //the recoverNT must have EOS in its follow, otherwise continue
                            var follow = ((ProductionBase)recoverNT).Info.Follow;

                            if (!follow.Contains(EndOfStream.Instance))
                            {
                                continue;
                            }
                        }

                        m_topStack = m_topStack.PrevNode;

                        var newNode = new StackNode(gotoState, m_topStack, null);
                        IncreaseErrorRecoverLevel();
                        AddError(new ErrorRecord(null, lastLocation));
                        m_topStack = newNode;

                        return(recoverNT);
                    }
                }

                if (m_topStack.PrevNode == null)
                {
                    throw new ParsingFailureException("There's no way to recover from parser error");
                }

                m_topStack       = m_topStack.PrevNode;
                m_lastShiftStack = m_topStack;
            }
        }
Exemplo n.º 3
0
        public void Reduce(IProduction production, ReduceVisitor reducer, Lexeme lookahead)
        {
#if HISTORY
            var from = m_topStack.StateIndex;
#endif

            if (production == null)
            {
                //Accept
                Debug.Assert(m_topStack.PrevNode.StateIndex == 0);

                //TODO: accepted
                IsAccepted = true;
                return;
            }

            if (production.AggregatesAmbiguities)
            {
                AmbiguityAggregator = ((ProductionBase)production).CreateAggregator();
            }

            var reduceResult = production.Accept(reducer, m_topStack);

            m_topStack = reduceResult.NewTopStack;
            var reduceError = reduceResult.ReduceError;

            if (reduceError != null)
            {
                IncreaseErrorRecoverLevel();

                if (reduceError.ErrorPosition == null)
                {
                    reduceError = new ErrorRecord(reduceError.ErrorId, lookahead.Value.Span);
                }

                AddError(reduceError);
            }

#if HISTORY
            var to = m_topStack.StateIndex;
            History.Add(String.Format("R{0}:{1}", from, to));
#endif
        }
Exemplo n.º 4
0
 /// <summary>
 /// Restore the stack to the state after last shift action, for error recovery
 /// </summary>
 public void RestoreToLastShift()
 {
     m_topStack = m_lastShiftStack;
 }
Exemplo n.º 5
0
 public ParserHead(StackNode topStack)
 {
     m_topStack          = topStack;
     m_lastShiftStack    = topStack;
     m_errorRecoverLevel = 0;
 }
Exemplo n.º 6
0
 public ParserHead(StackNode topStack)
 {
     m_topStack = topStack;
     m_lastShiftStack = topStack;
     m_errorRecoverLevel = 0;
 }
Exemplo n.º 7
0
        public void Shift(Lexeme z, int targetStateIndex)
        {
            #if HISTORY
            var from = m_topStack.StateIndex;
            #endif

            StackNode shiftNode = new StackNode(targetStateIndex, m_topStack, z);

            m_topStack = shiftNode;
            m_lastShiftStack = shiftNode;

            #if HISTORY
            var to = m_topStack.StateIndex;
            History.Add(String.Format("S{0}:{1}", from, to));
            #endif
        }
Exemplo n.º 8
0
 /// <summary>
 /// Restore the stack to the state after last shift action, for error recovery
 /// </summary>
 public void RestoreToLastShift()
 {
     m_topStack = m_lastShiftStack;
 }
Exemplo n.º 9
0
        public void Reduce(IProduction production, ReduceVisitor reducer, Lexeme lookahead)
        {
            #if HISTORY
            var from = m_topStack.StateIndex;
            #endif

            if (production == null)
            {
                //Accept
                Debug.Assert(m_topStack.PrevNode.StateIndex == 0);

                //TODO: accepted
                IsAccepted = true;
                return;
            }

            if (production.AggregatesAmbiguities)
            {
                AmbiguityAggregator = ((ProductionBase)production).CreateAggregator();
            }

            var reduceResult = production.Accept(reducer, m_topStack);

            m_topStack = reduceResult.NewTopStack;
            var reduceError = reduceResult.ReduceError;

            if (reduceError != null)
            {
                IncreaseErrorRecoverLevel();

                if (reduceError.ErrorPosition == null)
                {
                    reduceError = new ErrorRecord(reduceError.ErrorId, lookahead.Value.Span);
                }

                AddError(reduceError);
            }

            #if HISTORY
            var to = m_topStack.StateIndex;
            History.Add(String.Format("R{0}:{1}", from, to));
            #endif
        }
Exemplo n.º 10
0
        public IProduction PanicRecover(TransitionTable transitions, SourceSpan lastLocation, bool isEos)
        {
            while(true)
            {
                int currentStateIndex = m_topStack.StateIndex;
                for (int i = 0; i < transitions.ProductionCount; i++)
                {
                    int gotoState = transitions.GetGoto(currentStateIndex, i);

                    if (gotoState > 0)
                    {
                        var recoverNT = transitions.NonTerminals[i];

                        if (isEos)
                        {
                            //the recoverNT must have EOS in its follow, otherwise continue
                            var follow = ((ProductionBase)recoverNT).Info.Follow;

                            if (!follow.Contains(EndOfStream.Instance))
                            {
                                continue;
                            }
                        }

                        m_topStack = m_topStack.PrevNode;

                        var newNode = new StackNode(gotoState, m_topStack, null);
                        IncreaseErrorRecoverLevel();
                        AddError(new ErrorRecord(null, lastLocation));
                        m_topStack = newNode;

                        return recoverNT;
                    }
                }

                if (m_topStack.PrevNode == null)
                {
                    throw new ParsingFailureException("There's no way to recover from parser error");
                }

                m_topStack = m_topStack.PrevNode;
                m_lastShiftStack = m_topStack;
            }
        }
Exemplo n.º 11
0
 public StackNode(int stateIndex, StackNode prev, object value)
 {
     StateIndex = stateIndex;
     PrevNode = prev;
     ReducedValue = value;
 }
Exemplo n.º 12
0
        public IProduction PanicRecover(TransitionTable transitions, SourceSpan lastLocation)
        {
            while(true)
            {
                int currentStateIndex = m_topStack.StateIndex;
                for (int i = 0; i < transitions.ProductionCount; i++)
                {
                    int gotoState = transitions.GetGoto(currentStateIndex, i);

                    if (gotoState > 0)
                    {
                        var recoverNT = transitions.NonTerminals[i];

                        m_topStack = m_topStack.PrevNode;

                        var newNode = new StackNode(gotoState, m_topStack, null);
                        IncreaseErrorRecoverLevel();
                        AddError(new ErrorRecord(null, lastLocation));
                        m_topStack = newNode;

                        return recoverNT;
                    }
                }

                if (m_topStack.PrevNode == null)
                {
                    throw new ParsingFailureException("There's no way to recover from parser error");
                }

                m_topStack = m_topStack.PrevNode;
            }
        }
Exemplo n.º 13
0
 public ReduceResult(StackNode newTopStack, ErrorRecord reduceError = null)
 {
     NewTopStack = newTopStack;
     ReduceError = reduceError;
 }
Exemplo n.º 14
0
 public StackNode(int stateIndex, StackNode prev, object value)
 {
     StateIndex   = stateIndex;
     PrevNode     = prev;
     ReducedValue = value;
 }
Exemplo n.º 15
0
        public void Reduce(IProduction production, ReduceVisitor reducer, Lexeme lookahead)
        {
            #if HISTORY
            var from = m_topStack.StateIndex;
            #endif

            if (production == null)
            {
                //Accept
                Debug.Assert(m_topStack.PrevNode.StateIndex == 0);

                //TODO: accepted
                IsAccepted = true;
                return;
            }

            if (Priority < production.Priority) Priority = production.Priority;

            var reduceResult = production.Accept(reducer, m_topStack);

            m_topStack = reduceResult.NewTopStack;
            var reduceError = reduceResult.ReduceError;

            if (reduceError != null)
            {
                IncreaseErrorRecoverLevel();

                if (reduceError.ErrorPosition == null)
                {
                    reduceError.ErrorPosition = lookahead.Span;
                }

                AddError(reduceError);
            }

            #if HISTORY
            var to = m_topStack.StateIndex;
            History.Add(String.Format("R{0}:{1}", from, to));
            #endif
        }