@sapphire/lexure 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1073 @@
1
+ "use strict";
2
+ var SapphireLexure = (() => {
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
+ var __publicField = (obj, key, value) => {
23
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
24
+ return value;
25
+ };
26
+
27
+ // src/index.ts
28
+ var src_exports = {};
29
+ __export(src_exports, {
30
+ ArgumentStream: () => ArgumentStream,
31
+ BaseParameter: () => BaseParameter,
32
+ EmptyStrategy: () => EmptyStrategy,
33
+ Lexer: () => Lexer,
34
+ ParameterStream: () => ParameterStream,
35
+ Parser: () => Parser,
36
+ ParserResult: () => ParserResult,
37
+ PrefixedStrategy: () => PrefixedStrategy,
38
+ QuotedParameter: () => QuotedParameter,
39
+ TokenStream: () => TokenStream,
40
+ TokenType: () => TokenType,
41
+ WordParameter: () => WordParameter
42
+ });
43
+
44
+ // ../result/dist/index.mjs
45
+ var __defProp2 = Object.defineProperty;
46
+ var __defNormalProp2 = /* @__PURE__ */ __name((obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value, "__defNormalProp");
47
+ var __name2 = /* @__PURE__ */ __name((target, value) => __defProp2(target, "name", { value, configurable: true }), "__name");
48
+ var __publicField2 = /* @__PURE__ */ __name((obj, key, value) => {
49
+ __defNormalProp2(obj, typeof key !== "symbol" ? key + "" : key, value);
50
+ return value;
51
+ }, "__publicField");
52
+ function isFunction(input) {
53
+ return typeof input === "function";
54
+ }
55
+ __name(isFunction, "isFunction");
56
+ __name2(isFunction, "isFunction");
57
+ var ResultError = /* @__PURE__ */ __name(class extends Error {
58
+ constructor(message, value) {
59
+ super(message);
60
+ __publicField2(this, "value");
61
+ this.value = value;
62
+ }
63
+ get name() {
64
+ return this.constructor.name;
65
+ }
66
+ }, "ResultError");
67
+ __name2(ResultError, "ResultError");
68
+ var Ok = /* @__PURE__ */ __name(class {
69
+ constructor(value) {
70
+ __publicField2(this, "value");
71
+ this.value = value;
72
+ }
73
+ isOk() {
74
+ return true;
75
+ }
76
+ isOkAnd(cb) {
77
+ return cb(this.value);
78
+ }
79
+ isErr() {
80
+ return false;
81
+ }
82
+ isErrAnd() {
83
+ return false;
84
+ }
85
+ ok() {
86
+ return some(this.value);
87
+ }
88
+ err() {
89
+ return none;
90
+ }
91
+ map(cb) {
92
+ return ok(cb(this.value));
93
+ }
94
+ mapOr(_, cb) {
95
+ return cb(this.value);
96
+ }
97
+ mapOrElse(_, cb) {
98
+ return cb(this.value);
99
+ }
100
+ mapErr() {
101
+ return this;
102
+ }
103
+ inspect(cb) {
104
+ cb(this.value);
105
+ return this;
106
+ }
107
+ inspectErr() {
108
+ return this;
109
+ }
110
+ *iter() {
111
+ yield this.value;
112
+ }
113
+ expect() {
114
+ return this.value;
115
+ }
116
+ expectErr(message) {
117
+ throw new ResultError(message, this.value);
118
+ }
119
+ unwrap() {
120
+ return this.value;
121
+ }
122
+ unwrapErr() {
123
+ throw new ResultError("Unwrap failed", this.value);
124
+ }
125
+ unwrapOr() {
126
+ return this.value;
127
+ }
128
+ unwrapOrElse() {
129
+ return this.value;
130
+ }
131
+ and(result) {
132
+ return result;
133
+ }
134
+ andThen(cb) {
135
+ return cb(this.value);
136
+ }
137
+ or() {
138
+ return this;
139
+ }
140
+ orElse() {
141
+ return this;
142
+ }
143
+ contains(value) {
144
+ return this.value === value;
145
+ }
146
+ containsErr() {
147
+ return false;
148
+ }
149
+ transpose() {
150
+ return this.value.match({
151
+ some: (value) => some(ok(value)),
152
+ none: () => none
153
+ });
154
+ }
155
+ flatten() {
156
+ return this.value;
157
+ }
158
+ intoOkOrErr() {
159
+ return this.value;
160
+ }
161
+ eq(other) {
162
+ return other.isOkAnd((value) => this.value === value);
163
+ }
164
+ ne(other) {
165
+ return !this.eq(other);
166
+ }
167
+ match(branches) {
168
+ return branches.ok(this.value);
169
+ }
170
+ *[Symbol.iterator]() {
171
+ yield this.value;
172
+ }
173
+ }, "Ok");
174
+ __name2(Ok, "Ok");
175
+ function ok(x) {
176
+ return new Ok(x);
177
+ }
178
+ __name(ok, "ok");
179
+ __name2(ok, "ok");
180
+ var Some = /* @__PURE__ */ __name(class {
181
+ constructor(value) {
182
+ __publicField2(this, "value");
183
+ this.value = value;
184
+ }
185
+ isSome() {
186
+ return true;
187
+ }
188
+ isSomeAnd(cb) {
189
+ return cb(this.value);
190
+ }
191
+ isNone() {
192
+ return false;
193
+ }
194
+ expect() {
195
+ return this.value;
196
+ }
197
+ unwrap() {
198
+ return this.value;
199
+ }
200
+ unwrapOr() {
201
+ return this.value;
202
+ }
203
+ unwrapOrElse() {
204
+ return this.value;
205
+ }
206
+ map(cb) {
207
+ return some(cb(this.value));
208
+ }
209
+ mapOr(_, cb) {
210
+ return cb(this.value);
211
+ }
212
+ mapOrElse(_, cb) {
213
+ return cb(this.value);
214
+ }
215
+ inspect(cb) {
216
+ cb(this.value);
217
+ return this;
218
+ }
219
+ okOr() {
220
+ return ok(this.value);
221
+ }
222
+ okOrElse() {
223
+ return ok(this.value);
224
+ }
225
+ *iter() {
226
+ yield this.value;
227
+ }
228
+ and(option) {
229
+ return option;
230
+ }
231
+ andThen(cb) {
232
+ return cb(this.value);
233
+ }
234
+ or() {
235
+ return this;
236
+ }
237
+ orElse() {
238
+ return this;
239
+ }
240
+ xor(option) {
241
+ return option.isSome() ? none : this;
242
+ }
243
+ filter(predicate) {
244
+ return predicate(this.value) ? this : none;
245
+ }
246
+ contains(value) {
247
+ return this.value === value;
248
+ }
249
+ zip(other) {
250
+ return other.map((o) => [this.value, o]);
251
+ }
252
+ zipWith(other, f) {
253
+ return other.map((o) => f(this.value, o));
254
+ }
255
+ unzip() {
256
+ const [s, o] = this.value;
257
+ return [some(s), some(o)];
258
+ }
259
+ transpose() {
260
+ return this.value.match({
261
+ ok: (v) => ok(some(v)),
262
+ err: (e) => err(e)
263
+ });
264
+ }
265
+ flatten() {
266
+ return this.value;
267
+ }
268
+ eq(other) {
269
+ return other.isSomeAnd((value) => this.value === value);
270
+ }
271
+ ne(other) {
272
+ return !this.eq(other);
273
+ }
274
+ match(branches) {
275
+ return branches.some(this.value);
276
+ }
277
+ *[Symbol.iterator]() {
278
+ yield this.value;
279
+ }
280
+ }, "Some");
281
+ __name2(Some, "Some");
282
+ function some(value) {
283
+ return new Some(value);
284
+ }
285
+ __name(some, "some");
286
+ __name2(some, "some");
287
+ var Err = /* @__PURE__ */ __name(class {
288
+ constructor(error) {
289
+ __publicField2(this, "error");
290
+ this.error = error;
291
+ }
292
+ isOk() {
293
+ return false;
294
+ }
295
+ isOkAnd() {
296
+ return false;
297
+ }
298
+ isErr() {
299
+ return true;
300
+ }
301
+ isErrAnd(cb) {
302
+ return cb(this.error);
303
+ }
304
+ ok() {
305
+ return none;
306
+ }
307
+ err() {
308
+ return some(this.error);
309
+ }
310
+ map() {
311
+ return this;
312
+ }
313
+ mapOr(defaultValue) {
314
+ return defaultValue;
315
+ }
316
+ mapOrElse(op) {
317
+ return op(this.error);
318
+ }
319
+ mapErr(cb) {
320
+ return err(cb(this.error));
321
+ }
322
+ inspect() {
323
+ return this;
324
+ }
325
+ inspectErr(cb) {
326
+ cb(this.error);
327
+ return this;
328
+ }
329
+ *iter() {
330
+ }
331
+ expect(message) {
332
+ throw new ResultError(message, this.error);
333
+ }
334
+ expectErr() {
335
+ return this.error;
336
+ }
337
+ unwrap() {
338
+ throw new ResultError("Unwrap failed", this.error);
339
+ }
340
+ unwrapErr() {
341
+ return this.error;
342
+ }
343
+ unwrapOr(defaultValue) {
344
+ return defaultValue;
345
+ }
346
+ unwrapOrElse(op) {
347
+ return op(this.error);
348
+ }
349
+ and() {
350
+ return this;
351
+ }
352
+ andThen() {
353
+ return this;
354
+ }
355
+ or(result) {
356
+ return result;
357
+ }
358
+ orElse(cb) {
359
+ return cb(this.error);
360
+ }
361
+ contains() {
362
+ return false;
363
+ }
364
+ containsErr(error) {
365
+ return this.error === error;
366
+ }
367
+ transpose() {
368
+ return some(this);
369
+ }
370
+ flatten() {
371
+ return this;
372
+ }
373
+ intoOkOrErr() {
374
+ return this.error;
375
+ }
376
+ eq(other) {
377
+ return other.isErrAnd((error) => this.error === error);
378
+ }
379
+ ne(other) {
380
+ return !this.eq(other);
381
+ }
382
+ match(branches) {
383
+ return branches.err(this.error);
384
+ }
385
+ *[Symbol.iterator]() {
386
+ }
387
+ }, "Err");
388
+ __name2(Err, "Err");
389
+ function err(x) {
390
+ return new Err(x);
391
+ }
392
+ __name(err, "err");
393
+ __name2(err, "err");
394
+ var OptionError = /* @__PURE__ */ __name(class extends Error {
395
+ get name() {
396
+ return this.constructor.name;
397
+ }
398
+ }, "OptionError");
399
+ __name2(OptionError, "OptionError");
400
+ var None = /* @__PURE__ */ __name(class {
401
+ isSome() {
402
+ return false;
403
+ }
404
+ isSomeAnd() {
405
+ return false;
406
+ }
407
+ isNone() {
408
+ return true;
409
+ }
410
+ expect(message) {
411
+ throw new OptionError(message);
412
+ }
413
+ unwrap() {
414
+ throw new OptionError("Unwrap failed");
415
+ }
416
+ unwrapOr(defaultValue) {
417
+ return defaultValue;
418
+ }
419
+ unwrapOrElse(cb) {
420
+ return cb();
421
+ }
422
+ map() {
423
+ return this;
424
+ }
425
+ mapOr(defaultValue) {
426
+ return defaultValue;
427
+ }
428
+ mapOrElse(defaultValue) {
429
+ return defaultValue();
430
+ }
431
+ inspect() {
432
+ return this;
433
+ }
434
+ okOr(error) {
435
+ return err(error);
436
+ }
437
+ okOrElse(cb) {
438
+ return err(cb());
439
+ }
440
+ *iter() {
441
+ }
442
+ and() {
443
+ return this;
444
+ }
445
+ andThen() {
446
+ return this;
447
+ }
448
+ or(option) {
449
+ return option;
450
+ }
451
+ orElse(cb) {
452
+ return cb();
453
+ }
454
+ xor(option) {
455
+ return option.isSome() ? option : this;
456
+ }
457
+ filter() {
458
+ return this;
459
+ }
460
+ contains() {
461
+ return false;
462
+ }
463
+ zip() {
464
+ return this;
465
+ }
466
+ zipWith() {
467
+ return this;
468
+ }
469
+ unzip() {
470
+ return [this, this];
471
+ }
472
+ transpose() {
473
+ return ok(this);
474
+ }
475
+ flatten() {
476
+ return this;
477
+ }
478
+ eq(other) {
479
+ return other.isNone();
480
+ }
481
+ ne(other) {
482
+ return other.isSome();
483
+ }
484
+ match(branches) {
485
+ return branches.none();
486
+ }
487
+ *[Symbol.iterator]() {
488
+ }
489
+ }, "None");
490
+ __name2(None, "None");
491
+ var none = new None();
492
+ var Option;
493
+ ((Option2) => {
494
+ function resolve(value) {
495
+ if (value === null || value === void 0)
496
+ return Option2.none;
497
+ if (is(value))
498
+ return value;
499
+ return Option2.some(value);
500
+ }
501
+ __name(resolve, "resolve");
502
+ __name2(resolve, "resolve");
503
+ function is(value) {
504
+ return value instanceof None || value instanceof Some;
505
+ }
506
+ __name(is, "is");
507
+ Option2.is = is;
508
+ __name2(is, "is");
509
+ function from(op) {
510
+ if (!isFunction(op))
511
+ return resolve(op);
512
+ try {
513
+ return resolve(op());
514
+ } catch {
515
+ return Option2.none;
516
+ }
517
+ }
518
+ __name(from, "from");
519
+ Option2.from = from;
520
+ __name2(from, "from");
521
+ async function fromAsync(op) {
522
+ try {
523
+ return resolve(await (isFunction(op) ? op() : op));
524
+ } catch {
525
+ return Option2.none;
526
+ }
527
+ }
528
+ __name(fromAsync, "fromAsync");
529
+ Option2.fromAsync = fromAsync;
530
+ __name2(fromAsync, "fromAsync");
531
+ Option2.none = none;
532
+ Option2.some = some;
533
+ })(Option || (Option = {}));
534
+ var Result;
535
+ ((Result2) => {
536
+ function resolve(value) {
537
+ if (is(value))
538
+ return value;
539
+ return Result2.ok(value);
540
+ }
541
+ __name(resolve, "resolve");
542
+ __name2(resolve, "resolve");
543
+ function is(value) {
544
+ return value instanceof Ok || value instanceof Err;
545
+ }
546
+ __name(is, "is");
547
+ Result2.is = is;
548
+ __name2(is, "is");
549
+ function from(op) {
550
+ if (!isFunction(op))
551
+ return resolve(op);
552
+ try {
553
+ return resolve(op());
554
+ } catch (error) {
555
+ return Result2.err(error);
556
+ }
557
+ }
558
+ __name(from, "from");
559
+ Result2.from = from;
560
+ __name2(from, "from");
561
+ async function fromAsync(op) {
562
+ try {
563
+ return resolve(await (isFunction(op) ? op() : op));
564
+ } catch (error) {
565
+ return Result2.err(error);
566
+ }
567
+ }
568
+ __name(fromAsync, "fromAsync");
569
+ Result2.fromAsync = fromAsync;
570
+ __name2(fromAsync, "fromAsync");
571
+ Result2.err = err;
572
+ Result2.ok = ok;
573
+ })(Result || (Result = {}));
574
+
575
+ // src/lib/ArgumentStream.ts
576
+ var ArgumentStream = class {
577
+ constructor(results) {
578
+ __publicField(this, "results");
579
+ __publicField(this, "state");
580
+ this.results = results;
581
+ this.state = { used: /* @__PURE__ */ new Set(), position: 0 };
582
+ }
583
+ get finished() {
584
+ return this.used === this.length;
585
+ }
586
+ get length() {
587
+ return this.results.ordered.length;
588
+ }
589
+ get remaining() {
590
+ return this.length - this.used;
591
+ }
592
+ get used() {
593
+ return this.state.used.size;
594
+ }
595
+ single() {
596
+ if (this.finished)
597
+ return Option.none;
598
+ while (this.state.used.has(this.state.position)) {
599
+ ++this.state.position;
600
+ }
601
+ this.state.used.add(this.state.position);
602
+ return Option.some(this.results.ordered[this.state.position++].value);
603
+ }
604
+ singleMap(predicate, useAnyways = false) {
605
+ if (this.finished)
606
+ return Option.none;
607
+ while (this.state.used.has(this.state.position)) {
608
+ ++this.state.position;
609
+ }
610
+ const result = predicate(this.results.ordered[this.state.position].value);
611
+ if (result.isSome() || useAnyways) {
612
+ this.state.used.add(this.state.position);
613
+ ++this.state.position;
614
+ }
615
+ return result;
616
+ }
617
+ async singleMapAsync(predicate, useAnyways = false) {
618
+ if (this.finished)
619
+ return Option.none;
620
+ while (this.state.used.has(this.state.position)) {
621
+ ++this.state.position;
622
+ }
623
+ const result = await predicate(this.results.ordered[this.state.position].value);
624
+ if (result.isSome() || useAnyways) {
625
+ this.state.used.add(this.state.position);
626
+ ++this.state.position;
627
+ }
628
+ return result;
629
+ }
630
+ singleParse(predicate, useAnyways = false) {
631
+ if (this.finished)
632
+ return Result.err(null);
633
+ while (this.state.used.has(this.state.position)) {
634
+ ++this.state.position;
635
+ }
636
+ const result = predicate(this.results.ordered[this.state.position].value);
637
+ if (result.isOk() || useAnyways) {
638
+ this.state.used.add(this.state.position);
639
+ ++this.state.position;
640
+ }
641
+ return result;
642
+ }
643
+ async singleParseAsync(predicate, useAnyways = false) {
644
+ if (this.finished)
645
+ return Result.err(null);
646
+ while (this.state.used.has(this.state.position)) {
647
+ ++this.state.position;
648
+ }
649
+ const result = await predicate(this.results.ordered[this.state.position].value);
650
+ if (result.isOk() || useAnyways) {
651
+ this.state.used.add(this.state.position);
652
+ ++this.state.position;
653
+ }
654
+ return result;
655
+ }
656
+ find(predicate, from = this.state.position) {
657
+ for (let i = from; i < this.length; ++i) {
658
+ if (this.state.used.has(i))
659
+ continue;
660
+ const parameter = this.results.ordered[i].value;
661
+ if (predicate(parameter)) {
662
+ this.state.used.add(i);
663
+ return Option.some(parameter);
664
+ }
665
+ }
666
+ return Option.none;
667
+ }
668
+ async findAsync(predicate, from = this.state.position) {
669
+ for (let i = from; i < this.length; ++i) {
670
+ if (this.state.used.has(i))
671
+ continue;
672
+ const parameter = this.results.ordered[i].value;
673
+ if (await predicate(parameter)) {
674
+ this.state.used.add(i);
675
+ return Option.some(parameter);
676
+ }
677
+ }
678
+ return Option.none;
679
+ }
680
+ findMap(predicate, from = this.state.position) {
681
+ for (let i = from; i < this.length; ++i) {
682
+ if (this.state.used.has(i))
683
+ continue;
684
+ const parameter = this.results.ordered[i].value;
685
+ const result = predicate(parameter);
686
+ if (result.isSome()) {
687
+ this.state.used.add(i);
688
+ return result;
689
+ }
690
+ }
691
+ return Option.none;
692
+ }
693
+ async findMapAsync(predicate, from = this.state.position) {
694
+ for (let i = from; i < this.length; ++i) {
695
+ if (this.state.used.has(i))
696
+ continue;
697
+ const parameter = this.results.ordered[i].value;
698
+ const result = await predicate(parameter);
699
+ if (result.isSome()) {
700
+ this.state.used.add(i);
701
+ return result;
702
+ }
703
+ }
704
+ return Option.none;
705
+ }
706
+ findParse(predicate, from = this.state.position) {
707
+ const errors = [];
708
+ for (let i = from; i < this.length; ++i) {
709
+ if (this.state.used.has(i))
710
+ continue;
711
+ const parameter = this.results.ordered[i].value;
712
+ const result = predicate(parameter);
713
+ if (result.isOk()) {
714
+ this.state.used.add(i);
715
+ return result;
716
+ }
717
+ errors.push(result.unwrapErr());
718
+ }
719
+ return Result.err(errors);
720
+ }
721
+ async findParseAsync(predicate, from = this.state.position) {
722
+ const errors = [];
723
+ for (let i = from; i < this.length; ++i) {
724
+ if (this.state.used.has(i))
725
+ continue;
726
+ const parameter = this.results.ordered[i].value;
727
+ const result = await predicate(parameter);
728
+ if (result.isOk()) {
729
+ this.state.used.add(i);
730
+ return result;
731
+ }
732
+ errors.push(result.unwrapErr());
733
+ }
734
+ return Result.err(errors);
735
+ }
736
+ many(limit = Infinity, from = this.state.position) {
737
+ if (this.finished)
738
+ return Option.none;
739
+ const parameters = [];
740
+ for (let i = from; i < this.length; ++i) {
741
+ if (this.state.used.has(i))
742
+ continue;
743
+ this.state.used.add(i);
744
+ parameters.push(this.results.ordered[i]);
745
+ if (parameters.length >= limit)
746
+ break;
747
+ }
748
+ return parameters.length ? Option.some(parameters) : Option.none;
749
+ }
750
+ filter(predicate, from = this.state.position) {
751
+ if (this.finished)
752
+ return Option.none;
753
+ const parameters = [];
754
+ for (let i = from; i < this.length; ++i) {
755
+ if (this.state.used.has(i))
756
+ continue;
757
+ const parameter = this.results.ordered[i].value;
758
+ if (predicate(parameter)) {
759
+ this.state.used.add(i);
760
+ parameters.push(parameter);
761
+ }
762
+ }
763
+ return Option.some(parameters);
764
+ }
765
+ async filterAsync(predicate, from = this.state.position) {
766
+ if (this.finished)
767
+ return Option.none;
768
+ const parameters = [];
769
+ for (let i = from; i < this.length; ++i) {
770
+ if (this.state.used.has(i))
771
+ continue;
772
+ const parameter = this.results.ordered[i].value;
773
+ if (await predicate(parameter)) {
774
+ this.state.used.add(i);
775
+ parameters.push(parameter);
776
+ }
777
+ }
778
+ return Option.some(parameters);
779
+ }
780
+ filterMap(predicate, from = this.state.position) {
781
+ if (this.finished)
782
+ return Option.none;
783
+ const parameters = [];
784
+ for (let i = from; i < this.length; ++i) {
785
+ if (this.state.used.has(i))
786
+ continue;
787
+ const parameter = this.results.ordered[i].value;
788
+ const result = predicate(parameter);
789
+ result.inspect((value) => {
790
+ this.state.used.add(i);
791
+ parameters.push(value);
792
+ });
793
+ }
794
+ return Option.some(parameters);
795
+ }
796
+ async filterMapAsync(predicate, from = this.state.position) {
797
+ if (this.finished)
798
+ return Option.none;
799
+ const parameters = [];
800
+ for (let i = from; i < this.length; ++i) {
801
+ if (this.state.used.has(i))
802
+ continue;
803
+ const parameter = this.results.ordered[i].value;
804
+ const result = await predicate(parameter);
805
+ result.inspect((value) => {
806
+ this.state.used.add(i);
807
+ parameters.push(value);
808
+ });
809
+ }
810
+ return Option.some(parameters);
811
+ }
812
+ flag(...keys) {
813
+ return keys.some((key) => this.results.flags.has(key));
814
+ }
815
+ option(...keys) {
816
+ return this.options(...keys).map((values) => values.at(-1));
817
+ }
818
+ options(...keys) {
819
+ const entries = [];
820
+ for (const key of keys) {
821
+ const values = this.results.options.get(key);
822
+ if (values)
823
+ entries.push(...values);
824
+ }
825
+ return entries.length ? Option.some(entries) : Option.none;
826
+ }
827
+ save() {
828
+ return {
829
+ used: new Set(this.state.used),
830
+ position: this.state.position
831
+ };
832
+ }
833
+ restore(state) {
834
+ this.state = state;
835
+ }
836
+ reset() {
837
+ this.restore({ used: /* @__PURE__ */ new Set(), position: 0 });
838
+ }
839
+ };
840
+ __name(ArgumentStream, "ArgumentStream");
841
+
842
+ // src/lib/lexer/streams/parameters/BaseParameter.ts
843
+ var BaseParameter = class {
844
+ constructor(separators) {
845
+ __publicField(this, "separators");
846
+ this.separators = separators;
847
+ }
848
+ get leading() {
849
+ return this.separators.join("");
850
+ }
851
+ };
852
+ __name(BaseParameter, "BaseParameter");
853
+
854
+ // src/lib/lexer/streams/parameters/QuotedParameter.ts
855
+ var QuotedParameter = class extends BaseParameter {
856
+ constructor(separators, part) {
857
+ super(separators);
858
+ __publicField(this, "value");
859
+ __publicField(this, "open");
860
+ __publicField(this, "close");
861
+ this.value = part.value;
862
+ this.open = part.open;
863
+ this.close = part.close;
864
+ }
865
+ get raw() {
866
+ return `${this.open}${this.value}${this.close}`;
867
+ }
868
+ };
869
+ __name(QuotedParameter, "QuotedParameter");
870
+
871
+ // src/lib/lexer/streams/parameters/WordParameter.ts
872
+ var WordParameter = class extends BaseParameter {
873
+ constructor(separators, part) {
874
+ super(separators);
875
+ __publicField(this, "value");
876
+ this.value = part.value;
877
+ }
878
+ get raw() {
879
+ return this.value;
880
+ }
881
+ };
882
+ __name(WordParameter, "WordParameter");
883
+
884
+ // src/lib/lexer/streams/raw/TokenStream.ts
885
+ var TokenStream = class {
886
+ constructor(lexer, input) {
887
+ __publicField(this, "input");
888
+ __publicField(this, "quotes");
889
+ __publicField(this, "separator");
890
+ __publicField(this, "position", 0);
891
+ this.quotes = lexer.quotes;
892
+ this.separator = lexer.separator;
893
+ this.input = input;
894
+ }
895
+ get finished() {
896
+ return this.position >= this.input.length;
897
+ }
898
+ *[Symbol.iterator]() {
899
+ while (!this.finished) {
900
+ yield this.getPossibleSeparator() ?? this.getPossibleQuotedArgument() ?? this.getParameter();
901
+ }
902
+ }
903
+ getPossibleSeparator() {
904
+ if (this.input.startsWith(this.separator, this.position)) {
905
+ this.position += this.separator.length;
906
+ return { type: TokenType.Separator, value: this.separator };
907
+ }
908
+ return null;
909
+ }
910
+ getPossibleQuotedArgument() {
911
+ for (const [open, close] of this.quotes) {
912
+ if (!this.input.startsWith(open, this.position))
913
+ continue;
914
+ const end = this.input.indexOf(close, this.position + open.length);
915
+ if (end === -1)
916
+ continue;
917
+ const value = this.input.slice(this.position + open.length, end);
918
+ this.position = end + close.length;
919
+ return { type: TokenType.Quoted, value, open, close };
920
+ }
921
+ return null;
922
+ }
923
+ getParameter() {
924
+ const index = this.input.indexOf(this.separator, this.position);
925
+ const value = index === -1 ? this.input.slice(this.position) : this.input.slice(this.position, index);
926
+ this.position += value.length;
927
+ return { type: TokenType.Parameter, value };
928
+ }
929
+ };
930
+ __name(TokenStream, "TokenStream");
931
+ var TokenType = /* @__PURE__ */ ((TokenType2) => {
932
+ TokenType2[TokenType2["Parameter"] = 0] = "Parameter";
933
+ TokenType2[TokenType2["Quoted"] = 1] = "Quoted";
934
+ TokenType2[TokenType2["Separator"] = 2] = "Separator";
935
+ return TokenType2;
936
+ })(TokenType || {});
937
+
938
+ // src/lib/lexer/streams/ParameterStream.ts
939
+ var ParameterStream = class {
940
+ constructor(stream) {
941
+ __publicField(this, "stream");
942
+ __publicField(this, "separators", []);
943
+ this.stream = stream;
944
+ }
945
+ *[Symbol.iterator]() {
946
+ for (const part of this.stream) {
947
+ if (part.type === 2 /* Separator */) {
948
+ this.separators.push(part.value);
949
+ continue;
950
+ }
951
+ yield part.type === 1 /* Quoted */ ? new QuotedParameter(this.separators, part) : new WordParameter(this.separators, part);
952
+ this.separators = [];
953
+ }
954
+ return this.separators;
955
+ }
956
+ };
957
+ __name(ParameterStream, "ParameterStream");
958
+
959
+ // src/lib/lexer/Lexer.ts
960
+ var Lexer = class {
961
+ constructor(options = {}) {
962
+ __publicField(this, "quotes");
963
+ __publicField(this, "separator");
964
+ this.quotes = options.quotes ?? [];
965
+ this.separator = options.separator ?? " ";
966
+ }
967
+ run(input) {
968
+ return new ParameterStream(this.raw(input));
969
+ }
970
+ raw(input) {
971
+ return new TokenStream(this, input);
972
+ }
973
+ };
974
+ __name(Lexer, "Lexer");
975
+
976
+ // src/lib/parser/ParserResult.ts
977
+ var ParserResult = class {
978
+ constructor(parser) {
979
+ __publicField(this, "ordered", []);
980
+ __publicField(this, "flags", /* @__PURE__ */ new Set());
981
+ __publicField(this, "options", /* @__PURE__ */ new Map());
982
+ __publicField(this, "strategy");
983
+ this.strategy = parser.strategy;
984
+ }
985
+ parse(parameters) {
986
+ for (const parameter of parameters) {
987
+ this.parsePossibleFlag(parameter) || this.parsePossibleOptions(parameter) || this.parseOrdered(parameter);
988
+ }
989
+ return this;
990
+ }
991
+ parsePossibleFlag(parameter) {
992
+ return this.strategy.matchFlag(parameter.value).inspect((value) => this.flags.add(value)).isSome();
993
+ }
994
+ parsePossibleOptions(parameter) {
995
+ return this.strategy.matchOption(parameter.value).inspect(([key, value]) => {
996
+ const existing = this.options.get(key);
997
+ if (existing)
998
+ existing.push(value);
999
+ else
1000
+ this.options.set(key, [value]);
1001
+ }).isSome();
1002
+ }
1003
+ parseOrdered(parameter) {
1004
+ this.ordered.push(parameter);
1005
+ return true;
1006
+ }
1007
+ };
1008
+ __name(ParserResult, "ParserResult");
1009
+
1010
+ // src/lib/parser/strategies/EmptyStrategy.ts
1011
+ var EmptyStrategy = class {
1012
+ matchFlag() {
1013
+ return Option.none;
1014
+ }
1015
+ matchOption() {
1016
+ return Option.none;
1017
+ }
1018
+ };
1019
+ __name(EmptyStrategy, "EmptyStrategy");
1020
+
1021
+ // src/lib/parser/Parser.ts
1022
+ var Parser = class {
1023
+ constructor(strategy) {
1024
+ __publicField(this, "strategy");
1025
+ this.strategy = strategy ?? new EmptyStrategy();
1026
+ }
1027
+ setUnorderedStrategy(strategy) {
1028
+ this.strategy = strategy;
1029
+ return this;
1030
+ }
1031
+ run(input) {
1032
+ return new ParserResult(this).parse(input);
1033
+ }
1034
+ };
1035
+ __name(Parser, "Parser");
1036
+
1037
+ // src/lib/parser/strategies/PrefixedStrategy.ts
1038
+ var PrefixedStrategy = class {
1039
+ constructor(prefixes, separators) {
1040
+ __publicField(this, "prefixes");
1041
+ __publicField(this, "separators");
1042
+ this.prefixes = prefixes;
1043
+ this.separators = separators;
1044
+ }
1045
+ matchFlag(input) {
1046
+ const prefix = this.prefixes.find((x) => input.startsWith(x));
1047
+ if (!prefix)
1048
+ return Option.none;
1049
+ if (this.separators.some((x) => input.includes(x, prefix.length)))
1050
+ return Option.none;
1051
+ return Option.some(input.slice(prefix.length));
1052
+ }
1053
+ matchOption(input) {
1054
+ const prefix = this.prefixes.find((x) => input.startsWith(x));
1055
+ if (!prefix)
1056
+ return Option.none;
1057
+ for (const separator of this.separators) {
1058
+ const index = input.indexOf(separator, prefix.length + 1);
1059
+ if (index === -1)
1060
+ continue;
1061
+ if (index + separator.length === input.length)
1062
+ return Option.none;
1063
+ const key = input.slice(prefix.length, index);
1064
+ const value = input.slice(index + separator.length);
1065
+ return Option.some([key, value]);
1066
+ }
1067
+ return Option.none;
1068
+ }
1069
+ };
1070
+ __name(PrefixedStrategy, "PrefixedStrategy");
1071
+ return __toCommonJS(src_exports);
1072
+ })();
1073
+ //# sourceMappingURL=index.global.js.map