1 package jfun.parsec; 2 3 import jfun.parsec.trace.Trace; 4 5 final class TracedParser extends Parser { 6 private final Parser fwd; 7 private final Trace trace; 8 9 TracedParser(String n, Parser fwd, Trace trace) { 10 super(n); 11 this.fwd = fwd; 12 this.trace = trace; 13 } 14 boolean apply(ParseContext ctxt) { 15 int original_step = ctxt.getStep(); 16 int original_at = ctxt.getAt(); 17 boolean ok = fwd.apply(ctxt); 18 final CharSequence src = ctxt.getSource(); 19 final int ind = ctxt.getIndex(); 20 final int steps = ctxt.getStep()-original_step; 21 final int offset = ctxt.getAt()-original_at; 22 if(ok){ 23 trace.onSuccess(ctxt.getReturn(), src, ind, steps, offset); 24 } 25 else{ 26 trace.onError(getException(ctxt.getError()), src, ind, steps, offset); 27 } 28 return ok; 29 } 30 31 private static Object getException(AbstractParsecError err){ 32 return err==null?null:err.getException(); 33 } 34 } 35 | Popular Tags |