-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathmi_parse.ts
More file actions
322 lines (295 loc) · 8.39 KB
/
mi_parse.ts
File metadata and controls
322 lines (295 loc) · 8.39 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
export interface MIInfo {
token: number;
outOfBandRecord: { isStream: boolean, type: string, asyncClass: string, output: [string, any][], content: string }[];
resultRecords: { resultClass: string, results: [string, any][] };
}
export function escape(str: string) {
return str.replace(/\\/g, "\\\\").replace(/"/g, "\\\"");
}
var octalMatch = /^[0-7]{3}/;
function parseString(str: string): string {
var ret = new Buffer(str.length * 4);
var bufIndex = 0;
if (str[0] != '"' || str[str.length - 1] != '"')
throw new Error("Not a valid string");
str = str.slice(1, -1);
var escaped = false;
for (var i = 0; i < str.length; i++) {
if (escaped) {
var m;
if (str[i] == '\\')
bufIndex += ret.write('\\', bufIndex);
else if (str[i] == '"')
bufIndex += ret.write('"', bufIndex);
else if (str[i] == '\'')
bufIndex += ret.write('\'', bufIndex);
else if (str[i] == 'n')
bufIndex += ret.write('\n', bufIndex);
else if (str[i] == 'r')
bufIndex += ret.write('\r', bufIndex);
else if (str[i] == 't')
bufIndex += ret.write('\t', bufIndex);
else if (str[i] == 'b')
bufIndex += ret.write('\b', bufIndex);
else if (str[i] == 'f')
bufIndex += ret.write('\f', bufIndex);
else if (str[i] == 'v')
bufIndex += ret.write('\v', bufIndex);
else if (str[i] == '0')
bufIndex += ret.write('\0', bufIndex);
else if (m = octalMatch.exec(str.substr(i))) {
ret.writeUInt8(parseInt(m[0], 8), bufIndex++);
i += 2;
}
else
bufIndex += ret.write(str[i], bufIndex);
escaped = false;
} else {
if (str[i] == '\\')
escaped = true;
else if (str[i] == '"')
throw new Error("Not a valid string");
else
bufIndex += ret.write(str[i], bufIndex);
}
}
return ret.slice(0, bufIndex).toString("utf8");
}
export class MINode implements MIInfo {
token: number;
outOfBandRecord: { isStream: boolean, type: string, asyncClass: string, output: [string, any][], content: string }[];
resultRecords: { resultClass: string, results: [string, any][] };
constructor(token: number, info: { isStream: boolean, type: string, asyncClass: string, output: [string, any][], content: string }[], result: { resultClass: string, results: [string, any][] }) {
this.token = token;
this.outOfBandRecord = info;
this.resultRecords = result;
}
record(path: string): any {
if (!this.outOfBandRecord)
return undefined;
return MINode.valueOf(this.outOfBandRecord[0].output, path);
}
result(path: string): any {
if (!this.resultRecords)
return undefined;
return MINode.valueOf(this.resultRecords.results, path);
}
static valueOf(start: any, path: string): any {
if (!start)
return undefined;
let pathRegex = /^\.?([a-zA-Z_\-][a-zA-Z0-9_\-]*)/;
let indexRegex = /^\[(\d+)\](?:$|\.)/;
path = path.trim();
if (!path)
return start;
let current = start;
do {
let target = pathRegex.exec(path);
if (target) {
path = path.substr(target[0].length);
if (current.length && typeof current != "string") {
let found = [];
for (let i = 0; i < current.length; i++) {
let element = current[i];
if (element[0] == target[1]) {
found.push(element[1]);
}
}
if (found.length > 1) {
current = found;
} else if (found.length == 1) {
current = found[0];
} else return undefined;
} else return undefined;
}
else if (path[0] == '@') {
current = [current];
path = path.substr(1);
}
else {
target = indexRegex.exec(path);
if (target) {
path = path.substr(target[0].length);
let i = parseInt(target[1]);
if (current.length && typeof current != "string" && i >= 0 && i < current.length) {
current = current[i];
} else if (i == 0) {
} else return undefined;
}
else return undefined;
}
path = path.trim();
} while (path);
return current;
}
}
const tokenRegex = /^\d+/;
const outOfBandRecordRegex = /^(?:(\d*|undefined)([\*\+\=])|([\~\@\&]))/;
const resultRecordRegex = /^(\d*)\^(done|running|connected|error|exit)/;
const newlineRegex = /^\r\n?/;
const endRegex = /^\(gdb\)\r\n?/;
const variableRegex = /^([a-zA-Z_\-][a-zA-Z0-9_\-]*)/;
const asyncClassRegex = /^(.*?),/;
export function parseMI(output: string): MINode {
/*
output ==>
(
exec-async-output = [ token ] "*" ("stopped" | others) ( "," variable "=" (const | tuple | list) )* \n
status-async-output = [ token ] "+" ("stopped" | others) ( "," variable "=" (const | tuple | list) )* \n
notify-async-output = [ token ] "=" ("stopped" | others) ( "," variable "=" (const | tuple | list) )* \n
console-stream-output = "~" c-string \n
target-stream-output = "@" c-string \n
log-stream-output = "&" c-string \n
)*
[
[ token ] "^" ("done" | "running" | "connected" | "error" | "exit") ( "," variable "=" (const | tuple | list) )* \n
]
"(gdb)" \n
*/
let token = undefined;
let outOfBandRecord = [];
let resultRecords = undefined;
let asyncRecordType = {
"*": "exec",
"+": "status",
"=": "notify"
};
let streamRecordType = {
"~": "console",
"@": "target",
"&": "log"
};
let parseCString = () => {
if (output[0] != '"')
return "";
let stringEnd = 1;
let inString = true;
let remaining = output.substr(1);
let escaped = false;
while (inString) {
if (escaped)
escaped = false;
else if (remaining[0] == '\\')
escaped = true;
else if (remaining[0] == '"')
inString = false;
remaining = remaining.substr(1);
stringEnd++;
}
let str;
try {
str = parseString(output.substr(0, stringEnd));
}
catch (e) {
str = output.substr(0, stringEnd);
}
output = output.substr(stringEnd);
return str;
};
let parseValue, parseCommaResult, parseCommaValue, parseResult;
let parseTupleOrList = () => {
if (output[0] != '{' && output[0] != '[')
return undefined;
let oldContent = output;
let canBeValueList = output[0] == '[';
output = output.substr(1);
if (output[0] == '}' || output[0] == ']')
return [];
if (canBeValueList) {
let value = parseValue();
if (value) { // is value list
let values = [];
values.push(value);
let remaining = output;
while (value = parseCommaValue())
values.push(value);
output = output.substr(1); // ]
return values;
}
}
let result = parseResult();
if (result) {
let results = [];
results.push(result);
while (result = parseCommaResult())
results.push(result);
output = output.substr(1); // }
return results;
}
output = (canBeValueList ? '[' : '{') + output;
return undefined;
};
parseValue = () => {
if (output[0] == '"')
return parseCString();
else if (output[0] == '{' || output[0] == '[')
return parseTupleOrList();
else
return undefined;
};
parseResult = () => {
let variableMatch = variableRegex.exec(output);
if (!variableMatch)
return undefined;
output = output.substr(variableMatch[0].length + 1);
let variable = variableMatch[1];
return [variable, parseValue()];
};
parseCommaValue = () => {
if (output[0] != ',')
return undefined;
output = output.substr(1);
return parseValue();
};
parseCommaResult = () => {
if (output[0] != ',')
return undefined;
output = output.substr(1);
return parseResult();
};
let match = undefined;
while (match = outOfBandRecordRegex.exec(output)) {
output = output.substr(match[0].length);
if (match[1] && token === undefined && match[1] !== "undefined") {
token = parseInt(match[1]);
}
if (match[2]) {
let classMatch = asyncClassRegex.exec(output);
output = output.substr(classMatch[1].length);
let asyncRecord = {
isStream: false,
type: asyncRecordType[match[2]],
asyncClass: classMatch[1],
output: []
};
let result;
while (result = parseCommaResult())
asyncRecord.output.push(result);
outOfBandRecord.push(asyncRecord);
}
else if (match[3]) {
let streamRecord = {
isStream: true,
type: streamRecordType[match[3]],
content: parseCString()
};
outOfBandRecord.push(streamRecord);
}
output = output.replace(newlineRegex, "");
}
if (match = resultRecordRegex.exec(output)) {
output = output.substr(match[0].length);
if (match[1] && token === undefined) {
token = parseInt(match[1]);
}
resultRecords = {
resultClass: match[2],
results: []
};
let result;
while (result = parseCommaResult())
resultRecords.results.push(result);
output = output.replace(newlineRegex, "");
}
return new MINode(token, <any>outOfBandRecord || [], resultRecords);
}