with Parse_Tab_Types;
use Parse_Tab_Types;
with Parse_Tab_1;
with Parse_Tab_2;
with Parse_Tab_3;
with Parse_Tab_4;
with Parse_Tab_5;
with Parse_Tab_6;
with Parse_Tab_7;
with Parse_Tab_8;
with Parse_Tab_9;
with Parse_Tab_10;
with Parse_Tab_11;
with Parse_Tab_12;
with Parse_Tab_13;
package body Parsetables is

    type Gsti_Type is
       array (Grammarsymbolrange'First .. Grammarsymbolrange'Last * 2) of
          Gc.Parserinteger;

    Grammarsymboltableindex : constant Gsti_Type :=
       Gsti_Type (Parse_Tab_1.Grammarsymboltableindex);



    Grammarsymboltable : constant String := Parse_Tab_2.Grammarsymboltable;
    --| Table of symbols used in the grammar.
    -- NYU Reference Name: NO_SYM



    type Lhs_Type is array (Lefthandsiderange) of Grammarsymbolrange;

    Lefthandside : constant Lhs_Type := Lhs_Type (Parse_Tab_3.Lefthandside);
    --| Map of the grammar rule number (constant array index) to
    --| numeric value of left hand side symbol.
    -- NYU Reference Name: LHS



    type Rhs_Type is array (Righthandsiderange) of Gc.Parserinteger;

    Righthandside : constant Rhs_Type := Rhs_Type (Parse_Tab_4.Righthandside);



    type Ato_Type is array (Actiontableonerange) of Gc.Parserinteger;

    Actiontableone : constant Ato_Type := Ato_Type (Parse_Tab_5.Actiontableone);



    type Att_Type is array (Actiontabletworange) of Gc.Parserinteger;

    Actiontabletwo : constant Att_Type := Att_Type (Parse_Tab_6.Actiontabletwo);
    --| Hash values to check against to verify that
    --| correct action has been found for this
    --| parser state and input token.
    -- NYU Reference Name: ACTION_TABLE2



    type Dfm_Type is array (Defaultmaprange) of Gc.Parserinteger;

    Defaultmap : constant Dfm_Type := Dfm_Type (Parse_Tab_7.Defaultmap);
    --| Map of states (constant array index) to default reductions.
    -- NYU Reference Name: DEFAULT



    subtype Followsymbolindexarray is Parse_Tab_8.Followsymbolindexarray;

    Followsymbolmapindex : constant Followsymbolindexarray :=
       Parse_Tab_8.Followsymbolmapindex;



    Followsymbolmap : constant Followsymbolarray := Parse_Tab_9.Followsymbolmap;
    --| Map of states to sets of follow symbols
    -- NYU Reference Name: FOLLOW

    ------------------------------------------------------------------
    -- Action_Token_Map
    ------------------------------------------------------------------

    subtype Action_Token_Array_Index is Parse_Tab_10.Action_Token_Array_Index;
    --| For indexing the All Action Token Array.
    --| Maps a given state into the lower and upper bounds of a slice
    --| of the All Action Index Array.

    Action_Token_Mapindex : constant Action_Token_Array_Index :=
       Parse_Tab_10.Action_Token_Mapindex;


    Action_Token_Map : constant Action_Token_Array :=
       Parse_Tab_11.Action_Token_Map;
    --| Action_Token_Map is an array that
    --| maps from each state (using action index map) to a set of
    --| action tokens. An action token is a terminal symbol
    --| (except EOF_Token) for which in the given state an
    --| explicit (non-default) shift or reduce action
    --| is defined.
    --| Used to cut reduce the
    --| number of primary recovery candidates.

    ------------------------------------------------------------------
    -- Shift_State_Map
    ------------------------------------------------------------------

    subtype Shift_State_Index_Array is Parse_Tab_12.Shift_State_Index_Array;
    --| For indexing the All Action Token Array.
    --| Maps a given state into the lower and upper bounds of a slice
    --| of the All Action Index Array.

    Shift_State_Mapindex : constant Shift_State_Index_Array :=
       Parse_Tab_12.Shift_State_Mapindex;


    Shift_State_Map : constant Shift_State_Array :=
       Parse_Tab_13.Shift_State_Map;
    --| Shift_State_ is an array that
    --| maps from non-terminals (using shift index map) to sets
    --| of states in which
    --| a shift to the non-terminal is defined.
    --| Used to determine the number of trials in primary
    --| error recovery.

    ------------------------------------------------------------------
    -- Subprogram Bodies Global to Package ErrorParseTables
    ------------------------------------------------------------------

    function Get_Action_Token_Map ( --| return the array of action tokens
                                    --| for the state passed in.
                                   In_Index : in Staterange
                                   --| the state to return action tokens
                                   --| for.
                                   ) return Action_Token_Record is
        --| Returns
        --| This subprogram returns the action token record for the
        --| state passed in.
        Result : Action_Token_Record;
        Lowerbound, Upperbound : Gc.Parserinteger;
        --| Lower and upper bounds of the slice of Action Token Map
    begin
        Lowerbound := Action_Token_Mapindex (In_Index * 2 - 1);
        Upperbound := Action_Token_Mapindex (In_Index * 2);

        Result.Set_Size := Upperbound - Lowerbound + 1;
        Result.Set := (others => Defaultvalue);
        Result.Set (Result.Set'First .. Result.Set_Size) :=
           Action_Token_Map (Lowerbound .. Upperbound);

        return Result;
    end Get_Action_Token_Map;

    ------------------------------------------------------------------

    function Get_Shift_State_Map
                (  --| return the array of shift states
                   --| for the grammar symbol passed in.
                 In_Index : in Grammarsymbolrange
                 --| the grammar symbol to return shifts
                 --| for.
                 )
                --| Raises: This subprogram raises no exceptions.
                return Shift_State_Record
                --| Returns
                --| This subprogram returns the array of shift states for the
                --| grammar symbol passed in.
                 is

        Result : Shift_State_Record;
        Lowerbound, Upperbound : Gc.Parserinteger;
        --| Lower and upper bounds of the slice of Shift State Map
    begin
        Lowerbound := Shift_State_Mapindex (In_Index * 2 - 1);
        Upperbound := Shift_State_Mapindex (In_Index * 2);

        Result.Set_Size := Upperbound - Lowerbound + 1;
        Result.Set := (others => Defaultvalue);
        Result.Set (Result.Set'First .. Result.Set_Size) :=
           Shift_State_Map (Lowerbound .. Upperbound);

        return Result;
    end Get_Shift_State_Map;

    function Get_Grammar_Symbol
                (   --| return the string representation
                    --| of the grammar symbol
                 In_Index : in Grammarsymbolrange) return String is
        Lowerbound, Upperbound : Gc.Parserinteger;
        --| Lower and upper bounds of the slice of Shift State Map
    begin
        Lowerbound := Grammarsymboltableindex (In_Index * 2 - 1);
        Upperbound := Grammarsymboltableindex (In_Index * 2);

        return Grammarsymboltable
                  (Integer (Lowerbound) .. Integer (Upperbound));
    end Get_Grammar_Symbol;

    ------------------------------------------------------------------

    function Get_Follow_Map (       --| return the array of follow symbols
                                    --| of the grammar symbol passed in
                             In_Index : in Followmaprange)
                            -- |
                            -- |Raises: This subprogram raises no exceptions.
                            -- |

                            return Followsymbolrecord is
        Result : Followsymbolrecord;
        Lowerbound, Upperbound : Gc.Parserinteger;
        Adjusted_Index : Gc.Parserinteger :=
           (In_Index - Followmaprange'First) + 1;
    begin
        Lowerbound := Followsymbolmapindex (Adjusted_Index * 2 - 1);
        Upperbound := Followsymbolmapindex (Adjusted_Index * 2);

        Result.Follow_Symbol_Count := Upperbound - Lowerbound + 1;
        Result.Follow_Symbol := (others => Defaultvalue);
        Result.Follow_Symbol (Result.Follow_Symbol'First ..
                                 Result.Follow_Symbol_Count) :=
           Followsymbolmap (Lowerbound .. Upperbound);

        return Result;
    end Get_Follow_Map;

    ------------------------------------------------------------------

    function Getaction (            -- see subprogram declaration
                        Instatevalue : in Staterange;
                        Insymbolvalue : in Grammarsymbolrange)
                       return Actionrange is

        Unique : Gc.Parserinteger;
        --| unique value to hash for Index.
        Index : Gc.Parserinteger;
        --| index into Action Tables.
        Action : Gc.Parserinteger;
        --| value from Action Tables.
        Collisioncount : Natural := 0; --| Number of collisions.
    begin
        -- GetAction function
        --| Algorithm
        --|-
        --| Definitions of key objects from package ParseTables:
        --|
        --| ActionCount: the number of actions in the action tables.
        --|
        --| ActionTableOne: table of action values for all combinations of
        --|     states and input actions.
        --|
        --| ActionTableTwo: hash values to check against to verify that action
        --|     value at same index in ActionTableOne is correct one.
        --|
        --| ActionTableSize: last index in ActionTableOne and ActionTableTwo
        --|     before the hash collision chains.
        --|
        --| DefaultMap: default action for each state.
        --|+
        --| The action to be returned is computed from parameters InStateValue
        --| and InSymbolValue. First, determine the unique single value:
        --|
        --|     Unique := (InStateValue * GrammarSymbolCountPlusOne) +
        --|                InSymbolValue;
        --|
        --| Unique is hashed by reducing modulo ActionTableSize and adding 1:
        --|
        --|     Index := (Unique mod ActionTableSize) + 1;
        --|
        --| This hash value, Index, is used to index ActionTableOne to
        --| obtain an Action:
        --|
        --|     Action := ActionTableOne(Index);
        --|
        --| Action is then used to determine the return value:
        --|
        --| Action = 0:
        --|     return DefaultMap(InStateValue);
        --|
        --| Action < ActionCount:
        --|     if (Unique = ActionTableTwo(Index)) then
        --|         return Action;
        --|     else
        --|         return DefaultMap(InStateValue);
        --|     end if;
        --|
        --| Action >= ActionCount:
        --|     --Search the hash collision chain
        --|     Index := Action - ActionCount;
        --|     while (Action /= 0) loop
        --|         Index := Index + 1;
        --|         Action := ActionTableTwo(Index);
        --|         if (Action = Unique) then
        --|             return ActionTableOne(Index);
        --|         end if;
        --|     end loop;
        --|     return DefaultMap(InStateValue);

        ------------------------------------------------------------------

        --| The actual code used folds this algorithm into a more efficient one:
        Parserdecisioncount := Natural'Succ (Parserdecisioncount);

        Unique := (Instatevalue * Grammarsymbolcountplusone) + Insymbolvalue;
        Index := (Unique mod Actiontablesize) + 1;
        Action := Actiontableone (Index);

        if (Action >= Actioncount) then
            Index := Action - Actioncount + 1;

            while ((Actiontabletwo (Index) /= Unique) and then
                   (Actiontabletwo (Index) /= 0)) loop
                Index := Index + 1;
                Collisioncount := Natural'Succ (Collisioncount);
            end loop;

            Action := Actiontableone (Index);
        end if;

        -- Collect statistics information.
        Totalcollisions := Collisioncount + Totalcollisions;

        if Collisioncount > Maxcollisions then
            Maxcollisions := Collisioncount;
        end if;

        if (Actiontabletwo (Index) /= Unique) then
            return Defaultmap (Instatevalue);
        else
            return Action;
        end if;

    end Getaction; -- function

    function Get_Lefthandside (Grammarrule : in Lefthandsiderange)
                              return Grammarsymbolrange is
    begin
        return Lefthandside (Grammarrule);
    end Get_Lefthandside;

    function Get_Righthandside
                (Grammarrule : in Righthandsiderange) return Gc.Parserinteger is
    begin
        return Righthandside (Grammarrule);
    end Get_Righthandside;

end Parsetables;

----------------------------------------------------------------------
