Newer
Older
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
import { MINode } from "./mi_parse";
const resultRegex = /^([0-9a-zA-Z_\-][a-zA-Z0-9_\-\/]*|\[\d+\])\s*=\s*/; // Dubner added \/ so that a COBOL A of B could be represented A/b
const variableRegex = /^[0-9a-zA-Z_\-][a-zA-Z0-9_\-\/]*/; // Dubner added \/ so that a COBOL A of B could be represented A/b
const errorRegex = /^\<.+?\>/;
const referenceStringRegex = /^(0x[0-9a-fA-F]+\s*)"/;
const referenceRegex = /^0x[0-9a-fA-F]+/;
const cppReferenceRegex = /^@0x[0-9a-fA-F]+/;
const nullpointerRegex = /^0x0+\b/;
const charRegex = /^(\d+) ['"]/;
const numberRegex = /^\d+(\.\d+)?/;
const pointerCombineChar = ".";
export function isExpandable(value: string): number {
let match;
value = value.trim();
if (value.length == 0) return 0;
else if (value.startsWith("{...}")) return 2; // lldb string/array
else if (value[0] == '{') return 1; // object
else if (value.startsWith("true")) return 0;
else if (value.startsWith("false")) return 0;
else if (match = nullpointerRegex.exec(value)) return 0;
else if (match = referenceStringRegex.exec(value)) return 0;
else if (match = referenceRegex.exec(value)) return 2; // reference
else if (match = charRegex.exec(value)) return 0;
else if (match = numberRegex.exec(value)) return 0;
else if (match = variableRegex.exec(value)) return 0;
else if (match = errorRegex.exec(value)) return 0;
else return 0;
}
export function expandValue(variableCreate: Function, value: string, root: string = "", extra: any = undefined): any {
const parseCString = () => {
value = value.trim();
if (value[0] != '"' && value[0] != '\'')
return "";
let stringEnd = 1;
let inString = true;
const charStr = value[0];
let remaining = value.substr(1);
let escaped = false;
while (inString) {
if (escaped)
escaped = false;
else if (remaining[0] == '\\')
escaped = true;
else if (remaining[0] == charStr)
inString = false;
remaining = remaining.substr(1);
stringEnd++;
}
const str = value.substr(0, stringEnd).trim();
value = value.substr(stringEnd).trim();
return str;
};
const stack = [root];
let parseValue, parseCommaResult, parseCommaValue, parseResult, createValue;
let variable = "";
const getNamespace = (variable) => {
let namespace = "";
let prefix = "";
stack.push(variable);
stack.forEach(name => {
prefix = "";
if (name != "") {
if (name.startsWith("["))
namespace = namespace + name;
else {
if (namespace) {
while (name.startsWith("*")) {
prefix += "*";
name = name.substr(1);
}
namespace = namespace + pointerCombineChar + name;
} else
namespace = name;
}
}
});
stack.pop();
return prefix + namespace;
};
const parseTupleOrList = () => {
value = value.trim();
if (value[0] != '{')
return undefined;
const oldContent = value;
value = value.substr(1).trim();
if (value[0] == '}') {
value = value.substr(1).trim();
return [];
}
if (value.startsWith("...")) {
value = value.substr(3).trim();
if (value[0] == '}') {
value = value.substr(1).trim();
return <any> "<...>";
}
}
const eqPos = value.indexOf("=");
const newValPos1 = value.indexOf("{");
const newValPos2 = value.indexOf(",");
let newValPos = newValPos1;
if (newValPos2 != -1 && newValPos2 < newValPos1)
newValPos = newValPos2;
if (newValPos != -1 && eqPos > newValPos || eqPos == -1) { // is value list
const values = [];
stack.push("[0]");
let val = parseValue();
stack.pop();
values.push(createValue("[0]", val));
const remaining = value;
let i = 0;
while (true) {
stack.push("[" + (++i) + "]");
if (!(val = parseCommaValue())) {
stack.pop();
break;
}
stack.pop();
values.push(createValue("[" + i + "]", val));
}
value = value.substr(1).trim(); // }
return values;
}
let result = parseResult(true);
if (result) {
const results = [];
results.push(result);
while (result = parseCommaResult(true))
results.push(result);
value = value.substr(1).trim(); // }
return results;
}
return undefined;
};
const parsePrimitive = () => {
let primitive: any;
let match;
value = value.trim();
if (value.length == 0)
primitive = undefined;
else if (value.startsWith("true")) {
primitive = "true";
value = value.substr(4).trim();
} else if (value.startsWith("false")) {
primitive = "false";
value = value.substr(5).trim();
} else if (match = nullpointerRegex.exec(value)) {
primitive = "<nullptr>";
value = value.substr(match[0].length).trim();
} else if (match = referenceStringRegex.exec(value)) {
value = value.substr(match[1].length).trim();
primitive = parseCString();
} else if (match = referenceRegex.exec(value)) {
primitive = "*" + match[0];
value = value.substr(match[0].length).trim();
} else if (match = cppReferenceRegex.exec(value)) {
primitive = match[0];
value = value.substr(match[0].length).trim();
} else if (match = charRegex.exec(value)) {
primitive = match[1];
value = value.substr(match[0].length - 1);
primitive += " " + parseCString();
} else if (match = numberRegex.exec(value)) {
primitive = match[0];
value = value.substr(match[0].length).trim();
} else if (match = variableRegex.exec(value)) {
primitive = match[0];
value = value.substr(match[0].length).trim();
} else if (match = errorRegex.exec(value)) {
primitive = match[0];
value = value.substr(match[0].length).trim();
} else {
primitive = value;
}
return primitive;
};
parseValue = () => {
value = value.trim();
if (value[0] == '"')
return parseCString();
else if (value[0] == '{')
return parseTupleOrList();
else
return parsePrimitive();
};
parseResult = (pushToStack: boolean = false) => {
value = value.trim();
const variableMatch = resultRegex.exec(value);
if (!variableMatch)
return undefined;
value = value.substr(variableMatch[0].length).trim();
const name = variable = variableMatch[1];
if (pushToStack)
stack.push(variable);
const val = parseValue();
if (pushToStack)
stack.pop();
return createValue(name, val);
};
createValue = (name, val) => {
let ref = 0;
if (typeof val == "object") {
ref = variableCreate(val);
val = "Object";
} else if (typeof val == "string" && val.startsWith("*0x")) {
if (extra && MINode.valueOf(extra, "arg") == "1") {
ref = variableCreate(getNamespace("*(" + name), { arg: true });
val = "<args>";
} else {
ref = variableCreate(getNamespace("*" + name));
val = "Object@" + val;
}
} else if (typeof val == "string" && val.startsWith("@0x")) {
ref = variableCreate(getNamespace("*&" + name.substr));
val = "Ref" + val;
} else if (typeof val == "string" && val.startsWith("<...>")) {
ref = variableCreate(getNamespace(name));
val = "...";
}
return {
name: name,
value: val,
variablesReference: ref
};
};
parseCommaValue = () => {
value = value.trim();
if (value[0] != ',')
return undefined;
value = value.substr(1).trim();
return parseValue();
};
parseCommaResult = (pushToStack: boolean = false) => {
value = value.trim();
if (value[0] != ',')
return undefined;
value = value.substr(1).trim();
return parseResult(pushToStack);
};
value = value.trim();
return parseValue();
}