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); }
public LeftRecursionDetector(Grammar g, ATN atn) { this.g = g; this.atn = atn; }
public TailEpsilonRemover([NotNull] ATN atn) { this._atn = atn; }
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) { }
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); }
public DFASerializer(DFA dfa, string[] tokenNames, string[] ruleNames, ATN atn) : this(dfa, Vocabulary.FromTokenNames(tokenNames), ruleNames, atn) { }
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; }
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); }
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); }
public DFASerializer([NotNull] DFA dfa, [Nullable] string[] tokenNames, [Nullable] string[] ruleNames, [Nullable] ATN atn) : this(dfa, Vocabulary.FromTokenNames(tokenNames), ruleNames, atn) { }
public FixedCompletionParserATNSimulator(Parser parser, ATN atn, IntervalSet wordlikeTokenTypes) : base(parser, atn) { _wordlikeTokenTypes = wordlikeTokenTypes; }
protected CompletionParserATNSimulator([NotNull] Parser parser, ATN atn) : base(parser, atn) { Requires.NotNull(parser, nameof(parser)); PredictionMode = PredictionMode.Sll; }
public GrammarHighlighterATNSimulator(Lexer recog, ATN atn) : base(recog, atn) { }
public NonCachingParserATNSimulator(Parser parser, ATN atn) : base(parser, atn) { }
public static void Optimize([NotNull] Grammar g, [NotNull] ATN atn) { OptimizeSets(g, atn); OptimizeStates(atn); }
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) { }
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."); }
public GroupHighlighterATNSimulator(Lexer recog, ATN atn, char openDelimiter, char closeDelimiter) : base(recog, atn) { this.OpenDelimiter = openDelimiter; this.CloseDelimiter = closeDelimiter; }
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); }
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); }
public TemplateLexerATNSimulator(Lexer recog, ATN atn, char openDelimiter, char closeDelimiter) : base(recog, atn) { this.openDelimiter = openDelimiter; this.closeDelimiter = closeDelimiter; }
public NonCachingLexerATNSimulator(Lexer recog, ATN atn) : base(recog, atn) { }
protected CompletionParserATNSimulator(Parser parser, ATN atn) : base(parser, atn) { Contract.Requires <ArgumentNullException>(parser != null, "parser"); PredictionMode = PredictionMode.Sll; }