3 // namespace: System.Text.RegularExpressions
6 // author: Dan Lewis (dlewis@gmx.co.uk)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using System
.Collections
;
33 namespace System
.Text
.RegularExpressions
{
36 public static void DisassemblePattern (ushort[] image
) {
37 DisassembleBlock (image
, 0, 0);
40 public static void DisassembleBlock (ushort[] image
, int pc
, int depth
) {
45 if (pc
>= image
.Length
)
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
56 case OpCode
.False
: case OpCode
.True
: case OpCode
.Until
:
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
:
66 case OpCode
.Balance
: case OpCode
.IfDefined
: case OpCode
.Range
:
67 case OpCode
.Test
: case OpCode
.Anchor
:
71 case OpCode
.Repeat
: case OpCode
.FastRepeat
: case OpCode
.Info
:
75 case OpCode
.String
: skip
= image
[pc
+ 1] + 2; break;
76 case OpCode
.Set
: skip
= image
[pc
+ 2] + 3; break;
87 public static string DisassembleOp (ushort[] image
, int pc
) {
91 PatternCompiler
.DecodeOp (image
[pc
], out op
, out flags
);
92 string str
= op
.ToString ();
94 str
+= "[" + flags
.ToString ("f") + "]";
97 case OpCode
.False
: case OpCode
.True
: case OpCode
.Until
:
102 str
+= " " + image
[pc
+ 1];
103 str
+= " (" + image
[pc
+ 2] + ", " + image
[pc
+ 3] + ")";
106 case OpCode
.Character
:
107 str
+= " '" + FormatChar ((char)image
[pc
+ 1]) + "'";
110 case OpCode
.Category
:
111 str
+= " /" + (Category
)image
[pc
+ 1];
115 str
+= " '" + FormatChar ((char)image
[pc
+ 1]) + "', ";
116 str
+= " '" + FormatChar ((char)image
[pc
+ 2]) + "'";
120 str
+= " " + FormatSet (image
, pc
+ 1);
124 str
+= " '" + ReadString (image
, pc
+ 1) + "'";
127 case OpCode
.Position
:
128 str
+= " /" + (Position
)image
[pc
+ 1];
131 case OpCode
.Open
: case OpCode
.Close
: case OpCode
.Reference
:
132 str
+= " " + image
[pc
+ 1];
136 str
+= " " + image
[pc
+ 1] + " " + image
[pc
+ 2];
139 case OpCode
.IfDefined
: case OpCode
.Anchor
:
140 str
+= " :" + FormatAddress (pc
+ image
[pc
+ 1]);
141 str
+= " " + image
[pc
+ 2];
144 case OpCode
.Sub
: case OpCode
.Branch
: case OpCode
.Jump
:
146 str
+= " :" + FormatAddress (pc
+ image
[pc
+ 1]);
150 str
+= " :" + FormatAddress (pc
+ image
[pc
+ 1]);
151 str
+= ", :" + FormatAddress (pc
+ image
[pc
+ 2]);
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)
160 str
+= image
[pc
+ 3];
169 // private static members
171 private static string ReadString (ushort[] image
, int 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;
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
200 else if (hot
& (!m
|| i
== hi
)) { // end of range
201 b
= (char)(lo
+ i
- 1);
203 str
+= FormatChar (a
);
205 str
+= "-" + FormatChar (b
);
215 private static string FormatChar (char c
) {
216 if (c
== '-' || 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");