1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
---|
2 | * JFlex 1.4.3 *
|
---|
3 | * Copyright (C) 1998-2009 Gerwin Klein <[email protected]> *
|
---|
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.3, $Revision: 433 $, $Date: 2009-01-31 19:52:34 +1100 (Sat, 31 Jan 2009) $
|
---|
31 | */
|
---|
32 | final public class Action {
|
---|
33 |
|
---|
34 | /** A normal action */
|
---|
35 | public final static int NORMAL = 0;
|
---|
36 | /** Action of a lookahead expression r1/r2 with fixed length r1 */
|
---|
37 | public final static int FIXED_BASE = 1;
|
---|
38 | /** Action of a lookahead expression r1/r2 with fixed length r2 */
|
---|
39 | public final static int FIXED_LOOK = 2;
|
---|
40 | /** Action of a lookahead expression r1/r2 with a finite choice of
|
---|
41 | * fixed lengths in r2 */
|
---|
42 | public final static int FINITE_CHOICE = 3;
|
---|
43 | /** Action of a general lookahead expression */
|
---|
44 | public final static int GENERAL_LOOK = 4;
|
---|
45 | /** Action of the 2nd forward pass for lookahead */
|
---|
46 | public final static int FORWARD_ACTION = 5;
|
---|
47 | /** Action of the backward pass for lookahead */
|
---|
48 | public final static int BACKWARD_ACTION = 6;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * The Java code this Action represents
|
---|
52 | */
|
---|
53 | String content;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * The priority (i.e. line number in the specification) of this Action.
|
---|
57 | */
|
---|
58 | int priority;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Which kind of action this is.
|
---|
62 | * (normal, <code>a/b</code> with fixed length a, fixed length b, etc)
|
---|
63 | */
|
---|
64 | private int kind = NORMAL;
|
---|
65 |
|
---|
66 | /** The length of the lookahead (if fixed) */
|
---|
67 | private int len;
|
---|
68 |
|
---|
69 | /** The entry state of the corresponding forward DFA (if general lookahead) */
|
---|
70 | private int entryState;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Creates a new Action object with specified content and line number.
|
---|
74 | *
|
---|
75 | * @param content java code
|
---|
76 | * @param priority line number
|
---|
77 | */
|
---|
78 | public Action(String content, int priority) {
|
---|
79 | this.content = content.trim();
|
---|
80 | this.priority = priority;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Creates a new Action object of the specified kind. Only
|
---|
85 | * accepts FORWARD_ACTION or BACKWARD_ACTION.
|
---|
86 | *
|
---|
87 | * @param kind the kind of action
|
---|
88 | *
|
---|
89 | * @see #FORWARD_ACTION
|
---|
90 | * @see #BACKWARD_ACTION
|
---|
91 | */
|
---|
92 | public Action(int kind) {
|
---|
93 | if (kind != FORWARD_ACTION && kind != BACKWARD_ACTION)
|
---|
94 | throw new GeneratorException();
|
---|
95 | this.content = "";
|
---|
96 | this.priority = Integer.MAX_VALUE;
|
---|
97 | this.kind = kind;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Compares the priority value of this Action with the specified action.
|
---|
102 | *
|
---|
103 | * @param other the other Action to compare this Action with.
|
---|
104 | *
|
---|
105 | * @return this Action if it has higher priority - the specified one, if not.
|
---|
106 | */
|
---|
107 | public Action getHigherPriority(Action other) {
|
---|
108 | if (other == null) return this;
|
---|
109 |
|
---|
110 | // the smaller the number the higher the priority
|
---|
111 | if (other.priority > this.priority)
|
---|
112 | return this;
|
---|
113 | else
|
---|
114 | return other;
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Returns the String representation of this object.
|
---|
120 | *
|
---|
121 | * @return string representation of the action
|
---|
122 | */
|
---|
123 | public String toString() {
|
---|
124 | return "Action (priority "+priority+", lookahead "+kind+") :" +
|
---|
125 | Out.NL+content; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Returns <code>true</code> iff the parameter is an
|
---|
131 | * Action with the same content as this one.
|
---|
132 | *
|
---|
133 | * @param a the object to compare this Action with
|
---|
134 | * @return true if the action strings are equal
|
---|
135 | */
|
---|
136 | public boolean isEquiv(Action a) {
|
---|
137 | return this == a ||
|
---|
138 | (this.content.equals(a.content) &&
|
---|
139 | this.kind == a.kind &&
|
---|
140 | this.len == a.len &&
|
---|
141 | this.entryState == a.entryState);
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Calculate hash value.
|
---|
147 | *
|
---|
148 | * @return a hash value for this Action
|
---|
149 | */
|
---|
150 | public int hashCode() {
|
---|
151 | return content.hashCode();
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Test for equality to another object.
|
---|
157 | *
|
---|
158 | * This action equals another object if the other
|
---|
159 | * object is an equivalent action.
|
---|
160 | *
|
---|
161 | * @param o the other object.
|
---|
162 | *
|
---|
163 | * @see Action#isEquiv(Action)
|
---|
164 | */
|
---|
165 | public boolean equals(Object o) {
|
---|
166 | if (o instanceof Action)
|
---|
167 | return isEquiv((Action) o);
|
---|
168 | else
|
---|
169 | return false;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Return true iff this is action belongs to a general lookahead rule.
|
---|
174 | *
|
---|
175 | * @return true if this actions belongs to a general lookahead rule.
|
---|
176 | */
|
---|
177 | public boolean isGenLookAction() {
|
---|
178 | return kind == GENERAL_LOOK;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Return true if code for this is action should be emitted, false
|
---|
183 | * if it is a BACK/FORWARD lookahead action.
|
---|
184 | *
|
---|
185 | * @return true if code should be emitted for this action.
|
---|
186 | */
|
---|
187 | public boolean isEmittable() {
|
---|
188 | return kind != BACKWARD_ACTION && kind != FORWARD_ACTION;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Return kind of lookahead.
|
---|
193 | */
|
---|
194 | public int lookAhead() {
|
---|
195 | return kind;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Sets the lookahead kind and data for this action
|
---|
200 | *
|
---|
201 | * @param kind which kind of lookahead it is
|
---|
202 | * @param data the length for fixed length look aheads.
|
---|
203 | *
|
---|
204 | */
|
---|
205 | public void setLookAction(int kind, int data) {
|
---|
206 | this.kind = kind;
|
---|
207 | this.len = data;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * The length of the lookahead or base if this is a fixed length
|
---|
212 | * lookahead action.
|
---|
213 | */
|
---|
214 | public int getLookLength() {
|
---|
215 | return len;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Return the corresponding entry state for the forward DFA (if this
|
---|
220 | * is a general lookahead expression)
|
---|
221 | *
|
---|
222 | * @return the forward DFA entry state (+1 is the backward DFA)
|
---|
223 | */
|
---|
224 | public int getEntryState() {
|
---|
225 | return entryState;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Set the corresponding entry state for the forward DFA of this action
|
---|
230 | * (if this is a general lookahead expression)
|
---|
231 | *
|
---|
232 | * @param the entry state for the forward DFA of this action
|
---|
233 | */
|
---|
234 | public void setEntryState(int entryState) {
|
---|
235 | this.entryState = entryState;
|
---|
236 | }
|
---|
237 |
|
---|
238 | public Action copyChoice(int length) {
|
---|
239 | Action a = new Action(this.content, this.priority);
|
---|
240 | a.setLookAction(FINITE_CHOICE, length);
|
---|
241 | return a;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * String representation of the lookahead kind of this action.
|
---|
246 | *
|
---|
247 | * @return the string representation
|
---|
248 | */
|
---|
249 | public String lookString() {
|
---|
250 | switch (kind) {
|
---|
251 | case NORMAL: return "";
|
---|
252 | case BACKWARD_ACTION: return "LOOK_BACK";
|
---|
253 | case FIXED_BASE: return "FIXED_BASE";
|
---|
254 | case FIXED_LOOK: return "FIXED_LOOK";
|
---|
255 | case FINITE_CHOICE: return "FINITE_CHOICE";
|
---|
256 | case FORWARD_ACTION: return "LOOK_FORWARD";
|
---|
257 | case GENERAL_LOOK: return "LOOK_ACTION";
|
---|
258 | default: return "unknown lookahead type";
|
---|
259 | }
|
---|
260 | }
|
---|
261 | }
|
---|