KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > SumParser


1 /**
2  *
3  */

4 package jfun.parsec;
5
6 final class SumParser extends Parser<Object JavaDoc> {
7   private final Parser<?>[] ps;
8
9   SumParser(String JavaDoc n, Parser<?>[] ps) {
10     super(n);
11     this.ps = ps;
12   }
13
14   boolean apply(final ParseContext ctxt) {
15     return apply(ctxt, 1);
16   }
17
18   boolean apply(final ParseContext ctxt, final int look_ahead) {
19     final Object JavaDoc ustate = ctxt.getUserState();
20     final Object JavaDoc ret = ctxt.getReturn();
21     final int at = ctxt.getAt();
22     final int step = ctxt.getStep();
23     final AbstractParsecError error = ctxt.getError();
24     AbstractParsecError err = error;
25     for (int i = 0; i < ps.length; i++) {
26       final Parser p1 = ps[i];
27       if (p1.parse(ctxt))
28         return true;
29       if (ctxt.hasException())
30         return false;
31       if (ctxt.getAt() != at && ctxt.getStep() - step >= look_ahead)
32         return false;
33       // if(state.getAt() - at >= look_ahead) return false;
34
err = AbstractParsecError.mergeError(err, ctxt.getError());
35       ctxt.set(step, at, ret, ustate, error);
36     }
37     ctxt.setError(err);
38     return false;
39   }
40 }
Popular Tags