예제 #1
0
 public LexerInterpreter(string grammarFileName, IVocabulary vocabulary, IEnumerable <string> ruleNames, IEnumerable <string> channelNames, IEnumerable <string> modeNames, ATN atn, ICharStream input)
     : base(input)
 {
     if (atn.grammarType != ATNType.Lexer)
     {
         throw new ArgumentException("The ATN must be a lexer ATN.");
     }
     this.grammarFileName = grammarFileName;
     this.atn             = atn;
     this.ruleNames       = ruleNames.ToArray();
     this.channelNames    = channelNames.ToArray();
     this.modeNames       = modeNames.ToArray();
     this.vocabulary      = vocabulary;
     this.decisionToDFA   = new DFA[atn.NumberOfDecisions];
     for (int i = 0; i < decisionToDFA.Length; i++)
     {
         decisionToDFA[i] = new DFA(atn.GetDecisionState(i), i);
     }
     this.Interpreter = new LexerATNSimulator(this, atn, decisionToDFA, sharedContextCache);
 }
예제 #2
0
 public LeftRecursionDetector(Grammar g, ATN atn)
 {
     this.g   = g;
     this.atn = atn;
 }
예제 #3
0
 public TailEpsilonRemover([NotNull] ATN atn)
 {
     this._atn = atn;
 }
예제 #4
0
 public LexerInterpreter(string grammarFileName, IVocabulary vocabulary, IEnumerable <string> ruleNames, IEnumerable <string> modeNames, ATN atn, ICharStream input)
     : this(grammarFileName, vocabulary, ruleNames, new string[0], modeNames, atn, input)
 {
 }
예제 #5
0
 public ParserInterpreter(string grammarFileName, IVocabulary vocabulary, IEnumerable <string> ruleNames, ATN atn, ITokenStream input)
     : base(input)
 {
     this._grammarFileName = grammarFileName;
     this._atn             = atn;
     this._ruleNames       = ruleNames.ToArray();
     this.vocabulary       = vocabulary;
     // identify the ATN states where pushNewRecursionContext must be called
     this.pushRecursionContextStates = new BitSet(atn.states.Count);
     foreach (ATNState state in atn.states)
     {
         if (!(state is StarLoopEntryState))
         {
             continue;
         }
         if (((StarLoopEntryState)state).isPrecedenceDecision)
         {
             this.pushRecursionContextStates.Set(state.stateNumber);
         }
     }
     // get atn simulator that knows how to do predictions
     Interpreter = new ParserATNSimulator(this, atn, null, null);
 }
예제 #6
0
 public DFASerializer(DFA dfa, string[] tokenNames, string[] ruleNames, ATN atn)
     : this(dfa, Vocabulary.FromTokenNames(tokenNames), ruleNames, atn)
 {
 }
예제 #7
0
 public DFASerializer([NotNull] DFA dfa, [NotNull] IVocabulary vocabulary, [Nullable] string[] ruleNames, [Nullable] ATN atn)
 {
     this.dfa        = dfa;
     this.vocabulary = vocabulary;
     this.ruleNames  = ruleNames;
     this.atn        = atn;
 }
예제 #8
0
        public ParserInterpreter(string grammarFileName, IVocabulary vocabulary, IEnumerable <string> ruleNames, ATN atn, ITokenStream input)
            : base(input)
        {
            this.grammarFileName = grammarFileName;
            this.atn             = atn;
#pragma warning disable 612 // Type or member is obsolete
            this.tokenNames = new string[atn.maxTokenType];
            for (int i = 0; i < tokenNames.Length; i++)
            {
                tokenNames[i] = vocabulary.GetDisplayName(i);
            }
#pragma warning restore 612 // Type or member is obsolete
            this.ruleNames  = ruleNames.ToArray();
            this.vocabulary = vocabulary;
            // identify the ATN states where pushNewRecursionContext() must be called
            this.pushRecursionContextStates = new BitSet(atn.states.Count);
            foreach (ATNState state in atn.states)
            {
                if (!(state is StarLoopEntryState))
                {
                    continue;
                }
                if (((StarLoopEntryState)state).precedenceRuleDecision)
                {
                    this.pushRecursionContextStates.Set(state.stateNumber);
                }
            }
            // get atn simulator that knows how to do predictions
            Interpreter = new ParserATNSimulator(this, atn);
        }
예제 #9
0
 public LexerInterpreter(string grammarFileName, IVocabulary vocabulary, IEnumerable <string> ruleNames, IEnumerable <string> modeNames, ATN atn, ICharStream input)
     : base(input)
 {
     if (atn.grammarType != ATNType.Lexer)
     {
         throw new ArgumentException("The ATN must be a lexer ATN.");
     }
     this.grammarFileName = grammarFileName;
     this.atn             = atn;
     this.ruleNames       = ruleNames.ToArray();
     this.modeNames       = modeNames.ToArray();
     this.vocabulary      = vocabulary;
     this.Interpreter     = new LexerATNSimulator(this, atn);
 }
예제 #10
0
 public DFASerializer([NotNull] DFA dfa, [Nullable] string[] tokenNames, [Nullable] string[] ruleNames, [Nullable] ATN atn)
     : this(dfa, Vocabulary.FromTokenNames(tokenNames), ruleNames, atn)
 {
 }
예제 #11
0
 public FixedCompletionParserATNSimulator(Parser parser, ATN atn, IntervalSet wordlikeTokenTypes)
     : base(parser, atn)
 {
     _wordlikeTokenTypes = wordlikeTokenTypes;
 }
예제 #12
0
 protected CompletionParserATNSimulator([NotNull] Parser parser, ATN atn)
     : base(parser, atn)
 {
     Requires.NotNull(parser, nameof(parser));
     PredictionMode = PredictionMode.Sll;
 }
예제 #13
0
 public GrammarHighlighterATNSimulator(Lexer recog, ATN atn)
     : base(recog, atn)
 {
 }
예제 #14
0
 public NonCachingParserATNSimulator(Parser parser, ATN atn)
     : base(parser, atn)
 {
 }
예제 #15
0
 public static void Optimize([NotNull] Grammar g, [NotNull] ATN atn)
 {
     OptimizeSets(g, atn);
     OptimizeStates(atn);
 }
예제 #16
0
 public ParserInterpreter(string grammarFileName, IEnumerable <string> tokenNames, IEnumerable <string> ruleNames, ATN atn, ITokenStream input)
     : this(grammarFileName, Antlr4.Runtime.Vocabulary.FromTokenNames(tokenNames.ToArray()), ruleNames, atn, input)
 {
 }
예제 #17
0
        private static void OptimizeSets(Grammar g, ATN atn)
        {
            if (g.IsParser())
            {
                // parser codegen doesn't currently support SetTransition
                return;
            }

            int removedStates = 0;
            IList <DecisionState> decisions = atn.decisionToState;

            foreach (DecisionState decision in decisions)
            {
                if (decision.ruleIndex >= 0)
                {
                    Rule rule = g.GetRule(decision.ruleIndex);
                    if (char.IsLower(rule.name[0]))
                    {
                        // parser codegen doesn't currently support SetTransition
                        continue;
                    }
                }

                IntervalSet setTransitions = new IntervalSet();
                for (int i = 0; i < decision.NumberOfTransitions; i++)
                {
                    Transition epsTransition = decision.Transition(i);
                    if (!(epsTransition is EpsilonTransition))
                    {
                        continue;
                    }

                    if (epsTransition.target.NumberOfTransitions != 1)
                    {
                        continue;
                    }

                    Transition transition = epsTransition.target.Transition(0);
                    if (!(transition.target is BlockEndState))
                    {
                        continue;
                    }

                    if (transition is NotSetTransition)
                    {
                        // TODO: not yet implemented
                        continue;
                    }

                    if (transition is AtomTransition ||
                        transition is RangeTransition ||
                        transition is SetTransition)
                    {
                        setTransitions.Add(i);
                    }
                }

                // due to min alt resolution policies, can only collapse sequential alts
                for (int i = setTransitions.GetIntervals().Count - 1; i >= 0; i--)
                {
                    Interval interval = setTransitions.GetIntervals()[i];
                    if (interval.Length <= 1)
                    {
                        continue;
                    }

                    ATNState    blockEndState = decision.Transition(interval.a).target.Transition(0).target;
                    IntervalSet matchSet      = new IntervalSet();
                    for (int j = interval.a; j <= interval.b; j++)
                    {
                        Transition matchTransition = decision.Transition(j).target.Transition(0);
                        if (matchTransition is NotSetTransition)
                        {
                            throw new NotImplementedException();
                        }

                        IntervalSet set     = matchTransition.Label;
                        int         minElem = set.MinElement;
                        int         maxElem = set.MaxElement;
                        for (int k = minElem; k <= maxElem; k++)
                        {
                            if (matchSet.Contains(k))
                            {
                                char setMin = (char)set.MinElement;
                                char setMax = (char)set.MaxElement;
                                // TODO: Token is missing (i.e. position in source will not be displayed).
                                g.tool.errMgr.GrammarError(ErrorType.CHARACTERS_COLLISION_IN_SET, g.fileName,
                                                           null, (char)minElem + "-" + (char)maxElem, "[" + setMin + "-" + setMax + "]");
                                break;
                            }
                        }

                        matchSet.AddAll(set);
                    }

                    Transition newTransition;
                    if (matchSet.GetIntervals().Count == 1)
                    {
                        if (matchSet.Count == 1)
                        {
                            newTransition = new AtomTransition(blockEndState, matchSet.MinElement);
                        }
                        else
                        {
                            Interval matchInterval = matchSet.GetIntervals()[0];
                            newTransition = new RangeTransition(blockEndState, matchInterval.a, matchInterval.b);
                        }
                    }
                    else
                    {
                        newTransition = new SetTransition(blockEndState, matchSet);
                    }

                    decision.Transition(interval.a).target.SetTransition(0, newTransition);
                    for (int j = interval.a + 1; j <= interval.b; j++)
                    {
                        Transition removed = decision.Transition(interval.a + 1);
                        decision.RemoveTransition(interval.a + 1);
                        atn.RemoveState(removed.target);
                        removedStates++;
                    }
                }
            }

            //System.Console.WriteLine("ATN optimizer removed " + removedStates + " states by collapsing sets.");
        }
예제 #18
0
 public GroupHighlighterATNSimulator(Lexer recog, ATN atn, char openDelimiter, char closeDelimiter)
     : base(recog, atn)
 {
     this.OpenDelimiter  = openDelimiter;
     this.CloseDelimiter = closeDelimiter;
 }
예제 #19
0
        public LexerInterpreter(string grammarFileName, IVocabulary vocabulary, IEnumerable <string> ruleNames, IEnumerable <string> modeNames, ATN atn, ICharStream input)
            : base(input)
        {
            if (atn.grammarType != ATNType.Lexer)
            {
                throw new ArgumentException("The ATN must be a lexer ATN.");
            }
            this.grammarFileName = grammarFileName;
            this.atn             = atn;
#pragma warning disable 612 // 'fieldName' is obsolete
            this.tokenNames = new string[atn.maxTokenType];
            for (int i = 0; i < tokenNames.Length; i++)
            {
                tokenNames[i] = vocabulary.GetDisplayName(i);
            }
#pragma warning restore 612
            this.ruleNames  = ruleNames.ToArray();
            this.modeNames  = modeNames.ToArray();
            this.vocabulary = vocabulary;
            this._interp    = new LexerATNSimulator(this, atn);
        }
예제 #20
0
        private HashSet <ATNState> ComputeSingle(List <Edge> parse)
        {
            List <Edge>        copy   = parse.ToList();
            HashSet <ATNState> result = new HashSet <ATNState>();

            if (_log_closure)
            {
                System.Console.Error.WriteLine("Computing closure for the following parse:");
                System.Console.Error.Write(PrintSingle(parse));
                System.Console.Error.WriteLine();
            }

            if (!copy.Any())
            {
                return(result);
            }

            Edge last_transaction = copy.First();

            if (last_transaction == null)
            {
                return(result);
            }

            ATNState current_state = last_transaction._to;

            if (current_state == null)
            {
                throw new Exception();
            }

            for (; ;)
            {
                if (_log_closure)
                {
                    System.Console.Error.WriteLine("Getting closure of " + current_state.stateNumber);
                }
                HashSet <ATNState> c = closure(current_state);
                if (_log_closure)
                {
                    System.Console.Error.WriteLine("closure " + string.Join(" ", c.Select(s => s.stateNumber)));
                }
                bool           do_continue = false;
                ATN            atn         = current_state.atn;
                int            rule        = current_state.ruleIndex;
                RuleStartState start_state = atn.ruleToStartState[rule];
                RuleStopState  stop_state  = atn.ruleToStopState[rule];
                bool           changed     = false;
                foreach (ATNState s in c)
                {
                    if (result.Contains(s))
                    {
                        continue;
                    }

                    changed = true;
                    result.Add(s);
                    if (s == stop_state)
                    {
                        do_continue = true;
                    }
                }
                if (!changed)
                {
                    break;
                }

                if (!do_continue)
                {
                    break;
                }

                for (; ;)
                {
                    if (!copy.Any())
                    {
                        break;
                    }

                    copy.RemoveAt(0);
                    if (!copy.Any())
                    {
                        break;
                    }

                    last_transaction = copy.First();
                    if (start_state == last_transaction._from)
                    {
                        copy.RemoveAt(0);
                        if (!copy.Any())
                        {
                            break;
                        }

                        last_transaction = copy.First();
                        // Get follow state of rule-type transition.
                        ATNState from_state = last_transaction._from;
                        if (from_state == null)
                        {
                            break;
                        }

                        ATNState follow_state = last_transaction._follow;
                        current_state = follow_state;
                        if (current_state == null)
                        {
                            throw new Exception();
                        }

                        break;
                    }
                }
            }
            return(result);
        }
예제 #21
0
 public TemplateLexerATNSimulator(Lexer recog, ATN atn, char openDelimiter, char closeDelimiter)
     : base(recog, atn)
 {
     this.openDelimiter  = openDelimiter;
     this.closeDelimiter = closeDelimiter;
 }
예제 #22
0
 public NonCachingLexerATNSimulator(Lexer recog, ATN atn)
     : base(recog, atn)
 {
 }
예제 #23
0
 protected CompletionParserATNSimulator(Parser parser, ATN atn)
     : base(parser, atn)
 {
     Contract.Requires <ArgumentNullException>(parser != null, "parser");
     PredictionMode = PredictionMode.Sll;
 }