本文整理汇总了C#中Antlr4.Runtime.Atn.ATN.AddState方法的典型用法代码示例。如果您正苦于以下问题:C# ATN.AddState方法的具体用法?C# ATN.AddState怎么用?C# ATN.AddState使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Antlr4.Runtime.Atn.ATN
的用法示例。
在下文中一共展示了ATN.AddState方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadStates
protected internal virtual void ReadStates(ATN atn)
{
//
// STATES
//
IList<Tuple<LoopEndState, int>> loopBackStateNumbers = new List<Tuple<LoopEndState, int>>();
IList<Tuple<BlockStartState, int>> endStateNumbers = new List<Tuple<BlockStartState, int>>();
int nstates = ReadInt();
for (int i_1 = 0; i_1 < nstates; i_1++)
{
StateType stype = (StateType)ReadInt();
// ignore bad type of states
if (stype == StateType.InvalidType)
{
atn.AddState(null);
continue;
}
int ruleIndex = ReadInt();
if (ruleIndex == char.MaxValue)
{
ruleIndex = -1;
}
ATNState s = StateFactory(stype, ruleIndex);
if (stype == StateType.LoopEnd)
{
// special case
int loopBackStateNumber = ReadInt();
loopBackStateNumbers.Add(Tuple.Create((LoopEndState)s, loopBackStateNumber));
}
else
{
if (s is BlockStartState)
{
int endStateNumber = ReadInt();
endStateNumbers.Add(Tuple.Create((BlockStartState)s, endStateNumber));
}
}
atn.AddState(s);
}
// delay the assignment of loop back and end states until we know all the state instances have been initialized
foreach (Tuple<LoopEndState, int> pair in loopBackStateNumbers)
{
pair.Item1.loopBackState = atn.states[pair.Item2];
}
foreach (Tuple<BlockStartState, int> pair_1 in endStateNumbers)
{
pair_1.Item1.endState = (BlockEndState)atn.states[pair_1.Item2];
}
int numNonGreedyStates = ReadInt();
for (int i_2 = 0; i_2 < numNonGreedyStates; i_2++)
{
int stateNumber = ReadInt();
((DecisionState)atn.states[stateNumber]).nonGreedy = true;
}
int numPrecedenceStates = ReadInt();
for (int i_4 = 0; i_4 < numPrecedenceStates; i_4++)
{
int stateNumber = ReadInt();
((RuleStartState)atn.states[stateNumber]).isPrecedenceRule = true;
}
}
示例2: GenerateRuleBypassTransitions
protected internal virtual void GenerateRuleBypassTransitions(ATN atn)
{
atn.ruleToTokenType = new int[atn.ruleToStartState.Length];
for (int i_10 = 0; i_10 < atn.ruleToStartState.Length; i_10++)
{
atn.ruleToTokenType[i_10] = atn.maxTokenType + i_10 + 1;
}
for (int i_13 = 0; i_13 < atn.ruleToStartState.Length; i_13++)
{
BasicBlockStartState bypassStart = new BasicBlockStartState();
bypassStart.ruleIndex = i_13;
atn.AddState(bypassStart);
BlockEndState bypassStop = new BlockEndState();
bypassStop.ruleIndex = i_13;
atn.AddState(bypassStop);
bypassStart.endState = bypassStop;
atn.DefineDecisionState(bypassStart);
bypassStop.startState = bypassStart;
ATNState endState;
Transition excludeTransition = null;
if (atn.ruleToStartState[i_13].isPrecedenceRule)
{
// wrap from the beginning of the rule to the StarLoopEntryState
endState = null;
foreach (ATNState state_3 in atn.states)
{
if (state_3.ruleIndex != i_13)
{
continue;
}
if (!(state_3 is StarLoopEntryState))
{
continue;
}
ATNState maybeLoopEndState = state_3.Transition(state_3.NumberOfTransitions - 1).target;
if (!(maybeLoopEndState is LoopEndState))
{
continue;
}
if (maybeLoopEndState.epsilonOnlyTransitions && maybeLoopEndState.Transition(0).target is RuleStopState)
{
endState = state_3;
break;
}
}
if (endState == null)
{
throw new NotSupportedException("Couldn't identify final state of the precedence rule prefix section.");
}
excludeTransition = ((StarLoopEntryState)endState).loopBackState.Transition(0);
}
else
{
endState = atn.ruleToStopState[i_13];
}
// all non-excluded transitions that currently target end state need to target blockEnd instead
foreach (ATNState state_4 in atn.states)
{
foreach (Transition transition in state_4.transitions)
{
if (transition == excludeTransition)
{
continue;
}
if (transition.target == endState)
{
transition.target = bypassStop;
}
}
}
// all transitions leaving the rule start state need to leave blockStart instead
while (atn.ruleToStartState[i_13].NumberOfTransitions > 0)
{
Transition transition = atn.ruleToStartState[i_13].Transition(atn.ruleToStartState[i_13].NumberOfTransitions - 1);
atn.ruleToStartState[i_13].RemoveTransition(atn.ruleToStartState[i_13].NumberOfTransitions - 1);
bypassStart.AddTransition(transition);
}
// link the new states
atn.ruleToStartState[i_13].AddTransition(new EpsilonTransition(bypassStart));
bypassStop.AddTransition(new EpsilonTransition(endState));
ATNState matchState = new BasicState();
atn.AddState(matchState);
matchState.AddTransition(new AtomTransition(bypassStop, atn.ruleToTokenType[i_13]));
bypassStart.AddTransition(new EpsilonTransition(matchState));
}
if (deserializationOptions.VerifyAtn)
{
// reverify after modification
VerifyATN(atn);
}
}
示例3: OptimizeSets
private static int OptimizeSets(ATN atn, bool preserveOrder)
{
if (preserveOrder)
{
// this optimization currently doesn't preserve edge order.
return 0;
}
int removedPaths = 0;
IList<DecisionState> decisions = atn.decisionToState;
foreach (DecisionState decision in decisions)
{
IntervalSet setTransitions = new IntervalSet();
for (int i = 0; i < decision.NumberOfOptimizedTransitions; i++)
{
Transition epsTransition = decision.GetOptimizedTransition(i);
if (!(epsTransition is EpsilonTransition))
{
continue;
}
if (epsTransition.target.NumberOfOptimizedTransitions != 1)
{
continue;
}
Transition transition = epsTransition.target.GetOptimizedTransition(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);
}
}
if (setTransitions.Count <= 1)
{
continue;
}
IList<Transition> optimizedTransitions = new List<Transition>();
for (int i_1 = 0; i_1 < decision.NumberOfOptimizedTransitions; i_1++)
{
if (!setTransitions.Contains(i_1))
{
optimizedTransitions.Add(decision.GetOptimizedTransition(i_1));
}
}
ATNState blockEndState = decision.GetOptimizedTransition(setTransitions.MinElement).target.GetOptimizedTransition(0).target;
IntervalSet matchSet = new IntervalSet();
for (int i_2 = 0; i_2 < setTransitions.GetIntervals().Count; i_2++)
{
Interval interval = setTransitions.GetIntervals()[i_2];
for (int j = interval.a; j <= interval.b; j++)
{
Transition matchTransition = decision.GetOptimizedTransition(j).target.GetOptimizedTransition(0);
if (matchTransition is NotSetTransition)
{
throw new NotSupportedException("Not yet implemented.");
}
else
{
matchSet.AddAll(matchTransition.Label);
}
}
}
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);
}
ATNState setOptimizedState = new BasicState();
setOptimizedState.SetRuleIndex(decision.ruleIndex);
atn.AddState(setOptimizedState);
setOptimizedState.AddTransition(newTransition);
optimizedTransitions.Add(new EpsilonTransition(setOptimizedState));
removedPaths += decision.NumberOfOptimizedTransitions - optimizedTransitions.Count;
if (decision.IsOptimized)
{
while (decision.NumberOfOptimizedTransitions > 0)
{
decision.RemoveOptimizedTransition(decision.NumberOfOptimizedTransitions - 1);
}
}
foreach (Transition transition_1 in optimizedTransitions)
{
//.........这里部分代码省略.........
示例4: InlineSetRules
private static int InlineSetRules(ATN atn)
{
int inlinedCalls = 0;
Transition[] ruleToInlineTransition = new Transition[atn.ruleToStartState.Length];
for (int i = 0; i < atn.ruleToStartState.Length; i++)
{
RuleStartState startState = atn.ruleToStartState[i];
ATNState middleState = startState;
while (middleState.OnlyHasEpsilonTransitions && middleState.NumberOfOptimizedTransitions == 1 && middleState.GetOptimizedTransition(0).TransitionType == TransitionType.EPSILON)
{
middleState = middleState.GetOptimizedTransition(0).target;
}
if (middleState.NumberOfOptimizedTransitions != 1)
{
continue;
}
Transition matchTransition = middleState.GetOptimizedTransition(0);
ATNState matchTarget = matchTransition.target;
if (matchTransition.IsEpsilon || !matchTarget.OnlyHasEpsilonTransitions || matchTarget.NumberOfOptimizedTransitions != 1 || !(matchTarget.GetOptimizedTransition(0).target is RuleStopState))
{
continue;
}
switch (matchTransition.TransitionType)
{
case TransitionType.ATOM:
case TransitionType.RANGE:
case TransitionType.SET:
{
ruleToInlineTransition[i] = matchTransition;
break;
}
case TransitionType.NOT_SET:
case TransitionType.WILDCARD:
{
// not implemented yet
continue;
}
default:
{
continue;
}
}
}
for (int stateNumber = 0; stateNumber < atn.states.Count; stateNumber++)
{
ATNState state = atn.states[stateNumber];
if (state.ruleIndex < 0)
{
continue;
}
IList<Transition> optimizedTransitions = null;
for (int i_1 = 0; i_1 < state.NumberOfOptimizedTransitions; i_1++)
{
Transition transition = state.GetOptimizedTransition(i_1);
if (!(transition is RuleTransition))
{
if (optimizedTransitions != null)
{
optimizedTransitions.Add(transition);
}
continue;
}
RuleTransition ruleTransition = (RuleTransition)transition;
Transition effective = ruleToInlineTransition[ruleTransition.target.ruleIndex];
if (effective == null)
{
if (optimizedTransitions != null)
{
optimizedTransitions.Add(transition);
}
continue;
}
if (optimizedTransitions == null)
{
optimizedTransitions = new List<Transition>();
for (int j = 0; j < i_1; j++)
{
optimizedTransitions.Add(state.GetOptimizedTransition(i_1));
}
}
inlinedCalls++;
ATNState target = ruleTransition.followState;
ATNState intermediateState = new BasicState();
intermediateState.SetRuleIndex(target.ruleIndex);
atn.AddState(intermediateState);
optimizedTransitions.Add(new EpsilonTransition(intermediateState));
switch (effective.TransitionType)
{
case TransitionType.ATOM:
{
intermediateState.AddTransition(new AtomTransition(target, ((AtomTransition)effective).token));
break;
}
case TransitionType.RANGE:
{
intermediateState.AddTransition(new RangeTransition(target, ((RangeTransition)effective).from, ((RangeTransition)effective).to));
break;
//.........这里部分代码省略.........
示例5: Deserialize
public virtual ATN Deserialize(char[] data)
{
data = (char[])data.Clone();
// don't adjust the first value since that's the version number
for (int i = 1; i < data.Length; i++)
{
data[i] = (char)(data[i] - 2);
}
int p = 0;
int version = ToInt(data[p++]);
if (version != SerializedVersion)
{
string reason = string.Format(CultureInfo.CurrentCulture, "Could not deserialize ATN with version {0} (expected {1}).", version, SerializedVersion);
throw new NotSupportedException(reason);
}
Guid uuid = ToUUID(data, p);
p += 8;
if (!SupportedUuids.Contains(uuid))
{
string reason = string.Format(CultureInfo.CurrentCulture, "Could not deserialize ATN with UUID {0} (expected {1} or a legacy UUID).", uuid, SerializedUuid);
throw new NotSupportedException(reason);
}
bool supportsLexerActions = IsFeatureSupported(AddedLexerActions, uuid);
ATNType grammarType = (ATNType)ToInt(data[p++]);
int maxTokenType = ToInt(data[p++]);
ATN atn = new ATN(grammarType, maxTokenType);
//
// STATES
//
IList<Tuple<LoopEndState, int>> loopBackStateNumbers = new List<Tuple<LoopEndState, int>>();
IList<Tuple<BlockStartState, int>> endStateNumbers = new List<Tuple<BlockStartState, int>>();
int nstates = ToInt(data[p++]);
for (int i_1 = 0; i_1 < nstates; i_1++)
{
StateType stype = (StateType)ToInt(data[p++]);
// ignore bad type of states
if (stype == StateType.InvalidType)
{
atn.AddState(null);
continue;
}
int ruleIndex = ToInt(data[p++]);
if (ruleIndex == char.MaxValue)
{
ruleIndex = -1;
}
ATNState s = StateFactory(stype, ruleIndex);
if (stype == StateType.LoopEnd)
{
// special case
int loopBackStateNumber = ToInt(data[p++]);
loopBackStateNumbers.Add(Tuple.Create((LoopEndState)s, loopBackStateNumber));
}
else
{
if (s is BlockStartState)
{
int endStateNumber = ToInt(data[p++]);
endStateNumbers.Add(Tuple.Create((BlockStartState)s, endStateNumber));
}
}
atn.AddState(s);
}
// delay the assignment of loop back and end states until we know all the state instances have been initialized
foreach (Tuple<LoopEndState, int> pair in loopBackStateNumbers)
{
pair.Item1.loopBackState = atn.states[pair.Item2];
}
foreach (Tuple<BlockStartState, int> pair_1 in endStateNumbers)
{
pair_1.Item1.endState = (BlockEndState)atn.states[pair_1.Item2];
}
int numNonGreedyStates = ToInt(data[p++]);
for (int i_2 = 0; i_2 < numNonGreedyStates; i_2++)
{
int stateNumber = ToInt(data[p++]);
((DecisionState)atn.states[stateNumber]).nonGreedy = true;
}
int numSllDecisions = ToInt(data[p++]);
for (int i_3 = 0; i_3 < numSllDecisions; i_3++)
{
int stateNumber = ToInt(data[p++]);
((DecisionState)atn.states[stateNumber]).sll = true;
}
int numPrecedenceStates = ToInt(data[p++]);
for (int i_4 = 0; i_4 < numPrecedenceStates; i_4++)
{
int stateNumber = ToInt(data[p++]);
((RuleStartState)atn.states[stateNumber]).isPrecedenceRule = true;
}
//
// RULES
//
int nrules = ToInt(data[p++]);
if (atn.grammarType == ATNType.Lexer)
{
atn.ruleToTokenType = new int[nrules];
}
atn.ruleToStartState = new RuleStartState[nrules];
for (int i_5 = 0; i_5 < nrules; i_5++)
//.........这里部分代码省略.........
示例6: Deserialize
public static ATN Deserialize(char[] data, bool optimize)
{
data = (char[])data.Clone();
// don't adjust the first value since that's the version number
for (int i = 1; i < data.Length; i++)
{
data[i] = (char)(data[i] - 2);
}
int p = 0;
int version = ToInt(data[p++]);
if (version != SerializedVersion)
{
string reason = string.Format(CultureInfo.CurrentCulture, "Could not deserialize ATN with version {0} (expected {1})."
, version, SerializedVersion);
throw new NotSupportedException(reason);
}
Guid uuid = ToUUID(data, p);
p += 8;
if (!uuid.Equals(SerializedUuid))
{
string reason = string.Format(CultureInfo.CurrentCulture, "Could not deserialize ATN with UUID {0} (expected {1})."
, uuid, SerializedUuid);
throw new NotSupportedException(reason);
}
ATNType grammarType = (ATNType)ToInt(data[p++]);
int maxTokenType = ToInt(data[p++]);
ATN atn = new ATN(grammarType, maxTokenType);
//
// STATES
//
IList<Tuple<LoopEndState, int>> loopBackStateNumbers = new List<Tuple<LoopEndState
, int>>();
IList<Tuple<BlockStartState, int>> endStateNumbers = new List<Tuple<BlockStartState
, int>>();
int nstates = ToInt(data[p++]);
for (int i_1 = 0; i_1 < nstates; i_1++)
{
StateType stype = (StateType)ToInt(data[p++]);
// ignore bad type of states
if (stype == StateType.InvalidType)
{
atn.AddState(null);
continue;
}
int ruleIndex = ToInt(data[p++]);
if (ruleIndex == char.MaxValue)
{
ruleIndex = -1;
}
ATNState s = StateFactory(stype, ruleIndex);
if (stype == StateType.LoopEnd)
{
// special case
int loopBackStateNumber = ToInt(data[p++]);
loopBackStateNumbers.Add(Tuple.Create((LoopEndState)s, loopBackStateNumber));
}
else
{
if (s is BlockStartState)
{
int endStateNumber = ToInt(data[p++]);
endStateNumbers.Add(Tuple.Create((BlockStartState)s, endStateNumber));
}
}
atn.AddState(s);
}
// delay the assignment of loop back and end states until we know all the state instances have been initialized
foreach (Tuple<LoopEndState, int> pair in loopBackStateNumbers)
{
pair.Item1.loopBackState = atn.states[pair.Item2];
}
foreach (Tuple<BlockStartState, int> pair_1 in endStateNumbers)
{
pair_1.Item1.endState = (BlockEndState)atn.states[pair_1.Item2];
}
int numNonGreedyStates = ToInt(data[p++]);
for (int i_2 = 0; i_2 < numNonGreedyStates; i_2++)
{
int stateNumber = ToInt(data[p++]);
((DecisionState)atn.states[stateNumber]).nonGreedy = true;
}
int numSllDecisions = ToInt(data[p++]);
for (int i_3 = 0; i_3 < numSllDecisions; i_3++)
{
int stateNumber = ToInt(data[p++]);
((DecisionState)atn.states[stateNumber]).sll = true;
}
int numPrecedenceStates = ToInt(data[p++]);
for (int i_4 = 0; i_4 < numPrecedenceStates; i_4++)
{
int stateNumber = ToInt(data[p++]);
((RuleStartState)atn.states[stateNumber]).isPrecedenceRule = true;
}
//
// RULES
//
int nrules = ToInt(data[p++]);
if (atn.grammarType == ATNType.Lexer)
{
atn.ruleToTokenType = new int[nrules];
//.........这里部分代码省略.........