KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > regexp > REDebugCompiler


1 package org.apache.regexp;
2
3 /*
4  * ====================================================================
5  *
6  * The Apache Software License, Version 1.1
7  *
8  * Copyright (c) 1999 The Apache Software Foundation. All rights
9  * reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution, if
24  * any, must include the following acknowlegement:
25  * "This product includes software developed by the
26  * Apache Software Foundation (http://www.apache.org/)."
27  * Alternately, this acknowlegement may appear in the software itself,
28  * if and wherever such third-party acknowlegements normally appear.
29  *
30  * 4. The names "The Jakarta Project", "Jakarta-Regexp", and "Apache Software
31  * Foundation" must not be used to endorse or promote products derived
32  * from this software without prior written permission. For written
33  * permission, please contact apache@apache.org.
34  *
35  * 5. Products derived from this software may not be called "Apache"
36  * nor may "Apache" appear in their names without prior written
37  * permission of the Apache Group.
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This software consists of voluntary contributions made by many
54  * individuals on behalf of the Apache Software Foundation. For more
55  * information on the Apache Software Foundation, please see
56  * <http://www.apache.org/>.
57  *
58  */

59
60 import java.util.*;
61 import java.io.*;
62
63 /**
64  * A subclass of RECompiler which can dump a regular expression program
65  * for debugging purposes.
66  *
67  * @author <a HREF="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
68  * @version $Id: REDebugCompiler.java,v 1.2 2001/02/27 08:37:06 gholam Exp $
69  */

70 public class REDebugCompiler extends RECompiler
71 {
72     /**
73      * Mapping from opcodes to descriptive strings
74      */

75     static Hashtable hashOpcode = new Hashtable();
76     static
77     {
78         hashOpcode.put(new Integer JavaDoc(RE.OP_RELUCTANTSTAR), "OP_RELUCTANTSTAR");
79         hashOpcode.put(new Integer JavaDoc(RE.OP_RELUCTANTPLUS), "OP_RELUCTANTPLUS");
80         hashOpcode.put(new Integer JavaDoc(RE.OP_RELUCTANTMAYBE), "OP_RELUCTANTMAYBE");
81         hashOpcode.put(new Integer JavaDoc(RE.OP_END), "OP_END");
82         hashOpcode.put(new Integer JavaDoc(RE.OP_BOL), "OP_BOL");
83         hashOpcode.put(new Integer JavaDoc(RE.OP_EOL), "OP_EOL");
84         hashOpcode.put(new Integer JavaDoc(RE.OP_ANY), "OP_ANY");
85         hashOpcode.put(new Integer JavaDoc(RE.OP_ANYOF), "OP_ANYOF");
86         hashOpcode.put(new Integer JavaDoc(RE.OP_BRANCH), "OP_BRANCH");
87         hashOpcode.put(new Integer JavaDoc(RE.OP_ATOM), "OP_ATOM");
88         hashOpcode.put(new Integer JavaDoc(RE.OP_STAR), "OP_STAR");
89         hashOpcode.put(new Integer JavaDoc(RE.OP_PLUS), "OP_PLUS");
90         hashOpcode.put(new Integer JavaDoc(RE.OP_MAYBE), "OP_MAYBE");
91         hashOpcode.put(new Integer JavaDoc(RE.OP_NOTHING), "OP_NOTHING");
92         hashOpcode.put(new Integer JavaDoc(RE.OP_GOTO), "OP_GOTO");
93         hashOpcode.put(new Integer JavaDoc(RE.OP_ESCAPE), "OP_ESCAPE");
94         hashOpcode.put(new Integer JavaDoc(RE.OP_OPEN), "OP_OPEN");
95         hashOpcode.put(new Integer JavaDoc(RE.OP_CLOSE), "OP_CLOSE");
96         hashOpcode.put(new Integer JavaDoc(RE.OP_BACKREF), "OP_BACKREF");
97         hashOpcode.put(new Integer JavaDoc(RE.OP_POSIXCLASS), "OP_POSIXCLASS");
98         hashOpcode.put(new Integer JavaDoc(RE.OP_OPEN_CLUSTER), "OP_OPEN_CLUSTER");
99         hashOpcode.put(new Integer JavaDoc(RE.OP_CLOSE_CLUSTER), "OP_CLOSE_CLUSTER");
100     }
101
102     /**
103      * Returns a descriptive string for an opcode.
104      * @param opcode Opcode to convert to a string
105      * @return Description of opcode
106      */

107     String JavaDoc opcodeToString(char opcode)
108     {
109         // Get string for opcode
110
String JavaDoc ret =(String JavaDoc)hashOpcode.get(new Integer JavaDoc(opcode));
111
112         // Just in case we have a corrupt program
113
if (ret == null)
114         {
115             ret = "OP_????";
116         }
117         return ret;
118     }
119
120     /**
121      * Return a string describing a (possibly unprintable) character.
122      * @param c Character to convert to a printable representation
123      * @return String representation of character
124      */

125     String JavaDoc charToString(char c)
126     {
127         // If it's unprintable, convert to '\###'
128
if (c < ' ' || c > 127)
129         {
130             return "\\" + (int)c;
131         }
132
133         // Return the character as a string
134
return String.valueOf(c);
135     }
136
137     /**
138      * Returns a descriptive string for a node in a regular expression program.
139      * @param node Node to describe
140      * @return Description of node
141      */

142     String JavaDoc nodeToString(int node)
143     {
144         // Get opcode and opdata for node
145
char opcode = instruction[node + RE.offsetOpcode];
146         int opdata = (int)instruction[node + RE.offsetOpdata];
147
148         // Return opcode as a string and opdata value
149
return opcodeToString(opcode) + ", opdata = " + opdata;
150     }
151
152     /**
153      * Inserts a node with a given opcode and opdata at insertAt. The node relative next
154      * pointer is initialized to 0.
155      * @param opcode Opcode for new node
156      * @param opdata Opdata for new node (only the low 16 bits are currently used)
157      * @param insertAt Index at which to insert the new node in the program * /
158     void nodeInsert(char opcode, int opdata, int insertAt) {
159         System.out.println( "====> " + opcode + " " + opdata + " " + insertAt );
160         PrintWriter writer = new PrintWriter( System.out );
161         dumpProgram( writer );
162         super.nodeInsert( opcode, opdata, insertAt );
163         System.out.println( "====< " );
164         dumpProgram( writer );
165         writer.flush();
166     }/**/

167
168
169     /**
170     * Appends a node to the end of a node chain
171     * @param node Start of node chain to traverse
172     * @param pointTo Node to have the tail of the chain point to * /
173     void setNextOfEnd(int node, int pointTo) {
174         System.out.println( "====> " + node + " " + pointTo );
175         PrintWriter writer = new PrintWriter( System.out );
176         dumpProgram( writer );
177         super.setNextOfEnd( node, pointTo );
178         System.out.println( "====< " );
179         dumpProgram( writer );
180         writer.flush();
181     }/**/

182
183
184     /**
185      * Dumps the current program to a PrintWriter
186      * @param p PrintWriter for program dump output
187      */

188     public void dumpProgram(PrintWriter p)
189     {
190         // Loop through the whole program
191
for (int i = 0; i < lenInstruction; )
192         {
193             // Get opcode, opdata and next fields of current program node
194
char opcode = instruction[i + RE.offsetOpcode];
195             char opdata = instruction[i + RE.offsetOpdata];
196             short next = (short)instruction[i + RE.offsetNext];
197
198             // Display the current program node
199
p.print(i + ". " + nodeToString(i) + ", next = ");
200
201             // If there's no next, say 'none', otherwise give absolute index of next node
202
if (next == 0)
203             {
204                 p.print("none");
205             }
206             else
207             {
208                 p.print(i + next);
209             }
210
211             // Move past node
212
i += RE.nodeSize;
213
214             // If character class
215
if (opcode == RE.OP_ANYOF)
216             {
217                 // Opening bracket for start of char class
218
p.print(", [");
219
220                 // Show each range in the char class
221
int rangeCount = opdata;
222                 for (int r = 0; r < rangeCount; r++)
223                 {
224                     // Get first and last chars in range
225
char charFirst = instruction[i++];
226                     char charLast = instruction[i++];
227
228                     // Print range as X-Y, unless range encompasses only one char
229
if (charFirst == charLast)
230                     {
231                         p.print(charToString(charFirst));
232                     }
233                     else
234                     {
235                         p.print(charToString(charFirst) + "-" + charToString(charLast));
236                     }
237                 }
238
239                 // Annotate the end of the char class
240
p.print("]");
241             }
242
243             // If atom
244
if (opcode == RE.OP_ATOM)
245             {
246                 // Open quote
247
p.print(", \"");
248
249                 // Print each character in the atom
250
for (int len = opdata; len-- != 0; )
251                 {
252                     p.print(charToString(instruction[i++]));
253                 }
254
255                 // Close quote
256
p.print("\"");
257             }
258
259             // Print a newline
260
p.println("");
261         }
262     }
263 }
264
Popular Tags