Welcome to mirror list, hosted at ThFree Co, Russian Federation.

debug.cs « System.Text.RegularExpressions « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fdc3ab87bc3442ec709a8cf2b0300e45b166187d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//
// assembly:	System
// namespace:	System.Text.RegularExpressions
// file:	debug.cs
//
// author:	Dan Lewis (dlewis@gmx.co.uk)
// 		(c) 2002

using System;
using System.Collections;

namespace System.Text.RegularExpressions {

	class Disassembler {
		public static void DisassemblePattern (ushort[] image) {
			DisassembleBlock (image, 0, 0);
		}
	
		public static void DisassembleBlock (ushort[] image, int pc, int depth) {
			OpCode op;
			OpFlags flags;

			for (;;) {
				if (pc >= image.Length)
					return;
			
				PatternCompiler.DecodeOp (image[pc], out op, out flags);
				Console.Write (FormatAddress (pc) + ": ");		// address
				Console.Write (new string (' ', depth * 2));		// indent
				Console.Write (DisassembleOp (image, pc));		// instruction
				Console.WriteLine ();

				int skip;
				switch (op) {
				case OpCode.False: case OpCode.True: case OpCode.Until:
					skip = 1;
					break;

				case OpCode.Character: case OpCode.Category: case OpCode.Position:
				case OpCode.Open: case OpCode.Close: case OpCode.Reference:
				case OpCode.Sub: case OpCode.Branch: case OpCode.Jump: case OpCode.In:
					skip = 2;
					break;

				case OpCode.Balance: case OpCode.IfDefined: case OpCode.Range:
				case OpCode.Test: case OpCode.Anchor:
					skip = 3;
					break;

				case OpCode.Repeat: case OpCode.FastRepeat: case OpCode.Info:
					skip = 4;
					break;

				case OpCode.String: skip = image[pc + 1] + 2; break;
				case OpCode.Set: skip = image[pc + 2] + 3; break;

				default:
					skip = 1;
					break;
				}

				pc += skip;
			}
		}

		public static string DisassembleOp (ushort[] image, int pc) {
			OpCode op;
			OpFlags flags;

			PatternCompiler.DecodeOp (image[pc], out op, out flags);
			string str = op.ToString ();
			if (flags != 0)
				str += "[" + flags.ToString ("f") + "]";

			switch (op) {
			case OpCode.False: case OpCode.True: case OpCode.Until:
			default:
				break;

			case OpCode.Info:
				str += " " + image[pc + 1];
				str += " (" + image[pc + 2] + ", " + image[pc + 3] + ")";
				break;
			
			case OpCode.Character:
				str += " '" + FormatChar ((char)image[pc + 1]) + "'";
				break;

			case OpCode.Category:
				str += " /" + (Category)image[pc + 1];
				break;
			
			case OpCode.Range:
				str += " '" + FormatChar ((char)image[pc + 1]) + "', ";
				str += " '" + FormatChar ((char)image[pc + 2]) + "'";
				break;

			case OpCode.Set:
				str += " " + FormatSet (image, pc + 1);
				break;

			case OpCode.String:
				str += " '" + ReadString (image, pc + 1) + "'";
				break;

			case OpCode.Position:
				str += " /" + (Position)image[pc + 1];
				break;

			case OpCode.Open: case OpCode.Close: case OpCode.Reference:
				str += " " + image[pc + 1];
				break;

			case OpCode.Balance:
				str += " " + image[pc + 1] + " " + image[pc + 2];
				break;

			case OpCode.IfDefined: case OpCode.Anchor:
				str += " :" + FormatAddress (pc + image[pc + 1]);
				str += " " + image[pc + 2];
				break;
			
			case OpCode.Sub: case OpCode.Branch: case OpCode.Jump:
			case OpCode.In:
				str += " :" + FormatAddress (pc + image[pc + 1]);
				break;

			case OpCode.Test:
				str += " :" + FormatAddress (pc + image[pc + 1]);
				str += ", :" + FormatAddress (pc + image[pc + 2]);
				break;

			case OpCode.Repeat: case OpCode.FastRepeat:
				str += " :" + FormatAddress (pc + image[pc + 1]);
				str += " (" + image[pc + 2] + ", ";
				if (image[pc + 3] == 0xffff)
					str += "Inf";
				else
					str += image[pc + 3];
				str += ")";
				break;

			}

			return str;
		}

		// private static members
	
		private static string ReadString (ushort[] image, int pc) {
			int len = image[pc];
			char[] chars = new char[len];

			for (int i = 0; i < len; ++ i)
				chars[i] = (char)image[pc + i + 1];

			return new string (chars);
		}

		private static string FormatAddress (int pc) {
			return pc.ToString ("x4");
		}

		private static string FormatSet (ushort[] image, int pc) {
			int lo = image[pc ++];
			int hi = (image[pc ++] << 4) - 1;

			string str = "[";

			bool hot = false;
			char a = (char)0, b;
			for (int i = 0; i <= hi; ++ i) {
				bool m = (image[pc + (i >> 4)] & (1 << (i & 0xf))) != 0;

				if (m & !hot) {				// start of range
					a = (char)(lo + i);
					hot = true;
				}
				else if (hot & (!m || i == hi)) {	// end of range
					b = (char)(lo + i - 1);

					str += FormatChar (a);
					if (b != a)
						str += "-" + FormatChar (b);
					
					hot = false;
				}
			}

			str += "]";
			return str;
		}

		private static string FormatChar (char c) {
			if (c == '-' || c == ']')
				return "\\" + c;

			if (Char.IsLetterOrDigit (c) || Char.IsSymbol (c))
				return c.ToString ();
			
			if (Char.IsControl (c)) {
				return "^" + (char)('@' + c);
			}

			return "\\u" + ((int)c).ToString ("x4");
		}
	}
}