KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > model > lexicon > Lexeme


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.model.lexicon;
10
11 import net.sourceforge.chaperon.model.Violations;
12 import net.sourceforge.chaperon.model.pattern.Pattern;
13 import net.sourceforge.chaperon.model.symbol.Symbol;
14
15 /**
16  * This class represents a lexeme within a lexicon. The lexeme associates a terminal symbol to a
17  * pattern.
18  *
19  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels </a>
20  * @version CVS $Id: Lexeme.java,v 1.4 2003/12/09 19:55:52 benedikta Exp $
21  */

22 public class Lexeme
23 {
24   private Symbol symbol = null;
25   private Pattern definition = null;
26   private String JavaDoc location = null;
27
28   /**
29    * Create a lexeme.
30    */

31   public Lexeme() {}
32
33   /**
34    * Create a lexeme.
35    *
36    * @param symbol Terminal symbol for this lexeme.
37    */

38   public Lexeme(Symbol symbol)
39   {
40     setSymbol(symbol);
41   }
42
43   /**
44    * Set the terminal symbol for this lexeme.
45    *
46    * @param symbol Terminal symbol.
47    */

48   public void setSymbol(Symbol symbol)
49   {
50     this.symbol = symbol;
51   }
52
53   /**
54    * Return the symbol for this lexeme.
55    *
56    * @return Terminal symbol.
57    */

58   public Symbol getSymbol()
59   {
60     return symbol;
61   }
62
63   /**
64    * Set a pattern as definition for this lexeme.
65    *
66    * @param definition Definition of the lexeme.
67    */

68   public void setDefinition(Pattern definition)
69   {
70     this.definition = definition;
71   }
72
73   /**
74    * Return the definition.
75    *
76    * @return Definition of the lexeme.
77    */

78   public Pattern getDefinition()
79   {
80     return definition;
81   }
82
83   /**
84    * Set the location from the input source.
85    *
86    * @param location Location in the input source.
87    */

88   public void setLocation(String JavaDoc location)
89   {
90     this.location = location;
91   }
92
93   /**
94    * Returns the location from the input source.
95    *
96    * @return Location in the input source.
97    */

98   public String JavaDoc getLocation()
99   {
100     return location;
101   }
102
103   /**
104    * Validate the lexeme.
105    *
106    * @return Return a list of violations, if this object isn't valid.
107    */

108   public Violations validate()
109   {
110     Violations violations = new Violations();
111
112     if (definition==null)
113     {
114       if (symbol!=null)
115         violations.addViolation("Lexeme "+symbol+" contains no definition", location);
116       else
117         violations.addViolation("Lexeme contains no definition", location);
118     }
119
120     if ((symbol!=null) && (symbol.getName().equals("error")))
121       violations.addViolation("Symbol with name \"error\" is not allowed", location);
122
123     return violations;
124   }
125
126   /**
127    * Return a string representation of the lexeme.
128    *
129    * @return String representation.
130    */

131   public String JavaDoc toString()
132   {
133     if (symbol!=null)
134       return symbol.toString()+" = \""+definition+"\"";
135
136     return "/* noname */ = \""+definition+"\"";
137   }
138
139   /**
140    * Creates a clone of this lexeme.
141    *
142    * @return Clone of this lexeme.
143    *
144    * @throws CloneNotSupportedException If an exception occurs during the cloning.
145    */

146   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
147   {
148     Lexeme clone = new Lexeme();
149
150     clone.symbol = symbol;
151     if (definition!=null)
152       clone.definition = (Pattern)definition.clone();
153
154     clone.location = location;
155
156     return clone;
157   }
158 }
159
Popular Tags