001: /*
002: * xtc - The eXTensible Compiler
003: * Copyright (C) 2004-2007 Robert Grimm
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public License
007: * version 2.1 as published by the Free Software Foundation.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
017: * USA.
018: */
019: package xtc.parser;
020:
021: /**
022: * The superclass of all parser results.
023: *
024: * @author Robert Grimm
025: * @version $Revision: 1.22 $
026: */
027: public abstract class Result {
028:
029: /** The index into the parser's memoization table. */
030: public final int index;
031:
032: /**
033: * Create a new result with the specified index.
034: *
035: * @param index The index.
036: */
037: public Result(int index) {
038: this .index = index;
039: }
040:
041: /**
042: * Determine whether this result has a value.
043: *
044: * @return <code>true</code> if this result has a value.
045: */
046: public abstract boolean hasValue();
047:
048: /**
049: * Determine whether this result has the specified string value.
050: * The specified value must not be <code>null</code>.
051: *
052: * @param s The string value.
053: * @return <code>true</code> if this result has the specified string
054: * value.
055: */
056: public abstract boolean hasValue(String s);
057:
058: /**
059: * Determine whether this result has the specified string value,
060: * ignoring case. The specified value must not be
061: * <code>null</code>.
062: *
063: * @param s The string value.
064: * @return <code>true</code> if this result has the specified string
065: * value, ignoring case.
066: */
067: public abstract boolean hasValueIgnoreCase(String s);
068:
069: /**
070: * Get the semantic value for this result.
071: *
072: * @return The semantic value for this result.
073: * @throws IllegalStateException
074: * Signals that this result does not have a semantic value.
075: */
076: public abstract <T> T semanticValue();
077:
078: /**
079: * Get the parse error for this result. If the result does not
080: * represent a parse error or does not have an embedded parse error,
081: * this method must return the {@link ParseError#DUMMY dummy parse
082: * error}.
083: *
084: * @return The parse error for this result.
085: */
086: public abstract ParseError parseError();
087:
088: /**
089: * Select the more specific parse error. This method compares this
090: * result (i.e., either the parse error or the embedded parse error)
091: * with the specified parse error and returns the one representing
092: * the longer parse.
093: *
094: * @param error The error.
095: * @return The more specific parse error.
096: */
097: public abstract ParseError select(ParseError error);
098:
099: /**
100: * Create a semantic value based on this result. A straight-forward
101: * implementation of this method simply creates a new semantic
102: * value, using the specified actual value, this result's index,
103: * and the specified parse error:<pre>
104: * public SemanticValue value(Object value, ParseError error) {
105: * return new SemanticValue(value, index, error);
106: * }
107: * </pre>However, for a result that already is a semantic value, a
108: * more sophisticated implementation can avoid creating a new
109: * semantic value if the specified actual value and parse error are
110: * <i>identical</i> to those for this result.
111: *
112: * @param value The actual value.
113: * @param error The embedded parse error.
114: * @throws IllegalStateException Signals that this result is a
115: * parse error.
116: */
117: public abstract SemanticValue createValue(Object value,
118: ParseError error);
119:
120: }
|