~ubuntu-branches/ubuntu/gutsy/jflex/gutsy

« back to all changes in this revision

Viewing changes to src/JFlex/Macros.java

  • Committer: Bazaar Package Importer
  • Author(s): Takashi Okamoto
  • Date: 2002-02-16 13:38:21 UTC
  • Revision ID: james.westby@ubuntu.com-20020216133821-5wsdprpt9xl7ondr
Tags: upstream-1.3.5
ImportĀ upstreamĀ versionĀ 1.3.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 
2
 * JFlex 1.3.5                                                             *
 
3
 * Copyright (C) 1998-2001  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
import java.util.*;
 
24
 
 
25
 
 
26
/**
 
27
 * Symbol table and expander for macros.
 
28
 * 
 
29
 * Maps macros to their (expanded) definitions, detects cycles and 
 
30
 * unused macros.
 
31
 *
 
32
 * @author Gerwin Klein
 
33
 * @version JFlex 1.3.5, $Revision: 1.14 $, $Date: 2001/10/08 10:08:02 $
 
34
 */
 
35
final public class Macros {
 
36
 
 
37
  /**
 
38
   * Maps names of macros to their definition
 
39
   */
 
40
  private Hashtable macros;
 
41
 
 
42
  private Hashtable used;
 
43
 
 
44
 
 
45
  /**
 
46
   * Creates a new macro expander.
 
47
   */
 
48
  public Macros() {
 
49
    macros = new Hashtable();
 
50
    used = new Hashtable();
 
51
  }
 
52
 
 
53
 
 
54
  /**
 
55
   * Stores a new macro and its definition.
 
56
   *
 
57
   * @param name         the name of the new macro
 
58
   * @param definition   the definition of the new macro
 
59
   *
 
60
   * @return <code>true</code>, iff the macro name has not been
 
61
   *         stored before.
 
62
   */
 
63
  public boolean insert(String name, RegExp definition) {
 
64
    
 
65
    if (Out.DEBUG) 
 
66
      Out.debug("inserting macro "+name+" with definition :"+Out.NL+definition);
 
67
      
 
68
    used.put(name, new Boolean(false));
 
69
    return macros.put(name,definition) == null;    
 
70
  }
 
71
 
 
72
 
 
73
  /**
 
74
   * Marks a makro as used.
 
75
   *
 
76
   * @return <code>true</code>, iff the macro name has been
 
77
   *         stored before.
 
78
   */
 
79
  public boolean markUsed(String name) {
 
80
    return used.put(name, new Boolean(true)) != null;
 
81
  }
 
82
 
 
83
 
 
84
  /**
 
85
   * Tests if a macro has been used.
 
86
   *
 
87
   * @return <code>true</code>, iff the macro has been used in 
 
88
   *         a regular expression.
 
89
   */
 
90
  public boolean isUsed(String name) {
 
91
    return ((Boolean)used.get(name)).booleanValue();
 
92
  }
 
93
 
 
94
 
 
95
  /**
 
96
   * Returns all unused macros.
 
97
   *
 
98
   * @return the enumeration of macro-names that have not been used.
 
99
   */
 
100
  public Enumeration unused() {
 
101
    
 
102
    Vector unUsed = new Vector();
 
103
 
 
104
    Enumeration names = used.keys();
 
105
    while ( names.hasMoreElements() ) {
 
106
      String name = (String) names.nextElement();
 
107
      Boolean isUsed = (Boolean) used.get( name );
 
108
      if ( !isUsed.booleanValue() ) unUsed.addElement(name);
 
109
    }
 
110
    
 
111
    return unUsed.elements();
 
112
  }
 
113
 
 
114
 
 
115
  /**
 
116
   * Fetches the definition of the macro with the specified name,
 
117
   * <p>
 
118
   * The definition will either be the same as stored (expand() not 
 
119
   * called), or an equivalent one, that doesn't contain any macro 
 
120
   * usages (expand() called before).
 
121
   *
 
122
   * @param name   the name of the macro
 
123
   *
 
124
   * @return the definition of the macro, <code>null</code> if 
 
125
   *         no macro with the specified name has been stored.
 
126
   *
 
127
   * @see JFlex.Macros#expand
 
128
   */
 
129
  public RegExp getDefinition(String name) {
 
130
    return (RegExp) macros.get(name);
 
131
  }
 
132
 
 
133
 
 
134
  /**
 
135
   * Expands all stored macros, so that getDefinition always returns
 
136
   * a defintion that doesn't contain any macro usages.
 
137
   *
 
138
   * @throws MacroException   if there is a cycle in the macro usage graph.
 
139
   */
 
140
   public void expand() throws MacroException {
 
141
    
 
142
    Enumeration names;
 
143
 
 
144
    names = macros.keys();
 
145
    
 
146
    while ( names.hasMoreElements() ) {
 
147
      String name = (String) names.nextElement();
 
148
      if ( isUsed(name) )
 
149
        macros.put(name, expandMacro(name, getDefinition(name))); 
 
150
      // this put doesn't get a new key, so only the a new value
 
151
      // is set for the key "name" (without changing the enumeration 
 
152
      // "names"!)
 
153
    }
 
154
  }
 
155
 
 
156
 
 
157
  
 
158
 
 
159
  /**
 
160
   * Expands the specified macro by replacing each macro usage
 
161
   * with the stored definition. 
 
162
   *   
 
163
   * @param name        the name of the macro to expand (for detecting cycles)
 
164
   * @param definition  the definition of the macro to expand
 
165
   *
 
166
   * @return the expanded definition of the macro.
 
167
   * 
 
168
   * @throws MacroException when an error (such as a cyclic definition)
 
169
   *                              occurs during expansion
 
170
   */
 
171
  private RegExp expandMacro(String name, RegExp definition) throws MacroException {
 
172
 
 
173
    // Out.print("checking macro "+name);
 
174
    // Out.print("definition is "+definition);
 
175
 
 
176
    switch ( definition.type ) {
 
177
      case sym.BAR: 
 
178
      case sym.CONCAT:   
 
179
        RegExp2 binary = (RegExp2) definition;
 
180
        binary.r1 = expandMacro(name, binary.r1);
 
181
        binary.r2 = expandMacro(name, binary.r2);
 
182
        break;
 
183
        
 
184
      case sym.STAR:
 
185
      case sym.PLUS:
 
186
      case sym.QUESTION: 
 
187
        RegExp1 unary = (RegExp1) definition;
 
188
        unary.content = expandMacro(name, (RegExp) unary.content);
 
189
        break;
 
190
      
 
191
      case sym.MACROUSE: 
 
192
        String usename = (String) ((RegExp1) definition).content;
 
193
 
 
194
        if ( name.equals(usename) )
 
195
          throw new MacroException("Macro "+name+" contains a cycle");
 
196
          
 
197
        RegExp usedef = getDefinition(usename);
 
198
 
 
199
        if ( usedef == null ) 
 
200
          throw new MacroException("Found no definition for {"+usename+"} while expanding {"+name+"}");
 
201
 
 
202
        markUsed(usename);
 
203
          
 
204
        return expandMacro(name, usedef);
 
205
    }
 
206
 
 
207
    return definition;    
 
208
  }
 
209
}