-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathparser.js
476 lines (448 loc) · 16.1 KB
/
parser.js
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Patterns argument parser
import $ from "jquery";
import utils from "./utils.js";
import logging from "./logging";
class ArgumentParser {
constructor(name) {
this.order = [];
this.parameters = {};
this.attribute = "data-pat-" + name;
this.enum_values = {};
this.enum_conflicts = [];
this.groups = {};
this.possible_groups = {};
this.log = logging.getLogger(name + ".parser");
this.group_pattern = /([a-z][a-z0-9]*)-([A-Z][a-z0-0\-]*)/i;
this.json_param_pattern = /^\s*\[?\s*{/i;
this.named_param_pattern = /^\s*([a-z][a-z0-9\-]*)\s*:(.*)/is;
this.token_pattern = /((["']).*?(?!\\)\2)|\s*(\S+)\s*/g;
}
_camelCase(str) {
return str.replace(/\-([a-z])/g, (__, p1) => p1.toUpperCase());
}
addAlias(alias, original) {
/* Add an alias for a previously added parser argument.
*
* Useful when you want to support both US and UK english argument
* names.
*/
if (this.parameters[original]) {
this.parameters[original].alias = alias;
} else {
throw `Attempted to add an alias "${alias}" for a non-existing parser argument "${original}".`;
}
}
addGroupToSpec(spec) {
/* Determine wether an argument being parsed can be grouped and
* update its specifications object accordingly.
*
* Internal method used by addArgument and addJSONArgument
*/
const m = spec.name.match(this.group_pattern);
if (m) {
const group = m[1];
const field = m[2];
if (group in this.possible_groups) {
const first_spec = this.possible_groups[group];
const first_name = first_spec.name.match(this.group_pattern)[2];
first_spec.group = group;
first_spec.dest = first_name;
this.groups[group] = new ArgumentParser();
this.groups[group].addArgument(
first_name,
first_spec.value,
first_spec.choices,
first_spec.multiple
);
delete this.possible_groups[group];
}
if (group in this.groups) {
this.groups[group].addArgument(
field,
spec.value,
spec.choices,
spec.multiple
);
spec.group = group;
spec.dest = field;
} else {
this.possible_groups[group] = spec;
spec.dest = this._camelCase(spec.name);
}
}
return spec;
}
addJSONArgument(name, default_value) {
/* Add an argument where the value is provided in JSON format.
*
* This is a different usecase than specifying all arguments to
* the data-pat-... attributes in JSON format, and instead is part
* of the normal notation except that a value is in JSON instead of
* for example a string.
*/
this.order.push(name);
this.parameters[name] = this.addGroupToSpec({
name: name,
value: default_value,
dest: name,
group: null,
type: "json",
});
}
addArgument(name, default_value, choices, multiple) {
const spec = {
name: name,
value:
multiple && !Array.isArray(default_value)
? [default_value]
: default_value,
multiple: multiple,
dest: name,
group: null,
};
if (choices && Array.isArray(choices) && choices.length) {
spec.choices = choices;
spec.type = this._typeof(choices[0]);
for (const choice of choices) {
if (this.enum_conflicts.indexOf(choice) !== -1) {
continue;
} else if (choice in this.enum_values) {
this.enum_conflicts.push(choice);
delete this.enum_values[choice];
} else {
this.enum_values[choice] = name;
}
}
} else if (typeof spec.value === "string" && spec.value.slice(0, 1) === "$") {
spec.type = this.parameters[spec.value.slice(1)].type;
} else {
// Note that this will get reset by _defaults if default_value is a function.
spec.type = this._typeof(multiple ? spec.value[0] : spec.value);
}
this.order.push(name);
this.parameters[name] = this.addGroupToSpec(spec);
}
_typeof(obj) {
if (obj === null) {
return "null";
}
return typeof obj;
}
_coerce(name, value) {
const spec = this.parameters[name];
if (typeof value !== spec.type)
try {
switch (spec.type) {
case "json":
value = JSON.parse(value);
break;
case "boolean":
if (typeof value === "string") {
value = value.toLowerCase();
const num = parseInt(value, 10);
if (!isNaN(num)) value = !!num;
else
value =
value === "true" ||
value === "y" ||
value === "yes" ||
value === "y" ||
value === "on";
} else if (typeof value === "number") {
value = !!value;
} else {
throw `Cannot convert value for ${name} to boolean.`;
}
break;
case "number":
if (typeof value === "string") {
value = parseInt(value, 10);
if (isNaN(value)) {
throw `Cannot convert value for ${name} to number.`;
}
} else if (typeof value === "boolean") {
value = value + 0;
} else {
throw `Cannot convert value for ${name} to number.`;
}
break;
case "string":
value = value.toString();
break;
case "null": // Missing default values
case "undefined":
break;
default:
throw `Do not know how to convert value for ${name} of type ${typeof value} to ${
spec.type
}.`;
}
} catch (e) {
this.log.warn(e);
return null;
}
if (spec.choices && spec.choices.indexOf(value) === -1) {
this.log.warn(`Illegal value for ${name}: ${value}.`);
return null;
}
return value;
}
_set(opts, name, value) {
if (!(name in this.parameters)) {
this.log.debug(`Ignoring value for unknown argument: ${name}.`);
return;
}
const spec = this.parameters[name];
let parts;
if (spec.multiple) {
if (typeof value === "string") {
parts = value.split(/,+/);
} else {
parts = value;
}
value = [];
for (const part of parts) {
const v = this._coerce(name, part.trim());
if (v !== null) {
value.push(v);
}
}
} else {
value = this._coerce(name, value);
if (value === null) {
return;
}
}
opts[name] = value;
}
_split(text) {
const tokens = [];
text.replace(this.token_pattern, (match, quoted, __, simple) => {
if (quoted) {
tokens.push(quoted);
} else if (simple) {
tokens.push(simple);
}
});
return tokens;
}
_parseExtendedNotation(argstring) {
const opts = {};
const parts = argstring
.replace(/;;/g, "\0x1f")
.replace(/&/g, "&\0x1f")
.split(/;/)
.map((el) => el.replace(new RegExp("\0x1f", "g"), ";"));
for (const part of parts) {
if (!part) {
continue;
}
const matches = part.match(this.named_param_pattern);
if (!matches) {
this.log.warn(`Invalid parameter: ${part}: ${argstring}.`);
continue;
}
const name = matches[1];
const value = matches[2].trim();
const arg = Object.values(this.parameters).filter((it) => it.alias === name);
const is_alias = arg.length === 1;
if (is_alias) {
this._set(opts, arg[0].name, value);
} else if (name in this.parameters) {
this._set(opts, name, value);
} else if (name in this.groups) {
const subopt = this.groups[name]._parseShorthandNotation(value);
for (const field in subopt) {
this._set(opts, name + "-" + field, subopt[field]);
}
} else {
this.log.warn(`Unknown named parameter: ${matches[1]}.`);
continue;
}
}
return opts;
}
_parseShorthandNotation(parameter) {
const parts = this._split(parameter);
const opts = {};
let i = 0;
while (parts.length) {
const part = parts.shift().trim();
let sense;
let flag;
let positional = true;
if (part.slice(0, 3) === "no-") {
sense = false;
flag = part.slice(3);
} else {
sense = true;
flag = part;
}
if (flag in this.parameters && this.parameters[flag].type === "boolean") {
positional = false;
this._set(opts, flag, sense);
} else if (flag in this.enum_values) {
positional = false;
this._set(opts, this.enum_values[flag], flag);
} else if (positional) this._set(opts, this.order[i], part);
else {
parts.unshift(part);
break;
}
i++;
if (i >= this.order.length) {
break;
}
}
if (parts.length) this.log.warn(`Ignore extra arguments: ${parts.join(" ")}.`);
return opts;
}
_parse(parameter) {
if (!parameter) {
return {};
}
if (parameter.match(this.json_param_pattern)) {
try {
return JSON.parse(parameter);
} catch {
this.log.warn(`Invalid JSON argument found: ${parameter}.`);
}
}
if (parameter.match(this.named_param_pattern)) {
return this._parseExtendedNotation(parameter);
}
const sep = parameter.indexOf(";");
if (sep === -1) {
return this._parseShorthandNotation(parameter);
}
const opts = this._parseShorthandNotation(parameter.slice(0, sep));
const extended = this._parseExtendedNotation(parameter.slice(sep + 1));
for (const name in extended) {
opts[name] = extended[name];
}
return opts;
}
_defaults($el) {
const result = {};
for (const name in this.parameters) {
if (typeof this.parameters[name].value === "function") {
try {
result[name] = this.parameters[name].value($el, name);
this.parameters[name].type = typeof result[name];
} catch {
this.log.error(`Default function for ${name} failed.`);
}
} else {
result[name] = this.parameters[name].value;
}
}
return result;
}
_cleanupOptions(options, group_options = true) {
// Resolve references
for (const name of Object.keys(options)) {
const spec = this.parameters[name];
if (spec === undefined) {
continue;
}
if (
options[name] === spec.value &&
typeof spec.value === "string" &&
spec.value.slice(0, 1) === "$"
) {
options[name] = options[spec.value.slice(1)];
}
}
if (group_options) {
// Move options into groups and do renames
for (const name of Object.keys(options)) {
const spec = this.parameters[name];
let target;
if (spec === undefined) {
continue;
}
if (spec.group) {
if (typeof options[spec.group] !== "object") {
options[spec.group] = {};
}
target = options[spec.group];
} else {
target = options;
}
if (spec.dest !== name) {
target[spec.dest] = options[name];
delete options[name];
}
}
}
return options;
}
parse($el, options, multiple, inherit = true, group_options = true) {
if (!$el.jquery) {
$el = $($el);
}
if (typeof options === "boolean" && multiple === undefined) {
// Fix argument order: ``multiple`` passed instead of ``options``.
multiple = options;
options = {};
}
const stack = inherit ? [[this._defaults($el)]] : [[{}]];
let $possible_config_providers;
let final_length = 1;
/*
* XXX this is a workaround for:
* - https://github.com/Patternslib/Patterns/issues/393
*
* Prevents the parser to pollute the pat-modal configuration
* with data-pat-inject elements define in a `.pat-modal` parent element.
*
* Probably this function should be completely revisited, see:
* - https://github.com/Patternslib/Patterns/issues/627
*
*/
if (
!inherit ||
($el.hasClass("pat-modal") && this.attribute === "data-pat-inject")
) {
$possible_config_providers = $el;
} else {
$possible_config_providers = $el.parents(`[${this.attribute}]`).addBack();
}
for (const provider of $possible_config_providers) {
let frame;
const data = ($(provider).attr(this.attribute) || "").trim();
if (!data) {
continue;
}
const _parse = this._parse.bind(this);
if (data.match(/&&/)) {
frame = data.split(/\s*&&\s*/).map(_parse);
} else {
frame = _parse(data);
}
if (!Array.isArray(frame)) {
frame = [frame];
}
final_length = Math.max(frame.length, final_length);
stack.push(frame);
}
if (typeof options === "object") {
if (Array.isArray(options)) {
stack.push(options);
final_length = Math.max(options.length, final_length);
} else {
stack.push([options]);
}
}
if (!multiple) {
final_length = 1;
}
const results = utils
.removeDuplicateObjects(utils.mergeStack(stack, final_length))
.map((current_value) => this._cleanupOptions(current_value, group_options));
return multiple ? results : results[0];
}
}
// BBB
ArgumentParser.prototype.add_argument = ArgumentParser.prototype.addArgument;
export default ArgumentParser;