3 // namespace: System.Text.RegularExpressions
\r
6 // author: Dan Lewis (dlewis@gmx.co.uk)
\r
10 // Permission is hereby granted, free of charge, to any person obtaining
\r
11 // a copy of this software and associated documentation files (the
\r
12 // "Software"), to deal in the Software without restriction, including
\r
13 // without limitation the rights to use, copy, modify, merge, publish,
\r
14 // distribute, sublicense, and/or sell copies of the Software, and to
\r
15 // permit persons to whom the Software is furnished to do so, subject to
\r
16 // the following conditions:
\r
18 // The above copyright notice and this permission notice shall be
\r
19 // included in all copies or substantial portions of the Software.
\r
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
31 using System.Collections;
\r
33 namespace System.Text.RegularExpressions {
\r
35 class Disassembler {
\r
36 public static void DisassemblePattern (ushort[] image) {
\r
37 DisassembleBlock (image, 0, 0);
\r
40 public static void DisassembleBlock (ushort[] image, int pc, int depth) {
\r
45 if (pc >= image.Length)
\r
48 PatternCompiler.DecodeOp (image[pc], out op, out flags);
\r
49 Console.Write (FormatAddress (pc) + ": "); // address
\r
50 Console.Write (new string (' ', depth * 2)); // indent
\r
51 Console.Write (DisassembleOp (image, pc)); // instruction
\r
52 Console.WriteLine ();
\r
56 case OpCode.False: case OpCode.True: case OpCode.Until:
\r
60 case OpCode.Character: case OpCode.Category: case OpCode.Position:
\r
61 case OpCode.Open: case OpCode.Close: case OpCode.Reference:
\r
62 case OpCode.Sub: case OpCode.Branch: case OpCode.Jump: case OpCode.In:
\r
66 case OpCode.Balance: case OpCode.IfDefined: case OpCode.Range:
\r
67 case OpCode.Test: case OpCode.Anchor:
\r
71 case OpCode.Repeat: case OpCode.FastRepeat: case OpCode.Info:
\r
75 case OpCode.String: skip = image[pc + 1] + 2; break;
\r
76 case OpCode.Set: skip = image[pc + 2] + 3; break;
\r
87 public static string DisassembleOp (ushort[] image, int pc) {
\r
91 PatternCompiler.DecodeOp (image[pc], out op, out flags);
\r
92 string str = op.ToString ();
\r
94 str += "[" + flags.ToString ("f") + "]";
\r
97 case OpCode.False: case OpCode.True: case OpCode.Until:
\r
102 str += " " + image[pc + 1];
\r
103 str += " (" + image[pc + 2] + ", " + image[pc + 3] + ")";
\r
106 case OpCode.Character:
\r
107 str += " '" + FormatChar ((char)image[pc + 1]) + "'";
\r
110 case OpCode.Category:
\r
111 str += " /" + (Category)image[pc + 1];
\r
115 str += " '" + FormatChar ((char)image[pc + 1]) + "', ";
\r
116 str += " '" + FormatChar ((char)image[pc + 2]) + "'";
\r
120 str += " " + FormatSet (image, pc + 1);
\r
123 case OpCode.String:
\r
124 str += " '" + ReadString (image, pc + 1) + "'";
\r
127 case OpCode.Position:
\r
128 str += " /" + (Position)image[pc + 1];
\r
131 case OpCode.Open: case OpCode.Close: case OpCode.Reference:
\r
132 str += " " + image[pc + 1];
\r
135 case OpCode.Balance:
\r
136 str += " " + image[pc + 1] + " " + image[pc + 2];
\r
139 case OpCode.IfDefined: case OpCode.Anchor:
\r
140 str += " :" + FormatAddress (pc + image[pc + 1]);
\r
141 str += " " + image[pc + 2];
\r
144 case OpCode.Sub: case OpCode.Branch: case OpCode.Jump:
\r
146 str += " :" + FormatAddress (pc + image[pc + 1]);
\r
150 str += " :" + FormatAddress (pc + image[pc + 1]);
\r
151 str += ", :" + FormatAddress (pc + image[pc + 2]);
\r
154 case OpCode.Repeat: case OpCode.FastRepeat:
\r
155 str += " :" + FormatAddress (pc + image[pc + 1]);
\r
156 str += " (" + image[pc + 2] + ", ";
\r
157 if (image[pc + 3] == 0xffff)
\r
160 str += image[pc + 3];
\r
169 // private static members
\r
171 private static string ReadString (ushort[] image, int pc) {
\r
172 int len = image[pc];
\r
173 char[] chars = new char[len];
\r
175 for (int i = 0; i < len; ++ i)
\r
176 chars[i] = (char)image[pc + i + 1];
\r
178 return new string (chars);
\r
181 private static string FormatAddress (int pc) {
\r
182 return pc.ToString ("x4");
\r
185 private static string FormatSet (ushort[] image, int pc) {
\r
186 int lo = image[pc ++];
\r
187 int hi = (image[pc ++] << 4) - 1;
\r
192 char a = (char)0, b;
\r
193 for (int i = 0; i <= hi; ++ i) {
\r
194 bool m = (image[pc + (i >> 4)] & (1 << (i & 0xf))) != 0;
\r
196 if (m & !hot) { // start of range
\r
197 a = (char)(lo + i);
\r
200 else if (hot & (!m || i == hi)) { // end of range
\r
201 b = (char)(lo + i - 1);
\r
203 str += FormatChar (a);
\r
205 str += "-" + FormatChar (b);
\r
215 private static string FormatChar (char c) {
\r
216 if (c == '-' || c == ']')
\r
219 if (Char.IsLetterOrDigit (c) || Char.IsSymbol (c))
\r
220 return c.ToString ();
\r
222 if (Char.IsControl (c)) {
\r
223 return "^" + (char)('@' + c);
\r
226 return "\\u" + ((int)c).ToString ("x4");
\r