KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > ParsecError


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 2004-11-11
10  *
11  * Author Ben Yu
12  */

13 package jfun.parsec;
14
15 /**
16  * @author Ben Yu
17  *
18  * 2004-11-11
19  */

20 final class ParsecError extends AbstractParsecError implements ParseError{
21   //private static final String[] err0 = new String[0];
22
ParsecError render(){return this;}
23   private final Object JavaDoc sys_unexpected;
24   private final String JavaDoc[] unexpected;
25   private final String JavaDoc[] expecting;
26   private final String JavaDoc[] raw;
27   static int getPrecedenceForExpecting(String JavaDoc s){
28     return s!=null?2:1;
29   }
30   /**
31    * @param lno
32    * @param cno
33    * @param msgs
34    */

35   private ParsecError(final boolean nm, final int at,
36       Object JavaDoc sys, String JavaDoc[] unexpected, String JavaDoc[] expecting, String JavaDoc[] raw,
37       Object JavaDoc exception) {
38     super(nm, at, (expecting != null || unexpected != null || raw != null)?
39         2:1, exception);
40     this.sys_unexpected = sys;
41     this.unexpected = unexpected;
42     this.expecting = expecting;
43     this.raw = raw;
44     //Benchmark.instance().inc("ParsecError");
45
}
46   AbstractParsecError setExpecting(final String JavaDoc s){
47     return new ParsecError(false, getIndex(),
48         sys_unexpected, unexpected, new String JavaDoc[]{s}, raw, getException());
49   }
50   static ParsecError raiseRaw(final int at,
51       final String JavaDoc msg){
52     return new ParsecError(false, at, null, null, null, new String JavaDoc[]{msg}, null);
53   }
54   static ParsecError raiseSysUnexpected(final int at,
55       final Object JavaDoc obj){
56     return new ParsecError(true, at, obj, null, null, null, null);
57   }
58   static ParsecError raiseUnexpected(final int at
59       ,final String JavaDoc s){
60     return new ParsecError(false, at, null, new String JavaDoc[]{s}, null, null, null);
61   }
62   static ParsecError raiseExpecting(final int at,
63       final String JavaDoc s){
64     return new ParsecError(false, at, null, null, new String JavaDoc[]{s}, null, null);
65   }
66   static AbstractParsecError raiseExpecting(final int at, final String JavaDoc s,
67       final AbstractParsecError err){
68     return new ParsecErrorExpecting(false, at,
69         myPrecedence(err.getPrecedence(), s), err.getException(),
70         err, s);
71   }
72   private static int max(int a, int b){
73     return a>b?a:b;
74   }
75   private static int myPrecedence(int pred, String JavaDoc s){
76      return max(pred, ParsecError.getPrecedenceForExpecting(s));
77   }
78   static ParsecError throwException(final int at,
79       final Object JavaDoc e){
80     return new ParsecError(false, at, null, null, null, null, e);
81   }
82   Object JavaDoc getSysUnexpected(){return sys_unexpected;}
83   public String JavaDoc getEncountered(){
84     if(sys_unexpected == null) return null;
85     else return sys_unexpected.toString();
86   }
87   public String JavaDoc[] getUnexpected(){return unexpected;}
88   public String JavaDoc[] getExpecting(){return expecting;}
89   public String JavaDoc[] getMessages(){return raw;}
90   static ParsecError noError(){
91     return null;
92   }
93   private static String JavaDoc[] mergeMsgs(final String JavaDoc[] a, final String JavaDoc[] b){
94     if(a==null)return b;
95     if(b==null)return a;
96     if(a==b)return a;
97     final String JavaDoc[] msgs = new String JavaDoc[a.length+b.length];
98     /*for(int i=0; i<a.length; i++){
99       msgs[i] = a[i];
100     }
101     for(int i=0; i<b.length; i++){
102       msgs[i+a.length] = b[i];
103     }*/

104     System.arraycopy(a, 0, msgs, 0, a.length);
105     System.arraycopy(b, 0, msgs, a.length, b.length);
106     return msgs;
107   }
108   static ParsecError mergeError(
109       final int ind, final Object JavaDoc exception, final ParsecError e1, final ParsecError e2){
110     return new ParsecError(false, ind,
111         MergedParsecError.mergeObj(e1.sys_unexpected, e2.sys_unexpected),
112         mergeMsgs(e1.unexpected, e2.unexpected),
113         mergeMsgs(e1.expecting, e2.expecting),
114         mergeMsgs(e1.raw, e2.raw),
115         exception
116     );
117   }
118 }
119
Popular Tags