KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > OrParser


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