summaryrefslogtreecommitdiff
path: root/src/packrat-parsers.ads
blob: 471d667f5fab8d9f46d6a0b8c2f8b91c2656c042 (plain)
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


with

    Packrat.Traits,
    Packrat.Parse_Graphs;

private with

    Ada.Containers.Vectors,
    Ada.Containers.Ordered_Maps,
    Ada.Containers.Ordered_Sets,
    Ada.Containers.Indefinite_Holders;


generic

    with package Traits is new Packrat.Traits (<>);
    with package Graphs is new Packrat.Parse_Graphs (Traits);

package Packrat.Parsers is


    --  Memoize only adds to the Parse_Graph result when it is successful
    --  so the Parser_Context will need to keep track of unsuccessful combinator
    --  at given positions

    --  If all combinators are memoized in a memotable then no need to keep track of call stack
    --  To do that, use start point and function access as the key?

    --  If a combinator at a position is already in the memotable, return result
    --  Else run combinator, add/update result in memotable, then return result
    --  As a side effect of this, the entry in the memotable will be updated several times
    --  while left recursion unwinds

    --  Combinators need to return value strings as well as the finish sets

    --  Two functions, Symbolize and Ignore?, to create a node in the graph and to discard
    --  the current return value string

    --  Some way to join tokens-of-tokens into just tokens




    type Parser_Context is private;

    Empty_Context : constant Parser_Context;




    type Combinator_Result is private;

    type Combinator is access function
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    type Combinator_Array is array (Positive range <>) of Combinator;




    type With_Input is access function
        return Traits.Element_Array;




    generic
        Label : in Traits.Label_Enum;
        with function Root
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    procedure Parse
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Result  :    out Graphs.Parse_Graph);

    generic
        Label : in Traits.Label_Enum;
        with function Root
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    function Parse_Only
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context)
        return Graphs.Parse_Graph;

    generic
        Label : in Traits.Label_Enum;
        with function Root
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    function Parse_With
           (Input   : in     With_Input;
            Context : in out Parser_Context)
        return Graphs.Parse_Graph;




    generic
        Label : in Traits.Label_Enum;
        with function Combo
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    function Stamp
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        Label : in Traits.Label_Enum;
        with function Combo
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    function Ignore
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;




    generic
        Params : in Combinator_Array;
    function Sequence
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        Params : in Combinator_Array;
    function Choice
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Param
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
        Number : in Positive;
    function Count
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Param
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
        Minimum : in Natural := 0;
    function Many
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Param
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
        with function Test
               (Item : in Traits.Element_Type)
            return Boolean;
        Minimum : in Natural := 0;
    function Many_Until
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Param
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    function Optional
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;




    generic
        with function Test
               (Item : in Traits.Element_Type)
            return Boolean;
    function Satisfy
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Test
               (Item : in Traits.Element_Type)
            return Boolean;
        with function Change
               (From : in Traits.Element_Type)
            return Traits.Element_Type;
    function Satisfy_With
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        Item : in Traits.Element_Type;
    function Match
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        Item : in Traits.Element_Type;
        with function Change
               (From : in Traits.Element_Type)
            return Traits.Element_Type;
    function Match_With
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        Items : in Traits.Element_Array;
    function Multimatch
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        Number : in Positive := 1;
    function Take
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Test
               (Item : in Traits.Element_Type)
            return Boolean;
    function Take_While
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Test
               (Item : in Traits.Element_Type)
            return Boolean;
    function Take_Until
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;




    function Empty
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Combo
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    function Not_Empty
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;


private


    --  refactor Finish_Type from Parse_Graphs into Packrat and use here and in Lexers

    --  does the lexer handle input that doesn't start from 1 at the beginning?


    --  results need to record what combinators were curtailed, if any, with leftrec count

    --  choice combinators can be curtailed by multiple combinators at once

    --  results need to deal with tokens to put in the graph somehow


    --  Curtail when leftrec count exceeds number of remaining tokens plus 1
    --  for a given combinator/position


    package Elem_Holds is new Ada.Containers.Indefinite_Holders
       (Element_Type => Traits.Element_Array,
        "="          => Traits."=");

    package Tok_Holds is new Ada.Containers.Indefinite_Holders
       (Element_Type => Graphs.Finished_Token_Array,
        "="          => Graphs."=");

    function "<"
           (Left, Right : in Elem_Holds.Holder)
        return Boolean;

    function "<"
           (Left, Right : in Tok_Holds.Holder)
        return Boolean;




    type Combo_Key is record
        Start : Positive;
        Func  : Combinator;
    end record;

    function "<"
           (Left, Right : in Combo_Key)
        return Boolean;

    package Combo_Key_Vectors is new Ada.Containers.Vectors
       (Index_Type   => Positive,
        Element_Type => Combo_Key);

    --  This is needed to avoid some issues with using non-anonymous
    --  access values in a generic subprogram instantiation.
    function To_Key
           (Start : in Positive;
            Func  : access function
                   (Input   : in     Traits.Element_Array;
                    Context : in out Parser_Context;
                    Start   : in     Positive)
                return Combinator_Result)
        return Combo_Key;




    type Combo_Result_Part is record
        Finish : Natural := 0;
        Value  : Elem_Holds.Holder := Elem_Holds.Empty_Holder;
        Tokens : Tok_Holds.Holder := Tok_Holds.Empty_Holder;
    end record;

    function "<"
           (Left, Right : in Combo_Result_Part)
        return Boolean;

    package Result_Sets is new Ada.Containers.Ordered_Sets
       (Element_Type => Combo_Result_Part);




    function "<"
           (Left, Right : in Combinator)
        return Boolean;

    package Curtail_Maps is new Ada.Containers.Ordered_Maps
       (Key_Type     => Combinator,
        Element_Type => Positive);




    --  If there's anything in the Curtails, then Results should be empty
    --  and vice versa... union?

    --  need to add a record of the total length of input available when
    --  result was computed, to allow for knowing when to recompute
    --  optional_more/need_more results

    --  actually no, just not putting optional/needmore results in the
    --  memotable will work fine, but how to figure out how much to pass forward?
    type Combinator_Result is record
        Results  : Result_Sets.Set := Result_Sets.Empty_Set;
        Curtails : Curtail_Maps.Map := Curtail_Maps.Empty_Map;
        Status   : Result_Status := Failure;
    end record;

    Empty_Fail : constant Combinator_Result :=
       (Results  => Result_Sets.Empty_Set,
        Curtails => Curtail_Maps.Empty_Map,
        Status   => Failure);




    package Needs_Sets is new Ada.Containers.Ordered_Sets
       (Element_Type => Positive);

    package Memotables is new Ada.Containers.Ordered_Maps
       (Key_Type     => Combo_Key,
        Element_Type => Combinator_Result);

    package Leftrectables is new Ada.Containers.Ordered_Maps
       (Key_Type     => Combo_Key,
        Element_Type => Positive);




    type Parser_Context is record
        Result_So_Far    : Graphs.Parse_Graph;
        Used_Before      : Boolean := False;
        Global_Start     : Positive := 1;
        Current_Position : Positive := 1;
        Needs_More       : Needs_Sets.Set;
        Pass_Forward     : Elem_Holds.Holder;
        Memotable        : Memotables.Map;
        Leftrectable     : Leftrectables.Map;
        Allow_Incomplete : Boolean := True;
    end record;

    Empty_Context : constant Parser_Context :=
       (Result_So_Far    => Graphs.Empty_Graph,
        Used_Before      => False,
        Global_Start     => 1,
        Current_Position => 1,
        Needs_More       => Needs_Sets.Empty_Set,
        Pass_Forward     => Elem_Holds.Empty_Holder,
        Memotable        => Memotables.Empty_Map,
        Leftrectable     => Leftrectables.Empty_Map,
        Allow_Incomplete => True);




    function Reusable
           (Result   : in Combinator_Result;
            Position : in Positive;
            Leftrecs : in Leftrectables.Map)
        return Boolean;

    generic
        My_Key : in Combo_Key;
        with function Actual
               (Context : in out Parser_Context)
            return Combinator_Result;
    function Memoize
           (Context : in out Parser_Context)
        return Combinator_Result;




    procedure Inc_Leftrec
           (Key      : in     Combo_Key;
            Leftrecs : in out Leftrectables.Map);

    procedure Dec_Leftrec
           (Key      : in     Combo_Key;
            Leftrecs : in out Leftrectables.Map);

    function Exceeds_Curtail
           (Key      : in Combo_Key;
            Leftrecs : in Leftrectables.Map;
            Input    : in Traits.Element_Array)
        return Boolean;

    generic
        My_Key : in Combo_Key;
        Input  : in Traits.Element_Array;
        with function Actual
               (Context : in out Parser_Context)
            return Combinator_Result;
    function Curtailment
           (Context : in out Parser_Context)
        return Combinator_Result;




    procedure Merge
           (Target : in out Curtail_Maps.Map;
            Add    : in     Curtail_Maps.Map);

    function Merge
           (Left, Right : in Curtail_Maps.Map)
        return Curtail_Maps.Map;

    --  Should only be used to merge results obtained from
    --  the same input location.
    procedure Merge
           (Target : in out Combinator_Result;
            Add    : in     Combinator_Result);

    function Merge
           (Left, Right : in Combinator_Result)
        return Combinator_Result;

    generic
        with function Next
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    function Continue
           (From    : in     Combinator_Result;
            Input   : in     Traits.Element_Array;
            Context : in out Parser_Context)
        return Combinator_Result;




    procedure Complete_Status
           (Result : in out Combinator_Result;
            Allow  : in     Boolean);




    function Slide
           (Input    : in Traits.Element_Array;
            Position : in Positive)
        return Traits.Element_Array;


end Packrat.Parsers;