KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > BestParser


1 /**
2  *
3  */

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