KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JFlex > RegExps


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 package JFlex;
21
22 import java.util.*;
23
24
25 /**
26  * Stores all rules of the specification for later access in RegExp -> NFA
27  *
28  * @author Gerwin Klein
29  * @version JFlex 1.4.1, $Revision: 2.4 $, $Date: 2004/11/06 23:03:31 $
30  */

31 public class RegExps {
32   
33   /** the spec line in which a regexp is used */
34   Vector /* of Integer */ lines;
35
36   /** the lexical states in wich the regexp is used */
37   Vector /* of Vector of Integer */ states;
38
39   /** the regexp */
40   Vector /* of RegExp */ regExps;
41
42   /** the action of a regexp */
43   Vector /* of Action */ actions;
44   
45   /** flag if it is a BOL regexp */
46   Vector /* of Boolean */ BOL;
47
48   /** the lookahead expression */
49   Vector /* of RegExp */ look;
50
51   public RegExps() {
52     states = new Vector();
53     regExps = new Vector();
54     actions = new Vector();
55     BOL = new Vector();
56     look = new Vector();
57     lines = new Vector();
58   }
59
60   public int insert(int line, Vector stateList, RegExp regExp, Action action,
61                      Boolean JavaDoc isBOL, RegExp lookAhead) {
62     if (Options.DEBUG) {
63       Out.debug("Inserting regular expression with statelist :"+Out.NL+stateList); //$NON-NLS-1$
64
Out.debug("and action code :"+Out.NL+action.content+Out.NL); //$NON-NLS-1$
65
Out.debug("expression :"+Out.NL+regExp); //$NON-NLS-1$
66
}
67
68     states.addElement(stateList);
69     regExps.addElement(regExp);
70     actions.addElement(action);
71     BOL.addElement(isBOL);
72     look.addElement(lookAhead);
73     lines.addElement(new Integer JavaDoc(line));
74     
75     return states.size()-1;
76   }
77
78   public int insert(Vector stateList, Action action) {
79
80     if (Options.DEBUG) {
81       Out.debug("Inserting eofrule with statelist :"+Out.NL+stateList); //$NON-NLS-1$
82
Out.debug("and action code :"+Out.NL+action.content+Out.NL); //$NON-NLS-1$
83
}
84
85     states.addElement(stateList);
86     regExps.addElement(null);
87     actions.addElement(action);
88     BOL.addElement(null);
89     look.addElement(null);
90     lines.addElement(null);
91     
92     return states.size()-1;
93   }
94
95   public void addStates(int regNum, Vector newStates) {
96     Enumeration s = newStates.elements();
97     
98     while (s.hasMoreElements())
99       ((Vector)states.elementAt(regNum)).addElement(s.nextElement());
100   }
101
102   public int getNum() {
103     return states.size();
104   }
105
106   public boolean isBOL(int num) {
107     return ((Boolean JavaDoc) BOL.elementAt(num)).booleanValue();
108   }
109   
110   public RegExp getLookAhead(int num) {
111     return (RegExp) look.elementAt(num);
112   }
113
114   public boolean isEOF(int num) {
115     return BOL.elementAt(num) == null;
116   }
117
118   public Vector getStates(int num) {
119     return (Vector) states.elementAt(num);
120   }
121
122   public RegExp getRegExp(int num) {
123     return (RegExp) regExps.elementAt(num);
124   }
125
126   public int getLine(int num) {
127     return ((Integer JavaDoc) lines.elementAt(num)).intValue();
128   }
129
130   public void checkActions() {
131     if ( actions.elementAt(actions.size()-1) == null ) {
132       Out.error(ErrorMessages.NO_LAST_ACTION);
133       throw new GeneratorException();
134     }
135   }
136
137   public Action getAction(int num) {
138     while ( num < actions.size() && actions.elementAt(num) == null )
139       num++;
140
141     return (Action) actions.elementAt(num);
142   }
143
144   public int NFASize(Macros macros) {
145     int size = 0;
146     Enumeration e = regExps.elements();
147     while (e.hasMoreElements()) {
148       RegExp r = (RegExp) e.nextElement();
149       if (r != null) size += r.size(macros);
150     }
151     e = look.elements();
152     while (e.hasMoreElements()) {
153       RegExp r = (RegExp) e.nextElement();
154       if (r != null) size += r.size(macros);
155     }
156     return size;
157   }
158 }
159
Popular Tags