anagram_1.0.0_49233f56/sources/anagram-grammars-lr_tables.ads

  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
--  Copyright (c) 2010-2017 Maxim Reznik <reznikmm@gmail.com>
--
--  SPDX-License-Identifier: MIT
--  License-Filename: LICENSE
-------------------------------------------------------------

with Ada.Containers.Vectors;

with Anagram.Grammars.LR;

package Anagram.Grammars.LR_Tables is
   pragma Preelaborate;

   type Table (<>) is tagged private;
   type Table_Access is access all Table;

   procedure Free (Self : in out Table_Access);

   type Reduce_Iterator is private;

   function Is_Empty (Self : Reduce_Iterator) return Boolean;

   function Production (Self : Reduce_Iterator) return Production_Index;

   function Part (Self : Reduce_Iterator) return Part_Count;
   --  This is used only for Right Nulled Tables.
   --  Point to last (right) part included in reduction.

   procedure Next
     (Self : Table;
      Item : in out Reduce_Iterator);

   function Create
     (Last_State        : LR.State_Index;
      Last_Terminal     : Terminal_Index;
      Last_Non_Terminal : Non_Terminal_Index) return Table_Access;

   function Last_State (Self : Table) return LR.State_Index;

   function Shift
     (Self  : Table;
      State : LR.State_Index;
      NT    : Non_Terminal_Index) return LR.State_Count;

   End_Of_File : constant Terminal_Count := 0;

   function Shift
     (Self  : Table;
      State : LR.State_Index;
      T     : Terminal_Count) return LR.State_Count;

   function Reduce
     (Self  : Table;
      State : LR.State_Index;
      T     : Terminal_Count) return Reduce_Iterator;

   function Finish
     (Self  : Table;
      State : LR.State_Index)
      return Boolean;

   procedure Set_Shift
     (Self  : in out Table;
      State : LR.State_Index;
      NT    : Non_Terminal_Index;
      Value : LR.State_Index);

   procedure Set_Shift
     (Self  : in out Table;
      State : LR.State_Index;
      T     : Terminal_Count;
      Value : LR.State_Index);

   procedure Set_Reduce
     (Self  : in out Table;
      State : LR.State_Index;
      T     : Terminal_Count;
      Value : Production_Index;
      Part  : Part_Count := 0);

   procedure Set_Finish
     (Self  : in out Table;
      State : LR.State_Index);

   procedure Remove_Shift
     (Self  : in out Table;
      State : LR.State_Index;
      T     : Terminal_Count);

   procedure Remove_Reduce
     (Self  : in out Table;
      State : LR.State_Index;
      T     : Terminal_Count;
      Value : Production_Index);

   --  Extension of Table to support incremental parsing

   procedure Set_Reduce
     (Self  : in out Table;
      State : LR.State_Index;
      NT    : Non_Terminal_Index;
      Value : Production_Index;
      Part  : Part_Count := 0);

   function Reduce
     (Self  : Table;
      State : LR.State_Index;
      NT    : Non_Terminal_Index) return Reduce_Iterator;

private

   type Reduce_Iterator is record
      Production : Production_Count;
      Part       : Part_Count;
      Next       : Natural;
   end record;

   package Reduce_Iterator_Vectors is
     new Ada.Containers.Vectors (Positive, Reduce_Iterator);

   type Terminal_State_Array is array
     (LR.State_Index range <>,
      Terminal_Count range <>) of LR.State_Count;

   type Boolean_Array is array (LR.State_Index range <>) of Boolean;

   type Terminal_Natural_Array is array
     (LR.State_Index range <>,
      Terminal_Count range <>) of Natural;

   type Non_Terminal_Natural_Array is array
     (LR.State_Index range <>,
      Non_Terminal_Index range <>) of Natural;

   type Non_Terminal_State_Array is array
     (LR.State_Index range <>,
      Non_Terminal_Index range <>) of LR.State_Count;

   type Table (Last_State        : LR.State_Index;
               Last_Terminal     : Terminal_Index;
               Last_Non_Terminal : Non_Terminal_Index) is
   tagged record
      Reduce_Vector : Reduce_Iterator_Vectors.Vector;
      T_Finish      : Boolean_Array (1 .. Last_State);
      T_Shift  : Terminal_State_Array
        (1 .. Last_State, End_Of_File .. Last_Terminal);
      T_Reduce : Terminal_Natural_Array
        (1 .. Last_State, End_Of_File .. Last_Terminal);
      NT_Shift : Non_Terminal_State_Array
        (1 .. Last_State, 1 .. Last_Non_Terminal);
      NT_Reduce : Non_Terminal_Natural_Array
        (1 .. Last_State, 1 .. Last_Non_Terminal);
   end record;

end Anagram.Grammars.LR_Tables;