public virtual Antlr4.Runtime.Misc.IntervalSet Or(IIntSet a) { Antlr4.Runtime.Misc.IntervalSet o = new Antlr4.Runtime.Misc.IntervalSet(); o.AddAll(this); o.AddAll(a); return(o); }
/** Return whether lookahead sets are disjoint; no lookahead ⇒ not disjoint */ public static bool Disjoint(IntervalSet[] altLook) { bool collision = false; IntervalSet combined = new IntervalSet(); if (altLook == null) { return(false); } foreach (IntervalSet look in altLook) { if (look == null) { return(false); // lookahead must've computation failed } if (!look.And(combined).IsNil) { collision = true; break; } combined.AddAll(look); } return(!collision); }
/// <summary>combine all sets in the array returned the or'd value</summary> public static Antlr4.Runtime.Misc.IntervalSet Or(Antlr4.Runtime.Misc.IntervalSet[] sets) { Antlr4.Runtime.Misc.IntervalSet r = new Antlr4.Runtime.Misc.IntervalSet(); foreach (Antlr4.Runtime.Misc.IntervalSet s in sets) { r.AddAll(s); } return(r); }
public virtual Antlr4.Runtime.Misc.IntervalSet Subtract(IIntSet a) { if (a == null || a.IsNil) { return(new Antlr4.Runtime.Misc.IntervalSet(this)); } if (a is Antlr4.Runtime.Misc.IntervalSet) { return(Subtract(this, (Antlr4.Runtime.Misc.IntervalSet)a)); } Antlr4.Runtime.Misc.IntervalSet other = new Antlr4.Runtime.Misc.IntervalSet(); other.AddAll(a); return(Subtract(this, other)); }
/// <summary> /// <inheritDoc/> /// /// </summary> public virtual Antlr4.Runtime.Misc.IntervalSet Complement(IIntSet vocabulary) { if (vocabulary == null || vocabulary.IsNil) { return(null); } // nothing in common with null set Antlr4.Runtime.Misc.IntervalSet vocabularyIS; if (vocabulary is Antlr4.Runtime.Misc.IntervalSet) { vocabularyIS = (Antlr4.Runtime.Misc.IntervalSet)vocabulary; } else { vocabularyIS = new Antlr4.Runtime.Misc.IntervalSet(); vocabularyIS.AddAll(vocabulary); } return(vocabularyIS.Subtract(this)); }
public override Handle Set(GrammarAST associatedAST, IList<GrammarAST> alts, bool invert) { ATNState left = NewState(associatedAST); ATNState right = NewState(associatedAST); IntervalSet set = new IntervalSet(); foreach (GrammarAST t in alts) { if (t.Type == ANTLRParser.RANGE) { int a = CharSupport.GetCharValueFromGrammarCharLiteral(t.GetChild(0).Text); int b = CharSupport.GetCharValueFromGrammarCharLiteral(t.GetChild(1).Text); if (CheckRange((GrammarAST)t.GetChild(0), (GrammarAST)t.GetChild(1), a, b)) { CheckSetCollision(associatedAST, set, a, b); set.Add(a, b); } } else if (t.Type == ANTLRParser.LEXER_CHAR_SET) { set.AddAll(GetSetFromCharSetLiteral(t)); } else if (t.Type == ANTLRParser.STRING_LITERAL) { int c = CharSupport.GetCharValueFromGrammarCharLiteral(t.Text); if (c != -1) { CheckSetCollision(associatedAST, set, c); set.Add(c); } else { g.tool.errMgr.GrammarError(ErrorType.INVALID_LITERAL_IN_LEXER_SET, g.fileName, t.Token, t.Text); } } else if (t.Type == ANTLRParser.TOKEN_REF) { g.tool.errMgr.GrammarError(ErrorType.UNSUPPORTED_REFERENCE_IN_LEXER_SET, g.fileName, t.Token, t.Text); } } if (invert) { left.AddTransition(new NotSetTransition(right, set)); } else { Transition transition; if (set.GetIntervals().Count == 1) { Interval interval = set.GetIntervals()[0]; transition = new RangeTransition(right, interval.a, interval.b); } else { transition = new SetTransition(right, set); } left.AddTransition(transition); } associatedAST.atnState = left; return new Handle(left, right); }
public virtual Antlr4.Runtime.Misc.IntervalSet Subtract(IIntSet a) { if (a == null || a.IsNil) { return new Antlr4.Runtime.Misc.IntervalSet(this); } if (a is Antlr4.Runtime.Misc.IntervalSet) { return Subtract(this, (Antlr4.Runtime.Misc.IntervalSet)a); } Antlr4.Runtime.Misc.IntervalSet other = new Antlr4.Runtime.Misc.IntervalSet(); other.AddAll(a); return Subtract(this, other); }
public virtual Antlr4.Runtime.Misc.IntervalSet Or(IIntSet a) { Antlr4.Runtime.Misc.IntervalSet o = new Antlr4.Runtime.Misc.IntervalSet(); o.AddAll(this); o.AddAll(a); return o; }
/// <summary> /// <inheritDoc/> /// /// </summary> public virtual Antlr4.Runtime.Misc.IntervalSet Complement(IIntSet vocabulary) { if (vocabulary == null || vocabulary.IsNil) { return null; } // nothing in common with null set Antlr4.Runtime.Misc.IntervalSet vocabularyIS; if (vocabulary is Antlr4.Runtime.Misc.IntervalSet) { vocabularyIS = (Antlr4.Runtime.Misc.IntervalSet)vocabulary; } else { vocabularyIS = new Antlr4.Runtime.Misc.IntervalSet(); vocabularyIS.AddAll(vocabulary); } return vocabularyIS.Subtract(this); }
/// <summary>combine all sets in the array returned the or'd value</summary> public static Antlr4.Runtime.Misc.IntervalSet Or(Antlr4.Runtime.Misc.IntervalSet[] sets) { Antlr4.Runtime.Misc.IntervalSet r = new Antlr4.Runtime.Misc.IntervalSet(); foreach (Antlr4.Runtime.Misc.IntervalSet s in sets) { r.AddAll(s); } return r; }
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 override Handle Set(GrammarAST associatedAST, IList <GrammarAST> alts, bool invert) { ATNState left = NewState(associatedAST); ATNState right = NewState(associatedAST); IntervalSet set = new IntervalSet(); foreach (GrammarAST t in alts) { if (t.Type == ANTLRParser.RANGE) { int a = CharSupport.GetCharValueFromGrammarCharLiteral(t.GetChild(0).Text); int b = CharSupport.GetCharValueFromGrammarCharLiteral(t.GetChild(1).Text); if (CheckRange((GrammarAST)t.GetChild(0), (GrammarAST)t.GetChild(1), a, b)) { CheckSetCollision(associatedAST, set, a, b); set.Add(a, b); } } else if (t.Type == ANTLRParser.LEXER_CHAR_SET) { set.AddAll(GetSetFromCharSetLiteral(t)); } else if (t.Type == ANTLRParser.STRING_LITERAL) { int c = CharSupport.GetCharValueFromGrammarCharLiteral(t.Text); if (c != -1) { CheckSetCollision(associatedAST, set, c); set.Add(c); } else { g.tool.errMgr.GrammarError(ErrorType.INVALID_LITERAL_IN_LEXER_SET, g.fileName, t.Token, t.Text); } } else if (t.Type == ANTLRParser.TOKEN_REF) { g.tool.errMgr.GrammarError(ErrorType.UNSUPPORTED_REFERENCE_IN_LEXER_SET, g.fileName, t.Token, t.Text); } } if (invert) { left.AddTransition(new NotSetTransition(right, set)); } else { Transition transition; if (set.GetIntervals().Count == 1) { Interval interval = set.GetIntervals()[0]; transition = new RangeTransition(right, interval.a, interval.b); } else { transition = new SetTransition(right, set); } left.AddTransition(transition); } associatedAST.atnState = left; return(new Handle(left, right)); }
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."); }
/** Return whether lookahead sets are disjoint; no lookahead ⇒ not disjoint */ public static bool Disjoint(IntervalSet[] altLook) { bool collision = false; IntervalSet combined = new IntervalSet(); if (altLook == null) return false; foreach (IntervalSet look in altLook) { if (look == null) return false; // lookahead must've computation failed if (!look.And(combined).IsNil) { collision = true; break; } combined.AddAll(look); } return !collision; }