boolean matches(BitSet bitSet) { long[] array = bitSet.toPackedArray(); int len = array.length; if (len < 3 || array[0] != 0L) { return false; } long leftBits = array[1]; long rightBits = array[2]; if (rightBits == semicolonRecoverySetRight && (leftBits == semicolonRecoverySetLeft || leftBits == semicolonRecoverySetWithComma)) { for (int i = 3; i < len; i++) { if (array[i] != 0L) { return false; } } return true; } return false; }
/** * Returns {@code true} if the set of expected follow-states includes an implicit or explicit semicolon. */ private static boolean followedBySemicolon(RecognizerSharedState state, Callback.RecoverySets recoverySets, int currentIndex) { int top = state._fsp; if (currentIndex != state.lastErrorIndex) { long[] array = state.following[top].toPackedArray(); if (array.length == 1 && array[0] == (1L << Token.EOR_TOKEN_TYPE)) { return false; } } for (int i = top; i >= 0; i--) { BitSet localFollowSet = state.following[i]; if (recoverySets.matches(localFollowSet)) { return true; } } return false; }
/** Consume tokens until one matches the given token set */ @Override public void consumeUntil(IntStream i, BitSet set) { // System.out.println("consumeUntil(" + set.toString(getTokenNames()) + ")"); Token ttype; List<Token> skipped = new ArrayList<>(); beginResync(); try { while ((ttype = input.LT(1)) != null && ttype.getType() != Token.EOF && !set.member(ttype.getType())) { // System.out.println("consume during recover LA(1)=" + getTokenNames()[input.LA(1)]); input.consume(); skipped.add(ttype); } } finally { endResync(); } ((NbParseTreeBuilder) dbg).consumeSkippedTokens(skipped); }
/** Added for debugging purposes only. */ @SuppressWarnings("unused") private String bitsetName(BitSet bitset) { Optional<Field> findField = Stream.of(InternalN4JSParser.class.getDeclaredFields()) .filter(f -> Modifier.isStatic(f.getModifiers())).filter(f -> { try { return bitset == f.get(null); } catch (Exception ex) { // } return false; }).findFirst(); return findField.map(Field::getName).orElse("NN"); }
/** * Computes the ASI recovery sets. */ default RecoverySets computeRecoverySets() { BitSet followSet = getSemicolonFollowSet(); long[] array = followSet.toPackedArray(); if (array.length != 3 || array[0] != 0L) { throw new RuntimeException("Internal token types changed. Need to rework ASI."); } int commaBit = getCommaBit() - Long.SIZE; if (commaBit < 0 || commaBit > Long.SIZE) { throw new RuntimeException("Internal token types changed. Need to rework ASI."); } RecoverySets result = new RecoverySets(array, commaBit); return result; }
@Test public void testBug326509() { for(int i = 0; i <= 200; i++) { testMe.pushFollow(new BitSet()); } BitSet[] following = testMe.getFollowing(); for(int i = 0; i < following.length && i <= 200; i++) { assertNotNull(Integer.toString(i), following[i]); } }
@Override protected Object recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow) throws RecognitionException { try { mismatch = true; return super.recoverFromMismatchedToken(input, ttype, follow); } finally { mismatch = false; } }
@Override protected Object recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow) throws RecognitionException { throw new MismatchedTokenException(ttype, input); }
@Override public BitSet getSemicolonFollowSet() { return FOLLOW_ruleExpression_in_ruleExpressionStatement; }
@Override protected Object recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow) throws RecognitionException { //disable the default token auto-insertion/deletion recovery throw new MismatchedTokenException(ttype, input); }
public BitSet[] getFollowing() { return state.following; }
@Override public void pushFollow(BitSet bitSet) { super.pushFollow(bitSet); }
@Override public boolean mismatchIsMissingToken(IntStream input, BitSet follow) { return false; }
protected void mismatch( IntStream input, int ttype, BitSet follow ) throws RecognitionException { throw new MismatchedTokenException(ttype, input); }
public Object recoverFromMismatchedSet( IntStream input, RecognitionException e, BitSet follow ) throws RecognitionException{ throw e; }
protected void mismatch( IntStream input, int ttype, BitSet follow ) throws RecognitionException { throw new MismatchedTokenException( ttype, input ); }
public Object recoverFromMismatchedSet( IntStream input, RecognitionException e, BitSet follow ) throws RecognitionException { throw e; }
@Override public void reportAmbiguity(final Parser recognizer, final DFA dfa, final int startIndex, final int stopIndex, final boolean exact, final java.util.BitSet ambigAlts, final ATNConfigSet configs) { // Empty implementation }
@Override public void reportAttemptingFullContext(final Parser recognizer, final DFA dfa, final int startIndex, final int stopIndex, final java.util.BitSet conflictingAlts, final ATNConfigSet configs) { // Empty implementation }
@Override public void reportError(final org.antlr.v4.runtime.IntStream input, final org.antlr.v4.runtime.RecognitionException re, final BitSet follow) { // Empty implementation }
/** * Obtain the follow set that is relevant for the ASI. */ BitSet getSemicolonFollowSet();