1 4 package jfun.parsec; 5 6 final class TryParser<R> extends Parser<R> { 7 private final Parser<R> p; 8 9 private final Catch<? extends R> hdl; 10 11 TryParser(String n, Parser<R> p, Catch<? extends R> hdl) { 12 super(n); 13 this.p = p; 14 this.hdl = hdl; 15 } 16 17 boolean apply(final ParseContext ctxt) { 18 final AbstractParsecError err0 = ctxt.getError(); 19 final boolean r = p.parse(ctxt); 20 if (!ctxt.hasException()) 21 return r; 22 final Parser h = hdl.catchException(ctxt.getReturn(), ctxt.getError() 23 .getException()); 24 ctxt.setError(err0); 25 return h.parse(ctxt); 26 } 27 } | Popular Tags |