KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JFlex > Action


1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * JFlex 1.4.1 *
3  * Copyright (C) 1998-2004 Gerwin Klein <lsf@jflex.de> *
4  * All rights reserved. *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License. See the file *
8  * COPYRIGHT for more information. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License along *
16  * with this program; if not, write to the Free Software Foundation, Inc., *
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
18  * *
19  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

20
21 package JFlex;
22
23
24 /**
25  * Encapsulates an action in the specification.
26  *
27  * It stores the Java code as String together with a priority (line number in the specification).
28  *
29  * @author Gerwin Klein
30  * @version JFlex 1.4.1, $Revision: 2.6 $, $Date: 2004/11/06 23:03:30 $
31  */

32 final public class Action {
33
34
35   /**
36    * The Java code this Action represents
37    */

38   String JavaDoc content;
39
40   /**
41    * The priority (i.e. line number in the specification) of this Action.
42    */

43   int priority;
44
45   /**
46    * True iff the action belongs to an lookahead expresstion
47    * (<code>a/b</code> or <code>r$</code>)
48    */

49   private boolean isLookAction;
50
51
52   /**
53    * Creates a new Action object with specified content and line number.
54    *
55    * @param content java code
56    * @param priority line number
57    */

58   public Action(String JavaDoc content, int priority) {
59     this.content = content.trim();
60     this.priority = priority;
61   }
62
63
64   /**
65    * Compares the priority value of this Action with the specified action.
66    *
67    * @param other the other Action to compare this Action with.
68    *
69    * @return this Action if it has higher priority - the specified one, if not.
70    */

71   public Action getHigherPriority(Action other) {
72     if (other == null) return this;
73
74     // the smaller the number the higher the priority
75
if (other.priority > this.priority)
76       return this;
77     else
78       return other;
79   }
80
81
82   /**
83    * Returns the String representation of this object.
84    *
85    * @return string representation of the action
86    */

87   public String JavaDoc toString() {
88     return "Action (priority "+priority+", lookahead "+isLookAction+") :"+Out.NL+content; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
89
}
90
91
92   /**
93    * Returns <code>true</code> iff the parameter is an
94    * Action with the same content as this one.
95    *
96    * @param a the object to compare this Action with
97    * @return true if the action strings are equal
98    */

99   public boolean isEquiv(Action a) {
100     return this == a || this.content.equals(a.content);
101   }
102
103
104   /**
105    * Calculate hash value.
106    *
107    * @return a hash value for this Action
108    */

109   public int hashCode() {
110     return content.hashCode();
111   }
112
113
114   /**
115    * Test for equality to another object.
116    *
117    * This action equals another object if the other
118    * object is an equivalent action.
119    *
120    * @param o the other object.
121    *
122    * @see Action#isEquiv(Action)
123    */

124   public boolean equals(Object JavaDoc o) {
125     if (o instanceof Action)
126       return isEquiv((Action) o);
127     else
128       return false;
129   }
130   
131   /**
132    * Return look ahead flag.
133    *
134    * @return true if this actions belongs to a lookahead rule
135    */

136   public boolean isLookAction() {
137     return isLookAction;
138   }
139
140   /**
141    * Sets the look ahead flag for this action
142    *
143    * @param b set to true if this action belongs to a look ahead rule
144    */

145   public void setLookAction(boolean b) {
146     isLookAction = b;
147   }
148   
149 }
150
Popular Tags