KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > ParserException


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

13 package jfun.parsec;
14
15 import java.io.PrintStream JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17 import java.util.Stack JavaDoc;
18
19 /**
20  * ParserException is thrown when a grammar error happens.
21  * <p>
22  * @author Ben Yu
23  *
24  * 2004-11-12
25  */

26 public class ParserException extends RuntimeException JavaDoc {
27   private final ParseError err;
28   private final Pos pos;
29   private final String JavaDoc module;
30   private final Stack JavaDoc frames = new Stack JavaDoc();
31   void pushFrame(ParsingFrame frame){
32     frames.push(frame);
33   }
34   /**
35    * Get the parsing trace.
36    * @return the parsing trace
37    * with objects of {@link ParsingFrame} as the elements.
38    */

39   public Stack JavaDoc getParsingTrace(){
40     return frames;
41   }
42   
43   /**
44    * Print the parsing trace.
45    * @param out the output stream.
46    */

47   public void printParsingTrace(PrintStream JavaDoc out){
48     printParsingTrace(new java.io.PrintWriter JavaDoc(out, true));
49   }
50   /**
51    * Print the resultion trace.
52    * @param out the output writer.
53    */

54   public void printParsingTrace(java.io.PrintWriter JavaDoc out){
55     final int size = frames.size();
56     for(int i=0; i<size; i++){
57       out.println(frames.get(i));
58     }
59   }
60   /**
61    * Prints the parsing trace to the standard error output.
62    */

63   public void printParsingTrace(){
64     printParsingTrace(System.err);
65   }
66   public void printStackTrace(PrintStream JavaDoc s) {
67     printParsingTrace(s);
68     super.printStackTrace(s);
69   }
70   public void printStackTrace(PrintWriter JavaDoc s) {
71     printParsingTrace(s);
72     super.printStackTrace(s);
73   }
74
75   /**
76    * Create a ParserException object.
77    * @param err the ParseError object.
78    * @param mname the module name.
79    * @param pos the position.
80    */

81   public ParserException(final ParseError err,
82       final String JavaDoc mname, final Pos pos) {
83     this.err = err;
84     this.pos = pos;
85     this.module = mname;
86   }
87
88   /**
89    * Create a ParserException object.
90    * @param message the error message.
91    * @param err the ParseError object.
92    * @param mname the module name.
93    * @param pos the position.
94    */

95   public ParserException(String JavaDoc message, final ParseError err,
96       final String JavaDoc mname, final Pos pos) {
97     super(message);
98     this.err = err;
99     this.pos = pos;
100     this.module = mname;
101   }
102
103
104   /**
105    * @param cause the exception that causes this.
106    * @param err the ParseError object.
107    * @param mname the module name.
108    * @param pos the position.
109    */

110   public ParserException(Throwable JavaDoc cause, final ParseError err,
111       final String JavaDoc mname, final Pos pos) {
112     super(cause);
113     this.err = err;
114     this.pos = pos;
115     this.module = mname;
116   }
117
118   /**
119    * @param message the error message.
120    * @param cause the exception that causes this.
121    * @param err the ParseError object.
122    * @param mname the module name.
123    * @param pos the position.
124    */

125   public ParserException(String JavaDoc message, Throwable JavaDoc cause, final ParseError err,
126       final String JavaDoc mname, final Pos pos) {
127     super(message, cause);
128     this.err = err;
129     this.pos = pos;
130     this.module = mname;
131   }
132
133   /**
134    * Get the ParseError object.
135    * @return Returns the err.
136    */

137   public final ParseError getError() {
138     return err;
139   }
140   /**
141    * Get the default formatted error message.
142    * @see java.lang.Throwable#getMessage()
143    */

144   public String JavaDoc getMessage(){
145     return getErrorMessage();
146   }
147   private String JavaDoc getErrorMessage(){
148     final String JavaDoc msg = super.getMessage();
149     final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
150     if(msg != null) buf.append(msg).append("\n");
151     if(module != null)
152       buf.append(module).append(" - ");
153     buf.append(DefaultShowError.show(err, pos));
154     return buf.toString();
155   }
156   
157   /**
158    * Gets the module name.
159    * @return the module name.
160    */

161   public String JavaDoc getModuleName() {
162     return module;
163   }
164   /**
165    * Gets the line number of the error.
166    * @return the line number.
167    */

168   public int getLineNo() {
169     return pos.getLineNo();
170   }
171   /**
172    * Gets the column number of the error.
173    * @return the column number.
174    */

175   public int getColumnNo(){
176     return pos.getColumnNo();
177   }
178 }
179
Popular Tags