-- $Date: 2004/02/14 02:36:29 $
-- $Revision: 1.3 $
-- $Author: jcrocholl $

with Readers; use Readers;

with Real_Numbers; use Real_Numbers;

with Strings; use Strings;
with Ada.Strings.Maps; use Ada.Strings.Maps;

-- Light-weight token input file readers.
package Token_Readers is

   HT : Character := Character'Val(9);
   CR : Character := Character'Val(13);
   SP : Character := Character'Val(32);

   -- Light-weight token input file reader.
   type Token_Reader is
     new Reader with
      record
         -- Behaviour.
         Whitespace      : Character_Set := To_Set(HT & CR & SP);
         Word_Separators : Character_Set := To_Set(HT & CR & SP);
         String_Quote    : Character := '"'; -- For Read_String.
         -- Temporary data.
         Token : String_Access;
      end record;

   -- Type for instance variables.
   type Token_Reader_Access is access Token_Reader;

   -------------------------------------------
   -- Internal initialization and finalization
   -------------------------------------------

   -- These don't need to be called directly because the functions
   -- Current_Input, Open and Close will do it for you.

   -- Initialize a newly created reader instance.
   procedure Initialize_Token_Reader
     (This : access Token_Reader'Class);

   -- Initialize a newly created reader instance with a file to read.
   procedure Initialize_Token_Reader
     (This     : access Token_Reader'Class;
      Filename : in String); -- Open this file for reading.

   -- Close the input file.
   procedure Finalize_Token_Reader
     (This : access Token_Reader'Class); -- Object instance.

   -------------------
   -- Creating readers
   -------------------

   -- Create an Token reader for current input (stdin).
   function Current_Input
     return Token_Reader_Access-- The newly created token reader instance.

   -- Create an Token reader for file input.
   function Open
     (Filename : in String)      -- Open this file.
     return Token_Reader_Access-- The newly created token reader instance.

   -- Close an Token reader's input file.
   procedure Close
     (This : in out Token_Reader_Access); -- Close this token reader.

   -------------
   -- Whitespace
   -------------

   -- Check if the next input character is whitespace.
   function Found_Whitespace
     (This : access Token_Reader'Class)
     return Boolean-- Whitespace at look-ahead?

   -- Skip forward until the next input character is not whitespace.
   procedure Skip_Whitespace
     (This : access Token_Reader'Class);

   -----------------
   -- Reading tokens
   -----------------

   -- Read characters from input until one of the Word_Separators is
   -- found.
   function Read_Word
     (This : access Token_Reader'Class)
     return String-- The word that was read.

   -- Read a real number for a token reader.
   function Read_Real
     (This : access Token_Reader'Class)
     return Real-- The real number that was read.

   -- Read characters from input until String_Quote is found.
   function Read_String
     (This : access Token_Reader'Class)
     return String-- The string that was read.

   -- If the current character is String_Quote, read a string,
   -- otherwise read a word. The result is stored in This.Token.
   procedure Read_Token
     (This : access Token_Reader'Class);

   -- If the current character is String_Quote, read a string,
   -- otherwise read a word. The result is stored in This.Token and
   -- also returned for immediate checking.
   function Read_Token
     (This : access Token_Reader'Class)
     return String-- The token that was read.

   -- Get the current token from a token reader.
   function Get_Token
     (This : access Token_Reader'Class)
     return String-- The token that was last read.

   -- Modify the current token of a token reader.
   procedure Set_Token
     (This  : access Token_Reader'Class;
      Token : in String); -- The new token value.

   -- Check if a given token was found.
   function Found
     (This  : access Token_Reader'Class;
      Token : in String-- Check if this text was found.
     return Boolean;     -- True if the given token was found.

end Token_Readers;