1.1 --- a/src/com/sun/org/apache/regexp/internal/REDebugCompiler.java Sat Oct 24 16:18:47 2020 +0800 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,225 +0,0 @@ 1.4 -/* 1.5 - * reserved comment block 1.6 - * DO NOT REMOVE OR ALTER! 1.7 - */ 1.8 -/* 1.9 - * Copyright 1999-2004 The Apache Software Foundation. 1.10 - * 1.11 - * Licensed under the Apache License, Version 2.0 (the "License"); 1.12 - * you may not use this file except in compliance with the License. 1.13 - * You may obtain a copy of the License at 1.14 - * 1.15 - * http://www.apache.org/licenses/LICENSE-2.0 1.16 - * 1.17 - * Unless required by applicable law or agreed to in writing, software 1.18 - * distributed under the License is distributed on an "AS IS" BASIS, 1.19 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.20 - * See the License for the specific language governing permissions and 1.21 - * limitations under the License. 1.22 - */ 1.23 - 1.24 -package com.sun.org.apache.regexp.internal; 1.25 - 1.26 -import java.io.PrintWriter; 1.27 -import java.util.Hashtable; 1.28 - 1.29 -/** 1.30 - * A subclass of RECompiler which can dump a regular expression program 1.31 - * for debugging purposes. 1.32 - * 1.33 - * @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a> 1.34 - */ 1.35 -public class REDebugCompiler extends RECompiler 1.36 -{ 1.37 - /** 1.38 - * Mapping from opcodes to descriptive strings 1.39 - */ 1.40 - static Hashtable hashOpcode = new Hashtable(); 1.41 - static 1.42 - { 1.43 - hashOpcode.put(new Integer(RE.OP_RELUCTANTSTAR), "OP_RELUCTANTSTAR"); 1.44 - hashOpcode.put(new Integer(RE.OP_RELUCTANTPLUS), "OP_RELUCTANTPLUS"); 1.45 - hashOpcode.put(new Integer(RE.OP_RELUCTANTMAYBE), "OP_RELUCTANTMAYBE"); 1.46 - hashOpcode.put(new Integer(RE.OP_END), "OP_END"); 1.47 - hashOpcode.put(new Integer(RE.OP_BOL), "OP_BOL"); 1.48 - hashOpcode.put(new Integer(RE.OP_EOL), "OP_EOL"); 1.49 - hashOpcode.put(new Integer(RE.OP_ANY), "OP_ANY"); 1.50 - hashOpcode.put(new Integer(RE.OP_ANYOF), "OP_ANYOF"); 1.51 - hashOpcode.put(new Integer(RE.OP_BRANCH), "OP_BRANCH"); 1.52 - hashOpcode.put(new Integer(RE.OP_ATOM), "OP_ATOM"); 1.53 - hashOpcode.put(new Integer(RE.OP_STAR), "OP_STAR"); 1.54 - hashOpcode.put(new Integer(RE.OP_PLUS), "OP_PLUS"); 1.55 - hashOpcode.put(new Integer(RE.OP_MAYBE), "OP_MAYBE"); 1.56 - hashOpcode.put(new Integer(RE.OP_NOTHING), "OP_NOTHING"); 1.57 - hashOpcode.put(new Integer(RE.OP_GOTO), "OP_GOTO"); 1.58 - hashOpcode.put(new Integer(RE.OP_ESCAPE), "OP_ESCAPE"); 1.59 - hashOpcode.put(new Integer(RE.OP_OPEN), "OP_OPEN"); 1.60 - hashOpcode.put(new Integer(RE.OP_CLOSE), "OP_CLOSE"); 1.61 - hashOpcode.put(new Integer(RE.OP_BACKREF), "OP_BACKREF"); 1.62 - hashOpcode.put(new Integer(RE.OP_POSIXCLASS), "OP_POSIXCLASS"); 1.63 - hashOpcode.put(new Integer(RE.OP_OPEN_CLUSTER), "OP_OPEN_CLUSTER"); 1.64 - hashOpcode.put(new Integer(RE.OP_CLOSE_CLUSTER), "OP_CLOSE_CLUSTER"); 1.65 - } 1.66 - 1.67 - /** 1.68 - * Returns a descriptive string for an opcode. 1.69 - * @param opcode Opcode to convert to a string 1.70 - * @return Description of opcode 1.71 - */ 1.72 - String opcodeToString(char opcode) 1.73 - { 1.74 - // Get string for opcode 1.75 - String ret =(String)hashOpcode.get(new Integer(opcode)); 1.76 - 1.77 - // Just in case we have a corrupt program 1.78 - if (ret == null) 1.79 - { 1.80 - ret = "OP_????"; 1.81 - } 1.82 - return ret; 1.83 - } 1.84 - 1.85 - /** 1.86 - * Return a string describing a (possibly unprintable) character. 1.87 - * @param c Character to convert to a printable representation 1.88 - * @return String representation of character 1.89 - */ 1.90 - String charToString(char c) 1.91 - { 1.92 - // If it's unprintable, convert to '\###' 1.93 - if (c < ' ' || c > 127) 1.94 - { 1.95 - return "\\" + (int)c; 1.96 - } 1.97 - 1.98 - // Return the character as a string 1.99 - return String.valueOf(c); 1.100 - } 1.101 - 1.102 - /** 1.103 - * Returns a descriptive string for a node in a regular expression program. 1.104 - * @param node Node to describe 1.105 - * @return Description of node 1.106 - */ 1.107 - String nodeToString(int node) 1.108 - { 1.109 - // Get opcode and opdata for node 1.110 - char opcode = instruction[node + RE.offsetOpcode]; 1.111 - int opdata = (int)instruction[node + RE.offsetOpdata]; 1.112 - 1.113 - // Return opcode as a string and opdata value 1.114 - return opcodeToString(opcode) + ", opdata = " + opdata; 1.115 - } 1.116 - 1.117 - /** 1.118 - * Inserts a node with a given opcode and opdata at insertAt. The node relative next 1.119 - * pointer is initialized to 0. 1.120 - * @param opcode Opcode for new node 1.121 - * @param opdata Opdata for new node (only the low 16 bits are currently used) 1.122 - * @param insertAt Index at which to insert the new node in the program * / 1.123 - void nodeInsert(char opcode, int opdata, int insertAt) { 1.124 - System.out.println( "====> " + opcode + " " + opdata + " " + insertAt ); 1.125 - PrintWriter writer = new PrintWriter( System.out ); 1.126 - dumpProgram( writer ); 1.127 - super.nodeInsert( opcode, opdata, insertAt ); 1.128 - System.out.println( "====< " ); 1.129 - dumpProgram( writer ); 1.130 - writer.flush(); 1.131 - }/**/ 1.132 - 1.133 - 1.134 - /** 1.135 - * Appends a node to the end of a node chain 1.136 - * @param node Start of node chain to traverse 1.137 - * @param pointTo Node to have the tail of the chain point to * / 1.138 - void setNextOfEnd(int node, int pointTo) { 1.139 - System.out.println( "====> " + node + " " + pointTo ); 1.140 - PrintWriter writer = new PrintWriter( System.out ); 1.141 - dumpProgram( writer ); 1.142 - super.setNextOfEnd( node, pointTo ); 1.143 - System.out.println( "====< " ); 1.144 - dumpProgram( writer ); 1.145 - writer.flush(); 1.146 - }/**/ 1.147 - 1.148 - 1.149 - /** 1.150 - * Dumps the current program to a PrintWriter 1.151 - * @param p PrintWriter for program dump output 1.152 - */ 1.153 - public void dumpProgram(PrintWriter p) 1.154 - { 1.155 - // Loop through the whole program 1.156 - for (int i = 0; i < lenInstruction; ) 1.157 - { 1.158 - // Get opcode, opdata and next fields of current program node 1.159 - char opcode = instruction[i + RE.offsetOpcode]; 1.160 - char opdata = instruction[i + RE.offsetOpdata]; 1.161 - short next = (short)instruction[i + RE.offsetNext]; 1.162 - 1.163 - // Display the current program node 1.164 - p.print(i + ". " + nodeToString(i) + ", next = "); 1.165 - 1.166 - // If there's no next, say 'none', otherwise give absolute index of next node 1.167 - if (next == 0) 1.168 - { 1.169 - p.print("none"); 1.170 - } 1.171 - else 1.172 - { 1.173 - p.print(i + next); 1.174 - } 1.175 - 1.176 - // Move past node 1.177 - i += RE.nodeSize; 1.178 - 1.179 - // If character class 1.180 - if (opcode == RE.OP_ANYOF) 1.181 - { 1.182 - // Opening bracket for start of char class 1.183 - p.print(", ["); 1.184 - 1.185 - // Show each range in the char class 1.186 - int rangeCount = opdata; 1.187 - for (int r = 0; r < rangeCount; r++) 1.188 - { 1.189 - // Get first and last chars in range 1.190 - char charFirst = instruction[i++]; 1.191 - char charLast = instruction[i++]; 1.192 - 1.193 - // Print range as X-Y, unless range encompasses only one char 1.194 - if (charFirst == charLast) 1.195 - { 1.196 - p.print(charToString(charFirst)); 1.197 - } 1.198 - else 1.199 - { 1.200 - p.print(charToString(charFirst) + "-" + charToString(charLast)); 1.201 - } 1.202 - } 1.203 - 1.204 - // Annotate the end of the char class 1.205 - p.print("]"); 1.206 - } 1.207 - 1.208 - // If atom 1.209 - if (opcode == RE.OP_ATOM) 1.210 - { 1.211 - // Open quote 1.212 - p.print(", \""); 1.213 - 1.214 - // Print each character in the atom 1.215 - for (int len = opdata; len-- != 0; ) 1.216 - { 1.217 - p.print(charToString(instruction[i++])); 1.218 - } 1.219 - 1.220 - // Close quote 1.221 - p.print("\""); 1.222 - } 1.223 - 1.224 - // Print a newline 1.225 - p.println(""); 1.226 - } 1.227 - } 1.228 -}