final boolean stillThere(int wasAt, int originalStep) { if (step == originalStep) { // logical step didn't change, so logically we are still there, undo any physical offset setAt(originalStep, wasAt); return true; } return false; }
@Override boolean apply(ParseContext ctxt) { int step = ctxt.step; int at = ctxt.at; boolean ok = Parser.this.apply(ctxt); if (ok) ctxt.setAt(step, at); return ok; } @Override public String toString() {
private boolean repeatAtMost(int times, ParseContext ctxt) { for (int i = 0; i < times; i++) { int physical = ctxt.at; int logical = ctxt.step; if (!parser.apply(ctxt)) { ctxt.setAt(logical, physical); return true; } } return true; } }
@Override boolean apply(ParseContext ctxt) { int at = ctxt.at; int step = ctxt.step; boolean r = Parser.this.apply(ctxt); if (r) ctxt.step = step + 1; else ctxt.setAt(step, at); return r; } @Override public String toString() {
private boolean applyMany(ParseContext ctxt) { int physical = ctxt.at; int logical = ctxt.step; for (;;logical = ctxt.step) { if (!parser.apply(ctxt)) { ctxt.setAt(logical, physical); return true; } int at2 = ctxt.at; if (physical == at2) return true; physical = at2; } } }
private boolean repeatAtMost(int times, Collection<T> collection, ParseContext ctxt) { for (int i = 0; i < times; i++) { int physical = ctxt.at; int logical = ctxt.step; if (!parser.apply(ctxt)) { ctxt.setAt(logical, physical); return true; } collection.add(parser.getReturn(ctxt)); } return true; } }
private boolean applyMany(ParseContext ctxt, Collection<T> collection) { int physical = ctxt.at; int logical = ctxt.step; for (;;logical = ctxt.step) { if (!parser.apply(ctxt)) { ctxt.setAt(logical, physical); return true; } int at2 = ctxt.at; if (physical == at2) return true; physical = at2; collection.add(parser.getReturn(ctxt)); } } }
@Override final boolean apply(final ParseContext ctxt) { final List<T> result = listFactory.newList(); for (;;) { final int step0 = ctxt.step; final int at0 = ctxt.at; boolean r = ctxt.applyAsDelimiter(delim); if (!r) { ctxt.result = result; ctxt.setAt(step0, at0); return true; } final int step1 = ctxt.step; final int at1 = ctxt.at; r = parser.apply(ctxt); if (!r) { ctxt.result = result; ctxt.setAt(step1, at1); return true; } if (at0 == ctxt.at) { // infinite loop ctxt.result = result; return true; } result.add(parser.getReturn(ctxt)); } }