@Override public Handle range(GrammarAST a, GrammarAST b) { ATNState left = newState(a); ATNState right = newState(b); int t1 = CharSupport.getCharValueFromGrammarCharLiteral(a.getText()); int t2 = CharSupport.getCharValueFromGrammarCharLiteral(b.getText()); left.addTransition(new RangeTransition(right, t1, t2)); a.atnState = left; b.atnState = left; return new Handle(left, right); }
private static Transition createSetTransition(ATNState target, IntervalSet set) { if (set.getIntervals().size() == 1) { Interval interval = set.getIntervals().get(0); if (interval.a == interval.b) { return new AtomTransition(target, interval.a); } else { return new RangeTransition(target, interval.a, interval.b); } } else { return new SetTransition(target, set); } }
@Override public ATNState getReachableTarget(ATNConfig source, Transition trans, int ttype) { if (ttype == CaretToken.CARET_TOKEN_TYPE) { ATNState target = null; if (trans instanceof AtomTransition) { AtomTransition at = (AtomTransition)trans; if (getWordlikeTokenTypes().contains(at.label)) { target = at.target; } } else if (trans instanceof SetTransition) { SetTransition st = (SetTransition)trans; boolean not = trans instanceof NotSetTransition; // TODO: this could probably be done with an intersects method? for (int t : getWordlikeTokenTypes().toArray()) { if (!not && st.set.contains(t) || not && !st.set.contains(t)) { target = st.target; break; } } } else if (trans instanceof RangeTransition) { RangeTransition rt = (RangeTransition)trans; // TODO: there must be a better algorithm here :) int[] wordlikeTokenTypes = getWordlikeTokenTypes().toArray(); int lb = Arrays.binarySearch(wordlikeTokenTypes, rt.from); int ub = Arrays.binarySearch(wordlikeTokenTypes, rt.to); if (lb >= 0 || ub >= 0 || lb != ub) { target = rt.target; } } else if (trans instanceof WildcardTransition) { target = trans.target; } if (caretTransitions == null) { caretTransitions = new LinkedHashMap<>(); } List<Transition> configTransitions = caretTransitions.get(source); if (configTransitions == null) { configTransitions = new ArrayList<>(); caretTransitions.put(source, configTransitions); } configTransitions.add(trans); return target; } return super.getReachableTarget(source, trans, ttype); }