Пример #1
0
 protected static void DebugLocation(IDebugEventListener dbg, int line, int charPositionInLine)
 {
     if (dbg != null)
     {
         dbg.Location(line, charPositionInLine);
     }
 }
Пример #2
0
 protected static void DebugEnterAlt(IDebugEventListener dbg, int alt)
 {
     if (dbg != null)
     {
         dbg.EnterAlt(alt);
     }
 }
Пример #3
0
 protected static void DebugExitDecision(IDebugEventListener dbg, int decisionNumber)
 {
     if (dbg != null)
     {
         dbg.ExitDecision(decisionNumber);
     }
 }
Пример #4
0
 protected static void DebugEnterSubRule(IDebugEventListener dbg, int decisionNumber)
 {
     if (dbg != null)
     {
         dbg.EnterSubRule(decisionNumber);
     }
 }
Пример #5
0
 protected static void DebugExitRule(IDebugEventListener dbg, string grammarFileName, string ruleName)
 {
     if (dbg != null)
     {
         dbg.ExitRule(grammarFileName, ruleName);
     }
 }
Пример #6
0
        /** For a given input char stream, try to match against the NFA
         *  starting at startRule.  This is a deterministic parse even though
         *  it is using an NFA because it uses DFAs at each decision point to
         *  predict which alternative will succeed.  This is exactly what the
         *  generated parser will do.
         *
         *  This only does lexer grammars.
         *
         *  Return the token type associated with the final rule end state.
         */
        public virtual void Scan(string startRule,
                                 IDebugEventListener actions,
                                 IList <NFAState> visitedStates)
        {
            if (grammar.type != GrammarType.Lexer)
            {
                return;
            }

            //Console.Out.WriteLine( "scan(" + startRule + ",'" + @in.substring( @in.Index, @in.Size() - 1 ) + "')" );
            // Build NFAs/DFAs from the grammar AST if NFAs haven't been built yet
            if (grammar.GetRuleStartState(startRule) == null)
            {
                grammar.BuildNFA();
            }

            if (!grammar.AllDecisionDFAHaveBeenCreated)
            {
                // Create the DFA predictors for each decision
                grammar.CreateLookaheadDFAs();
            }

            // do the parse
            Stack <object> ruleInvocationStack = new Stack <object>();
            NFAState       start = grammar.GetRuleStartState(startRule);
            NFAState       stop  = grammar.GetRuleStopState(startRule);

            ParseEngine(startRule, start, stop, input, ruleInvocationStack,
                        actions, visitedStates);
        }
Пример #7
0
 protected static void DebugSemanticPredicate(IDebugEventListener dbg, bool result, string predicate)
 {
     if (dbg != null)
     {
         dbg.SemanticPredicate(result, predicate);
     }
 }
Пример #8
0
        protected virtual void DebugBeginBacktrack(int level)
        {
            IDebugEventListener dbg = DebugListener;

            if (dbg != null)
            {
                dbg.BeginBacktrack(level);
            }
        }
Пример #9
0
        protected virtual void DebugRecognitionException(RecognitionException ex)
        {
            IDebugEventListener dbg = recognizer.DebugListener;

            if (dbg != null)
            {
                dbg.RecognitionException(ex);
            }
        }
Пример #10
0
        protected virtual void DebugLocation(int line, int charPositionInLine)
        {
            IDebugEventListener dbg = DebugListener;

            if (dbg != null)
            {
                dbg.Location(line, charPositionInLine);
            }
        }
Пример #11
0
        protected virtual void DebugSemanticPredicate(bool result, string predicate)
        {
            IDebugEventListener dbg = DebugListener;

            if (dbg != null)
            {
                dbg.SemanticPredicate(result, predicate);
            }
        }
Пример #12
0
        protected virtual void DebugEnterDecision(int decisionNumber, bool couldBacktrack)
        {
            IDebugEventListener dbg = DebugListener;

            if (dbg != null)
            {
                dbg.EnterDecision(decisionNumber, couldBacktrack);
            }
        }
Пример #13
0
        protected virtual void DebugExitDecision(int decisionNumber)
        {
            IDebugEventListener dbg = DebugListener;

            if (dbg != null)
            {
                dbg.ExitDecision(decisionNumber);
            }
        }
Пример #14
0
        protected virtual void DebugEnterSubRule(int decisionNumber)
        {
            IDebugEventListener dbg = DebugListener;

            if (dbg != null)
            {
                dbg.EnterSubRule(decisionNumber);
            }
        }
Пример #15
0
        protected virtual void DebugEnterAlt(int alt)
        {
            IDebugEventListener dbg = DebugListener;

            if (dbg != null)
            {
                dbg.EnterAlt(alt);
            }
        }
Пример #16
0
        protected virtual void DebugEndBacktrack(int level, bool successful)
        {
            IDebugEventListener dbg = DebugListener;

            if (dbg != null)
            {
                dbg.EndBacktrack(level, successful);
            }
        }
Пример #17
0
        protected virtual void DebugExitRule(string grammarFileName, string ruleName)
        {
            IDebugEventListener dbg = DebugListener;

            if (dbg != null)
            {
                dbg.ExitRule(grammarFileName, ruleName);
            }
        }
Пример #18
0
 protected static void DebugExitDecision( IDebugEventListener dbg, int decisionNumber )
 {
     if ( dbg != null )
         dbg.ExitDecision( decisionNumber );
 }
Пример #19
0
        /** Fill a list of all NFA states visited during the parse */
        protected virtual void ParseEngine(string startRule,
                                           NFAState start,
                                           NFAState stop,
                                           IIntStream input,
                                           Stack <object> ruleInvocationStack,
                                           IDebugEventListener actions,
                                           IList <NFAState> visitedStates)
        {
            NFAState s = start;

            if (actions != null)
            {
                actions.EnterRule(s.nfa.Grammar.FileName, start.enclosingRule.Name);
            }
            int t = input.LA(1);

            while (s != stop)
            {
                if (visitedStates != null)
                {
                    visitedStates.Add(s);
                }
                //Console.Out.WriteLine( "parse state " + s.stateNumber + " input=" + s.nfa.Grammar.getTokenDisplayName( t ) );
                // CASE 1: decision state
                if (s.DecisionNumber > 0 && s.nfa.Grammar.GetNumberOfAltsForDecisionNFA(s) > 1)
                {
                    // decision point, must predict and jump to alt
                    DFA dfa = s.nfa.Grammar.GetLookaheadDFA(s.DecisionNumber);
                    //if ( s.nfa.Grammar.type != GrammarType.Lexer )
                    //{
                    //    Console.Out.WriteLine( "decision: " +
                    //                   dfa.getNFADecisionStartState().Description +
                    //                   " input=" + s.nfa.Grammar.getTokenDisplayName( t ) );
                    //}
                    int m            = input.Mark();
                    int predictedAlt = Predict(dfa);
                    if (predictedAlt == NFA.INVALID_ALT_NUMBER)
                    {
                        string description        = dfa.NFADecisionStartState.Description;
                        NoViableAltException nvae =
                            new NoViableAltException(description,
                                                     dfa.NfaStartStateDecisionNumber,
                                                     s.StateNumber,
                                                     input);
                        if (actions != null)
                        {
                            actions.RecognitionException(nvae);
                        }
                        input.Consume(); // recover
                        throw nvae;
                    }
                    input.Rewind(m);
                    int parseAlt =
                        s.TranslateDisplayAltToWalkAlt(predictedAlt);
                    //if ( s.nfa.Grammar.type != GrammarType.Lexer )
                    //{
                    //    Console.Out.WriteLine( "predicted alt " + predictedAlt + ", parseAlt " + parseAlt );
                    //}
                    NFAState alt;
                    if (parseAlt > s.nfa.Grammar.GetNumberOfAltsForDecisionNFA(s))
                    {
                        // implied branch of loop etc...
                        alt = s.nfa.Grammar.nfa.GetState(s.endOfBlockStateNumber);
                    }
                    else
                    {
                        alt = s.nfa.Grammar.GetNFAStateForAltOfDecision(s, parseAlt);
                    }
                    s = (NFAState)alt.transition[0].Target;
                    continue;
                }

                // CASE 2: finished matching a rule
                if (s.IsAcceptState)
                { // end of rule node
                    if (actions != null)
                    {
                        actions.ExitRule(s.nfa.Grammar.FileName, s.enclosingRule.Name);
                    }
                    if (ruleInvocationStack.Count == 0)
                    {
                        // done parsing.  Hit the start state.
                        //Console.Out.WriteLine( "stack empty in stop state for " + s.enclosingRule );
                        break;
                    }
                    // pop invoking state off the stack to know where to return to
                    NFAState invokingState = (NFAState)ruleInvocationStack.Pop();
                    RuleClosureTransition invokingTransition =
                        (RuleClosureTransition)invokingState.transition[0];
                    // move to node after state that invoked this rule
                    s = invokingTransition.FollowState;
                    continue;
                }

                Transition trans = s.transition[0];
                Label      label = trans.Label;
                if (label.IsSemanticPredicate)
                {
                    FailedPredicateException fpe =
                        new FailedPredicateException(input,
                                                     s.enclosingRule.Name,
                                                     "can't deal with predicates yet");
                    if (actions != null)
                    {
                        actions.RecognitionException(fpe);
                    }
                }

                // CASE 3: epsilon transition
                if (label.IsEpsilon)
                {
                    // CASE 3a: rule invocation state
                    if (trans is RuleClosureTransition)
                    {
                        ruleInvocationStack.Push(s);
                        s = (NFAState)trans.Target;
                        //Console.Out.WriteLine( "call " + s.enclosingRule.name + " from " + s.nfa.Grammar.getFileName() );
                        if (actions != null)
                        {
                            actions.EnterRule(s.nfa.Grammar.FileName, s.enclosingRule.Name);
                        }
                        // could be jumping to new grammar, make sure DFA created
                        if (!s.nfa.Grammar.AllDecisionDFAHaveBeenCreated)
                        {
                            s.nfa.Grammar.CreateLookaheadDFAs();
                        }
                    }
                    // CASE 3b: plain old epsilon transition, just move
                    else
                    {
                        s = (NFAState)trans.Target;
                    }
                }

                // CASE 4: match label on transition
                else if (label.Matches(t))
                {
                    if (actions != null)
                    {
                        if (s.nfa.Grammar.type == GrammarType.Parser ||
                            s.nfa.Grammar.type == GrammarType.Combined)
                        {
                            actions.ConsumeToken(((ITokenStream)input).LT(1));
                        }
                    }
                    s = (NFAState)s.transition[0].Target;
                    input.Consume();
                    t = input.LA(1);
                }

                // CASE 5: error condition; label is inconsistent with input
                else
                {
                    if (label.IsAtom)
                    {
                        MismatchedTokenException mte =
                            new MismatchedTokenException(label.Atom, input);
                        if (actions != null)
                        {
                            actions.RecognitionException(mte);
                        }
                        input.Consume(); // recover
                        throw mte;
                    }
                    else if (label.IsSet)
                    {
                        MismatchedSetException mse =
                            new MismatchedSetException(((IntervalSet)label.Set).ToRuntimeBitSet(),
                                                       input);
                        if (actions != null)
                        {
                            actions.RecognitionException(mse);
                        }
                        input.Consume(); // recover
                        throw mse;
                    }
                    else if (label.IsSemanticPredicate)
                    {
                        FailedPredicateException fpe =
                            new FailedPredicateException(input,
                                                         s.enclosingRule.Name,
                                                         label.SemanticContext.ToString());
                        if (actions != null)
                        {
                            actions.RecognitionException(fpe);
                        }
                        input.Consume(); // recover
                        throw fpe;
                    }
                    else
                    {
                        throw new RecognitionException(input);   // unknown error
                    }
                }
            }
            //Console.Out.WriteLine( "hit stop state for " + stop.enclosingRule );
            if (actions != null)
            {
                actions.ExitRule(s.nfa.Grammar.FileName, stop.enclosingRule.Name);
            }
        }
Пример #20
0
        /** Fill a list of all NFA states visited during the parse */
        protected virtual void ParseEngine( String startRule,
                                   NFAState start,
                                   NFAState stop,
                                   IIntStream input,
                                   Stack<object> ruleInvocationStack,
                                   IDebugEventListener actions,
                                   IList visitedStates )
        {
            NFAState s = start;
            if ( actions != null )
            {
                actions.EnterRule( s.nfa.grammar.FileName, start.enclosingRule.Name );
            }
            int t = input.LA( 1 );
            while ( s != stop )
            {
                if ( visitedStates != null )
                {
                    visitedStates.Add( s );
                }
                //Console.Out.WriteLine( "parse state " + s.stateNumber + " input=" + s.nfa.grammar.getTokenDisplayName( t ) );
                // CASE 1: decision state
                if ( s.DecisionNumber > 0 && s.nfa.grammar.GetNumberOfAltsForDecisionNFA( s ) > 1 )
                {
                    // decision point, must predict and jump to alt
                    DFA dfa = s.nfa.grammar.GetLookaheadDFA( s.DecisionNumber );
                    //if ( s.nfa.grammar.type != GrammarType.Lexer )
                    //{
                    //    Console.Out.WriteLine( "decision: " +
                    //                   dfa.getNFADecisionStartState().Description +
                    //                   " input=" + s.nfa.grammar.getTokenDisplayName( t ) );
                    //}
                    int m = input.Mark();
                    int predictedAlt = Predict( dfa );
                    if ( predictedAlt == NFA.INVALID_ALT_NUMBER )
                    {
                        String description = dfa.NFADecisionStartState.Description;
                        NoViableAltException nvae =
                            new NoViableAltException( description,
                                                          dfa.DecisionNumber,
                                                          s.stateNumber,
                                                          input );
                        if ( actions != null )
                        {
                            actions.RecognitionException( nvae );
                        }
                        input.Consume(); // recover
                        throw nvae;
                    }
                    input.Rewind( m );
                    int parseAlt =
                        s.TranslateDisplayAltToWalkAlt( predictedAlt );
                    //if ( s.nfa.grammar.type != GrammarType.Lexer )
                    //{
                    //    Console.Out.WriteLine( "predicted alt " + predictedAlt + ", parseAlt " + parseAlt );
                    //}
                    NFAState alt;
                    if ( parseAlt > s.nfa.grammar.GetNumberOfAltsForDecisionNFA( s ) )
                    {
                        // implied branch of loop etc...
                        alt = s.nfa.grammar.nfa.GetState( s.endOfBlockStateNumber );
                    }
                    else
                    {
                        alt = s.nfa.grammar.GetNFAStateForAltOfDecision( s, parseAlt );
                    }
                    s = (NFAState)alt.transition[0].target;
                    continue;
                }

                // CASE 2: finished matching a rule
                if ( s.IsAcceptState )
                { // end of rule node
                    if ( actions != null )
                    {
                        actions.ExitRule( s.nfa.grammar.FileName, s.enclosingRule.Name );
                    }
                    if ( ruleInvocationStack.Count == 0 )
                    {
                        // done parsing.  Hit the start state.
                        //Console.Out.WriteLine( "stack empty in stop state for " + s.enclosingRule );
                        break;
                    }
                    // pop invoking state off the stack to know where to return to
                    NFAState invokingState = (NFAState)ruleInvocationStack.Pop();
                    RuleClosureTransition invokingTransition =
                            (RuleClosureTransition)invokingState.transition[0];
                    // move to node after state that invoked this rule
                    s = invokingTransition.followState;
                    continue;
                }

                Transition trans = s.transition[0];
                Label label = trans.label;
                if ( label.IsSemanticPredicate )
                {
                    FailedPredicateException fpe =
                        new FailedPredicateException( input,
                                                     s.enclosingRule.Name,
                                                     "can't deal with predicates yet" );
                    if ( actions != null )
                    {
                        actions.RecognitionException( fpe );
                    }
                }

                // CASE 3: epsilon transition
                if ( label.IsEpsilon )
                {
                    // CASE 3a: rule invocation state
                    if ( trans is RuleClosureTransition )
                    {
                        ruleInvocationStack.Push( s );
                        s = (NFAState)trans.target;
                        //Console.Out.WriteLine( "call " + s.enclosingRule.name + " from " + s.nfa.grammar.getFileName() );
                        if ( actions != null )
                        {
                            actions.EnterRule( s.nfa.grammar.FileName, s.enclosingRule.Name );
                        }
                        // could be jumping to new grammar, make sure DFA created
                        if ( !s.nfa.grammar.AllDecisionDFAHaveBeenCreated )
                        {
                            s.nfa.grammar.CreateLookaheadDFAs();
                        }
                    }
                    // CASE 3b: plain old epsilon transition, just move
                    else
                    {
                        s = (NFAState)trans.target;
                    }
                }

                // CASE 4: match label on transition
                else if ( label.Matches( t ) )
                {
                    if ( actions != null )
                    {
                        if ( s.nfa.grammar.type == GrammarType.Parser ||
                             s.nfa.grammar.type == GrammarType.Combined )
                        {
                            actions.ConsumeToken( ( (ITokenStream)input ).LT( 1 ) );
                        }
                    }
                    s = (NFAState)s.transition[0].target;
                    input.Consume();
                    t = input.LA( 1 );
                }

                // CASE 5: error condition; label is inconsistent with input
                else
                {
                    if ( label.IsAtom )
                    {
                        MismatchedTokenException mte =
                            new MismatchedTokenException( label.Atom, input );
                        if ( actions != null )
                        {
                            actions.RecognitionException( mte );
                        }
                        input.Consume(); // recover
                        throw mte;
                    }
                    else if ( label.IsSet )
                    {
                        MismatchedSetException mse =
                            new MismatchedSetException( ( (IntervalSet)label.Set ).ToRuntimeBitSet(),
                                                       input );
                        if ( actions != null )
                        {
                            actions.RecognitionException( mse );
                        }
                        input.Consume(); // recover
                        throw mse;
                    }
                    else if ( label.IsSemanticPredicate )
                    {
                        FailedPredicateException fpe =
                            new FailedPredicateException( input,
                                                         s.enclosingRule.Name,
                                                         label.SemanticContext.ToString() );
                        if ( actions != null )
                        {
                            actions.RecognitionException( fpe );
                        }
                        input.Consume(); // recover
                        throw fpe;
                    }
                    else
                    {
                        throw new RecognitionException( input ); // unknown error
                    }
                }
            }
            //Console.Out.WriteLine( "hit stop state for " + stop.enclosingRule );
            if ( actions != null )
            {
                actions.ExitRule( s.nfa.grammar.FileName, stop.enclosingRule.Name );
            }
        }
Пример #21
0
 protected static void DebugSemanticPredicate( IDebugEventListener dbg, bool result, string predicate )
 {
     if ( dbg != null )
         dbg.SemanticPredicate( result, predicate );
 }
Пример #22
0
        /** For a given input char stream, try to match against the NFA
         *  starting at startRule.  This is a deterministic parse even though
         *  it is using an NFA because it uses DFAs at each decision point to
         *  predict which alternative will succeed.  This is exactly what the
         *  generated parser will do.
         *
         *  This only does lexer grammars.
         *
         *  Return the token type associated with the final rule end state.
         */
        public virtual void Scan( String startRule,
                         IDebugEventListener actions,
                         IList visitedStates )
        {
            if ( grammar.type != GrammarType.Lexer )
            {
                return;
            }
            ICharStream @in = (ICharStream)this.input;
            //Console.Out.WriteLine( "scan(" + startRule + ",'" + @in.substring( @in.Index, @in.Size() - 1 ) + "')" );
            // Build NFAs/DFAs from the grammar AST if NFAs haven't been built yet
            if ( grammar.GetRuleStartState( startRule ) == null )
            {
                grammar.BuildNFA();
            }

            if ( !grammar.AllDecisionDFAHaveBeenCreated )
            {
                // Create the DFA predictors for each decision
                grammar.CreateLookaheadDFAs();
            }

            // do the parse
            Stack<object> ruleInvocationStack = new Stack<object>();
            NFAState start = grammar.GetRuleStartState( startRule );
            NFAState stop = grammar.GetRuleStopState( startRule );
            ParseEngine( startRule, start, stop, @in, ruleInvocationStack,
                        actions, visitedStates );
        }
Пример #23
0
 protected static void DebugEnterAlt( IDebugEventListener dbg, int alt )
 {
     if ( dbg != null )
         dbg.EnterAlt( alt );
 }
Пример #24
0
 protected static void DebugEnterSubRule( IDebugEventListener dbg, int decisionNumber )
 {
     if ( dbg != null )
         dbg.EnterSubRule( decisionNumber );
 }
Пример #25
0
 protected static void DebugExitRule( IDebugEventListener dbg, string grammarFileName, string ruleName )
 {
     if ( dbg != null )
         dbg.ExitRule( grammarFileName, ruleName );
 }
Пример #26
0
 protected static void DebugLocation( IDebugEventListener dbg, int line, int charPositionInLine )
 {
     if ( dbg != null )
         dbg.Location( line, charPositionInLine );
 }
Пример #27
0
 public virtual void Parse( String startRule,
                   IDebugEventListener actions,
                   IList visitedStates )
 {
     //Console.Out.WriteLine( "parse(" + startRule + ")" );
     // Build NFAs/DFAs from the grammar AST if NFAs haven't been built yet
     if ( grammar.GetRuleStartState( startRule ) == null )
     {
         grammar.BuildNFA();
     }
     if ( !grammar.AllDecisionDFAHaveBeenCreated )
     {
         // Create the DFA predictors for each decision
         grammar.CreateLookaheadDFAs();
     }
     // do the parse
     Stack<object> ruleInvocationStack = new Stack<object>();
     NFAState start = grammar.GetRuleStartState( startRule );
     NFAState stop = grammar.GetRuleStopState( startRule );
     ParseEngine( startRule, start, stop, input, ruleInvocationStack,
                 actions, visitedStates );
 }