This file is indexed.

/usr/share/ada/adainclude/opentoken/opentoken-production-parser-lalr-parser_lists.adb is in libopentoken5-dev 6.0b-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
--  Abstract :
--
--  see spec
--
--  Copyright (C) 2014-2015  All Rights Reserved.
--
--  This program is free software; you can redistribute it and/or
--  modify it under terms of the GNU General Public License as
--  published by the Free Software Foundation; either version 3, or (at
--  your option) any later version. This program is distributed in the
--  hope that it will be useful, but WITHOUT ANY WARRANTY; without even
--  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
--  PURPOSE. See the GNU General Public License for more details. You
--  should have received a copy of the GNU General Public License
--  distributed with this program; see file COPYING. If not, write to
--  the Free Software Foundation, 51 Franklin Street, Suite 500, Boston,
--  MA 02110-1335, USA.

pragma License (GPL);

with Ada.Characters.Handling;
with Ada.Text_IO;
package body OpenToken.Production.Parser.LALR.Parser_Lists is

   function Initialize return List
   is begin
      return
        (Parser_Label       => First_Parser_Label,
         Head               => new Parser_Node'
           (Item            =>
              (Label        => First_Parser_Label,
               Verb         => Parse_Action_Verbs'First,
               Stack        => new Stack_Node'
                 (Item      =>
                    (State  => State_Index'First,
                     Token  => null),
                  Next      => null),
               Action_Token => (null, null)),
            Next            => null,
            Prev            => null),
         Parser_Free        => null,
         Stack_Free         => null,
         Action_Token_Free  => null,
         Count              => 1);

   end Initialize;

   function Count (List : in Parser_Lists.List) return Integer
   is begin
      return List.Count;
   end Count;

   function First (List : aliased in out Parser_Lists.List'Class) return Cursor
   is begin
      --  WORKAROUND: with 'Access, Debian gnat 4.9.2 reports
      --  "non-local pointer cannot point to local object", even
      --  though GNAT Pro 7.3.1 and GNAT GPL 2014 allow 'Access There
      --  doesn't seem to be a way to use a legitimate access param
      --  while still meeting the Iterator requirements.
      return (List'Unchecked_Access, Ptr => List.Head);
   end First;

   procedure Next (Cursor : in out Parser_Lists.Cursor)
   is begin
      if Cursor.Ptr /= null then
         Cursor.Ptr := Cursor.Ptr.Next;
      end if;
   end Next;

   function Is_Done (Cursor : in Parser_Lists.Cursor) return Boolean
   is begin
      return Cursor.Ptr = null;
   end Is_Done;

   function Active_Parser_Count (Cursor : in Parser_Lists.Cursor) return Integer
   is begin
      return Cursor.List.Count;
   end Active_Parser_Count;

   function Label (Cursor : in Parser_Lists.Cursor) return Integer
   is begin
      return Cursor.Ptr.Item.Label;
   end Label;

   procedure Set_Verb (Cursor : in Parser_Lists.Cursor; Verb : in Parse_Action_Verbs)
   is begin
      Cursor.Ptr.Item.Verb := Verb;
   end Set_Verb;

   function Verb (Cursor : in Parser_Lists.Cursor) return Parse_Action_Verbs
   is begin
      return Cursor.Ptr.Item.Verb;
   end Verb;

   function Stack_Empty (Cursor : in Parser_Lists.Cursor) return Boolean
   is begin
      return Cursor.Ptr.Item.Stack = null;
   end Stack_Empty;

   function Peek (Cursor : in Parser_Lists.Cursor) return Stack_Item
   is begin
      return Cursor.Ptr.Item.Stack.Item;
   end Peek;

   procedure Free (List : in out Parser_Lists.List; Stack : in out Stack_Node_Access)
   is
      Temp_Free : constant Stack_Node_Access := List.Stack_Free;
   begin
      List.Stack_Free := Stack;
      Stack           := Stack.Next; -- not null; for free (cursor) below

      List.Stack_Free.all :=
        (Item     =>
           (State => Unknown_State,
            Token => null), -- Token is free'd after being passed to user action. FIXME: say what?
         Next     => Temp_Free);
   end Free;

   function Pop (Cursor : in Parser_Lists.Cursor) return Stack_Item
   is
      Result : constant Stack_Item := Cursor.Ptr.Item.Stack.Item;
   begin
      Free (Cursor.List.all, Cursor.Ptr.Item.Stack);

      return Result;
   end Pop;

   procedure Push (Cursor : in Parser_Lists.Cursor; Item : in Stack_Item)
   is
      Temp : constant Stack_Node_Access := Cursor.List.Stack_Free;
   begin
      if Temp = null then
         Cursor.Ptr.Item.Stack := new Stack_Node'(Item, Cursor.Ptr.Item.Stack);
      else
         Cursor.List.Stack_Free := Cursor.List.Stack_Free.Next;
         Temp.all               := (Item, Cursor.Ptr.Item.Stack);
         Cursor.Ptr.Item.Stack  := Temp;
      end if;
   end Push;

   function Stack_Equal (Cursor_1, Cursor_2 : in Parser_Lists.Cursor) return Boolean
   is
      use type Token.Handle;
      use type Token.Token_ID;

      Stack_1 : Stack_Node_Access := Cursor_1.Ptr.Item.Stack;
      Stack_2 : Stack_Node_Access := Cursor_2.Ptr.Item.Stack;
   begin
      loop
         exit when Stack_1 = null or Stack_2 = null;
         if not
           (Stack_1.Item.State = Stack_2.Item.State and
              ((Stack_1.Item.Token = null or Stack_2.Item.Token = null) or else
                 Stack_1.Item.Token.ID = Stack_2.Item.Token.ID))
         then
            return False;
         end if;
         Stack_1 := Stack_1.Next;
         Stack_2 := Stack_2.Next;
      end loop;
      return Stack_1 = null and Stack_2 = null;
   end Stack_Equal;

   procedure Put_Top_10 (Cursor : in Parser_Lists.Cursor)
   is
      use Ada.Text_IO;
      use type Token.Handle;
      Stack_I : Stack_Node_Access := Cursor.Ptr.Item.Stack;
   begin
      Put (Integer'Image (Cursor.Ptr.Item.Label) & " stack: ");
      for I in 1 .. 10 loop
         exit when Stack_I = null;
         Put
           (State_Index'Image (Stack_I.Item.State) & " : " &
              (if Stack_I.Item.Token = null then ""
               else Stack_I.Item.Token.Image) &
              ", ");
         Stack_I := Stack_I.Next;
      end loop;
      New_Line;
   end Put_Top_10;

   function Action_Token_Count (Cursor : in Parser_Lists.Cursor) return Integer
   is
      Action_Token : Action_Token_Node_Access := Cursor.Ptr.Item.Action_Token.Head;
      Result       : Integer                  := 0;
   begin
      loop
         exit when Action_Token = null;
         Result := Result + 1;
         Action_Token := Action_Token.Next;
      end loop;
      return Result;
   end Action_Token_Count;

   procedure Enqueue
     (List              : in out Action_Token_List;
      Action_Token_Free : in out Action_Token_Node_Access;
      Action_Token      : in     Parser_Lists.Action_Token)
   is
      Temp : constant Action_Token_Node_Access := Action_Token_Free;

      Node : constant Action_Token_Node := (Action_Token, null, List.Tail);
   begin
      if Temp = null then
         if List.Tail = null then
            List.Tail := new Action_Token_Node'(Node);
            List.Head := List.Tail;
         else
            List.Tail := new Action_Token_Node'(Node);
         end if;
      else
         Action_Token_Free := Action_Token_Free.Next;
         Temp.all          := Node;
         if List.Tail = null then
            List.Tail := Temp;
            List.Head := List.Tail;
         else
            List.Tail := Temp;
         end if;
      end if;

      if List.Tail.Prev /= null then
         List.Tail.Prev.Next := List.Tail;
      end if;
   end Enqueue;

   procedure Enqueue
     (Cursor       : in Parser_Lists.Cursor;
      Action_Token : in Parser_Lists.Action_Token)
   is begin
      Enqueue (Cursor.Ptr.Item.Action_Token, Cursor.List.Action_Token_Free, Action_Token);
   end Enqueue;

   procedure Free (List : in out Parser_Lists.List; Action_Token : in out Action_Token_Node_Access)
   is
      Temp_Free : constant Action_Token_Node_Access := List.Action_Token_Free;
   begin
      List.Action_Token_Free := Action_Token;
      Action_Token           := Action_Token.Next; -- not null; for free (cursor) below

      List.Action_Token_Free.all :=
        (Item         => Null_Action_Token, -- New_Token, Tokens are free'd after being passed to user action
         Next         => Temp_Free,
         Prev         => null);
   end Free;

   function Dequeue (Cursor : in Parser_Lists.Cursor) return Action_Token
   is
      Result : constant Action_Token := Cursor.Ptr.Item.Action_Token.Head.Item;
   begin
      Free (Cursor.List.all, Cursor.Ptr.Item.Action_Token.Head);

      if Cursor.Ptr.Item.Action_Token.Head = null then
         Cursor.Ptr.Item.Action_Token.Tail := null;
      end if;

      return Result;
   end Dequeue;

   function Action_Tokens_Empty (Cursor : in Parser_Lists.Cursor) return Boolean
   is begin
      return Cursor.Ptr.Item.Action_Token.Head = null;
   end Action_Tokens_Empty;

   procedure Deep_Copy
     (Stack             : in     Stack_Node_Access;
      Stack_Free        : in out Stack_Node_Access;
      Action_Token      : in     Action_Token_List;
      Action_Token_Free : in out Action_Token_Node_Access;
      New_Stack         :    out Stack_Node_Access;
      New_Action_Tokens :    out Action_Token_List)
   is
      use Token_List;
      use type Token.Handle;

      --  All Action_Token.New_Token must point either to a token on
      --  Stack or to tokens in later Action_Token.Tokens; preserve
      --  that in the new copy.
      --
      --  1) Create a map of old action.new_token => new action.new_tokens
      --  2) Create new Stack, using new action.New_Token pointers or new tokens
      --  3) Create new Action, using new action.New_Token pointers or new tokens

      type New_Token_Item is record
         Old_Pointer : Token.Handle;
         New_Pointer : Token.Handle;
      end record;

      New_Token_Items : array (Integer range 1 .. Count (Action_Token)) of New_Token_Item;

      J          : Action_Token_Node_Access := Action_Token.Head;
      Action_Pos : Integer := New_Token_Items'Last;
      Iter       : List_Iterator;
      New_Tokens : Token_List.Instance;
      New_Token  : Token.Handle;

      I    : Stack_Node_Access := Stack;
      Copy : Stack_Node_Access;
      Temp : Stack_Node_Access;

      New_Stack_Item   : Stack_Item;
      Stack_Item_Found : Boolean;
   begin

      for K in New_Token_Items'Range loop
         New_Token_Items (K).Old_Pointer := Token.Handle (J.Item.New_Token);
         New_Token_Items (K).New_Pointer := Token.Copy (New_Token_Items (K).Old_Pointer);

         J := J.Next;
      end loop;

      --  Create a copy of Stack in Copy, in reverse order, using
      --  New_Token_Items. New_Token_Items appear on the stack in
      --  reverse order; not all are on stack (some are in later
      --  actions).
      loop
         exit when I = null;

         Stack_Item_Found := False;
         for K in reverse New_Token_Items'First .. Action_Pos loop
            if I.Item.Token = New_Token_Items (K).Old_Pointer then
               New_Stack_Item   := (I.Item.State, New_Token_Items (K).New_Pointer);
               Stack_Item_Found := True;
               Action_Pos       := K - 1;
            end if;
         end loop;
         if not Stack_Item_Found then
            New_Stack_Item := (I.Item.State, Token.Copy (I.Item.Token));
         end if;

         if Stack_Free = null then
            Copy := new Stack_Node'(New_Stack_Item, Copy);
         else
            Temp       := Copy;
            Copy       := Stack_Free;
            Stack_Free := Stack_Free.Next;
            Copy.all   := (New_Stack_Item, Temp);
         end if;
         I := I.Next;
      end loop;

      --  Move to New_Stack, in correct order.
      I         := Copy;
      New_Stack := null;

      loop
         exit when I = null;
         Temp      := I.Next;
         I.Next    := New_Stack;
         New_Stack := I;
         I         := Temp;
      end loop;

      --  Copy Action_Tokens, using New_Token_Items.New_Token from previous actions
      J          := Action_Token.Head;
      Action_Pos := 1;
      loop
         exit when J = null;

         Iter       := Initial_Iterator (J.Item.Tokens);
         New_Tokens := Null_List;
         loop
            exit when Iter = Null_Iterator;

            New_Token := null;
            Find_New_Token :
            for K in New_Token_Items'First .. Action_Pos - 1 loop
               if Token_Handle (Iter) = New_Token_Items (K).Old_Pointer then
                  New_Token := New_Token_Items (K).New_Pointer;
                  exit Find_New_Token;
               end if;
            end loop Find_New_Token;

            if New_Token = null then
               Append (New_Tokens, Token.Copy (Token_Handle (Iter)));
            else
               Append (New_Tokens, New_Token);
            end if;

            Next (Iter);
         end loop;

         Enqueue
           (New_Action_Tokens,
            Action_Token_Free,
            (J.Item.Action, Nonterminal.Handle (New_Token_Items (Action_Pos).New_Pointer), New_Tokens));

         J          := J.Next;
         Action_Pos := Action_Pos + 1;
      end loop;
   end Deep_Copy;

   procedure Prepend_Copy (List : in out Parser_Lists.List; Cursor : in Parser_Lists.Cursor'Class)
   is
      Temp : constant Parser_Node_Access := List.Parser_Free;

      New_Stack        : Stack_Node_Access;
      New_Action_Token : Action_Token_List;

      New_Parser : Parser_Node;
   begin
      Deep_Copy
        (Cursor.Ptr.Item.Stack, List.Stack_Free, Cursor.Ptr.Item.Action_Token, List.Action_Token_Free,
         New_Stack, New_Action_Token);

      New_Parser :=
        (Item =>
           (List.Parser_Label + 1,
            Cursor.Ptr.Item.Verb,
            New_Stack,
            New_Action_Token),
         Next => List.Head,
         Prev => null);

      List.Parser_Label := List.Parser_Label + 1;
      List.Count        := List.Count + 1;

      if Temp = null then
         List.Head := new Parser_Node'(New_Parser);

         List.Head.Next.Prev := List.Head;
      else
         List.Parser_Free    := List.Parser_Free.Next;
         Temp.all            := New_Parser;
         List.Head           := Temp;
         List.Head.Next.Prev := List.Head;
      end if;
   end Prepend_Copy;

   procedure Free (Cursor : in out Parser_Lists.Cursor'Class)
   is
      Temp_Free    : constant Parser_Node_Access := Cursor.List.Parser_Free;
      Stack        : Stack_Node_Access           := Cursor.Ptr.Item.Stack;
      Action_Token : Action_Token_Node_Access    := Cursor.Ptr.Item.Action_Token.Head;
   begin
      Cursor.List.Count := Cursor.List.Count - 1;

      if Cursor.List.Head = Cursor.Ptr then
         Cursor.List.Head := Cursor.Ptr.Next;
      end if;

      if Cursor.Ptr.Prev /= null then
         Cursor.Ptr.Prev.Next := Cursor.Ptr.Next;
      end if;

      if Cursor.Ptr.Next /= null then
         Cursor.Ptr.Next.Prev := Cursor.Ptr.Prev;
      end if;

      Cursor.List.Parser_Free := Cursor.Ptr;
      Cursor.Ptr              := Cursor.Ptr.Next;

      Cursor.List.Parser_Free.Next := Temp_Free;
      Cursor.List.Parser_Free.Prev := null;

      loop
         exit when Stack = null;
         Free (Cursor.List.all, Stack);
      end loop;

      loop
         exit when Action_Token = null;
         Free (Cursor.List.all, Action_Token);
      end loop;
   end Free;

   ----------
   --  stuff for iterators

   function To_Cursor
     (List : aliased in out Parser_Lists.List'Class;
      Ptr  :         in     Parser_Node_Access)
     return Cursor
   is begin
      --  see WORKAROUND in First
      return (List'Unchecked_Access, Ptr);
   end To_Cursor;

   function Constant_Reference
     (Container : aliased in List'Class;
      Position  : in Parser_Node_Access)
     return Constant_Reference_Type
   is
      pragma Unreferenced (Container);
   begin
      return (Element => Position.Item'Access);
   end Constant_Reference;

   type List_Access_Constant is access constant List;

   type Iterator is new Iterator_Interfaces.Forward_Iterator with record
      Container : List_Access_Constant;
   end record;

   overriding function First (Object : Iterator) return Parser_Node_Access;
   overriding function Next
     (Object   : Iterator;
      Position : Parser_Node_Access)
     return Parser_Node_Access;

   overriding function First (Object : Iterator) return Parser_Node_Access
   is begin
      return Object.Container.Head;
   end First;

   overriding function Next
     (Object   : Iterator;
      Position : Parser_Node_Access)
     return Parser_Node_Access
   is
      pragma Unreferenced (Object);
   begin
      if Position = null then
         return null;
      else
         return Position.Next;
      end if;
   end Next;

   function Has_Element (Cursor : in Parser_Node_Access) return Boolean
   is begin
      return Cursor /= null;
   end Has_Element;

   function Verb (Cursor : in Parser_Node_Access) return Parse_Action_Verbs
   is begin
      return Cursor.Item.Verb;
   end Verb;

   function Iterate (Container : aliased List) return Iterator_Interfaces.Forward_Iterator'Class
   is begin
      --  see WORKAROUND in First
      return Iterator'(Container => Container'Unchecked_Access);
   end Iterate;

   function Count (Action_Token : in Action_Token_List) return Integer
   is
      Result : Integer := 0;
      I : Action_Token_Node_Access := Action_Token.Head;
   begin
      loop
         exit when I = null;
         Result := Result + 1;
         I      := I.Next;
      end loop;
      return Result;
   end Count;

   ----------
   --  For unit tests

   function Parser_Free_Count (List : in Parser_Lists.List) return Integer
   is
      Result : Integer := 0;
      Node   : Parser_Node_Access := List.Parser_Free;
   begin
      loop
         exit when Node = null;
         Result := Result + 1;
         Node   := Node.Next;
      end loop;
      return Result;
   end Parser_Free_Count;

   function Stack_Free_Count (List : in Parser_Lists.List) return Integer
   is
      Result : Integer := 0;
      Node   : Stack_Node_Access := List.Stack_Free;
   begin
      loop
         exit when Node = null;
         Result := Result + 1;
         Node   := Node.Next;
      end loop;
      return Result;
   end Stack_Free_Count;

   function Action_Token_Free_Count (List : in Parser_Lists.List) return Integer
   is
      Result : Integer := 0;
      Node   : Action_Token_Node_Access := List.Action_Token_Free;
   begin
      loop
         exit when Node = null;
         Result := Result + 1;
         Node   := Node.Next;
      end loop;
      return Result;
   end Action_Token_Free_Count;

   function Is_In (Item : in Nonterminal.Handle; Stack : in Stack_Node_Access) return Boolean
   is
      use type Token.Handle;
      Stack_Node : Stack_Node_Access := Stack;
   begin
      loop
         exit when Stack_Node = null;
         if Stack_Node.Item.Token = Token.Handle (Item) then
            return True;
         end if;
         Stack_Node := Stack_Node.Next;
      end loop;
      return False;
   end Is_In;

   function Is_In (Item : in Nonterminal.Handle; Tokens : Token_List.Instance) return Boolean
   is
      use type Token.Handle;
      use Token_List;
      Iter : List_Iterator := Initial_Iterator (Tokens);
   begin
      loop
         exit when Iter = Null_Iterator;

         if Token.Handle (Item) = Token_Handle (Iter) then
            return True;
         end if;
         Next (Iter);
      end loop;
      return False;
   end Is_In;

   function Is_In_Later_Tokens
     (Token        : in Nonterminal.Handle;
      Action_Token : in Action_Token_Node_Access)
     return Boolean
   is
      Iter : Action_Token_Node_Access := Action_Token;
   begin
      loop
         exit when Iter = null;
         if Is_In (Token, Iter.Item.Tokens) then
            return True;
         end if;
         Iter := Iter.Next;
      end loop;
      return False;
   end Is_In_Later_Tokens;

   function Is_In_Prev_New_Token
     (Item         : in Token.Handle;
      Action_Token : in Action_Token_Node_Access)
     return Boolean
   is
      use type Token.Handle;
      Iter : Action_Token_Node_Access := Action_Token;
   begin
      loop
         exit when Iter = null;
         if Item = Token.Handle (Iter.Item.New_Token) then
            return True;
         end if;
         Iter := Iter.Prev;
      end loop;
      return False;
   end Is_In_Prev_New_Token;

   procedure Check_Are_In_Prev_New_Token
     (Label        : in String;
      Tokens       : in Token_List.Instance;
      Action_Token : in Action_Token_Node_Access)
   is
      use Token_List;
      Iter : List_Iterator := Initial_Iterator (Tokens);
   begin
      loop
         exit when Iter = Null_Iterator;
         if Token_Handle (Iter).all in Nonterminal.Instance and then
           (not Is_In_Prev_New_Token (Token_Handle (Iter), Action_Token.Prev))
         then
            raise Programmer_Error with Label &
              " - action token " & Token_Handle (Iter).Image & " not in prev actions tokens";
         end if;
         Next (Iter);
      end loop;
   end Check_Are_In_Prev_New_Token;

   procedure Check_Action_Stack
     (Label  : in String;
      Cursor : in Parser_Lists.Cursor)
   is
      use type Token.Handle;
      Stack        : Stack_Node_Access        := Cursor.Ptr.Item.Stack;
      Action_Token : Action_Token_Node_Access := Cursor.Ptr.Item.Action_Token.Head;
   begin
      loop
         exit when Action_Token = null;
         if not (Is_In_Later_Tokens (Action_Token.Item.New_Token, Action_Token.Next) or
                   Is_In (Action_Token.Item.New_Token, Stack))
         then
            raise Programmer_Error with Label &
              " - action.new_token " & Token.Handle (Action_Token.Item.New_Token).Image &
              " not in later action tokens or stack";
         end if;
         Action_Token := Action_Token.Next;
      end loop;

      Action_Token := Cursor.Ptr.Item.Action_Token.Tail;
      loop
         exit when Stack = null;
         --  last item on stack has no token
         if Stack.Item.Token /= null and then Stack.Item.Token.all in Nonterminal.Instance and then
           not Is_In_Prev_New_Token (Stack.Item.Token, Action_Token)
         then
            raise Programmer_Error with Label & " - stack " & Stack.Item.Token.Image &
              " not in action.new_token";
         end if;
         Stack := Stack.Next;
      end loop;

      loop
         exit when Action_Token = null;
         Check_Are_In_Prev_New_Token (Label, Action_Token.Item.Tokens, Action_Token);
         Action_Token := Action_Token.Prev;
      end loop;

   end Check_Action_Stack;

   procedure Put (Action_Token : in Parser_Lists.Action_Token)
   is
      use type Nonterminal.Handle;
      use Ada.Characters.Handling;
      Action_Name : constant String := To_Lower
        (Token.Token_ID'Image
           --  LHS is null in unit tests
           ((if Action_Token.Action.LHS = null then Action_Token.New_Token.ID else Action_Token.Action.LHS.ID))) &
        "_" & OpenToken.Int_Image (Action_Token.Action.Index);
   begin
      Ada.Text_IO.Put (Action_Name & ": " & Action_Token.New_Token.Image & " ");
      Token_List.Print (Action_Token.Tokens);
   end Put;

   procedure Put_Action_Tokens (Cursor : in Parser_Lists.Cursor)
   is
      Action_Token : Action_Token_Node_Access := Cursor.Ptr.Item.Action_Token.Head;
   begin
      loop
         exit when Action_Token = null;
         Put (Action_Token.Item);
         Ada.Text_IO.New_Line;
         Action_Token := Action_Token.Next;
      end loop;
   end Put_Action_Tokens;

end OpenToken.Production.Parser.LALR.Parser_Lists;