summaryrefslogtreecommitdiff
path: root/doc/parser_comb.html
blob: c6fd63c9349de05e0dda2d21376c4ad97b17d4f2 (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
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

<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Parser Components - Packrat Docs</title>
    <link href="default.css" rel="stylesheet">
  </head>

  <body>


  <h2>Parser Components</h2>

  <a href="index.html">Return to Contents</a>


  <p>Several of the combinators were inspired from Haskell's Parsec library.</p>

  <table>
    <tr>
<td><pre>
generic
    with function Root
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
package Parse_Parts is

    procedure Parse
           (Input  : in     Traits.Element_Array;
            Result :    out Graphs.Parse_Graph);

    procedure Reset;

end Parse_Parts;
</pre></td>
<td>The parser that allows for piecewise parsing. It will continue to accept more input until an empty
input array is supplied, at which point it is assumed that the end of input has been reached. No result
will be given until end of input.<br>
<br>
The <em>Reset</em> procedure returns the parser to a clean state as if it had just been declared.</td>
    </tr>
    <tr>
<td><pre>
generic
    with function Root
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
package Parse_Once is

    function Parse
           (Input : in Traits.Element_Array)
        return Graphs.Parse_Graph;

    procedure Reset;

end Parse_Once;
</pre></td>
<td>Parser that only accepts input all in one go, returning the result after one call of the <em>Parse</em>
function.<br>
<br>
The <em>Reset</em> procedure returns the parser to a clean state as if it had just been declared.</td>
    </tr>
    <tr>
<td><pre>
generic
    with function Root
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
package Parse_With is

    function Parse
           (Input : in With_Input)
        return Graphs.Parse_Graph;

    procedure Reset;

end Parse_With;
</pre></td>
<td>Piecewise parsing, but uses the supplied input function to retrieve further pieces of input as
needed, until the function return an empty array upon which end of input is assumed.<br>
<br>
The <em>Reset</em> procedure returns the parser to a clean state as if it had just been declared.</td>
    </tr>
    <tr>
<td><pre>
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;
</pre></td>
<td>Runs the supplied <em>Combo</em> combinator. If the result is successful, generates a node in the
parse graph with label of <em>Label</em>. Any nodes generated by <em>Combo</em> are made into children of
the new node. Any non-node input parsed by <em>Combo</em> is made into the value of the new node.<br>
<br>
If the <em>Combo</em> combinator is not successful, stores the error result in case a <em>Parser_Error</em>
needs to be raised.</td>
    </tr>
    <tr>
<td><pre>
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 Discard
       (Input   : in     Traits.Element_Array;
        Context : in out Parser_Context;
        Start   : in     Positive)
    return Combinator_Result;
</pre></td>
<td>Operates similar to <em>Stamp</em> but discards any successful result of <em>Combo</em> instead of
adding it to the parse graph. Any error result is still stored in case of a <em>Parser_Error</em> needing
to be raised.</td>
    </tr>
    <tr>
<td><pre>
generic
    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;
</pre></td>
<td>Operates similar to <em>Discard</em> but no error result is stored. Any result of <em>Combo</em> is
ultimately discarded regardless of success or failure.</td>
    </tr>
    <tr>
<td><pre>
generic
package Redirect is

    procedure Set
           (Target : in Combinator);

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

end Redirect;
</pre></td>
<td>Use this whenever you need to refer to a combinator that you haven't declared yet in a combinator
instantiation. This generally happens when there is some form of recursion in the grammar. Instantiate
<em>Redirect</em>, use <em>Call</em> in the instantiation of other combinators, then set the actual
target of the redirect with <em>Set</em> before you call the parser. If the redirect is not set when
the parser is called then a <em>Constraint_Error</em> is raised.</td>
    </tr>
    <tr>
<td><pre>
generic
    Params : in Combinator_Array;
function Sequence
       (Input   : in     Traits.Element_Array;
        Context : in out Parser_Context;
        Start   : in     Positive)
    return Combinator_Result;
</pre></td>
<td>Attempts to parse all the combinators in <em>Params</em> one after the other. Only succeeds if
all combinators succeed.</td>
    </tr>
    <tr>
<td><pre>
generic
    with function Part_One
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
    with function Part_Two
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
function Sequence_2
       (Input   : in     Traits.Element_Array;
        Context : in out Parser_Context;
        Start   : in     Positive)
    return Combinator_Result;
</pre></td>
<td>Parses <em>Part_One</em>, then <em>Part_Two</em> directly after. Only succeeds if both succeed. This
combinator exists because <em>Sequence</em> can have instantiation issues due to access level restrictions.</td>
    </tr>
    <tr>
<td><pre>
generic
    Params : in Combinator_Array;
function Choice
       (Input   : in     Traits.Element_Array;
        Context : in out Parser_Context;
        Start   : in     Positive)
    return Combinator_Result;
</pre></td>
<td>Parses all the combinators in <em>Params</em> all starting from the current starting point, then combines
all the results. In this way all possible valid parse choices are taken.</td>
    </tr>
    <tr>
<td><pre>
generic
    with function Choice_One
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
    with function Choice_Two
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
function Choice_2
       (Input   : in     Traits.Element_Array;
        Context : in out Parser_Context;
        Start   : in     Positive)
    return Combinator_Result;
</pre></td>
<td>Parses <em>Choice_One</em> and <em>Choice_Two</em> both starting at the current starting point, then
combines the results. In other words it takes both possible parse choices. Like <em>Sequence_2</em>, this
exists because the <em>Choice</em> can have instantiation issues due to access level restrictions.</td>
    </tr>
    <tr>
<td><pre>
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;
</pre></td>
<td>Parses the supplied <em>Param</em> combinator <em>Number</em> of times sequentially.</td>
    </tr>
    <tr>
<td><pre>
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;
</pre></td>
<td>Parses the supplied <em>Param</em> combinator as many times as it can, one after the other, until it
fails. If at least <em>Minimum</em> number of parses are successful then the whole result is successful.<br>
<br>
The result includes every possible valid number of times that <em>Param</em> could be parsed. For example,
if <em>Param</em> could be parsed 0-6 times and <em>Minimum</em> is 3, then the result will include cases
where <em>Param</em> was parsed 3, 4, 5, 6 times.</td>
    </tr>
    <tr>
<td><pre>
generic
    with function Param
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
function Followed_By
       (Input   : in     Traits.Element_Array;
        Context : in out Parser_Context;
        Start   : in     Positive)
    return Combinator_Result;
</pre></td>
<td>Checks whether <em>Param</em> succeeds at the given point in the input and succeeds if <em>Param</em>
succeeds. Does not actually progress the input, regardless of what <em>Param</em> does.</td>
    </tr>
    <tr>
<td><pre>
generic
    with function Param
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
function Not_Followed_By
       (Input   : in     Traits.Element_Array;
        Context : in out Parser_Context;
        Start   : in     Positive)
    return Combinator_Result;
</pre></td>
<td>Checks whether <em>Param</em> succeeds at the given point in the input and succeeds if <em>Param</em>
fails. Does not actually progress the input, regardless of what <em>Param</em> does.</td>
    </tr>
    <tr>
<td><pre>
generic
    with function Param
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
    with function Test
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
    Minimum : in Natural := 0;
function Many_Until
       (Input   : in     Traits.Element_Array;
        Context : in out Parser_Context;
        Start   : in     Positive)
    return Combinator_Result;
</pre></td>
<td>Parses the supplied <em>Param</em> combinator as many times as it can until a point is reached when
<em>Test</em> would succeed. The overall result succeeds if <em>Param</em> was successfully parsed at least
<em>Minimum</em> number of times.</td>
    </tr>
    <tr>
<td><pre>
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;
</pre></td>
<td>Combines the result of parsing <em>Param</em> with the result of not parsing it.</td>
    </tr>
    <tr>
<td><pre>
generic
    with function Item
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
    with function Separator
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
    Minimum : in Natural := 0;
function Separate_By
       (Input   : in     Traits.Element_Array;
        Context : in out Parser_Context;
        Start   : in     Positive)
    return Combinator_Result;
</pre></td>
<td>Parses as many of <em>Item</em> as it can, separated by <em>Separator</em> and requiring a minimum of
<em>Minimum</em> items parsed. The separators are ignored in the final result. Combines different length
results in the same way that <em>Many</em> does.</td>
    </tr>
    <tr>
<td><pre>
generic
    with function Item
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
    with function Separator
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
    with function Ender
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
    Minimum : in Natural := 0;
function Separate_End_By
       (Input   : in     Traits.Element_Array;
        Context : in out Parser_Context;
        Start   : in     Positive)
    return Combinator_Result;
</pre></td>
<td>Works the same way as <em>Separate_By</em> in sequence with <em>Ender</em>. The ender is ignored in
the final result.</td>
    </tr>
    <tr>
<td><pre>
generic
    with function Starter
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
    with function Item
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
    with function Ender
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;
function Between
       (Input   : in     Traits.Element_Array;
        Context : in out Parser_Context;
        Start   : in     Positive)
    return Combinator_Result;
</pre></td>
<td>Parses <em>Starter</em> then <em>Item</em> then <em>Ender</em> in sequence. The results of
<em>Starter</em> and <em>Ender</em> are discarded.</td>
    </tr>
    <tr>
<td><pre>
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;
</pre></td>
<td>Parses the next item of input if the supplied <em>Test</em> function succeeds on it.</td>
    </tr>
    <tr>
<td><pre>
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;
</pre></td>
<td>Parses the next item of input if the supplied <em>Test</em> function succeeds on it after it has
been transformed by the <em>Change</em> function. The original unchanged item is what is parsed.</td>
    </tr>
    <tr>
<td><pre>
generic
    Item : in Traits.Element_Type;
function Match
       (Input   : in     Traits.Element_Array;
        Context : in out Parser_Context;
        Start   : in     Positive)
    return Combinator_Result;
</pre></td>
<td>Parses the next item of input if it matches <em>Item</em>.</td>
    </tr>
    <tr>
<td><pre>
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;
</pre></td>
<td>Parses the next item of input if it matches <em>Item</em> after the input item has been transformed
by the <em>Change</em> function. The original unchanged item is what is parsed.</td>
    </tr>
    <tr>
<td><pre>
generic
    Items : in Traits.Element_Array;
function Multimatch
       (Input   : in     Traits.Element_Array;
        Context : in out Parser_Context;
        Start   : in     Positive)
    return Combinator_Result;
</pre></td>
<td>Parses the next <em>Items'Length</em> items of input if they match <em>Items</em>.</td>
    </tr>
    <tr>
<td><pre>
generic
    Number : in Positive := 1;
function Take
       (Input   : in     Traits.Element_Array;
        Context : in out Parser_Context;
        Start   : in     Positive)
    return Combinator_Result;
</pre></td>
<td>Parses the next <em>Number</em> items of input.</td>
    </tr>
    <tr>
<td><pre>
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;
</pre></td>
<td>Parses items of input as long as the supplied <em>Test</em> function succeeds on them.</td>
    </tr>
    <tr>
<td><pre>
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;
</pre></td>
<td>Parses items of input until the supplied <em>Test</em> function succeeds on the next item of input.</td>
    </tr>
    <tr>
<td><pre>
function Empty
       (Input   : in     Traits.Element_Array;
        Context : in out Parser_Context;
        Start   : in     Positive)
    return Combinator_Result;
</pre></td>
<td>Does not parse any input but returns an empty, yet successful, result.</td>
    </tr>
    <tr>
<td><pre>
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;
</pre></td>
<td>Parses the supplied <em>Combo</em> combinator as normal but excludes any empty results.</td>
    </tr>
    <tr>
<td><pre>
function End_Of_Input
       (Input   : in     Traits.Element_Array;
        Context : in out Parser_Context;
        Start   : in     Positive)
    return Combinator_Result;
</pre></td>
<td>A combinator that only succeeds if the end of input has been reached and there is nothing more
to parse, at which point it will return an empty yet successful result.</td>
    </tr>
  </table>


  </body>
</html>