01: /*
02: * xtc - The eXTensible Compiler
03: * Copyright (C) 2004-2007 Robert Grimm
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public License
07: * version 2.1 as published by the Free Software Foundation.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17: * USA.
18: */
19: package xtc.parser;
20:
21: /**
22: * An erroneous parse.
23: *
24: * @author Robert Grimm
25: * @version $Revision: 1.25 $
26: */
27: public final class ParseError extends Result {
28:
29: /**
30: * The dummy parse error. The dummy parse error is used for
31: * initializing a production's parse error and then threading the
32: * most specific parse error through the production. It works like
33: * a sentinel for managing linked lists, avoiding repeated tests for
34: * a <code>null</code> value.
35: *
36: * @see SemanticValue#error
37: */
38: public static final ParseError DUMMY = new ParseError(
39: "parse error", -1);
40:
41: /** The error message. */
42: public final String msg;
43:
44: /**
45: * Create a new parse error.
46: *
47: * @param msg The error message.
48: * @param index The index for the error location.
49: */
50: public ParseError(final String msg, final int index) {
51: super (index);
52: this .msg = msg;
53: }
54:
55: public boolean hasValue() {
56: return false;
57: }
58:
59: public boolean hasValue(final String s) {
60: return false;
61: }
62:
63: public boolean hasValueIgnoreCase(final String s) {
64: return false;
65: }
66:
67: public <T> T semanticValue() {
68: throw new IllegalStateException(
69: "Parse error does not have a semantic value");
70: }
71:
72: public ParseError parseError() {
73: return this ;
74: }
75:
76: public ParseError select(final ParseError other) {
77: return (index <= other.index) ? other : this ;
78: }
79:
80: /**
81: * Select the more specific parse error. This method compares this
82: * parse error with the specified index and returns a parse error
83: * representing the longer parse (creating a new parse error with
84: * the specified message and index if necessary).
85: *
86: * @param msg The error message.
87: * @param index The index of the parse error.
88: */
89: public ParseError select(final String msg, final int index) {
90: return (this .index < index) ? new ParseError(msg, index) : this ;
91: }
92:
93: public SemanticValue createValue(final Object value,
94: final ParseError error) {
95: throw new IllegalStateException(
96: "Parse error cannot lead to semantic value");
97: }
98:
99: }
|