1 4 package jfun.parsec; 5 6 final class DelimitedByParser<R, E1, R1, E extends E1> extends Parser<R> { 7 private final Parser<E> p; 8 9 private final Accumulatable<E1, R1> accm; 10 11 private final Parser<?> delim; 12 13 DelimitedByParser(String n, Parser<E> p, Accumulatable<E1, R1> accm, Parser<?> delim) { 14 super(n); 15 this.p = p; 16 this.accm = accm; 17 this.delim = delim; 18 } 19 20 boolean apply(final ParseContext ctxt) { 21 final Accumulator<E1, R1> acc = accm.getAccumulator(); 22 for (;;) { 23 final int at0 = ctxt.getAt(); 24 boolean r = delim.parse(ctxt); 25 final int at1 = ctxt.getAt(); 26 if (!r) { 27 if (at0 != at1) { 28 return false; 29 } 30 return ParserInternals.returnValue(acc.getResult(), ctxt); 31 } 32 r = p.parse(ctxt); 33 final int at2 = ctxt.getAt(); 34 if (!r) { 35 if (at1 != at2) { 36 return false; 37 } 38 return ParserInternals.returnValue(acc.getResult(), ctxt); 39 } 40 if (ParserInternals.isInfiniteLoop(at0, at2)) { 41 return true; 42 } 43 acc.accumulate(p.getReturn(ctxt)); 44 } 45 } 46 } | Popular Tags |