KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > process > extended > State


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.process.extended;
10
11 import net.sourceforge.chaperon.model.extended.ExtendedGrammar;
12 import net.sourceforge.chaperon.model.extended.Pattern;
13 import net.sourceforge.chaperon.model.extended.PatternSet;
14
15 /**
16  * This class represents a set of items, which means positions of definition, in definition. These
17  * states were used to decribes states.
18  *
19  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
20  * @version CVS $Id: State.java,v 1.1 2004/01/04 16:49:12 benedikta Exp $
21  */

22 public class State
23 {
24   public State next = null;
25
26   //private Item[] items = new Item[0];
27
public Item first = null;
28   private ShiftAction[] shiftActions = new ShiftAction[0];
29   private GotoAction[] gotoActions = new GotoAction[0];
30   private LookaheadReduceAction[] lookaheadReduceActions = new LookaheadReduceAction[0];
31   private ReduceAction[] reduceActions = new ReduceAction[0];
32   private ExtendedGrammar grammar;
33
34   public State(ExtendedGrammar grammar)
35   {
36     this.grammar = grammar;
37   }
38
39   public boolean addItem(Item newItem)
40   {
41     if (first==null)
42     {
43       first = newItem;
44       return true;
45     }
46
47     for (Item item = first; item!=null; item = item.next)
48     {
49       if (item.equals(newItem))
50         return false;
51
52       if (item.next==null)
53       {
54         item.next = newItem;
55         return true;
56       }
57     }
58
59     return true;
60   }
61
62   public boolean isEmpty()
63   {
64     return first==null;
65   }
66
67   /**
68    * Compares two states.
69    *
70    * @param o Other state.
71    *
72    * @return True, if the states are equal.
73    */

74   public boolean equals(Object JavaDoc o)
75   {
76     if (o instanceof State)
77     {
78       State state = (State)o;
79
80       for (Item item = first; item!=null; item = item.next)
81         for (Item foreignItem = state.first; foreignItem!=null; foreignItem = foreignItem.next)
82         {
83           if (item.equals(foreignItem))
84             break;
85
86           if (foreignItem.next==null)
87             return false;
88         }
89
90       for (Item foreignItem = state.first; foreignItem!=null; foreignItem = foreignItem.next)
91         for (Item item = first; item!=null; item = item.next)
92         {
93           if (item.equals(foreignItem))
94             break;
95
96           if (item.next==null)
97             return false;
98         }
99
100       return true;
101     }
102
103     return false;
104   }
105
106   public PatternSet getNextPattern()
107   {
108     PatternSet pattern = new PatternSet();
109
110     for (Item item = first; item!=null; item = item.next)
111       if (item.position==Item.SHIFT)
112         pattern.addPattern(item.pattern);
113
114     return pattern;
115   }
116
117   public PatternSet getPreviousPattern()
118   {
119     PatternSet pattern = new PatternSet();
120
121     for (Item item = first; item!=null; item = item.next)
122       if ((item.position==Item.GOTO) && (item.pattern!=null))
123         pattern.addPattern(item.pattern);
124
125     return pattern;
126   }
127
128   /**
129    * Add a transition to this state.
130    *
131    * @param symbol Symbol, which forces a transition into another state.
132    * @param state Destination state.
133    */

134   public boolean addShiftAction(char minimum, char maximum, State state)
135   {
136     return addShiftAction(new ShiftAction(minimum, maximum, state));
137   }
138
139   public boolean addShiftAction(ShiftAction action)
140   {
141     for (int i = 0; i<shiftActions.length; i++)
142     {
143       if (shiftActions[i].equals(action))
144         if (shiftActions[i].state!=action.state)
145           throw new IllegalArgumentException JavaDoc("Already shift transition defined with different destination");
146         else
147           return false;
148
149       // merging actions
150
if ((shiftActions[i].minimum==(action.maximum+1)) && (shiftActions[i].state==action.state))
151       {
152         shiftActions[i] = new ShiftAction(action.minimum, shiftActions[i].maximum, action.state);
153
154         return true;
155       }
156
157       if ((shiftActions[i].maximum==(action.minimum-1)) && (shiftActions[i].state==action.state))
158       {
159         shiftActions[i] = new ShiftAction(shiftActions[i].minimum, action.maximum, action.state);
160
161         return true;
162       }
163     }
164
165     ShiftAction[] newShiftActions = new ShiftAction[shiftActions.length+1];
166     System.arraycopy(shiftActions, 0, newShiftActions, 0, shiftActions.length);
167     newShiftActions[shiftActions.length] = action;
168     shiftActions = newShiftActions;
169
170     return true;
171   }
172
173   public ShiftAction[] getShiftActions()
174   {
175     return shiftActions;
176   }
177
178   public ShiftAction getShiftAction(char character)
179   {
180     for (int i = 0; i<shiftActions.length; i++)
181       if ((shiftActions[i].minimum<=character) && (shiftActions[i].maximum>=character))
182         return shiftActions[i];
183
184     return null;
185   }
186
187   public void addGotoAction(String JavaDoc symbol, State state)
188   {
189     addGotoAction(new GotoAction(symbol, state));
190   }
191
192   public void addGotoAction(Pattern pattern, State state)
193   {
194     addGotoAction(new GotoAction(pattern, state));
195   }
196
197   public void addGotoAction(GotoAction action)
198   {
199     for (int i = 0; i<gotoActions.length; i++)
200       if (gotoActions[i].equals(action))
201         if (gotoActions[i].state!=action.state)
202           throw new IllegalArgumentException JavaDoc("Already goto transition defined with different destination");
203         else
204           return;
205
206     GotoAction[] newGotoActions = new GotoAction[gotoActions.length+1];
207
208     for (int i = 0; i<gotoActions.length; i++)
209       newGotoActions[i] = gotoActions[i];
210
211     newGotoActions[gotoActions.length] = action;
212     gotoActions = newGotoActions;
213   }
214
215   public GotoAction[] getGotoActions()
216   {
217     return gotoActions;
218   }
219
220   public GotoAction getGotoAction(String JavaDoc symbol)
221   {
222     for (int i = 0; i<gotoActions.length; i++)
223       if ((gotoActions[i].symbol!=null) && (gotoActions[i].symbol.equals(symbol)))
224         return gotoActions[i];
225
226     return null;
227   }
228
229   public GotoAction getGotoAction(Pattern pattern)
230   {
231     for (int i = 0; i<gotoActions.length; i++)
232       if (gotoActions[i].pattern==pattern)
233         return gotoActions[i];
234
235     return null;
236   }
237
238   public GotoAction getGotoAction(ReduceAction action)
239   {
240     if (action.symbol!=null)
241     {
242       for (int i = 0; i<gotoActions.length; i++)
243         if ((gotoActions[i].symbol!=null) && (gotoActions[i].symbol.equals(action.symbol)))
244           return gotoActions[i];
245     }
246     else
247     {
248       for (int i = 0; i<gotoActions.length; i++)
249         if (gotoActions[i].pattern==action.pattern)
250           return gotoActions[i];
251     }
252
253     return null;
254   }
255
256   public void addLookaheadReduceAction(char minimum, char maximum, String JavaDoc symbol, int length)
257   {
258     addLookaheadReduceAction(new LookaheadReduceAction(minimum, maximum, symbol, length));
259   }
260
261   public void addLookaheadReduceAction(char minimum, char maximum, Pattern pattern, int length)
262   {
263     addLookaheadReduceAction(new LookaheadReduceAction(minimum, maximum, pattern, length));
264   }
265
266   public boolean addLookaheadReduceAction(LookaheadReduceAction action)
267   {
268     for (int i = 0; i<lookaheadReduceActions.length; i++)
269     {
270       if (lookaheadReduceActions[i].equals(action))
271         return false;
272
273       // merging actions
274
if (lookaheadReduceActions[i].symbol!=null)
275       {
276         if ((lookaheadReduceActions[i].minimum==(action.maximum+1)) &&
277             (lookaheadReduceActions[i].symbol.equals(action.symbol)) &&
278             (lookaheadReduceActions[i].length==action.length))
279         {
280           lookaheadReduceActions[i] =
281             new LookaheadReduceAction(action.minimum, lookaheadReduceActions[i].maximum,
282                                       action.symbol, action.length);
283           return true;
284         }
285
286         if ((lookaheadReduceActions[i].maximum==(action.minimum-1)) &&
287             (lookaheadReduceActions[i].symbol.equals(action.symbol)) &&
288             (lookaheadReduceActions[i].length==action.length))
289         {
290           lookaheadReduceActions[i] =
291             new LookaheadReduceAction(lookaheadReduceActions[i].minimum, action.maximum,
292                                       action.symbol, action.length);
293           return true;
294         }
295       }
296       else
297       {
298         if ((lookaheadReduceActions[i].minimum==(action.maximum+1)) &&
299             (lookaheadReduceActions[i].pattern==action.pattern) &&
300             (lookaheadReduceActions[i].length==action.length))
301         {
302           lookaheadReduceActions[i] =
303             new LookaheadReduceAction(action.minimum, lookaheadReduceActions[i].maximum,
304                                       action.pattern, action.length);
305           return true;
306         }
307
308         if ((lookaheadReduceActions[i].maximum==(action.minimum-1)) &&
309             (lookaheadReduceActions[i].pattern==action.pattern) &&
310             (lookaheadReduceActions[i].length==action.length))
311         {
312           lookaheadReduceActions[i] =
313             new LookaheadReduceAction(lookaheadReduceActions[i].minimum, action.maximum,
314                                       action.pattern, action.length);
315           return true;
316         }
317       }
318     }
319
320     LookaheadReduceAction[] newLookaheadReduceActions =
321       new LookaheadReduceAction[lookaheadReduceActions.length+1];
322
323     for (int i = 0; i<lookaheadReduceActions.length; i++)
324       newLookaheadReduceActions[i] = lookaheadReduceActions[i];
325
326     newLookaheadReduceActions[lookaheadReduceActions.length] = action;
327     lookaheadReduceActions = newLookaheadReduceActions;
328     return true;
329   }
330
331   public LookaheadReduceAction[] getLookaheadReduceActions()
332   {
333     return lookaheadReduceActions;
334   }
335
336   public void addReduceAction(String JavaDoc symbol, int length)
337   {
338     addReduceAction(new ReduceAction(symbol, length));
339   }
340
341   public void addReduceAction(Pattern pattern, int length)
342   {
343     addReduceAction(new ReduceAction(pattern, length));
344   }
345
346   public void addReduceAction(ReduceAction action)
347   {
348     for (int i = 0; i<reduceActions.length; i++)
349       if (reduceActions[i].equals(action))
350         return;
351
352     ReduceAction[] newReduceActions = new ReduceAction[reduceActions.length+1];
353
354     for (int i = 0; i<reduceActions.length; i++)
355       newReduceActions[i] = reduceActions[i];
356
357     newReduceActions[reduceActions.length] = action;
358     reduceActions = newReduceActions;
359   }
360
361   public ReduceAction[] getReduceActions()
362   {
363     return reduceActions;
364   }
365
366   /**
367    * Return a string representation of this state.
368    *
369    * @return String representation of this state.
370    */

371   public String JavaDoc toString()
372   {
373     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
374
375     for (Item item = first; item!=null; item = item.next)
376     {
377       buffer.append(item);
378       buffer.append("\n");
379     }
380
381     return buffer.toString();
382   }
383 }
384
Popular Tags