--
-- DOCUMENT_HANDLER by Richard Conn, TI Ada Technology Branch
-- 27 Feb 85
--
with Text_Io, Machine_Dependencies, Token_Definition;
package Document_Handler is
    --------------------------------------------------------------------------
    -- Abstract   : DOCUMENT_HANDLER provides input from a document, which
    --               is specified to OPEN_SOURCE by a pathname/filename.
    --               It returns a WORD from this document each time the routine
    --               GET_WORD is called.  When there are no more WORDs in the
    --               document, the exception NO_MORE_WORDS is raised.
    --
    --              DOCUMENT_HANDLER can also create a second document, the
    --               destination, or output, document.  This facility is
    --               enabled when the routine OPEN_DESTINATION is called,
    --               where the pathname/filename of the new document is the
    --               passed parameter.  The destination document is a mirror
    --               of the source document, except that selected words can
    --               be replaced with other words via the RESTORE_WORD routine.
    --               RESTORE_WORD replaces the last word returned by GET_WORD
    --               with a new word.
    --
    --              The context, or lines surrounding the last word returned
    --               by GET_WORD, can be obtained from the CONTEXT_OF_LAST_
    --               GET_WORD routine.  This routine returns LINES, which is
    --               an array of strings, and an INDEX, which is the offset
    --               to the first character of the last word returned by
    --               GET_WORD, and LENGTH, which is the number of characters
    --               in this word.  The constant, TARGET_LINE_IN_CONTEXT,
    --               is the index of the line of the context which contains
    --               the indicated word.
    --
    --------------------------------------------------------------------------

    subtype Token is Token_Definition.Token_Type;

    Max_Line : constant Positive := Machine_Dependencies.File_Line_Length;
    Number_Of_Lines_In_Context : constant Natural := 3;
    Target_Line_In_Context : constant Natural := 2;
    type Context_Element is
        record
            Line : String (1 .. Max_Line);
            Last : Natural;
        end record;
    type Context is array (1 .. Number_Of_Lines_In_Context) of Context_Element;

    -- Note: Exceptions from TEXT_IO may also be raised
    Restore_Failed : exception;
    No_More_Words : exception;
    Source_Not_Open : exception;

    procedure Open_Source (File_Name : String);
    --------------------------------------------------------------------------
    -- Abstract   : The source document from which future words are to be
    --               returned is opened for input.  GET_WORD returns these
    --               words.
    --------------------------------------------------------------------------
    -- Parameters : FILE_NAME      - Pathname/Filename of file containing
    --                               document
    --------------------------------------------------------------------------


    procedure Open_Destination (File_Name : String);
    --------------------------------------------------------------------------
    -- Abstract   : The destination document which is built of words from the
    --               source document and new, correctly-spelled words provided
    --               through RESTORE_WORD
    --------------------------------------------------------------------------
    -- Parameters : FILE_NAME      - Pathname/Filename of file to contain
    --               the destination document
    --------------------------------------------------------------------------


    procedure Close_Source_And_Destination;
    --------------------------------------------------------------------------
    -- Abstract   : Close both the source document and destination document
    --               files
    --------------------------------------------------------------------------
    -- Parameters : None
    --------------------------------------------------------------------------


    procedure Get_Word (Word : out Token; Word_In_Bounds : out Boolean);
    --------------------------------------------------------------------------
    -- Abstract   : Return the next word from the Source Document
    --------------------------------------------------------------------------
    -- Parameters : WORD           - the next word from the source document;
    --                               this structure includes the word itself
    --                               (as a string) and the word length
    --              WORD_IN_BOUNDS - TRUE if WORD is small enough to fit in
    --                               a TOKEN; FALSE if WORD is larger than
    --                               the maximum size of a TOKEN; if FALSE,
    --                               the full word can be obtained from
    --                               the procedure CONTEXT
    --------------------------------------------------------------------------


    procedure Restore_Word (Word : String);
    --------------------------------------------------------------------------
    -- Abstract   : Replace the last word obtained from GET_WORD with the
    --               passed string (WORD); the replacement is done in the
    --               destination document only (the source document is not
    --               affected)
    --------------------------------------------------------------------------
    -- Parameters : WORD           - the string containing the new word
    --------------------------------------------------------------------------


    procedure Context_Of_Last_Get_Word (Lines : out Context;
                                        Index : out Natural;
                                        Length : out Natural);
    --------------------------------------------------------------------------
    -- Abstract   : Return the lines surrounding and including the last word
    --               returned by GET_WORD; also return the INDEX and LENGTH
    --               of the last word returned by GET_WORD which indicate
    --               its position in the TARGET_LINE_IN_CONTEXT
    --------------------------------------------------------------------------
    -- Parameters : LINES          - the context (lines) surrounding the last
    --                               word
    --              INDEX          - the offset to the first character of the
    --                               last word in TARGET_LINE_IN_CONTEXT
    --              LENGTH         - number of characters in the last word
    --                               in TARGET_LINE_IN_CONTEXT
    --------------------------------------------------------------------------

end Document_Handler;