private boolean tryFixBySingleCharDeletion(int fixIndex) { buffer.insertChar(fixIndex, DEL_ERROR); boolean nowErrorFree = performLocatingRun(buffer); if (nowErrorFree) { currentError.shiftIndexDeltaBy(1); // compensate for the inserted DEL_ERROR char } else { buffer.undoCharInsertion(fixIndex); errorIndex = Math.max(errorIndex - 1, 0); } return nowErrorFree; }
@SuppressWarnings( {"ConstantConditions"}) private Character findBestSingleCharInsertion(int fixIndex) { GetStarterCharVisitor getStarterCharVisitor = new GetStarterCharVisitor(); int bestNextErrorIndex = -1; Character bestChar = '\u0000'; // non-null default for (MatcherPath failedMatcherPath : currentError.getFailedMatchers()) { Character starterChar = failedMatcherPath.element.matcher.accept(getStarterCharVisitor); checkState(starterChar != null); // we should only have single character matchers if (starterChar == EOI) { continue; // we should never conjure up an EOI character (that would be cheating :) } buffer.insertChar(fixIndex, starterChar); buffer.insertChar(fixIndex, INS_ERROR); if (performLocatingRun(buffer)) { currentError.shiftIndexDeltaBy(2); // compensate for the inserted chars return null; // success, exit immediately } buffer.undoCharInsertion(fixIndex); buffer.undoCharInsertion(fixIndex); errorIndex = Math.max(errorIndex - 2, 0); if (bestNextErrorIndex < errorIndex) { bestNextErrorIndex = errorIndex; bestChar = starterChar; } } errorIndex = bestNextErrorIndex; return bestChar; }
performLocatingRun(buffer); // find the next parse error
performLocatingRun(inputBuffer); checkState(errorIndex >= 0); // we failed before so we must fail again