summaryrefslogtreecommitdiff
path: root/src/packrat-lexers.adb
blob: fc63e4afa95cc0e223eac1d130fd8a5be24ea587 (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
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


with

    Packrat.Errors;


package body Packrat.Lexers is


    function Join
           (Left, Right : in Combinator_Result)
        return Combinator_Result is
    begin
        if Left.Status = Success or Left.Status = Optional_More then
            return (Integer'Max (Left.Finish, Right.Finish), Right.Status);
        elsif Left.Status = Needs_More then
            return (Left.Finish, Failure);
        else
            return Left;
        end if;
    end Join;





    function Stamp
           (Input   : in     Traits.Element_Array;
            Context : in out Lexer_Context)
        return Component_Result
    is
        Current_Result : Combinator_Result :=
            Combo (Input, Context.Position);
    begin
        if  Context.Status /= Success or Context.Position > Input'Last or
            Context.Empty_Labels.Contains (Label)
        then
            return Component_Failure;
        end if;

        if (Current_Result.Status = Needs_More and not Context.Allow_Incomplete) or
            Current_Result.Status = Failure
        then
            Context.Error_Labels.Append (Label);
            return Component_Failure;
        end if;

        if (Current_Result.Status = Optional_More and not Context.Allow_Incomplete) or
            Current_Result.Status = Success
        then
            Context.Result_So_Far.Append (Traits.Tokens.Create
               (Label,
                Context.Position + Context.Offset,
                Input (Context.Position .. Current_Result.Finish)));
            if Current_Result.Finish = 0 then
                Context.Empty_Labels.Insert (Label);
            else
                Context.Empty_Labels.Clear;
                Context.Position := Current_Result.Finish + 1;
            end if;
        else
            Context.Status := Current_Result.Status;
            Context.Pass_Forward.Replace_Element
                (Input (Context.Position .. Current_Result.Finish));
            Context.Empty_Labels.Clear;
        end if;

        Context.Error_Labels.Clear;
        return Component_Success;
    end Stamp;


    function Ignore
           (Input   : in     Traits.Element_Array;
            Context : in out Lexer_Context)
        return Component_Result
    is
        Current_Result : Combinator_Result :=
            Combo (Input, Context.Position);
    begin
        if  Context.Status /= Success or Context.Position > Input'Last or
            Context.Empty_Labels.Contains (Label)
        then
            return Component_Failure;
        end if;

        if (Current_Result.Status = Needs_More and not Context.Allow_Incomplete) or
            Current_Result.Status = Failure
        then
            Context.Error_Labels.Append (Label);
            return Component_Failure;
        end if;

        if (Current_Result.Status = Optional_More and not Context.Allow_Incomplete) or
            Current_Result.Status = Success
        then
            if Current_Result.Finish = 0 then
                Context.Empty_Labels.Insert (Label);
            else
                Context.Empty_Labels.Clear;
                Context.Position := Current_Result.Finish + 1;
            end if;
        else
            Context.Status := Current_Result.Status;
            Context.Pass_Forward.Replace_Element
                (Input (Context.Position .. Current_Result.Finish));
            Context.Empty_Labels.Clear;
        end if;

        Context.Error_Labels.Clear;
        return Component_Success;
    end Ignore;





    procedure Tidy_Context
           (Details     : in out Lexer_Context;
            Number_Comp : in     Ada.Containers.Count_Type) is
    begin
        Details.Pass_Forward.Clear;

        Details.Empty_Labels.Clear;
        Details.Error_Labels.Clear;
        Details.Error_Labels.Reserve_Capacity (Number_Comp);

        Details.Offset := Details.Offset + Details.Position - 1;
        Details.Position := 1;
        Details.Status := Success;
    end Tidy_Context;


    procedure Raise_Lexer_Error
           (Label_List : in Label_Vectors.Vector;
            Position   : in Positive)
    is
        Error_Info_List : Packrat.Errors.Error_Info_Array
           (Label_List.First_Index .. Label_List.Last_Index);
    begin
        for I in Integer range Error_Info_List'First .. Error_Info_List'Last loop
            Error_Info_List (I).Symbol := +Traits.Label_Enum'Image (Label_List.Element (I));
            Error_Info_List (I).Position := Position;
        end loop;
        raise Lexer_Error with Packrat.Errors.Encode_Array (Error_Info_List);
    end Raise_Lexer_Error;


    function Token_Vector_To_Array
           (Input_Vector : in Token_Vectors.Vector)
        return Traits.Tokens.Token_Array
    is
        Result_Array : Traits.Tokens.Token_Array (1 .. Integer (Input_Vector.Length));
    begin
        for I in Integer range 1 .. Integer (Input_Vector.Length) loop
            Result_Array (I) := Input_Vector.Element (I);
        end loop;
        return Result_Array;
    end Token_Vector_To_Array;


    procedure Token_Vector_To_Array
           (Input_Vector : in     Token_Vectors.Vector;
            Padding      : in     Traits.Tokens.Token;
            Output_Array :    out Traits.Tokens.Token_Array) is
    begin
        for N in Integer range 1 .. Output_Array'Length loop
            if N <= Integer (Input_Vector.Length) then
                Output_Array (Output_Array'First + N - 1) := Input_Vector.Element (N);
            else
                Output_Array (Output_Array'First + N - 1) := Padding;
            end if;
        end loop;
    end Token_Vector_To_Array;


    function Slide
           (Input : in Traits.Element_Array)
        return Traits.Element_Array
    is
        subtype Slider is Traits.Element_Array (1 .. Input'Length);
    begin
        return Slider (Input);
    end Slide;


    procedure Internal_Scan_Core
           (Input      : in     Traits.Element_Array;
            Context    : in out Lexer_Context;
            Components : in     Component_Array)
    is
        Raise_Error : Boolean;
    begin
        Raise_Error := True;
        for C of Components loop
            if C (Input, Context) = Component_Success then
                Raise_Error := False;
                exit;
            end if;
        end loop;
        if Raise_Error then
            Raise_Lexer_Error (Context.Error_Labels, Context.Position);
        end if;
    end Internal_Scan_Core;





    function Scan
           (Input   : in     Traits.Element_Array;
            Context : in out Lexer_Context)
        return Traits.Tokens.Token_Array
    is
        Real_Input : Input_Holders.Holder;
    begin
        if not Context.Pass_Forward.Is_Empty then
            Real_Input.Replace_Element (Slide (Context.Pass_Forward.Element) & Input);
        else
            Real_Input.Replace_Element (Input);
        end if;

        Tidy_Context (Context, Components'Length);
        Context.Result_So_Far.Clear;
        Context.Allow_Incomplete := Input'Length > 0;

        while Context.Status = Success and Context.Position <= Real_Input.Element'Length loop
            Internal_Scan_Core (Real_Input.Element, Context, Components);
        end loop;

        return Token_Vector_To_Array (Context.Result_So_Far);
    end Scan;


    function Scan_Only
           (Input   : in     Traits.Element_Array;
            Context : in out Lexer_Context)
        return Traits.Tokens.Token_Array
    is
        Real_Input : Input_Holders.Holder;
    begin
        if not Context.Pass_Forward.Is_Empty then
            Real_Input.Replace_Element (Slide (Context.Pass_Forward.Element) & Input);
        else
            Real_Input.Replace_Element (Input);
        end if;

        Tidy_Context (Context, Components'Length);
        Context.Result_So_Far.Clear;
        Context.Allow_Incomplete := False;

        while Context.Status = Success and Context.Position <= Real_Input.Element'Length loop
            Internal_Scan_Core (Real_Input.Element, Context, Components);
        end loop;

        return Token_Vector_To_Array (Context.Result_So_Far);
    end Scan_Only;


    function Scan_With
           (Input   : in     With_Input;
            Context : in out Lexer_Context)
        return Traits.Tokens.Token_Array
    is
        Real_Input : Input_Holders.Holder;
        Empty_Input : Boolean;
    begin
        Context.Result_So_Far.Clear;
        loop
            Real_Input.Replace_Element (Input.all);
            Empty_Input := Real_Input.Element'Length = 0;
            if not Context.Pass_Forward.Is_Empty then
                Real_Input.Replace_Element
                    (Slide (Context.Pass_Forward.Element) & Real_Input.Element);
            end if;

            Tidy_Context (Context, Components'Length);
            Context.Allow_Incomplete := not Empty_Input;

            while Context.Status = Success and Context.Position <= Real_Input.Element'Length loop
                Internal_Scan_Core (Real_Input.Element, Context, Components);
            end loop;

            if Empty_Input then
                exit;
            end if;
        end loop;
        return Token_Vector_To_Array (Context.Result_So_Far);
    end Scan_With;


    procedure Scan_Set
           (Input   : in     Traits.Element_Array;
            Context : in out Lexer_Context;
            Output  :    out Traits.Tokens.Token_Array)
    is
        Real_Input : Input_Holders.Holder;
    begin
        if not Context.Pass_Forward.Is_Empty then
            Real_Input.Replace_Element (Slide (Context.Pass_Forward.Element) & Input);
        else
            Real_Input.Replace_Element (Input);
        end if;

        Tidy_Context (Context, Components'Length);
        Context.Result_So_Far.Clear;
        Context.Allow_Incomplete := not (Input'Length = 0 or else Input (Input'First) = Pad_In);

        while Context.Status = Success and then
            Integer (Context.Result_So_Far.Length) < Output'Length and then
            Context.Position <= Real_Input.Element'Length and then
            Real_Input.Element (Context.Position) /= Pad_In
        loop
            Internal_Scan_Core (Real_Input.Element, Context, Components);
        end loop;

        if Integer (Context.Result_So_Far.Length) = Output'Length then
            Context.Pass_Forward.Replace_Element
                (Real_Input.Element (Context.Position .. Real_Input.Element'Last));
        end if;
        Token_Vector_To_Array (Context.Result_So_Far, Pad_Out, Output);
    end Scan_Set;


    procedure Scan_Set_With
           (Input   : in     With_Input;
            Context : in out Lexer_Context;
            Output  :    out Traits.Tokens.Token_Array)
    is
        Real_Input : Input_Holders.Holder;
        Empty_Input : Boolean;
    begin
        Context.Result_So_Far.Clear;
        loop
            Real_Input.Replace_Element (Input.all);
            Empty_Input := Real_Input.Element'Length = 0 or else
                Real_Input.Element (Real_Input.Element'First) = Pad_In;
            if not Context.Pass_Forward.Is_Empty then
                Real_Input.Replace_Element
                    (Slide (Context.Pass_Forward.Element) & Real_Input.Element);
            end if;

            Tidy_Context (Context, Components'Length);
            Context.Allow_Incomplete := not Empty_Input;

            while Context.Status = Success and then
                Integer (Context.Result_So_Far.Length) < Output'Length and then
                Context.Position <= Real_Input.Element'Length and then
                Real_Input.Element (Context.Position) /= Pad_In
            loop
                Internal_Scan_Core (Real_Input.Element, Context, Components);
            end loop;

            if Empty_Input then
                exit;
            end if;

            if Integer (Context.Result_So_Far.Length) = Output'Length then
                Context.Pass_Forward.Replace_Element
                    (Real_Input.Element (Context.Position .. Real_Input.Element'Last));
                exit;
            end if;
        end loop;
        Token_Vector_To_Array (Context.Result_So_Far, Pad_Out, Output);
    end Scan_Set_With;





    function Sequence
           (Input : in Traits.Element_Array;
            Start : in Positive)
        return Combinator_Result
    is
        Result : Combinator_Result := (0, Success);
        Position : Positive := Start;
    begin
        if Start > Input'Last then
            return Empty_Fail;
        end if;
        for C of Params loop
            if Position > Input'Last then
                Result.Status := Needs_More;
                exit;
            end if;
            Result := Join (Result, C (Input, Position));
            exit when Result.Status = Failure;
            Position := Result.Finish + 1;
        end loop;
        return Result;
    end Sequence;


    function Count
           (Input : in Traits.Element_Array;
            Start : in Positive)
        return Combinator_Result
    is
        Result : Combinator_Result := (0, Success);
        Position : Positive := Start;
    begin
        if Start > Input'Last then
            return Empty_Fail;
        end if;
        for I in Integer range 1 .. Number loop
            if Position > Input'Last then
                Result.Status := Needs_More;
                exit;
            end if;
            Result := Join (Result, Param (Input, Position));
            exit when Result.Status = Failure;
            Position := Result.Finish + 1;
        end loop;
        return Result;
    end Count;


    function Many
           (Input : in Traits.Element_Array;
            Start : in Positive)
        return Combinator_Result
    is
        Result : Combinator_Result := (0, Success);
        Temp   : Combinator_Result;
        Position : Positive := Start;
        Counter  : Natural := 0;
    begin
        if Start > Input'Last then
            return Empty_Fail;
        end if;
        loop
            exit when Position > Input'Last;
            Temp := Param (Input, Position);
            exit when Temp.Status = Failure or Temp.Status = Needs_More;
            Result := Join (Result, Temp);
            Counter := Counter + 1;
            Position := Result.Finish + 1;
        end loop;
        if Counter < Minimum then
            if Position > Input'Last or Temp.Status = Needs_More then
                Result.Status := Needs_More;
            else
                Result.Status := Failure;
            end if;
        elsif Position > Input'Last or Temp.Status = Needs_More then
            Result.Status := Optional_More;
        else
            Result.Status := Success;
        end if;
        return Result;
    end Many;


    function Many_Until
           (Input : in Traits.Element_Array;
            Start : in Positive)
        return Combinator_Result
    is
        Result : Combinator_Result := (0, Success);
        Temp   : Combinator_Result;
        Position : Positive := Start;
        Counter  : Natural := 0;
    begin
        if Start > Input'Last then
            return Empty_Fail;
        end if;
        loop
            exit when Position > Input'Last;
            Temp := Param (Input, Position);
            exit when Temp.Status = Failure or Temp.Status = Needs_More or Test (Input (Position));
            Result := Join (Result, Temp);
            Counter := Counter + 1;
            Position := Result.Finish + 1;
        end loop;
        if Counter < Minimum then
            if Position > Input'Last or Temp.Status = Needs_More then
                Result.Status := Needs_More;
            else
                Result.Status := Failure;
            end if;
        elsif Position > Input'Last then
            Result.Status := Needs_More;
        elsif Temp.Status = Needs_More and Test (Input (Position)) then
            Result.Status := Optional_More;
        elsif Test (Input (Position)) then
            Result.Status := Success;
        else
            Result.Status := Failure;
        end if;
        return Result;
    end Many_Until;





    function Satisfy
           (Input : in Traits.Element_Array;
            Start : in Positive)
        return Combinator_Result is
    begin
        if Start > Input'Last then
            return Empty_Fail;
        elsif Test (Input (Start)) then
            return (Start, Success);
        else
            return Empty_Fail;
        end if;
    end Satisfy;


    function Satisfy_With
           (Input : in Traits.Element_Array;
            Start : in Positive)
        return Combinator_Result is
    begin
        if Start > Input'Last then
            return Empty_Fail;
        elsif Test (Change (Input (Start))) then
            return (Start, Success);
        else
            return Empty_Fail;
        end if;
    end Satisfy_With;


    function Match
           (Input : in Traits.Element_Array;
            Start : in Positive)
        return Combinator_Result is
    begin
        if Start > Input'Last then
            return Empty_Fail;
        elsif Input (Start) = Item then
            return (Start, Success);
        else
            return Empty_Fail;
        end if;
    end Match;


    function Match_With
           (Input : in Traits.Element_Array;
            Start : in Positive)
        return Combinator_Result is
    begin
        if Start > Input'Last then
            return Empty_Fail;
        elsif Change (Input (Start)) = Item then
            return (Start, Success);
        else
            return Empty_Fail;
        end if;
    end Match_With;


    function Multimatch
           (Input : in Traits.Element_Array;
            Start : in Positive)
        return Combinator_Result
    is
        Current_Offset : Natural := 0;
    begin
        if Items'Length = 0 then
            return (0, Success);
        end if;

        if Input'Last - Start + 1 <= 0 then
            return Empty_Fail;
        end if;

        while Input (Start + Current_Offset) = Items (Items'First + Current_Offset) loop
            if Items'First + Current_Offset = Items'Last then
                return (Start + Current_Offset, Success);
            elsif Start + Current_Offset = Input'Last then
                return (Start + Current_Offset, Needs_More);
            end if;
            Current_Offset := Current_Offset + 1;
        end loop;
        if Current_Offset = 0 then
            return Empty_Fail;
        else
            return (Start + Current_Offset - 1, Failure);
        end if;
    end Multimatch;


    function Take
           (Input : in Traits.Element_Array;
            Start : in Positive)
        return Combinator_Result is
    begin
        if Start > Input'Last then
            return Empty_Fail;
        elsif Start + Number - 1 > Input'Last then
            return (Input'Last, Needs_More);
        else
            return (Start + Number - 1, Success);
        end if;
    end Take;


    function Take_While
           (Input : in Traits.Element_Array;
            Start : in Positive)
        return Combinator_Result
    is
        Finish : Positive := Start;
        Status : Result_Status;
    begin
        if Start > Input'Last or else not Test (Input (Start)) then
            return Empty_Fail;
        end if;
        while Finish <= Input'Last and then Test (Input (Finish)) loop
            Finish := Finish + 1;
        end loop;
        if Finish > Input'Last then
            Status := Optional_More;
        else
            Status := Success;
        end if;
        return (Finish - 1, Status);
    end Take_While;


    function Take_Until
           (Input : in Traits.Element_Array;
            Start : in Positive)
        return Combinator_Result
    is
        Finish : Positive := Start;
        Status : Result_Status;
    begin
        if Start > Input'Last or else Test (Input (Start)) then
            return Empty_Fail;
        end if;
        while Finish <= Input'Last and then not Test (Input (Finish)) loop
            Finish := Finish + 1;
        end loop;
        if Finish > Input'Last then
            Status := Optional_More;
        else
            Status := Success;
        end if;
        return (Finish - 1, Status);
    end Take_Until;





    function Line_End
           (Input : in Traits.Element_Array;
            Start : in Positive)
        return Combinator_Result is
    begin
        if Start > Input'Last then
            return Empty_Fail;
        elsif Input (Start) = EOL_Item then
            return (Start, Success);
        else
            return Empty_Fail;
        end if;
    end Line_End;


    function Input_End
           (Input : in Traits.Element_Array;
            Start : in Positive)
        return Combinator_Result is
    begin
        if Start > Input'Last then
            return Empty_Fail;
        elsif Input (Start) = EOF_Item then
            return (Start, Success);
        else
            return Empty_Fail;
        end if;
    end Input_End;


end Packrat.Lexers;