public static Antlr4.Runtime.Atn.PredictionContext GetCachedContext(Antlr4.Runtime.Atn.PredictionContext context, ConcurrentDictionary <Antlr4.Runtime.Atn.PredictionContext, Antlr4.Runtime.Atn.PredictionContext> contextCache, PredictionContext.IdentityHashMap visited) { if (context.IsEmpty) { return(context); } Antlr4.Runtime.Atn.PredictionContext existing; if (visited.TryGetValue(context, out existing)) { return(existing); } if (contextCache.TryGetValue(context, out existing)) { visited[context] = existing; return(existing); } bool changed = false; Antlr4.Runtime.Atn.PredictionContext[] parents = new Antlr4.Runtime.Atn.PredictionContext[context.Size]; for (int i = 0; i < parents.Length; i++) { Antlr4.Runtime.Atn.PredictionContext parent = GetCachedContext(context.GetParent(i), contextCache, visited); if (changed || parent != context.GetParent(i)) { if (!changed) { parents = new Antlr4.Runtime.Atn.PredictionContext[context.Size]; for (int j = 0; j < context.Size; j++) { parents[j] = context.GetParent(j); } changed = true; } parents[i] = parent; } } if (!changed) { existing = contextCache.GetOrAdd(context, context); visited[context] = existing; return(context); } // We know parents.length>0 because context.isEmpty() is checked at the beginning of the method. Antlr4.Runtime.Atn.PredictionContext updated; if (parents.Length == 1) { updated = new SingletonPredictionContext(parents[0], context.GetReturnState(0)); } else { ArrayPredictionContext arrayPredictionContext = (ArrayPredictionContext)context; updated = new ArrayPredictionContext(parents, arrayPredictionContext.returnStates, context.cachedHashCode); } existing = contextCache.GetOrAdd(updated, updated); visited[updated] = existing; visited[context] = existing; return(updated); }
protected internal static int CalculateHashCode(Antlr4.Runtime.Atn.PredictionContext parent, int returnState) { int hash = MurmurHash.Initialize(InitialHash); hash = MurmurHash.Update(hash, parent); hash = MurmurHash.Update(hash, returnState); hash = MurmurHash.Finish(hash, 2); return(hash); }
public static PredictionContext FromRuleContext(ATN atn, RuleContext outerContext) { if (outerContext == null) { outerContext = ParserRuleContext.EMPTY; } if (outerContext.Parent == null || outerContext == ParserRuleContext.EMPTY) { return(PredictionContext.EMPTY); } PredictionContext parent = PredictionContext.FromRuleContext(atn, outerContext.Parent); ATNState state = atn.states[outerContext.invokingState]; RuleTransition transition = (RuleTransition)state.Transition(0); return(parent.GetChild(transition.followState.stateNumber)); }
public PredictionContext getCachedContext(PredictionContext context) { if (sharedContextCache == null) { return(context); } lock (sharedContextCache) { PredictionContext.IdentityHashMap visited = new PredictionContext.IdentityHashMap(); return(PredictionContext.GetCachedContext(context, sharedContextCache, visited)); } }
public virtual PredictionContext GetChild(PredictionContext context, int invokingState) { if (!enableCache) { return(context.GetChild(invokingState)); } PredictionContextCache.PredictionContextAndInt operands = new PredictionContextCache.PredictionContextAndInt(context, invokingState); PredictionContext result; if (!childContexts.TryGetValue(operands, out result)) { result = context.GetChild(invokingState); result = GetAsCached(result); childContexts[operands] = result; } return(result); }
public virtual bool Contains(Antlr4.Runtime.Atn.ATNConfig subconfig) { if (this.State.stateNumber != subconfig.State.stateNumber || this.Alt != subconfig .Alt || !this.SemanticContext.Equals(subconfig.SemanticContext)) { return(false); } Stack <PredictionContext> leftWorkList = new Stack <PredictionContext>(); Stack <PredictionContext> rightWorkList = new Stack <PredictionContext>(); leftWorkList.Push(Context); rightWorkList.Push(subconfig.Context); while (leftWorkList.Count > 0) { PredictionContext left = leftWorkList.Pop(); PredictionContext right = rightWorkList.Pop(); if (left == right) { return(true); } if (left.Size < right.Size) { return(false); } if (right.IsEmpty) { return(left.HasEmpty); } else { for (int i = 0; i < right.Size; i++) { int index = left.FindReturnState(right.GetReturnState(i)); if (index < 0) { // assumes invokingStates has no duplicate entries return(false); } leftWorkList.Push(left.GetParent(index)); rightWorkList.Push(right.GetParent(i)); } } } return(false); }
public virtual PredictionContext Join(PredictionContext x, PredictionContext y) { if (!enableCache) { return(PredictionContext.Join(x, y, this)); } PredictionContextCache.IdentityCommutativePredictionContextOperands operands = new PredictionContextCache.IdentityCommutativePredictionContextOperands(x, y); PredictionContext result; if (joinContexts.TryGetValue(operands, out result)) { return(result); } result = PredictionContext.Join(x, y, this); result = GetAsCached(result); joinContexts[operands] = result; return(result); }
protected static void CombineCommonParents(PredictionContext[] parents) { Dictionary <PredictionContext, PredictionContext> uniqueParents = new Dictionary <PredictionContext, PredictionContext>(); for (int p = 0; p < parents.Length; p++) { PredictionContext parent = parents[p]; if (!uniqueParents.ContainsKey(parent)) { // don't replace uniqueParents.Put(parent, parent); } } for (int p = 0; p < parents.Length; p++) { parents[p] = uniqueParents.Get(parents[p]); } }
public PredictionContext Get(PredictionContext a, PredictionContext b) { Dictionary <PredictionContext, PredictionContext> first; if (!data.TryGetValue(a, out first)) { return(null); } PredictionContext value; if (first.TryGetValue(b, out value)) { return(value); } else { return(null); } }
/** * Adding a new config means merging contexts with existing configs for * {@code (s, i, pi, _)}, where {@code s} is the * {@link ATNConfig#state}, {@code i} is the {@link ATNConfig#alt}, and * {@code pi} is the {@link ATNConfig#semanticContext}. We use * {@code (s,i,pi)} as key. * * <p>This method updates {@link #dipsIntoOuterContext} and * {@link #hasSemanticContext} when necessary.</p> */ public bool Add(ATNConfig config, MergeCache mergeCache) { if (readOnly) { throw new Exception("This set is readonly"); } if (config.semanticContext != SemanticContext.NONE) { hasSemanticContext = true; } if (config.OuterContextDepth > 0) { dipsIntoOuterContext = true; } ATNConfig existing = configLookup.GetOrAdd(config); if (existing == config) { // we added this new one cachedHashCode = -1; configs.Add(config); // track order here return(true); } // a previous (s,i,pi,_), merge with it and save result bool rootIsWildcard = !fullCtx; PredictionContext merged = PredictionContext.Merge(existing.context, config.context, rootIsWildcard, mergeCache); // no need to check for existing.context, config.context in cache // since only way to create new graphs is "call rule" and here. We // cache at both places. existing.reachesIntoOuterContext = Math.Max(existing.reachesIntoOuterContext, config.reachesIntoOuterContext); // make sure to preserve the precedence filter suppression during the merge if (config.IsPrecedenceFilterSuppressed) { existing.SetPrecedenceFilterSuppressed(true); } existing.context = merged; // replace context; no need to alt mapping return(true); }
internal static PredictionContext Merge(PredictionContext a, PredictionContext b, bool rootIsWildcard, MergeCache mergeCache) { if (a == b || a.Equals(b)) { return(a); } if (a is SingletonPredictionContext && b is SingletonPredictionContext) { return(MergeSingletons((SingletonPredictionContext)a, (SingletonPredictionContext)b, rootIsWildcard, mergeCache)); } // At least one of a or b is array // If one is $ and rootIsWildcard, return $ as * wildcard if (rootIsWildcard) { if (a is EmptyPredictionContext) { return(a); } if (b is EmptyPredictionContext) { return(b); } } // convert singleton so both are arrays to normalize if (a is SingletonPredictionContext) { a = new ArrayPredictionContext((SingletonPredictionContext)a); } if (b is SingletonPredictionContext) { b = new ArrayPredictionContext((SingletonPredictionContext)b); } return(MergeArrays((ArrayPredictionContext)a, (ArrayPredictionContext)b, rootIsWildcard, mergeCache)); }
public abstract Antlr4.Runtime.Atn.PredictionContext AppendContext(Antlr4.Runtime.Atn.PredictionContext suffix, PredictionContextCache contextCache);
public static bool IsEmptyLocal(Antlr4.Runtime.Atn.PredictionContext context) { return(context == EmptyLocal); }
internal static Antlr4.Runtime.Atn.PredictionContext Join(Antlr4.Runtime.Atn.PredictionContext context0, Antlr4.Runtime.Atn.PredictionContext context1, PredictionContextCache contextCache) { if (context0 == context1) { return(context0); } if (context0.IsEmpty) { return(IsEmptyLocal(context0) ? context0 : AddEmptyContext(context1)); } else { if (context1.IsEmpty) { return(IsEmptyLocal(context1) ? context1 : AddEmptyContext(context0)); } } int context0size = context0.Size; int context1size = context1.Size; if (context0size == 1 && context1size == 1 && context0.GetReturnState(0) == context1 .GetReturnState(0)) { Antlr4.Runtime.Atn.PredictionContext merged = contextCache.Join(context0.GetParent (0), context1.GetParent(0)); if (merged == context0.GetParent(0)) { return(context0); } else { if (merged == context1.GetParent(0)) { return(context1); } else { return(merged.GetChild(context0.GetReturnState(0))); } } } int count = 0; Antlr4.Runtime.Atn.PredictionContext[] parentsList = new Antlr4.Runtime.Atn.PredictionContext [context0size + context1size]; int[] returnStatesList = new int[parentsList.Length]; int leftIndex = 0; int rightIndex = 0; bool canReturnLeft = true; bool canReturnRight = true; while (leftIndex < context0size && rightIndex < context1size) { if (context0.GetReturnState(leftIndex) == context1.GetReturnState(rightIndex)) { parentsList[count] = contextCache.Join(context0.GetParent(leftIndex), context1.GetParent (rightIndex)); returnStatesList[count] = context0.GetReturnState(leftIndex); canReturnLeft = canReturnLeft && parentsList[count] == context0.GetParent(leftIndex ); canReturnRight = canReturnRight && parentsList[count] == context1.GetParent(rightIndex ); leftIndex++; rightIndex++; } else { if (context0.GetReturnState(leftIndex) < context1.GetReturnState(rightIndex)) { parentsList[count] = context0.GetParent(leftIndex); returnStatesList[count] = context0.GetReturnState(leftIndex); canReturnRight = false; leftIndex++; } else { System.Diagnostics.Debug.Assert(context1.GetReturnState(rightIndex) < context0.GetReturnState (leftIndex)); parentsList[count] = context1.GetParent(rightIndex); returnStatesList[count] = context1.GetReturnState(rightIndex); canReturnLeft = false; rightIndex++; } } count++; } while (leftIndex < context0size) { parentsList[count] = context0.GetParent(leftIndex); returnStatesList[count] = context0.GetReturnState(leftIndex); leftIndex++; canReturnRight = false; count++; } while (rightIndex < context1size) { parentsList[count] = context1.GetParent(rightIndex); returnStatesList[count] = context1.GetReturnState(rightIndex); rightIndex++; canReturnLeft = false; count++; } if (canReturnLeft) { return(context0); } else { if (canReturnRight) { return(context1); } } if (count < parentsList.Length) { parentsList = Arrays.CopyOf(parentsList, count); returnStatesList = Arrays.CopyOf(returnStatesList, count); } if (parentsList.Length == 0) { // if one of them was EMPTY_LOCAL, it would be empty and handled at the beginning of the method return(EmptyFull); } else { if (parentsList.Length == 1) { return(new SingletonPredictionContext(parentsList[0], returnStatesList[0])); } else { return(new ArrayPredictionContext(parentsList, returnStatesList)); } } }
public static Antlr4.Runtime.Atn.PredictionContext Join(Antlr4.Runtime.Atn.PredictionContext context0, Antlr4.Runtime.Atn.PredictionContext context1) { return(Join(context0, context1, PredictionContextCache.Uncached)); }
private static Antlr4.Runtime.Atn.PredictionContext RemoveEmptyContext(Antlr4.Runtime.Atn.PredictionContext context) { return(context.RemoveEmptyContext()); }
public static Antlr4.Runtime.Atn.ATNConfig Create([NotNull] ATNState state, int alt, [Nullable] PredictionContext context, [NotNull] Antlr4.Runtime.Atn.SemanticContext semanticContext, LexerActionExecutor lexerActionExecutor) { if (semanticContext != Antlr4.Runtime.Atn.SemanticContext.None) { if (lexerActionExecutor != null) { return(new ATNConfig.ActionSemanticContextATNConfig(lexerActionExecutor, semanticContext, state, alt, context, false)); } else { return(new ATNConfig.SemanticContextATNConfig(semanticContext, state, alt, context)); } } else { if (lexerActionExecutor != null) { return(new ATNConfig.ActionATNConfig(lexerActionExecutor, state, alt, context, false)); } else { return(new Antlr4.Runtime.Atn.ATNConfig(state, alt, context)); } } }
protected internal ATNConfig(Antlr4.Runtime.Atn.ATNConfig c, ATNState state, PredictionContext context) { this.state = state; this.altAndOuterContextDepth = c.altAndOuterContextDepth & unchecked ((int)(0x7FFFFFFF)); this.context = context; }
protected internal ActionATNConfig(LexerActionExecutor lexerActionExecutor, ATNConfig c, ATNState state, PredictionContext context, bool passedThroughNonGreedyDecision) : base(c, state, context) { if (c.SemanticContext != SemanticContext.None) { throw new NotSupportedException(); } this.lexerActionExecutor = lexerActionExecutor; this.passedThroughNonGreedyDecision = passedThroughNonGreedyDecision; }
public SemanticContextATNConfig(Antlr4.Runtime.Atn.SemanticContext semanticContext, ATNConfig c, ATNState state, PredictionContext context) : base(c, state, context) { this.semanticContext = semanticContext; }
public virtual string[] ToStrings(IRecognizer recognizer, Antlr4.Runtime.Atn.PredictionContext stop, int currentState) { List <string> result = new List <string>(); for (int perm = 0; ; perm++) { int offset = 0; bool last = true; Antlr4.Runtime.Atn.PredictionContext p = this; int stateNumber = currentState; StringBuilder localBuffer = new StringBuilder(); localBuffer.Append("["); while (!p.IsEmpty && p != stop) { int index = 0; if (p.Size > 0) { int bits = 1; while ((1 << bits) < p.Size) { bits++; } int mask = (1 << bits) - 1; index = (perm >> offset) & mask; last &= index >= p.Size - 1; if (index >= p.Size) { goto outer_continue; } offset += bits; } if (recognizer != null) { if (localBuffer.Length > 1) { // first char is '[', if more than that this isn't the first rule localBuffer.Append(' '); } ATN atn = recognizer.Atn; ATNState s = atn.states[stateNumber]; string ruleName = recognizer.RuleNames[s.ruleIndex]; localBuffer.Append(ruleName); } else { if (p.GetReturnState(index) != EmptyFullStateKey) { if (!p.IsEmpty) { if (localBuffer.Length > 1) { // first char is '[', if more than that this isn't the first rule localBuffer.Append(' '); } localBuffer.Append(p.GetReturnState(index)); } } } stateNumber = p.GetReturnState(index); p = p.GetParent(index); } localBuffer.Append("]"); result.Add(localBuffer.ToString()); if (last) { break; } outer_continue :; } return(result.ToArray()); }
public PredictionContextAndInt(PredictionContext obj, int value) { this.obj = obj; this.value = value; }
public Antlr4.Runtime.Atn.ATNConfig Transform(ATNState state, PredictionContext context, bool checkNonGreedy) { return(Transform(state, context, this.SemanticContext, checkNonGreedy, this.ActionExecutor)); }
public SemanticContextATNConfig(Antlr4.Runtime.Atn.SemanticContext semanticContext, [NotNull] ATNConfig c, [NotNull] ATNState state, [Nullable] PredictionContext context) : base(c, state, context) { this.semanticContext = semanticContext; }
public ActionATNConfig(LexerActionExecutor lexerActionExecutor, ATNState state, int alt, PredictionContext context, bool passedThroughNonGreedyDecision) : base(state, alt, context) { this.lexerActionExecutor = lexerActionExecutor; this.passedThroughNonGreedyDecision = passedThroughNonGreedyDecision; }
public ActionSemanticContextATNConfig(LexerActionExecutor lexerActionExecutor, [NotNull] SemanticContext semanticContext, [NotNull] ATNState state, int alt, [Nullable] PredictionContext context, bool passedThroughNonGreedyDecision) : base(semanticContext, state, alt, context) { this.lexerActionExecutor = lexerActionExecutor; this.passedThroughNonGreedyDecision = passedThroughNonGreedyDecision; }
public ActionSemanticContextATNConfig(LexerActionExecutor lexerActionExecutor, SemanticContext semanticContext, ATNConfig c, ATNState state, PredictionContext context, bool passedThroughNonGreedyDecision) : base(semanticContext, c, state, context) { this.lexerActionExecutor = lexerActionExecutor; this.passedThroughNonGreedyDecision = passedThroughNonGreedyDecision; }
protected internal ATNConfig([NotNull] Antlr4.Runtime.Atn.ATNConfig c, [NotNull] ATNState state, [NotNull] PredictionContext context) { this.state = state; this.altAndOuterContextDepth = c.altAndOuterContextDepth; this.context = context; }
public static Antlr4.Runtime.Atn.ATNConfig Create(ATNState state, int alt, PredictionContext context, Antlr4.Runtime.Atn.SemanticContext semanticContext) { return(Create(state, alt, context, semanticContext, null)); }
public static Antlr4.Runtime.Atn.ATNConfig Create([NotNull] ATNState state, int alt, [Nullable] PredictionContext context) { return(Create(state, alt, context, Antlr4.Runtime.Atn.SemanticContext.None, null)); }