コード例 #1
0
        public TokenStreamVisualizerForm(ITokenStream tokenStream)
        {
            if (tokenStream == null)
            {
                throw new ArgumentNullException("tokenStream");
            }

            InitializeComponent();

            List <IToken> tokens = new List <IToken>();

            int marker          = tokenStream.Mark();
            int currentPosition = tokenStream.Index;

            try
            {
                tokenStream.Seek(0);
                while (tokenStream.LA(1) != CharStreamConstants.EndOfFile)
                {
                    tokenStream.Consume();
                }

                for (int i = 0; i < tokenStream.Count; i++)
                {
                    tokens.Add(tokenStream.Get(i));
                }
            }
            finally
            {
                tokenStream.Rewind(marker);
            }

            this._tokenStream = tokenStream;
            this._tokens      = tokens.ToArray();

            if (tokenStream.TokenSource != null)
            {
                this._tokenNames = tokenStream.TokenSource.TokenNames;
            }

            this._tokenNames = this._tokenNames ?? new string[0];

            UpdateTokenTypes();
            UpdateHighlighting();

            listBox1.BackColor = Color.Wheat;
        }
コード例 #2
0
ファイル: Parser.cs プロジェクト: achoudh5/prando
        protected bool EvaluatePredicate(System.Action predicate)
        {
            state.backtracking++;
            int start = input.Mark();

            try
            {
                predicate();
            }
            catch (RecognitionException re)
            {
                System.Console.Error.WriteLine("impossible: " + re);
            }
            bool success = !state.failed;

            input.Rewind(start);
            state.backtracking--;
            state.failed = false;
            return(success);
        }
コード例 #3
0
        public TokenStreamVisualizerForm( ITokenStream tokenStream )
        {
            if (tokenStream == null)
                throw new ArgumentNullException("tokenStream");

            InitializeComponent();

            List<IToken> tokens = new List<IToken>();

            int marker = tokenStream.Mark();
            int currentPosition = tokenStream.Index;
            try
            {
                tokenStream.Seek(0);
                while (tokenStream.LA(1) != CharStreamConstants.EndOfFile)
                    tokenStream.Consume();

                for (int i = 0; i < tokenStream.Count; i++)
                    tokens.Add(tokenStream.Get(i));
            }
            finally
            {
                tokenStream.Rewind(marker);
            }

            this._tokenStream = tokenStream;
            this._tokens = tokens.ToArray();

            if (tokenStream.TokenSource != null)
                this._tokenNames = tokenStream.TokenSource.TokenNames;

            this._tokenNames = this._tokenNames ?? new string[0];

            UpdateTokenTypes();
            UpdateHighlighting();

            listBox1.BackColor = Color.Wheat;
        }
コード例 #4
0
 public virtual int Mark()
 {
     lastMarker = input.Mark();
     dbg.Mark(lastMarker);
     return(lastMarker);
 }
コード例 #5
0
ファイル: ParserATNSimulator.cs プロジェクト: antlr/antlr4
        public virtual int AdaptivePredict(ITokenStream input, int decision,
								   ParserRuleContext outerContext)
        {
            if (debug || debug_list_atn_decisions)
            {
                Console.WriteLine("adaptivePredict decision " + decision +
                                       " exec LA(1)==" + GetLookaheadName(input) +
                                  " line " + input.LT(1).Line + ":" + input.LT(1).Column);
            }

            this.input = input;
            startIndex = input.Index;
            context = outerContext;
            DFA dfa = decisionToDFA[decision];
            thisDfa = dfa;

            int m = input.Mark();
            int index = startIndex;

            // Now we are certain to have a specific decision's DFA
            // But, do we still need an initial state?
            try
            {
                DFAState s0;
                if (dfa.IsPrecedenceDfa)
                {
                    // the start state for a precedence DFA depends on the current
                    // parser precedence, and is provided by a DFA method.
                    s0 = dfa.GetPrecedenceStartState(parser.Precedence);
                }
                else {
                    // the start state for a "regular" DFA is just s0
                    s0 = dfa.s0;
                }

                if (s0 == null)
                {
                    if (outerContext == null) outerContext = ParserRuleContext.EmptyContext;
                    if (debug || debug_list_atn_decisions)
                    {
                        Console.WriteLine("predictATN decision " + dfa.decision +
                                           " exec LA(1)==" + GetLookaheadName(input) +
                                           ", outerContext=" + outerContext.ToString(parser));
                    }

                    bool fullCtx = false;
                    ATNConfigSet s0_closure =
                        ComputeStartState(dfa.atnStartState,
                                          ParserRuleContext.EmptyContext,
                                          fullCtx);

                    if (dfa.IsPrecedenceDfa)
                    {
                        /* If this is a precedence DFA, we use applyPrecedenceFilter
                         * to convert the computed start state to a precedence start
                         * state. We then use DFA.setPrecedenceStartState to set the
                         * appropriate start state for the precedence level rather
                         * than simply setting DFA.s0.
                         */
                        dfa.s0.configSet = s0_closure; // not used for prediction but useful to know start configs anyway
                        s0_closure = ApplyPrecedenceFilter(s0_closure);
                        s0 = AddDFAState(dfa, new DFAState(s0_closure));
                        dfa.SetPrecedenceStartState(parser.Precedence, s0);
                    }
                    else {
                        s0 = AddDFAState(dfa, new DFAState(s0_closure));
                        dfa.s0 = s0;
                    }
                }

                int alt = ExecATN(dfa, s0, input, index, outerContext);
                if (debug)
                    Console.WriteLine("DFA after predictATN: " + dfa.ToString(parser.Vocabulary));
                return alt;
            }
            finally
            {
                mergeCache = null; // wack cache after each prediction
                thisDfa = null;
                input.Seek(index);
                input.Release(m);
            }
        }