-- $Source: /nosc/work/parser/RCS/ParseStk.bdy,v $
-- $Revision: 4.0 $ -- $Date: 85/02/19 11:34:13 $ -- $Author: carol $

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

with Parsetables;  -- state tables generated by parser
--     generator
use Parsetables;

with Grammar_Constants;
use Grammar_Constants;
-- to have visibility on operations
-- on type ParserInteger declared there.
package body Parsestack is

    --| Overview
    --|
    --| The data structure is implemented as an array.
    --|

    ------------------------------------------------------------------
    -- Declarations Global to Package Body ParseStack
    ------------------------------------------------------------------

    Index : Pd.Stateparsestacksindex := 0;
    --| top element in stack.

    Space : array (Pd.Stateparsestacksrange) of Pd.Parsestackelement;
    --| Storage used to hold stack elements

    ------------------------------------------------------------------
    -- Subprogram Bodies Global to Package ParseStack
    -- (declared in package specification).
    ------------------------------------------------------------------

    procedure Push (Element : in Pd.Parsestackelement) is

    begin

        if (Index >= Pd.Stateparsestacksrange'Last) then
            raise Overflow;
        end if;

        Index := Index + 1;
        Space (Index) := Element;

    end Push;

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

    function Pop return Pd.Parsestackelement is

    begin

        if (Index < Pd.Stateparsestacksrange'First) then
            raise Underflow;
        end if;

        Index := Index - 1;
        return Space (Index + 1);

    end Pop;

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

    function Length return Pd.Stateparsestacksindex is

    begin

        return Index;

    end Length;

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

    procedure Reduce (Topn : in Pd.Stateparsestacksindex) is

    begin
        if (Topn > Index) then
            raise Underflow;
        end if;

        Index := Index - Topn;

    end Reduce;  -- procedure

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

end Parsestack;

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