3 // namespace: System.Text.RegularExpressions
\r
6 // author: Dan Lewis (dlewis@gmx.co.uk)
\r
10 using System.Collections;
\r
12 namespace System.Text.RegularExpressions {
\r
14 class Disassembler {
\r
15 public static void DisassemblePattern (ushort[] image) {
\r
16 DisassembleBlock (image, 0, 0);
\r
19 public static void DisassembleBlock (ushort[] image, int pc, int depth) {
\r
24 if (pc >= image.Length)
\r
27 PatternCompiler.DecodeOp (image[pc], out op, out flags);
\r
28 Console.Write (FormatAddress (pc) + ": "); // address
\r
29 Console.Write (new string (' ', depth * 2)); // indent
\r
30 Console.Write (DisassembleOp (image, pc)); // instruction
\r
31 Console.WriteLine ();
\r
35 case OpCode.False: case OpCode.True: case OpCode.Until:
\r
39 case OpCode.Character: case OpCode.Category: case OpCode.Position:
\r
40 case OpCode.Open: case OpCode.Close: case OpCode.Reference:
\r
41 case OpCode.Sub: case OpCode.Branch: case OpCode.Jump: case OpCode.In:
\r
45 case OpCode.Balance: case OpCode.IfDefined: case OpCode.Range:
\r
46 case OpCode.Test: case OpCode.Anchor:
\r
50 case OpCode.Repeat: case OpCode.FastRepeat: case OpCode.Info:
\r
54 case OpCode.String: skip = image[pc + 1] + 2; break;
\r
55 case OpCode.Set: skip = image[pc + 2] + 3; break;
\r
66 public static string DisassembleOp (ushort[] image, int pc) {
\r
70 PatternCompiler.DecodeOp (image[pc], out op, out flags);
\r
71 string str = op.ToString ();
\r
73 str += "[" + flags.ToString ("f") + "]";
\r
76 case OpCode.False: case OpCode.True: case OpCode.Until:
\r
81 str += " " + image[pc + 1];
\r
82 str += " (" + image[pc + 2] + ", " + image[pc + 3] + ")";
\r
85 case OpCode.Character:
\r
86 str += " '" + FormatChar ((char)image[pc + 1]) + "'";
\r
89 case OpCode.Category:
\r
90 str += " /" + (Category)image[pc + 1];
\r
94 str += " '" + FormatChar ((char)image[pc + 1]) + "', ";
\r
95 str += " '" + FormatChar ((char)image[pc + 2]) + "'";
\r
99 str += " " + FormatSet (image, pc + 1);
\r
102 case OpCode.String:
\r
103 str += " '" + ReadString (image, pc + 1) + "'";
\r
106 case OpCode.Position:
\r
107 str += " /" + (Position)image[pc + 1];
\r
110 case OpCode.Open: case OpCode.Close: case OpCode.Reference:
\r
111 str += " " + image[pc + 1];
\r
114 case OpCode.Balance:
\r
115 str += " " + image[pc + 1] + " " + image[pc + 2];
\r
118 case OpCode.IfDefined: case OpCode.Anchor:
\r
119 str += " :" + FormatAddress (pc + image[pc + 1]);
\r
120 str += " " + image[pc + 2];
\r
123 case OpCode.Sub: case OpCode.Branch: case OpCode.Jump:
\r
125 str += " :" + FormatAddress (pc + image[pc + 1]);
\r
129 str += " :" + FormatAddress (pc + image[pc + 1]);
\r
130 str += ", :" + FormatAddress (pc + image[pc + 2]);
\r
133 case OpCode.Repeat: case OpCode.FastRepeat:
\r
134 str += " :" + FormatAddress (pc + image[pc + 1]);
\r
135 str += " (" + image[pc + 2] + ", ";
\r
136 if (image[pc + 3] == 0xffff)
\r
139 str += image[pc + 3];
\r
148 // private static members
\r
150 private static string ReadString (ushort[] image, int pc) {
\r
151 int len = image[pc];
\r
152 char[] chars = new char[len];
\r
154 for (int i = 0; i < len; ++ i)
\r
155 chars[i] = (char)image[pc + i + 1];
\r
157 return new string (chars);
\r
160 private static string FormatAddress (int pc) {
\r
161 return pc.ToString ("x4");
\r
164 private static string FormatSet (ushort[] image, int pc) {
\r
165 int lo = image[pc ++];
\r
166 int hi = (image[pc ++] << 4) - 1;
\r
171 char a = (char)0, b;
\r
172 for (int i = 0; i <= hi; ++ i) {
\r
173 bool m = (image[pc + (i >> 4)] & (1 << (i & 0xf))) != 0;
\r
175 if (m & !hot) { // start of range
\r
176 a = (char)(lo + i);
\r
179 else if (hot & (!m || i == hi)) { // end of range
\r
180 b = (char)(lo + i - 1);
\r
182 str += FormatChar (a);
\r
184 str += "-" + FormatChar (b);
\r
194 private static string FormatChar (char c) {
\r
195 if (c == '-' || c == ']')
\r
198 if (Char.IsLetterOrDigit (c) || Char.IsSymbol (c))
\r
199 return c.ToString ();
\r
201 if (Char.IsControl (c)) {
\r
202 return "^" + (char)('@' + c);
\r
205 return "\\u" + ((int)c).ToString ("x4");
\r