use of org.antlr.v4.runtime.atn.Transition in project antlr4 by antlr.
the class ParserATNFactory method sempred.
/**
* Build what amounts to an epsilon transition with a semantic
* predicate action. The {@code pred} is a pointer into the AST of
* the {@link ANTLRParser#SEMPRED} token.
*/
@Override
public Handle sempred(PredAST pred) {
// System.out.println("sempred: "+ pred);
ATNState left = newState(pred);
ATNState right = newState(pred);
AbstractPredicateTransition p;
if (pred.getOptionString(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME) != null) {
int precedence = Integer.parseInt(pred.getOptionString(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME));
p = new PrecedencePredicateTransition(right, precedence);
} else {
boolean isCtxDependent = UseDefAnalyzer.actionIsContextDependent(pred);
p = new PredicateTransition(right, currentRule.index, g.sempreds.get(pred), isCtxDependent);
}
left.addTransition(p);
pred.atnState = left;
return new Handle(left, right);
}
use of org.antlr.v4.runtime.atn.Transition in project antlr4 by antlr.
the class ParserATNFactory method elemList.
public Handle elemList(List<Handle> els) {
int n = els.size();
for (int i = 0; i < n - 1; i++) {
// hook up elements (visit all but last)
Handle el = els.get(i);
// if el is of form o-x->o for x in {rule, action, pred, token, ...}
// and not last in alt
Transition tr = null;
if (el.left.getNumberOfTransitions() == 1)
tr = el.left.transition(0);
boolean isRuleTrans = tr instanceof RuleTransition;
if (el.left.getStateType() == ATNState.BASIC && el.right.getStateType() == ATNState.BASIC && tr != null && (isRuleTrans && ((RuleTransition) tr).followState == el.right || tr.target == el.right)) {
// we can avoid epsilon edge to next el
if (isRuleTrans)
((RuleTransition) tr).followState = els.get(i + 1).left;
else
tr.target = els.get(i + 1).left;
// we skipped over this state
atn.removeState(el.right);
} else {
// need epsilon if previous block's right end node is complicated
epsilon(el.right, els.get(i + 1).left);
}
}
Handle first = els.get(0);
Handle last = els.get(n - 1);
if (first == null || last == null) {
g.tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, "element list has first|last == null");
}
return new Handle(first.left, last.right);
}
use of org.antlr.v4.runtime.atn.Transition in project antlr4 by antlr.
the class ParserATNFactory method action.
/**
* Build what amounts to an epsilon transition with an action.
* The action goes into ATN though it is ignored during prediction
* if {@link ActionTransition#actionIndex actionIndex}{@code <0}.
*/
@Override
public Handle action(ActionAST action) {
// System.out.println("action: "+action);
ATNState left = newState(action);
ATNState right = newState(action);
ActionTransition a = new ActionTransition(right, currentRule.index);
left.addTransition(a);
action.atnState = left;
return new Handle(left, right);
}
use of org.antlr.v4.runtime.atn.Transition in project antlr4 by antlr.
the class ProfilingATNSimulator method getExistingTargetState.
@Override
protected DFAState getExistingTargetState(DFAState previousD, int t) {
// this method is called after each time the input position advances
// during SLL prediction
_sllStopIndex = _input.index();
DFAState existingTargetState = super.getExistingTargetState(previousD, t);
if (existingTargetState != null) {
// count only if we transition over a DFA state
decisions[currentDecision].SLL_DFATransitions++;
if (existingTargetState == ERROR) {
decisions[currentDecision].errors.add(new ErrorInfo(currentDecision, previousD.configs, _input, _startIndex, _sllStopIndex, false));
}
}
currentState = existingTargetState;
return existingTargetState;
}
Aggregations