summaryrefslogtreecommitdiff
path: root/src/packrat.ads
blob: d1fc9a06993938f69a8ec40cf81414f186b7b6de (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


with

    Ada.Strings.Unbounded,
    Ada.Finalization;


package Packrat is


    type Result_Status is (Failure, Needs_More, Optional_More, Success);


    Parser_Error : exception;
    Lexer_Error  : exception;




    package Errors is


        subtype Error_Message is String
            with Dynamic_Predicate => Valid_Message (Error_Message);

        type Error_Info is record
            Symbol   : Ada.Strings.Unbounded.Unbounded_String;
            Position : Natural;
        end record;

        type Error_Info_Array is array (Positive range <>) of Error_Info;


        --  Note: No consideration is given to ordering of Error_Info items
        --        encoded into an Error_Message string.

        --  Note: Using "&" to join two Valid Error_Messages together
        --        will result in an Error_Message that is also Valid,
        --        but for best results Join should be used instead to
        --        prevent duplication of Error_Info in the message.


        function Valid_Identifier
               (Check : in String)
            return Boolean;

        function Valid_Identifier
               (Check : in Ada.Strings.Unbounded.Unbounded_String)
            return Boolean;

        function Valid_Identifier_Array
               (Check : in Error_Info_Array)
            return Boolean;

        function Valid_Message
               (Check : in String)
            return Boolean;

        function Debug_String
               (This : in Error_Message)
            return String;


        function Join
               (Left, Right : in Error_Message)
            return Error_Message;


        function Encode
               (Name : in String;
                Pos  : in Natural)
            return Error_Message
            with Pre => Valid_Identifier (Name);

        function Encode
               (Name : in Ada.Strings.Unbounded.Unbounded_String;
                Pos  : in Natural)
            return Error_Message
            with Pre => Valid_Identifier (Name);

        function Encode
               (Info : in Error_Info)
            return Error_Message
            with Pre => Valid_Identifier (Info.Symbol);

        function Encode_Array
               (Info : in Error_Info_Array)
            return Error_Message
            with Pre => Valid_Identifier_Array (Info);

        function Decode
               (Msg : in Error_Message)
            return Error_Info_Array;


    end Errors;




    generic
        type Label_Enum is (<>);
        type Element is private;
        type Element_Array is array (Positive range <>) of Element;
    package Tokens is


        type Token is new Ada.Finalization.Controlled with private;
        type Token_Array is array (Positive range <>) of Token;


        function Create
               (Ident  : in Label_Enum;
                Start  : in Positive;
                Finish : in Natural;
                Value  : in Element_Array)
            return Token;

        function "="
               (Left, Right : in Token)
            return Boolean;


        --  Note: The Start and Finish indices indicate where the token was found
        --        in whatever array it was lexed from. The Value does *not* have
        --        to correspond with whatever is found in the Start .. Finish range.


        function Initialized
               (This : in Token)
            return Boolean;

        function Debug_String
               (This : in Token)
            return String;


        function Label
               (This : in Token)
            return Label_Enum
            with Pre => Initialized (This);

        function Start
               (This : in Token)
            return Positive;

        function Finish
               (This : in Token)
            return Natural;

        function Value
               (This : in Token)
            return Element_Array
            with Pre => Initialized (This);


    private


        type Element_Array_Access is access Element_Array;


        type Token is new Ada.Finalization.Controlled with record
            Identifier  : Label_Enum;
            Start_At    : Positive;
            Finish_At   : Natural;
            Token_Value : Element_Array_Access;
        end record;


        overriding procedure Initialize
               (This : in out Token);

        overriding procedure Adjust
               (This : in out Token);

        overriding procedure Finalize
               (This : in out Token);


    end Tokens;




    generic
        type Label_Enum is (<>);
        type Element is private;
        type Element_Array is array (Positive range <>) of Element;
    package Interfaces is


        type Node is interface;


        function Leaf
               (New_Item : in Element_Array;
                Start    : in Positive;
                Finish   : in Natural)
            return Node is abstract
        with Pre'Class =>
                Finish + 1 >= Start,
            Post'Class =>
                Is_Leaf (Leaf'Result);

        function Branch
               (Label  : in Label_Enum;
                Start  : in Positive;
                Finish : in Natural)
            return Node is abstract
        with Pre'Class =>
                Finish + 1 >= Start,
            Post'Class =>
                Is_Branch (Branch'Result);


        function Is_Leaf
               (This : in Node)
            return Boolean is abstract;

        function Is_Branch
               (This : in Node)
            return Boolean is abstract;


        function Label
               (This : in Node)
            return Label_Enum is abstract
        with Pre'Class =>
                This.Is_Branch;

        function Elements
               (This : in Node)
            return Element_Array is abstract
        with Pre'Class =>
                This.Is_Leaf;

        function Start
               (This : in Node)
            return Positive is abstract;

        function Finish
               (This : in Node)
            return Natural is abstract;




        type Cursor is interface;


        function Is_Nothing
               (Position : in Cursor)
            return Boolean is abstract;


        function Depth
               (Position : in Cursor)
            return Natural is abstract
        with Pre'Class =>
                not Position.Is_Nothing;

        function Is_Node
               (Position : in Cursor)
            return Boolean is abstract
        with Post'Class =>
               (if Is_Node'Result then not Position.Is_Nothing);

        function Is_Root
               (Position : in Cursor)
            return Boolean is abstract
        with Post'Class =>
               (if Is_Root'Result then
                not Position.Is_Nothing and
                Position.Parent.Is_Nothing and
                Position.Depth = 0);

        function Is_Branch
               (Position : in Cursor)
            return Boolean is abstract
        with Post'Class =>
               (if Is_Branch'Result then not Position.Is_Nothing);

        function Is_Leaf
               (Position : in Cursor)
            return Boolean is abstract
        with Post'Class =>
               (if Is_Leaf'Result then not Position.Is_Nothing);


        function Label
               (Position : in Cursor)
            return Label_Enum is abstract
        with Pre'Class =>
                Position.Is_Branch;

        function Elements
               (Position : in Cursor)
            return Element_Array is abstract
        with Pre'Class =>
                Position.Is_Leaf;

        function Start
               (Position : in Cursor)
            return Positive is abstract
        with Pre'Class =>
                not Position.Is_Nothing;

        function Finish
               (Position : in Cursor)
            return Natural is abstract
        with Pre'Class =>
                not Position.Is_Nothing;

        function Choices
               (Position : in Cursor)
            return Natural is abstract;


        function Parent
               (Position : in Cursor)
            return Cursor is abstract;

        function Child_Count
               (Position : in Cursor;
                Choice   : in Positive)
            return Natural is abstract
        with Pre'Class =>
                Choice <= Position.Choices;

        function Child_Count
               (Position : in Cursor)
            return Natural is abstract;

        function All_Child_Count
               (Position : in Cursor)
            return Natural is abstract;

        function First_Child
               (Position : in Cursor;
                Choice   : in Positive)
            return Cursor is abstract
        with Pre'Class =>
                Choice <= Position.Choices,
            Post'Class =>
                First_Child'Result.Is_Nothing or
                First_Child'Result.Parent = Position;

        function Last_Child
               (Position : in Cursor;
                Choice   : in Positive)
            return Cursor is abstract
        with Pre'Class =>
                Choice <= Position.Choices,
            Post'Class =>
                Last_Child'Result.Is_Nothing or
                Last_Child'Result.Parent = Position;

        function First_Child
               (Position : in Cursor)
            return Cursor is abstract
        with Post'Class =>
                First_Child'Result.Is_Nothing or
                First_Child'Result.Parent = Position;

        function Last_Child
               (Position : in Cursor)
            return Cursor is abstract
        with Post'Class =>
                Last_Child'Result.Is_Nothing or
                Last_Child'Result.Parent = Position;

        function Next_Sibling
               (Position : in Cursor)
            return Cursor is abstract
        with Post'Class =>
                Next_Sibling'Result.Is_Nothing or
                Next_Sibling'Result.Parent = Position.Parent;

        function Prev_Sibling
               (Position : in Cursor)
            return Cursor is abstract
        with Post'Class =>
                Prev_Sibling'Result.Is_Nothing or
                Prev_Sibling'Result.Parent = Position.Parent;

        procedure Delete_Children
               (Position : in out Cursor;
                Choice   : in     Positive) is abstract
        with Pre'Class =>
                Choice <= Position.Choices,
            Post'Class =>
                Position.Child_Count (Choice) = 0;

        procedure Delete_Children
               (Position : in out Cursor) is abstract
        with Post'Class =>
                Position.Child_Count = 0;

        procedure Delete_All_Children
               (Position : in out Cursor) is abstract
        with Post'Class =>
                Position.All_Child_Count = 0;


        function Equal_Subgraph
               (Left, Right : in Cursor)
            return Boolean is abstract;

        function Subgraph_Node_Count
               (Position : in Cursor)
            return Natural is abstract;

        function Find_In_Subgraph
               (Position : in Cursor;
                Item     : in Element_Array)
            return Cursor is abstract
        with Post'Class =>
                Find_In_Subgraph'Result.Is_Nothing or
                Find_In_Subgraph'Result.Is_Leaf;




        type Graph is interface;


        function Contains
               (Container : in Graph;
                Position  : in Cursor'Class)
            return Boolean is abstract
        with Post'Class =>
               (if Contains'Result then not Position.Is_Nothing);


        function Singleton
               (Input : in Node'Class)
            return Graph is abstract
        with Post'Class =>
                Singleton'Result.Node_Count = 1;


        function Is_Empty
               (Container : in Graph)
            return Boolean is abstract
        with Post'Class =>
                (if Is_Empty'Result then Container.Node_Count = 0 else Container.Node_Count /= 0);

        function Is_Ambiguous
               (Container : in Graph)
            return Boolean is abstract;

        function Node_Count
               (Container : in Graph)
            return Natural is abstract;


        function Root_Count
               (Container : in Graph)
            return Natural is abstract
        with Post'Class =>
                (if Container.Is_Empty then Root_Count'Result = 0 else Root_Count'Result > 0);

        function Root
               (Container : in Graph;
                Index     : in Positive)
            return Cursor'Class is abstract
        with Pre'Class =>
                Index <= Container.Root_Count;


        procedure Append
               (Container : in out Graph;
                Addition  : in     Graph) is abstract
        with Pre'Class =>
                Container.Is_Empty or else Addition.Is_Empty or else
                Container.Root (Container.Root_Count).Finish <
                    Addition.Root (1).Start;

        procedure Prepend
               (Container : in out Graph;
                Addition  : in     Graph) is abstract
        with Pre'Class =>
                Container.Is_Empty or else Addition.Is_Empty or else
                Container.Root (1).Start >
                    Addition.Root (Addition.Root_Count).Finish;

        procedure Attach_Choice
               (Container : in out Graph;
                Position  : in     Cursor'Class;
                Addition  : in     Graph) is abstract
        with Pre'Class =>
                Container.Contains (Position) and Position.Is_Branch and
                (Addition.Is_Empty or else
                   (Position.Start <= Addition.Root (1).Start and
                    Position.Finish >= Addition.Root (Addition.Root_Count).Finish));


        procedure Clear
               (Container : in out Graph) is abstract
        with Post'Class =>
                Container.Is_Empty;

        procedure Delete_Position
               (Container : in out Graph;
                Position  : in out Cursor'Class) is abstract
        with Pre'Class =>
                Container.Contains (Position),
            Post'Class =>
                not Container.Contains (Position);


        function Find
               (Container : in Graph;
                Item      : in Element_Array)
            return Cursor'Class is abstract
        with Post'Class =>
                Find'Result.Is_Leaf or
                Find'Result.Is_Nothing;


    end Interfaces;




private


    function "+"
           (S : in String)
        return Ada.Strings.Unbounded.Unbounded_String
        renames Ada.Strings.Unbounded.To_Unbounded_String;

    function "-"
           (US : in Ada.Strings.Unbounded.Unbounded_String)
        return String
        renames Ada.Strings.Unbounded.To_String;


end Packrat;