KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > AbstractParsecError


1 /*****************************************************************************
2  * Copyright (C) Zephyr Business Solutions Corp. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 /*
9  * Created on Feb 24, 2005
10  *
11  * Author Ben Yu
12  * ZBS
13  */

14 package jfun.parsec;
15
16 /**
17  * Internally used by the parsec framework.
18  * Because merging error messages can be expensive,
19  * this class is introduced to delay the expensive operation until render time.
20  * Operations in this class are used in Parsers and Scanners class.
21  * <p>
22  * Zephyr Business Solutions Corp.
23  *
24  * @author Ben Yu
25  *
26  */

27 abstract class AbstractParsecError implements java.io.Serializable JavaDoc{
28   final Object JavaDoc getException(){return exception;}
29   final boolean hasException(){return exception!=null;}
30   abstract ParsecError render();
31   abstract AbstractParsecError setExpecting(String JavaDoc s);
32   final int getPrecedence(){return precedence;}
33   public final int getIndex(){return at;}
34   final boolean noMerge(){return nomerge;}
35   private final boolean nomerge;
36   private final int at;
37   private final int precedence;
38   private final Object JavaDoc exception;
39   AbstractParsecError(final boolean nomerge, final int at,
40       final int pred, final Object JavaDoc exception) {
41     this.nomerge = nomerge;
42     this.at = at;
43     this.precedence = pred;
44     this.exception = exception;
45   }
46   static AbstractParsecError mergeError(final AbstractParsecError e1,
47       final AbstractParsecError e2){
48     if(e1==null) return e2;
49     if(e2==null) return e1;
50     if(e1==e2) return e1;
51     final int pred = e1.precedence;
52     final int pred2 = e2.precedence;
53     final int at = e1.at;
54     final int at2 = e2.at;
55     if(at==at2){
56       if(pred2>pred){
57         return e2;
58       }
59       else if(pred>pred2){
60         return e1;
61       }
62       //else return e1;
63
}
64     else if(at > at2){
65       /*if(pred < pred2){
66         return e2;
67       }
68       else */
return e1;
69     }
70     else if(at < at2){
71       /*if(pred > pred2){
72         return e1;
73       }
74       else */
return e2;
75     }
76     if(e1.nomerge && e2.nomerge){
77       return e1;
78     }
79     return new MergedParsecError(at, pred, e1, e2);
80   }
81 }
82
Popular Tags