-
Notifications
You must be signed in to change notification settings - Fork 11
/
masala-parser.d.ts
898 lines (733 loc) · 23.1 KB
/
masala-parser.d.ts
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
/**
* Written by Nicolas Zozol
*/
export interface Unit {
}
/**
* Represents an Option/Optional/Maybe/Either type
*/
export interface Option<T> {
/**
* The inner value wrapped by the Option
*/
value: T;
/**
* Returns true if value is present
*/
isPresent(): boolean;
/**
* Transform the result
* @param mapper : the map function
*/
map<Y>(mapper: (t: T) => Y): Option<Y>;
/**
* The flatmap mapper must returns an option
* @param bindCall
*/
flatMap<Y>(bindCall: (y: Y) => Option<Y>): Option<Y>;
filter(f: (value: T) => boolean): T | Option<T>;
/**
* Returns the inner value when present.
*/
get(): T;
/**
* Returns the inner value or another one
* @param value
*/
orElse<Y>(value: Y): T | Y;
/**
* Accepts a provider function called with no params. Will
* return the inner value when present, or call the provider.
* @param provider
*/
orLazyElse<Y>(provider: () => Y): T | Y;
}
/**
* The Try interface allow the parser to recover from a parse failure
* and try backtacking with another parser using for example `or()`.
* It accepts a value when success, or an error value when failed
*
*/
export interface Try<V, E> {
isSuccess(): boolean;
isFailure(): boolean;
/**
* Acts like a mapper called only in case of success
* @param f
*/
onSuccess<T>(f: (v: V) => void): Try<V, E>;
/**
* Acts like a mapper called only in case of failure
* @param f
*/
onFailure(f: (e: E) => void): Try<V, E>;
/**
* Transform the success value. If `map()` throws an error,
* then we have a failure.
* @param bindCall
*/
map<T>(bindCall: (v: V) => T): Try<T, E>;
flatMap<T>(bindCall: (v: V) => Try<T, E>): Try<T, E>;
/**
* Returns the success value
*/
success(): V;
/**
* Returns the failure value
*/
failure(): E;
/**
* Will provide another value in case of failure
* @param otherValue
*/
recoverWith<T>(otherValue: T): V | T;
/**
* Will call a provider accepting the error as argument when the
* try as failed, or the success value.
* @param recoverFunction
*/
lazyRecoverWith<T>(recoverFunction: (error?: E) => T): V | T;
/**
* Returns a new Try that will succeed only if first is a success
* AND is the predicate is accepted on the value
* @param f
*/
filter(predicate: (value: V) => boolean): Try<V, E>;
}
export declare type NEUTRAL = symbol;
/**
* Represents the sequence of tokens found by the parser.
* A Tuple accepts a `NEUTRAL` element
*/
export interface Tuple<T> {
/**
* Wrapped array
*/
value: T[];
isEmpty: boolean;
/**
* Number of elements in the wrapped array
*/
size(): number;
/**
* Returns the first token. It's up to the coder to know
* if there is only one token
* * ```js
* const parser = C.char('a')
* .then(C.char('b'))
* .drop()
* .then(C.char('c')
* .single(); // Parsing will return 'c'
* ```
* See [[array]]
*/
single(): T;
/**
* Returns all tokens in an array
* ```js
* const parser = C.char('a')
* .then(C.char('b'))
* .then(C.char('c')
* .array(); // Parsing will return ['a','b','c']
* ```
* See [[single]]
*/
array(): T[];
/**
* Returns the last element of the Tuple
*/
last(): T;
/**
* Returns the first element of the Tuple
*/
first(): T;
/**
* Returns value at index
* @param index
*/
at(index: number): T;
/**
* Join elements as one string joined by optional separator (ie empty '' separator by default)
* @param separator join separator
*/
join(separator?: string): string;
/**
* The tuple will not change with the NEUTRAL element.
* It will concatenate the two tuples as one, or add
* a single element if it's not a Tuple.
* Therefore a Tuple can wrap multiple arrays.
* @param neutral : neutral element
*
* See [[TupleParser]]
*/
append(neutral: NEUTRAL): this;
append<Y>(other: Tuple<Y>): Tuple<T | Y>;
append<Y>(element: Y): Tuple<T | Y>;
}
/**
* Creates an empty or not Tuple, which is the preferred way
*
* `const t = tuple().append(x)`
*
* `const t = tuple([2, 4, 5])`;
*/
export function tuple<T>(array?: T[]): Tuple<T>;
/**
* Represents a **source** of data that will be parsed.
* These data are characters with Stream<String> of tokens defined
* by low-level tokens (if, '{', number, class, function}) with Stream<Parser>.
* There are two types of Streams: low level where source is a text or an array,
* and high level where source is composed with another source.
*
* A source can be parsed multiple times by various parsers. A
* `Stream` is supposed to be parsed only once.
*
*/
export interface Stream<Data> {
/**
* For high-level stream, returns the index of the low-level source.
* For low-level, returns the same value as index.
* @param index
*/
location(index: number): number;
/**
* Return the token at the index.
* @param index
*/
get(index: number): Try<Data, void>;
subStreamAt(index: number, stream: Stream<Data>): any;
}
/**
* Data parsed by the parser
*/
interface Streams {
ofString(string: string): Stream<string>;
ofArray<X>(): Stream<X[]>;
ofParser<T, D>(tokenParser: IParser<T>, lowerStream: Stream<D>): Stream<IParser<T>>;
/**
* Creates a bufferedStream that checks into a cache
* @param source
*/
buffered<D>(source: D): Stream<D>;
}
/**
* When a Parser parses a Stream, it produces a Response.
* This response can be an Accept, or a Reject
*/
export interface Response<T> {
/**
* The value built by the parsing
*/
value: T;
/**
* Index in the stream. See [[location]].
*/
offset: number;
/**
* True if the parser succeed all steps, false if parser stopped
*/
isAccepted(): boolean;
/**
* Returns true if parser has reached end of stream to build the Response
*/
isEos(): boolean;
/**
* Execute accept if response in an Accept, or reject and create a new
* Response. A Reject could thus lead to a new Accept
* @param accept
* @param reject
*/
fold(accept: () => Response<T>, reject?: () => Response<T>): Response<T>;
/**
* Transform the response **in case of** success. Won't touch a Reject.
* @param f
*/
map<Y>(f: (v: T, r: readonly this) => Y): Response<Y>;
/**
* ```js
* Response.accept('a')
* .flatMap(a=>Response.accept(a))
* .isAccepted(),
* ```
*
* @param f mapping function
*/
flatMap<Y>(f: (v: T) => Response<Y>): Response<Y>;
filter(predicate: (value: any) => boolean): Response<T>;
/**
* Index in the final text. Is equal to
* [[offset]] for low level parsers.
*/
location(): number;
}
/**
* Response rejected. Can be transformed in
* `Accept` using [[fold]]
*/
export interface Reject<T> extends Response<T> {
}
export interface Accept<T> extends Response<T> {
}
/* Array with different values*/
export interface TupleParser<T> extends IParser<Tuple<T>> {
/**
* Combine two parsers for reading the stream
* ```js
* const parser = C.char('a')
* .then(C.char('b')) // first TupleParser
* .then(C.char('c'));
* const value = parser.parse(Streams.ofString('abc')).value;
* test.deepEqual(value, new Tuple(['a','b','c']));
* ```
* @param p next parser
*/
then(p: VoidParser): TupleParser<T>;
then(p: IParser<T>): TupleParser<T>;
then<Y>(p: IParser<Y>): TupleParser<T | Y>;
or(other: TupleParser<T>): TupleParser<T>;
or<Y>(other: TupleParser<Y>): TupleParser<T> | TupleParser<Y>;
or<T, P extends IParser<T>>(other: P): VoidParser | P;
/**
* Map the response value as an array
* ```js
* const parser = C.char('a')
* .then(C.char('b'))
* .array();
* const value = parser.parse(Streams.ofString('abcd')).value;
* test.deepEqual(value, ['a','b']); // parsing stopped at index 2
* ```
*
*/
array(): SingleParser<T[]>;
/**
* Map the response value as the **first** Tuple element.
* ```js
* const parser = C.char('a')
* // 'b' is dropped, but we still have a Tuple
* .then(C.char('b').drop())
* .single();
* const value = parser.parse(Streams.ofString('ab')).value;
* test.equal(value, 'a');
* ```
*
* WARNING: This may change and throw an exception if it's not a single value.
* `first()` may appear.
*/
single(): SingleParser<T>;
/**
* Map the response value as the last value
* ```js
* const parser = C.char('a')
* .then(C.char('b'))
* .then(C.char('c'))
* .last();
* const value = parser.parse(Streams.ofString('abc')).value;
* test.deepEqual(value, ['a','b']); // parsing stopped at index 2
* ```
*
*/
last(): SingleParser<T>;
first(): SingleParser<T>;
/**
* Accepted with one or more occurrences.Will produce an Tuple of at least one T
*/
rep(): TupleParser<T>;
/**
* Accepted with zero or more occurrences. Will produce a Tuple of zero or more T
*/
optrep(): TupleParser<T>;
}
declare type MASALA_VOID_TYPE = symbol;
/**
* Special case of a [[SingleParser]], but easier to write.
* Note that `VoidParser.then(VoidParser)` will produce a [[TupleParser]] where
* inner value is `[]`. Same result with `optrep()` and `rep()`
*/
export interface VoidParser extends SingleParser<MASALA_VOID_TYPE> {
/**
* Combine two parsers for reading the stream
* ```js
* const parser = C.char('a').drop()
* .then(C.char('b').drop());
* const value = parser.parse(Streams.ofString('ab')).value;
* test.ok(value.size() === 0 );
* ```
* @param p next parser
*/
then(p: VoidParser): TupleParser<MASALA_VOID_TYPE>;
then<Y>(p: TupleParser<Y>): TupleParser<Y>;
then<Y>(p: SingleParser<Y>): TupleParser<Y>;
then<Y>(p: IParser<Y>): TupleParser<Y>;
or(other: VoidParser): VoidParser;
or<T, P extends IParser<T>>(other: P): VoidParser | P;
opt(): SingleParser<Option<MASALA_VOID_TYPE>>;
/**
* Accepted with one or more occurrences.Will produce an Tuple of at least one T
*/
rep(): TupleParser<MASALA_VOID_TYPE>;
/**
* Accepted with zero or more occurrences. Will produce a Tuple of zero or more T
*/
optrep(): TupleParser<MASALA_VOID_TYPE>;
}
export interface SingleParser<T> extends IParser<T> {
/**
* Combine two parsers for reading the stream
* ```js
* const parser = C.char('a')
* .then(C.char('b'));
* const value = parser.parse(Streams.ofString('ab')).value;
* test.equal(value.size(), 2 );
* ```
* @param p next parser
*/
then(p: VoidParser): TupleParser<T>;
then(p: IParser<T>): TupleParser<T>;
then<Y>(p: IParser<Y>): TupleParser<T | Y>;
or(other: SingleParser<T>): SingleParser<T>;
or<Y>(other: SingleParser<Y>): SingleParser<T | Y>;
or(other: TupleParser<T>): TupleParser<T>;
or<Y>(other: TupleParser<Y>): TupleParser<Y> | TupleParser<T>;
or<Y, P extends IParser<Y>>(other: P): SingleParser<T> | P;
opt(): SingleParser<Option<T>>;
/**
* Accepted with one or more occurrences.Will produce an Tuple of at least one T
*/
rep(): TupleParser<T>;
/**
* Accepted with zero or more occurrences. Will produce a Tuple of zero or more T
*/
optrep(): TupleParser<T>;
}
/**
* Parsers are most of the time made by combination of Parsers given by
* [[CharBundle]] (**C**), [[NumberBundle]] (**N**), and/or [[FlowBundle]] (**F**).
* You can also create a custom Parser from scratch.
* T is the type of the Response
*/
export interface IParser<T> {
/**
* Parses a stream of items (usually characters or tokens) and returns a Response
* @param stream
* @param index optional, index start in the stream
*/
parse<D>(stream: Stream<D>, index?: number): Response<T>;
/**
* Mainly designed for quick debugging or unit tests
* Parses the text and returns the value, or **undefined** if parsing has failed.
* @param text text to parse
*/
val(text: string): T;
/**
* Creates a sequence of tokens accepted by the parser
* ```js
* const abParser = C.char('a').then(C.char('b'))
* ```
* @param p
*/
then(p: VoidParser): TupleParser<T>;
then(p: IParser<T>): TupleParser<T>;
then<Y>(p: IParser<Y>): TupleParser<T | Y>;
/**
* Transforms the Response value
*
* ```js
* const parser = N.number().map (n => n*2);
* const value = parser.parse(Streams.ofString('1')).value;
* test.equal(value, 2 );
* ```
*
* @param f
*/
map<Y>(f: (value: T, response: readonly Response<T>) => Y): SingleParser<Y>;
/**
* Create a new parser value *knowing* the current parsing value
*
* ```js
* // Next char must be the double of the previous
* function doubleNumber(param:number){
* return C.string(''+ (param*2) );
* }
*
* const combinator = N.digit()
* .flatMap(doubleNumber);
* let response = combinator.parse(Streams.ofString('12'));
* assertTrue(response.isAccepted());
* ```
*
* @param builder: the function building the parser
*/
flatMap<Y, P extends IParser<Y>>(builder: parserBuilder<Y, P>): P;
/**
* The parser will return the Neutral element for the Tuple
*/
drop(): VoidParser;
/**
* If accepted, the parser will return the given value
* @param value
*/
returns<Y>(value: Y): SingleParser<Y>;
/**
* Will display debug info when the previous parser is accepted.
*
* ```js
* // 'found digit' will be displayed only if N.digit() has been accepted
* N.digit().debug('found digit', false);
* ```
*
* @param hint: an indication that the previous parser attempted to parse
* @param showValue: if true, will display the response value given by the previous parser. true by default.
*/
debug(hint: string, showValue?: boolean): this;
/**
* Given an accepted parser and a response value, this parser.filter(predicate) could be rejected depending on
* the predicate applied on the response value.
* Filtering a rejected parser will always results in a rejected response.
* @param predicate : predicate applied on the response value
*/
filter(predicate: (value: T) => boolean): this;
/**
* If this parser is not accepted, the other will be used. It's often use with `F.try()` to
* make backtracking.
* @param other
*/
or<Y, P extends IParser<Y>>(other: P): IParser<T> | IParser<Y>;
/**
* If all parsers are accepted, the response value will be a Tuple of these values.
* It's a rare case where a Tuple could contain a Tuple.
* Warning: still experimental
* @param p
*/
and<P extends IParser<T>>(p: P): TupleParser<T>;
and<Y, P extends IParser<Y>>(p: P): TupleParser<T | Y>;
/**
* When `parser()` is rejected, then `parser().opt()` is accepted with an empty Option as response value.
* If `parser()` is accepted with a value `X`, then `parser().opt()` is accepted with an `Option.of(X)`
*/
opt(): IParser<Option<T>>;
/**
* Accepted with one or more occurrences.Will produce an Tuple of at least one T
*/
rep(): TupleParser<any>;
/**
* Accepted with zero or more occurrences. Will produce a Tuple of zero or more T
*/
optrep(): TupleParser<any>;
/**
* Search for next n occurrences. `TupleParser.occurence()` will continue to build one larger Tuple
* ```js
* let parser = C.char('a').then (C.char('b')).occurrence(3);
* let resp = parser.parse(Streams.ofString('ababab'));
* // -> Tuple { value: [ 'a', 'b', 'a', 'b', 'a', 'b' ] } }
* ```
* @param n
*/
occurrence(n: number): TupleParser<T>;
/**
* Specialized version of [[filter]] using `val` equality as predicate
* @param val the parser is rejected if it's response value is not `val`
*/
match(val: T): this;
/**
* Build a new High Leven parser that parses a ParserStream of tokens.
* Tokens are defined by `this` parser.
* It's internally used by [[GenLex]] which is more easy to work with
*
* @highParser high level parser
*/
chain<Y, P extends IParser<Y>>(highParser: P): P;
/**
* Accept the end of stream. If accepted, it will not change the response
*/
eos(): this;
}
/**
* A `parseFunction` is a function that returns a parser. To build this parser at runtime, any params are available.
*/
export type parseFunction<X, T> = (stream: Stream<X>, index?: number) => Response<T>;
interface Parser<T> extends IParser<T> {
}
/**
* Note: the documentation shows two parametric arguments `T` for `Parser<T,T>`, but it's really
* one argument. Looks like the only tooling bug, but sadly on the first line.
*/
export class Parser<T> {
new<D>(f: parseFunction<D, T>): Parser<T>;
}
interface CharBundle {
UTF8_LETTER: symbol;
OCCIDENTAL_LETTER: symbol;
ASCII_LETTER: symbol;
/**
* Accepts any letter, including one character emoji. Issue on two characters emoji
*/
utf8Letter(): SingleParser<string>;
/**
* Accepts any occidental letter, including most accents
*/
letter(): SingleParser<string>;
/**
* Choose s in [[UTF8_LETTER]], [[OCCIDENTAL_LETTER]], [[ASCII_LETTER]]
* Ascii letters are A-Za-z letters, excluding any accent
* @param s
*/
letterAs(s: symbol): SingleParser<string>;
/**
* Accepts a sequence of letters, joined as a string text
*/
letters(): SingleParser<string>;
/**
* Sequence of letters, joined as a string text. See [[letterAs]]
*/
lettersAs(s: symbol): SingleParser<string>;
/**
* Accepts an emoji
*
* WARNING: experimental, help needed; Working on emoji is a full-time specialist job.
*/
emoji(): SingleParser<string>;
/**
* Accepts any character that is not the `exclude` character
* @param exclude one character exclude
*/
notChar(exclude: string): SingleParser<string>;
/**
* Accepts any character that is not listed in the `exclude` chain
* @param exclude excluded characters
*
* WARNING: might be issues on emoji
*/
charNotIn(exclude: string): SingleParser<string>;
/**
* Accepts a char
* @param c
*/
char(c: string): SingleParser<string>;
/**
* Accepts any char listed in
* @param strings
*/
charIn(strings: string): SingleParser<string>;
/**
* Accepts a string
* @param string
*/
string(string: string): SingleParser<string>;
/**
* Accept one of these strings
* @param strings
*/
stringIn(strings: string[]): SingleParser<string>;
/**
* Accept anything that is not this string. Useful to build *stop*. See also [[FlowBundle.moveUntil]],
* [[FlowBundle.dropTo]]
* @param string
*/
notString(string: string): SingleParser<string>;
/**
* Single character inside single quote, like C/Java characters: `'a'`, `'x'`
*/
charLiteral(): SingleParser<string>;
/**
* String characters inside double quotes, like Java strings : `"Hello"`, `"World"`
*/
stringLiteral(): SingleParser<string>;
/**
* a-z single letter. WARNING: doesn't work yet on accents or utf-8 characters
*/
lowerCase(): SingleParser<string>;
/**
* A-Z single letter. WARNING: doesn't work yet on accents or utf-8 characters
*/
upperCase(): SingleParser<string>;
}
type parserBuilder<Y, P extends IParser<Y>> = (...rest: any[]) => P;
type extension<Y, T extends IParser<Y>> = T;
type Predicate<V> = (value: V) => boolean;
interface FlowBundle {
parse<Y, P extends IParser<Y>>(parser: P): P;
nop(): VoidParser;
layer<Y, P extends IParser<Y>>(parser: P): P;
try<Y, P extends IParser<Y>>(parser: P): P;
any(): SingleParser<any>;
subStream(length: number): VoidParser;
not<Y, P extends IParser<Y>>(parser: P): SingleParser<any>;
lazy<Y, P extends IParser<Y>>(builder: parserBuilder<Y, P>, args?: any[]): P;
returns<T>(value: T): SingleParser<T>;
error(): VoidParser;
eos(): SingleParser<Unit>;
satisfy<V>(predicate: Predicate<V>): SingleParser<any>;
startWith<V>(value: V): SingleParser<V>;
/**
* moveUntil moves the offset until stop is found and returns the text found between.
* The *stop* is **not** read
* @param stop
*/
moveUntil(stop: string): SingleParser<string>;
moveUntil(stops: string[]): SingleParser<string>;
moveUntil<Y>(p: IParser<Y>): SingleParser<string>;
/**
* Move until the stop, stop **included**, and drops it.
* @param s
*/
dropTo(s: string): VoidParser;
dropTo<Y>(p: IParser<Y>): VoidParser;
}
interface NumberBundle {
number(): SingleParser<number>;
integer(): SingleParser<number>;
digit(): SingleParser<number>;
digits(): SingleParser<number>;
}
type ParserOrString<T> = IParser<T> | string;
interface Token<T> extends SingleParser<T> {
}
type TokenCollection = {
[key: string]: Token<any>
};
export interface TokenResult<T> {
name: string;
value: T;
}
interface GenLex {
// TODO: make overload here
/**
*
* @param parser parser of the token
* @param name token name
* @param precedence the token with lowest precedence is taken before others.
*
* Choice with grammar is made after token selection !
*/
tokenize<T, P extends IParser<T>>(parser: P, name: string, precedence?: number): P;
use<T, P extends IParser<T>>(grammar: P): P;
tokens(): TokenCollection;
setSeparators(spacesCharacters: string): GenLex;
/**
* Select the parser used by genlex. It will be used as `separator.optrep().then(token).then(separator.optrep())`.
* So the separator must not be optional or it will make an infinite loop.
* The separation in your text can't be a strict one-time separation with Genlex.
* @param parser
*/
setSeparatorsParser<T>(parser: IParser<T>): GenLex;
/**
* Should separators be repeated ?
*
* `separators.optrep().then(myToken()).then(separators.optrep())`
* @param repeat default is true
*/
setSeparatorRepetition(repeat: boolean): GenLex;
/**
* tonkenize all items, given them the name of the token
* Exemple : keywords(['AND', 'OR']) will create the tokens named 'AND' and 'OR' with C.string('AND'), C.string('OR)
* @param tokens
*/
keywords(tokens: string[]): Array<Token<string>>;
get(tokenName: string): Token<any>;
}
export class GenLex implements GenLex {
}
export declare const F: FlowBundle;
export declare const C: CharBundle;
export declare const N: NumberBundle;
export declare const Streams: Streams;