001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.model.lexicon;
010:
011: import net.sourceforge.chaperon.model.Violations;
012: import net.sourceforge.chaperon.model.pattern.Pattern;
013: import net.sourceforge.chaperon.model.symbol.Symbol;
014:
015: /**
016: * This class represents a lexeme within a lexicon. The lexeme associates a terminal symbol to a
017: * pattern.
018: *
019: * @author <a href="mailto:stephan@apache.org">Stephan Michels </a>
020: * @version CVS $Id: Lexeme.java,v 1.4 2003/12/09 19:55:52 benedikta Exp $
021: */
022: public class Lexeme {
023: private Symbol symbol = null;
024: private Pattern definition = null;
025: private String location = null;
026:
027: /**
028: * Create a lexeme.
029: */
030: public Lexeme() {
031: }
032:
033: /**
034: * Create a lexeme.
035: *
036: * @param symbol Terminal symbol for this lexeme.
037: */
038: public Lexeme(Symbol symbol) {
039: setSymbol(symbol);
040: }
041:
042: /**
043: * Set the terminal symbol for this lexeme.
044: *
045: * @param symbol Terminal symbol.
046: */
047: public void setSymbol(Symbol symbol) {
048: this .symbol = symbol;
049: }
050:
051: /**
052: * Return the symbol for this lexeme.
053: *
054: * @return Terminal symbol.
055: */
056: public Symbol getSymbol() {
057: return symbol;
058: }
059:
060: /**
061: * Set a pattern as definition for this lexeme.
062: *
063: * @param definition Definition of the lexeme.
064: */
065: public void setDefinition(Pattern definition) {
066: this .definition = definition;
067: }
068:
069: /**
070: * Return the definition.
071: *
072: * @return Definition of the lexeme.
073: */
074: public Pattern getDefinition() {
075: return definition;
076: }
077:
078: /**
079: * Set the location from the input source.
080: *
081: * @param location Location in the input source.
082: */
083: public void setLocation(String location) {
084: this .location = location;
085: }
086:
087: /**
088: * Returns the location from the input source.
089: *
090: * @return Location in the input source.
091: */
092: public String getLocation() {
093: return location;
094: }
095:
096: /**
097: * Validate the lexeme.
098: *
099: * @return Return a list of violations, if this object isn't valid.
100: */
101: public Violations validate() {
102: Violations violations = new Violations();
103:
104: if (definition == null) {
105: if (symbol != null)
106: violations.addViolation("Lexeme " + symbol
107: + " contains no definition", location);
108: else
109: violations.addViolation(
110: "Lexeme contains no definition", location);
111: }
112:
113: if ((symbol != null) && (symbol.getName().equals("error")))
114: violations.addViolation(
115: "Symbol with name \"error\" is not allowed",
116: location);
117:
118: return violations;
119: }
120:
121: /**
122: * Return a string representation of the lexeme.
123: *
124: * @return String representation.
125: */
126: public String toString() {
127: if (symbol != null)
128: return symbol.toString() + " = \"" + definition + "\"";
129:
130: return "/* noname */ = \"" + definition + "\"";
131: }
132:
133: /**
134: * Creates a clone of this lexeme.
135: *
136: * @return Clone of this lexeme.
137: *
138: * @throws CloneNotSupportedException If an exception occurs during the cloning.
139: */
140: public Object clone() throws CloneNotSupportedException {
141: Lexeme clone = new Lexeme();
142:
143: clone.symbol = symbol;
144: if (definition != null)
145: clone.definition = (Pattern) definition.clone();
146:
147: clone.location = location;
148:
149: return clone;
150: }
151: }
|