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