001: /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 2.1 */
002: /* [The preceding line is no longer true: this file has been edited, and is
003: no longer generated by JavaCC. However, the comment on the first line
004: is necessary to suppress a warning message when JavaCC is run.] */
005:
006: /* ****************************************************************************
007: * ParseException.java
008: * ****************************************************************************/
009:
010: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
011: * Copyright 2001-2007 Laszlo Systems, Inc. All Rights Reserved. *
012: * Use is subject to license terms. *
013: * J_LZ_COPYRIGHT_END *********************************************************/
014:
015: package org.openlaszlo.sc.parser;
016:
017: import org.openlaszlo.sc.CompilerException;
018:
019: /**
020: * This exception is thrown when parse errors are encountered.
021: * You can explicitly create objects of this exception type by
022: * calling the method generateParseException in the generated
023: * parser.
024: *
025: * You can modify this class to customize your error reporting
026: * mechanisms so long as you retain the public fields.
027: */
028: public class ParseException extends CompilerException {
029:
030: /**
031: * This constructor is used by the method "generateParseException"
032: * in the generated parser. Calling this constructor generates
033: * a new object of this type with the fields "currentToken",
034: * "expectedTokenSequences", and "tokenImage" set. The boolean
035: * flag "specialConstructor" is also set to true to indicate that
036: * this constructor was used to create this object.
037: * This constructor calls its super class with the empty string
038: * to force the "toString" method of parent class "Throwable" to
039: * print the error message in the form:
040: * ParseException: <result of getMessage>
041: */
042: public ParseException(Token currentTokenVal,
043: int[][] expectedTokenSequencesVal, String[] tokenImageVal) {
044: super ("");
045: specialConstructor = true;
046: currentToken = currentTokenVal;
047: expectedTokenSequences = expectedTokenSequencesVal;
048: tokenImage = tokenImageVal;
049: }
050:
051: /**
052: * The following constructors are for use by you for whatever
053: * purpose you can think of. Constructing the exception in this
054: * manner makes the exception behave in the normal way - i.e., as
055: * documented in the class "Throwable". The fields "errorToken",
056: * "expectedTokenSequences", and "tokenImage" do not contain
057: * relevant information. The JavaCC generated code does not use
058: * these constructors.
059: */
060:
061: public ParseException() {
062: super ();
063: specialConstructor = false;
064: }
065:
066: public ParseException(String message) {
067: super (message);
068: specialConstructor = false;
069: }
070:
071: public ParseException(Token currentTokenVal, String message) {
072: super (message);
073: specialConstructor = false;
074: currentToken = currentTokenVal;
075: }
076:
077: /**
078: * This variable determines which constructor was used to create
079: * this object and thereby affects the semantics of the
080: * "getMessage" method (see below).
081: */
082: protected boolean specialConstructor;
083:
084: /**
085: * This is the last token that has been consumed successfully. If
086: * this object has been created due to a parse error, the token
087: * followng this token will (therefore) be the first error token.
088: */
089: public Token currentToken;
090:
091: /**
092: * Each entry in this array is an array of integers. Each array
093: * of integers represents a sequence of tokens (by their ordinal
094: * values) that is expected at this point of the parse.
095: */
096: public int[][] expectedTokenSequences;
097:
098: /**
099: * This is a reference to the "tokenImage" array of the generated
100: * parser within which the parse error occurred. This array is
101: * defined in the generated ...Constants interface.
102: */
103: public String[] tokenImage;
104:
105: public int getBeginLine() {
106: if (currentToken.next != null) {
107: return currentToken.next.beginLine;
108: } else {
109: return -1;
110: }
111: }
112:
113: public int getBeginColumn() {
114: if (currentToken.next != null) {
115: return currentToken.next.beginColumn;
116: } else {
117: return -1;
118: }
119: }
120:
121: String pathname = null;
122:
123: public String getPathname() {
124: return pathname;
125: }
126:
127: public void initPathname(String pathname) {
128: this .pathname = pathname;
129: }
130:
131: /**
132: * This method has the standard behavior when this object has been
133: * created using the standard constructors. Otherwise, it uses
134: * "currentToken" and "expectedTokenSequences" to generate a parse
135: * error message and returns it. If this object has been created
136: * due to a parse error, and you do not catch it (it gets thrown
137: * from the parser), then this method is called during the printing
138: * of the final stack trace, and hence the correct error message
139: * gets displayed.
140: */
141: public String getMessage() {
142: return getMessage(true);
143: }
144:
145: public String getMessage(boolean includeSourceLocation) {
146: if (!specialConstructor) {
147: String retval = super .getMessage();
148: if (currentToken != null && includeSourceLocation) {
149: retval += " at line " + currentToken.beginLine
150: + ", column " + currentToken.beginColumn;
151: }
152: return retval;
153: }
154: String expected = "";
155: int expectedCount = 0;
156: int maxSize = 0;
157: boolean firstTime = true;
158: for (int i = 0; i < expectedTokenSequences.length; i++) {
159: if (maxSize < expectedTokenSequences[i].length) {
160: maxSize = expectedTokenSequences[i].length;
161: }
162: String token = tokenImage[expectedTokenSequences[i][0]];
163: if (token.length() > 0 && token.charAt(0) == '"'
164: && token.charAt(token.length() - 1) == '"') {
165: token = token.substring(1, token.length() - 1);
166: }
167: if (token.charAt(0) != '#') {
168: if (!firstTime)
169: expected += ", ";
170: firstTime = false;
171: expected += token;
172: expectedCount += 1;
173: }
174: }
175: String tokenstr = "";
176: Token tok = currentToken.next;
177: for (int i = 0; i < maxSize; i++) {
178: if (i != 0)
179: tokenstr += " ";
180: if (tok.kind == 0) {
181: tokenstr += tokenImage[0];
182: break;
183: }
184: tokenstr += tok.image; //add_escapes(tok.image);
185: tok = tok.next;
186: }
187: String msg = "the token \"" + tokenstr
188: + "\" was not expected at this position";
189: boolean showMismatchSolution = false;
190: if (tokenstr.equals("#endAttribute")) {
191: msg = "the attribute value ended in mid-expression";
192: showMismatchSolution = true;
193: }
194: if (tokenstr.equals("#endAttributeStatements")) {
195: msg = "the attribute value ended in mid-statement";
196: showMismatchSolution = true;
197: }
198: if (tokenstr.equals("#endContent")) {
199: msg = "the element content ended in mid-program";
200: showMismatchSolution = true;
201: }
202: String retval = "Syntax error: " + msg;
203: if (currentToken.next != null && includeSourceLocation) {
204: retval += " at line " + currentToken.next.beginLine
205: + ", column " + currentToken.next.beginColumn;
206: }
207: retval += ".";
208: if (expectedCount == 1) {
209: retval += eol + "Was expecting " + expected;
210: } else if (0 < expectedCount && expectedCount <= 5) {
211: retval += eol + "Was expecting one of: " + expected;
212: } else if (showMismatchSolution) {
213: retval += eol + "Look for an unclosed '(', '{', or '['.";
214: }
215: return retval;
216: }
217:
218: /**
219: * The end of line string for this machine.
220: */
221: protected String eol = System.getProperty("line.separator", "\n");
222:
223: /**
224: * Used to convert raw characters to their escaped version
225: * when these raw version cannot be used as part of an ASCII
226: * string literal.
227: */
228: protected String add_escapes(String str) {
229: StringBuffer retval = new StringBuffer();
230: char ch;
231: for (int i = 0; i < str.length(); i++) {
232: switch (str.charAt(i)) {
233: case 0:
234: continue;
235: case '\b':
236: retval.append("\\b");
237: continue;
238: case '\t':
239: retval.append("\\t");
240: continue;
241: case '\n':
242: retval.append("\\n");
243: continue;
244: case '\f':
245: retval.append("\\f");
246: continue;
247: case '\r':
248: retval.append("\\r");
249: continue;
250: case '\"':
251: retval.append("\\\"");
252: continue;
253: case '\'':
254: retval.append("\\\'");
255: continue;
256: case '\\':
257: retval.append("\\\\");
258: continue;
259: default:
260: if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
261: String s = "0000" + Integer.toString(ch, 16);
262: retval.append("\\u"
263: + s.substring(s.length() - 4, s.length()));
264: } else {
265: retval.append(ch);
266: }
267: continue;
268: }
269: }
270: return retval.toString();
271: }
272:
273: }
|