--  Verada.Scanning: the values that `T'Value` and numeric Text_IO's Get read from strings,
--  proved. For each kind of value there is a query, which says whether a string is one, and a
--  function whose precondition is that query and whose postcondition is the value: a program
--  tests a string from outside first (`T'Valid_Value (S)` asks the query), and proof shows
--  that no `'Value` fails. Every scanner reads a `Wide_Wide_String` from 1; a `String` or a
--  `Wide_String` is widened to one, character for character (`Widened`).
--
--  The forms are Ada's (RM 2.4, 3.5): spaces around; for an integer an optional sign and an
--  integer literal, decimal (`1_000`, `12E3`) or based (`16#FF#`, `2#1010#E2`), its digits
--  with single underscores between them, a based literal's extended digits below its base, of
--  either case, and its exponent without a minus; for an enumeration value one of the type's
--  literals, an identifier in any case or a character literal as written.
with Verada.Storage;
with Verada.Strings.Unicode;

package Verada.Scanning is
   --  the integer type with the widest range: every integer and modular type's values are in
   --  it, so it holds every value an integer string can have
   subtype Big is Long_Long_Long_Integer;

   --  the bases a based literal may have
   subtype Base_Range is Integer range 2 .. 16;

   --  S's characters as code points, from 1
   function Widened (S : String) return Wide_Wide_String
     with Global => null,
          Post => Widened'Result'First = 1 and then Widened'Result'Length = S'Length
                  and then S'Length < Verada.Storage.Length_Bound
                  and then (for all I in 1 .. S'Length =>
                              Widened'Result (I) = Wide_Wide_Character'Val (Character'Pos (S (I - 1 + S'First))));

   --  a Wide_String's characters as code points, from 1
   function Widened (S : Wide_String) return Wide_Wide_String
     with Global => null,
          Post => Widened'Result'First = 1 and then Widened'Result'Length = S'Length
                  and then S'Length < Verada.Storage.Length_Bound
                  and then (for all I in 1 .. S'Length =>
                              Widened'Result (I) = Wide_Wide_Character'Val (Wide_Character'Pos (S (I - 1 + S'First))));

   --  a Wide_Wide_String's characters, from 1
   function Widened (S : Wide_Wide_String) return Wide_Wide_String
     with Global => null,
          Post => Widened'Result'First = 1 and then Widened'Result'Length = S'Length
                  and then S'Length < Verada.Storage.Length_Bound
                  and then (for all I in 1 .. S'Length => Widened'Result (I) = S (I - 1 + S'First));

   --  S's characters as a Wide_String (for `'Wide_Image`)
   function Wide (S : String) return Wide_String is
     (Wide_String'(for I in 1 .. S'Length => Wide_Character'Val (Character'Pos (S (I - 1 + S'First)))))
     with Post => Wide'Result'First = 1 and then Wide'Result'Length = S'Length;

   --  a string the scanners read: empty, or indexed from 1 (and, as every string in memory
   --  is, shorter than Length_Bound)
   function From_One (S : Wide_Wide_String) return Boolean is
     ((S'Length = 0 or else S'First = 1) and then S'Length < Verada.Storage.Length_Bound);

   --  how many of S's characters are spaces before the first that is not
   function Leading_Blanks (S : Wide_Wide_String) return Natural
     with Global => null,
          Pre => From_One (S),
          Post => Leading_Blanks'Result <= S'Length
                  and then (for all K in 1 .. Leading_Blanks'Result => S (K) = ' ')
                  and then (Leading_Blanks'Result = S'Length or else S (Leading_Blanks'Result + 1) /= ' ');

   --  how many are spaces after the last that is not (S'Length when all are)
   function Trailing_Blanks (S : Wide_Wide_String) return Natural
     with Global => null,
          Pre => From_One (S),
          Post => Trailing_Blanks'Result <= S'Length
                  and then (for all K in 1 .. Trailing_Blanks'Result => S (S'Length - Trailing_Blanks'Result + K) = ' ')
                  and then (Trailing_Blanks'Result = S'Length or else S (S'Length - Trailing_Blanks'Result) /= ' ');

   --  the last character of the value: the last that is not a space
   function Value_Last (S : Wide_Wide_String) return Natural is (S'Length - Trailing_Blanks (S))
     with Pre => From_One (S);

   --  whether the value has a sign: its first character, after the spaces
   function Signed (S : Wide_Wide_String) return Boolean is
     (Leading_Blanks (S) < Value_Last (S) and then S (Leading_Blanks (S) + 1) in '+' | '-')
     with Pre => From_One (S);

   --  whether that sign is a minus
   function Minus (S : Wide_Wide_String) return Boolean is
     (Signed (S) and then S (Leading_Blanks (S) + 1) = '-')
     with Pre => From_One (S);

   --  the characters before the literal: the spaces, and the sign
   function Literal_After (S : Wide_Wide_String) return Natural is
     (Leading_Blanks (S) + (if Signed (S) then 1 else 0))
     with Pre => From_One (S);

   --  a digit's value, 16 for a character that is not one
   function Digit_Value (C : Wide_Wide_Character) return Natural is
     (if C in '0' .. '9' then Wide_Wide_Character'Pos (C) - 48
      elsif C in 'A' .. 'F' then Wide_Wide_Character'Pos (C) - 55
      elsif C in 'a' .. 'f' then Wide_Wide_Character'Pos (C) - 87
      else 16)
     with Post => Digit_Value'Result <= 16;

   --  whether C is a digit of the base
   function Is_Digit (C : Wide_Wide_Character; Base : Base_Range) return Boolean is (Digit_Value (C) < Base);

   --  S (After + 1 .. Last) are digits of the base with single underscores between them
   function Is_Run (S : Wide_Wide_String; After, Last : Natural; Base : Base_Range) return Boolean is
     (After < Last and then Is_Digit (S (After + 1), Base) and then Is_Digit (S (Last), Base)
      and then (for all I in After + 1 .. Last =>
                  Is_Digit (S (I), Base)
                  or else (S (I) = '_' and then I > After + 1 and then I < Last
                           and then Is_Digit (S (I - 1), Base) and then Is_Digit (S (I + 1), Base))))
     with Pre => From_One (S) and then Last <= S'Length;

   --  the first of S (After + 1 .. Last) that is C1 or C2, 0 when none is
   function Find (S : Wide_Wide_String; After, Last : Natural; C1, C2 : Wide_Wide_Character) return Natural
     with Global => null,
          Pre => From_One (S) and then Last <= S'Length and then After <= S'Length,
          Post => (if Find'Result = 0 then (for all I in After + 1 .. Last => S (I) /= C1 and then S (I) /= C2)
                   else Find'Result in After + 1 .. Last and then (S (Find'Result) = C1 or else S (Find'Result) = C2)
                        and then (for all I in After + 1 .. Find'Result - 1 => S (I) /= C1 and then S (I) /= C2));

   --  Values are counted negatively, so that Big'First's magnitude has a value too: a count
   --  is a value's negation, or Saturated once it is beyond Big's range. Saturated is not a
   --  limit on the values read: no type has a value beyond Big's.
   Saturated : constant Big := 1;

   --  a count, then one more digit
   function Step (N : Big; Base : Base_Range; D : Natural) return Big is
     (if N > 0 or else N < (Big'First + Big (D)) / Big (Base) then Saturated else N * Big (Base) - Big (D))
     with Pre => D <= 16,
          Post => Step'Result = Saturated or else Step'Result <= 0;

   --  the count of S (After + 1 .. Last)'s digits, the underscores skipped
   function Run (S : Wide_Wide_String; After, Last : Natural; Base : Base_Range) return Big is
     (if Last <= After then 0
      elsif S (Last) = '_' then Run (S, After, Last - 1, Base)
      else Step (Run (S, After, Last - 1, Base), Base, Digit_Value (S (Last))))
     with Ghost,
          Pre => From_One (S) and then Last <= S'Length,
          Post => Run'Result = Saturated or else Run'Result <= 0,
          Subprogram_Variant => (Decreases => Last);

   --  a count times Base ** E
   function Scaled (N : Big; Base : Base_Range; E : Big) return Big is
     (if E <= 0 or else N = 0 or else N = Saturated then N else Scaled (Step (N, Base, 0), Base, E - 1))
     with Ghost,
          Post => (if N = Saturated or else N <= 0 then Scaled'Result = Saturated or else Scaled'Result <= 0),
          Subprogram_Variant => (Decreases => E);

   --  where an integer literal's first '#' is (0 for a decimal one)
   function Hash_1 (S : Wide_Wide_String) return Natural is (Find (S, Literal_After (S), Value_Last (S), '#', '#'))
     with Pre => From_One (S);

   --  its second '#' (0 for none)
   function Hash_2 (S : Wide_Wide_String) return Natural is
     (if Hash_1 (S) = 0 then 0 else Find (S, Hash_1 (S), Value_Last (S), '#', '#'))
     with Pre => From_One (S);

   --  the last character before the exponent: the second '#', or the last before an 'E'
   function Mantissa_Last (S : Wide_Wide_String) return Natural is
     (if Hash_1 (S) /= 0 then Hash_2 (S)
      elsif Find (S, Literal_After (S), Value_Last (S), 'E', 'e') = 0 then Value_Last (S)
      else Find (S, Literal_After (S), Value_Last (S), 'E', 'e') - 1)
     with Pre => From_One (S),
          Post => Mantissa_Last'Result <= Value_Last (S);

   --  the base, 10 for a decimal literal (its value, which may not be a base, for a based one)
   function Base_Count (S : Wide_Wide_String) return Big is
     (if Hash_1 (S) = 0 then -10 else Run (S, Literal_After (S), Hash_1 (S) - 1, 10))
     with Ghost, Pre => From_One (S);

   --  a based literal's base, as written (Base_Count's value)
   function Base_Number (S : Wide_Wide_String) return Big
     with Global => null,
          Pre => From_One (S) and then Hash_1 (S) /= 0,
          Post => Base_Number'Result = Base_Count (S);

   --  the base of the literal's digits: a based literal's when it is one of 2 .. 16,
   --  else 10
   function Base_Of (S : Wide_Wide_String) return Base_Range is
     (if Hash_1 (S) /= 0 and then Base_Number (S) in -16 .. -2 then Base_Range (-Base_Number (S)) else 10)
     with Pre => From_One (S);

   --  whether the literal has an exponent, and where its digits start
   function Has_Exponent (S : Wide_Wide_String) return Boolean is (Mantissa_Last (S) < Value_Last (S))
     with Pre => From_One (S);

   --  the characters before the exponent's digits: the mantissa, the 'E', and the
   --  exponent's sign
   function Exponent_After (S : Wide_Wide_String) return Natural is
     (Mantissa_Last (S) + 1
      + (if Mantissa_Last (S) < Value_Last (S) - 1 and then S (Mantissa_Last (S) + 2) in '+' | '-' then 1 else 0))
     with Pre => From_One (S) and then Has_Exponent (S);

   --  an exponent, 'E' or 'e', a sign (not a minus unless Negative_Allowed) and digits
   function Exponent_Form (S : Wide_Wide_String; Negative_Allowed : Boolean) return Boolean is
     (not Has_Exponent (S)
      or else (S (Mantissa_Last (S) + 1) in 'E' | 'e'
               and then (Negative_Allowed or else S (Exponent_After (S)) /= '-')
               and then Is_Run (S, Exponent_After (S), Value_Last (S), 10)))
     with Pre => From_One (S);

   --  the syntax of an integer literal, after the spaces and the sign
   function Integer_Syntax (S : Wide_Wide_String) return Boolean is
     (Literal_After (S) < Value_Last (S)
      and then (if Hash_1 (S) = 0 then Is_Run (S, Literal_After (S), Mantissa_Last (S), 10)
                else Hash_1 (S) - 1 > Literal_After (S)
                     and then Is_Run (S, Literal_After (S), Hash_1 (S) - 1, 10)
                     and then Hash_2 (S) - 1 > Hash_1 (S)
                     and then Base_Number (S) in -16 .. -2
                     and then Is_Run (S, Hash_1 (S), Hash_2 (S) - 1, Base_Of (S)))
      and then Exponent_Form (S, Negative_Allowed => False))
     with Pre => From_One (S);

   --  where the mantissa's digits are: after the sign, or between the '#'s
   function Digits_After (S : Wide_Wide_String) return Natural is
     (if Hash_1 (S) = 0 then Literal_After (S) else Hash_1 (S))
     with Pre => From_One (S);

   --  the mantissa's last digit: before the 'E', or before the second '#'
   function Digits_Last (S : Wide_Wide_String) return Natural is
     (if Hash_1 (S) = 0 then Mantissa_Last (S) else Natural'Max (Hash_2 (S) - 1, 0))
     with Pre => From_One (S),
          Post => Digits_Last'Result <= S'Length;

   --  the count of the mantissa's digits
   function Mantissa_Count (S : Wide_Wide_String) return Big is
     (Run (S, Digits_After (S), Digits_Last (S), Base_Of (S)))
     with Ghost, Pre => From_One (S);

   --  the exponent's value (Big'Last for any beyond Big's range)
   function Exponent_Value (S : Wide_Wide_String) return Big is
     (if not Has_Exponent (S) then 0
      --  (an exponent of 2 ** 127 or more makes any value but 0 one beyond every type's)
      elsif Run (S, Exponent_After (S), Value_Last (S), 10) = Saturated
            or else Run (S, Exponent_After (S), Value_Last (S), 10) = Big'First
      then Big'Last
      else -Run (S, Exponent_After (S), Value_Last (S), 10))
     with Ghost, Pre => From_One (S);

   --  the integer's magnitude, counted (negated), or Saturated
   function Integer_Count (S : Wide_Wide_String) return Big is
     (Scaled (Mantissa_Count (S), Base_Of (S), Exponent_Value (S)))
     with Ghost, Pre => From_One (S);

   --  the integer S has the syntax of, and whose value is in Big's range
   function Integer_Form (S : Wide_Wide_String) return Boolean is
     (Integer_Syntax (S)
      and then Integer_Count (S) /= Saturated
      and then (Minus (S) or else Integer_Count (S) /= Big'First))
     with Ghost, Pre => From_One (S);

   --  its value
   function Integer_Model (S : Wide_Wide_String) return Big is
     (if Minus (S) then Integer_Count (S) elsif Integer_Count (S) in Big'First + 1 .. 0 then -Integer_Count (S) else 0)
     with Ghost, Pre => From_One (S);

   --  whether S, from 1, is an integer in First .. Last (a modular type's has no minus sign)
   function Is_Integer_From_One (S : Wide_Wide_String; First, Last : Big; Modular : Boolean) return Boolean
     with Global => null,
          Pre => From_One (S),
          Post => Is_Integer_From_One'Result
                  = (Integer_Form (S) and then Integer_Model (S) in First .. Last and then (if Modular then not Minus (S)));

   --  its value
   function Integer_From_One (S : Wide_Wide_String; First, Last : Big; Modular : Boolean) return Big
     with Global => null,
          Pre => From_One (S) and then Is_Integer_From_One (S, First, Last, Modular),
          Post => Integer_From_One'Result = Integer_Model (S) and then Integer_From_One'Result in First .. Last;

   --  whether S is an integer in First .. Last (a modular type's has no minus sign)
   --  whether S is an integer in First .. Last (a modular type's has no minus sign)
   function Is_Integer (S : String; First, Last : Big; Modular : Boolean := False) return Boolean is
     (Is_Integer_From_One (Widened (S), First, Last, Modular));

   --  (of a Wide_String)
   function Is_Integer (S : Wide_String; First, Last : Big; Modular : Boolean := False) return Boolean is
     (Is_Integer_From_One (Widened (S), First, Last, Modular));

   --  (of a Wide_Wide_String)
   function Is_Integer (S : Wide_Wide_String; First, Last : Big; Modular : Boolean := False) return Boolean is
     (Is_Integer_From_One (Widened (S), First, Last, Modular));

   --  its value
   function Integer_Value (S : String; First, Last : Big; Modular : Boolean := False) return Big is
     (Integer_From_One (Widened (S), First, Last, Modular))
     with Pre => Is_Integer (S, First, Last, Modular);

   --  (of a Wide_String)
   function Integer_Value (S : Wide_String; First, Last : Big; Modular : Boolean := False) return Big is
     (Integer_From_One (Widened (S), First, Last, Modular))
     with Pre => Is_Integer (S, First, Last, Modular);

   --  (of a Wide_Wide_String)
   function Integer_Value (S : Wide_Wide_String; First, Last : Big; Modular : Boolean := False) return Big is
     (Integer_From_One (Widened (S), First, Last, Modular))
     with Pre => Is_Integer (S, First, Last, Modular);
   --  Enumeration values. Sema passes a table of the type's images, in the order of their
   --  positions: Names holds them one after another, and Ends (P) is where the one at
   --  position P ends. An identifier's image is in upper case, a character literal's as
   --  written.
   type Name_Ends is array (Natural range <>) of Natural;

   --  whether a table is one: Names from 1, and Ends from 0, never decreasing, within
   --  Names
   function Well_Formed (Names : Wide_Wide_String; Ends : Name_Ends) return Boolean is
     (From_One (Names) and then Ends'Length > 0 and then Ends'First = 0
      and then (for all P in Ends'Range => Ends (P) <= Names'Length)
      and then (for all P in Ends'Range => (if P > 0 then Ends (P - 1) <= Ends (P))));

   --  the characters before the image at position P
   function Image_After (Names : Wide_Wide_String; Ends : Name_Ends; P : Natural) return Natural is
     (if P = 0 then 0 else Ends (P - 1))
     with Pre => Well_Formed (Names, Ends) and then P in Ends'Range;

   --  a character's upper case (CPython's, where it is one character)
   function Upper (C : Wide_Wide_Character) return Wide_Wide_Character is
     (if C in 'a' .. 'z' then Wide_Wide_Character'Val (Wide_Wide_Character'Pos (C) - 32)
      elsif Wide_Wide_Character'Pos (C) in 128 .. 16#10FFFF#
      then Wide_Wide_Character'Val (Verada.Strings.Unicode.Upper_Code (Wide_Wide_Character'Pos (C)))
      else C);

   --  whether S, its spaces aside, is the image at position P: an identifier in any case, a
   --  character literal as written
   --  (the image's characters, Names (Image_After (Names, Ends, P) + 1 .. Ends (P)), compared
   --  with S's after its spaces, as many as S has before its trailing spaces)
   function Matches (S, Names : Wide_Wide_String; Ends : Name_Ends; P : Natural) return Boolean
     with Global => null,
          Pre => From_One (S) and then Well_Formed (Names, Ends) and then P in Ends'Range;

   --  the first position whose image S is, -1 for none
   function Match_Position (S, Names : Wide_Wide_String; Ends : Name_Ends) return Integer
     with Global => null,
          Pre => From_One (S) and then Well_Formed (Names, Ends),
          Post => (if Match_Position'Result = -1 then (for all P in Ends'Range => not Matches (S, Names, Ends, P))
                   else Match_Position'Result in Ends'Range
                        and then Matches (S, Names, Ends, Match_Position'Result)
                        and then (for all P in 0 .. Match_Position'Result - 1 => not Matches (S, Names, Ends, P)));

   --  whether S, from 1, is the image of a value at a position in First .. Last
   function Is_Enumeration_From_One (S, Names : Wide_Wide_String; Ends : Name_Ends; First, Last : Big) return Boolean is
     (Match_Position (S, Names, Ends) /= -1 and then Big (Match_Position (S, Names, Ends)) in First .. Last)
     with Pre => From_One (S) and then Well_Formed (Names, Ends);

   --  whether S is the image of a value at a position in First .. Last
   function Is_Enumeration (S : String; Names : Wide_Wide_String; Ends : Name_Ends; First, Last : Big) return Boolean is
     (Is_Enumeration_From_One (Widened (S), Names, Ends, First, Last))
     with Pre => Well_Formed (Names, Ends);

   --  (of a Wide_String)
   function Is_Enumeration (S : Wide_String; Names : Wide_Wide_String; Ends : Name_Ends; First, Last : Big) return Boolean is
     (Is_Enumeration_From_One (Widened (S), Names, Ends, First, Last))
     with Pre => Well_Formed (Names, Ends);

   --  (of a Wide_Wide_String)
   function Is_Enumeration (S : Wide_Wide_String; Names : Wide_Wide_String; Ends : Name_Ends; First, Last : Big) return Boolean is
     (Is_Enumeration_From_One (Widened (S), Names, Ends, First, Last))
     with Pre => Well_Formed (Names, Ends);

   --  the position of the value whose image S is
   function Enumeration_Value (S : String; Names : Wide_Wide_String; Ends : Name_Ends; First, Last : Big) return Big is
     (Big (Match_Position (Widened (S), Names, Ends)))
     with Pre => Well_Formed (Names, Ends) and then Is_Enumeration (S, Names, Ends, First, Last),
          Post => Enumeration_Value'Result in First .. Last;

   --  (of a Wide_String)
   function Enumeration_Value (S : Wide_String; Names : Wide_Wide_String; Ends : Name_Ends; First, Last : Big) return Big is
     (Big (Match_Position (Widened (S), Names, Ends)))
     with Pre => Well_Formed (Names, Ends) and then Is_Enumeration (S, Names, Ends, First, Last),
          Post => Enumeration_Value'Result in First .. Last;

   --  (of a Wide_Wide_String)
   function Enumeration_Value (S : Wide_Wide_String; Names : Wide_Wide_String; Ends : Name_Ends; First, Last : Big) return Big is
     (Big (Match_Position (Widened (S), Names, Ends)))
     with Pre => Well_Formed (Names, Ends) and then Is_Enumeration (S, Names, Ends, First, Last),
          Post => Enumeration_Value'Result in First .. Last;

   --  Characters: a character literal (`'a'`), the name of a control character as
   --  Character'Image writes it (`NUL`, `DEL`, `RESERVED_128`, in any case), or a code point
   --  in hexadecimal as a wide character's image writes it (`["03B1"]`)

   --  the control characters' names, by Name_Position
   Control_Names : constant Wide_Wide_String :=
     "NULSOHSTXETXEOTENQACKBELBSHTLFVTFFCRSOSIDLEDC1DC2DC3DC4NAKSYNETBCANEMSUBESCFSGSRSUSDELRESERVED_128RESERVED_129BPHNBHRESERVED_132NELSSAESAHTSHTJVTSPLDPLURISS2SS3DCSPU1PU2STSCCHMWSPAEPASOSRESERVED_153SCICSISTOSCPMAPC";

   --  where each control character's name ends in Control_Names
   Control_Ends : constant Name_Ends :=
     [3, 6, 9, 12, 15, 18, 21, 24, 26, 28, 30, 32, 34, 36, 38, 40, 43, 46, 49, 52, 55, 58, 61, 64, 67, 69, 72, 75, 77,
      79, 81, 83, 86, 98, 110, 113, 116, 128, 131, 134, 137, 140, 143, 146, 149, 152, 154, 157, 160, 163, 166, 169,
      172, 175, 177, 180, 183, 186, 198, 201, 204, 206, 209, 211, 214];

   --  the character a control name names
   function Name_Position (P : Natural) return Big is
     (if P < 32 then Big (P) elsif P = 32 then 127 else 128 + Big (P - 33))
     with Pre => P <= 64;

   --  a character literal
   function Is_Quoted (S : Wide_Wide_String) return Boolean is
     (Value_Last (S) - Leading_Blanks (S) = 3 and then S (Leading_Blanks (S) + 1) = ''' and then S (Value_Last (S)) = ''')
     with Pre => From_One (S);

   --  the hexadecimal form: `["`, digits, `"]`
   function Is_Hex_Form (S : Wide_Wide_String) return Boolean is
     (Value_Last (S) - Leading_Blanks (S) >= 5
      and then S (Leading_Blanks (S) + 1) = '[' and then S (Leading_Blanks (S) + 2) = '"'
      and then S (Value_Last (S) - 1) = '"' and then S (Value_Last (S)) = ']'
      and then (for all I in Leading_Blanks (S) + 3 .. Value_Last (S) - 2 => Is_Digit (S (I), 16)))
     with Pre => From_One (S);

   --  its digits' count
   function Hex_Count (S : Wide_Wide_String) return Big
     with Global => null,
          Pre => From_One (S) and then Is_Hex_Form (S),
          Post => Hex_Count'Result = Run (S, Leading_Blanks (S) + 2, Value_Last (S) - 2, 16);

   --  the position of the character S names, -1 for none
   function Character_Position (S : Wide_Wide_String) return Big is
     (if Is_Quoted (S) then Big (Wide_Wide_Character'Pos (S (Leading_Blanks (S) + 2)))
      elsif Is_Hex_Form (S) then (if Hex_Count (S) = Saturated or else Hex_Count (S) = Big'First then -1 else -Hex_Count (S))
      elsif Match_Position (S, Control_Names, Control_Ends) in 0 .. 64
      then Name_Position (Match_Position (S, Control_Names, Control_Ends))
      else -1)
     with Pre => From_One (S);

   --  whether S names a character at a position in First .. Last
   function Is_Character (S : String; First, Last : Big) return Boolean is
     (Character_Position (Widened (S)) /= -1 and then Character_Position (Widened (S)) in First .. Last);

   --  (of a Wide_String)
   function Is_Character (S : Wide_String; First, Last : Big) return Boolean is
     (Character_Position (Widened (S)) /= -1 and then Character_Position (Widened (S)) in First .. Last);

   --  (of a Wide_Wide_String)
   function Is_Character (S : Wide_Wide_String; First, Last : Big) return Boolean is
     (Character_Position (Widened (S)) /= -1 and then Character_Position (Widened (S)) in First .. Last);

   --  the position of the character S names
   function Character_Value (S : String; First, Last : Big) return Big is (Character_Position (Widened (S)))
     with Pre => Is_Character (S, First, Last), Post => Character_Value'Result in First .. Last;

   --  (of a Wide_String)
   function Character_Value (S : Wide_String; First, Last : Big) return Big is (Character_Position (Widened (S)))
     with Pre => Is_Character (S, First, Last), Post => Character_Value'Result in First .. Last;

   --  (of a Wide_Wide_String)
   function Character_Value (S : Wide_Wide_String; First, Last : Big) return Big is (Character_Position (Widened (S)))
     with Pre => Is_Character (S, First, Last), Post => Character_Value'Result in First .. Last;
   --  Reals: an integer's forms, or with a point between two numerals of its digits, and an
   --  exponent that may be negative (RM 2.4.1, 2.4.2). The value is the run time's reading of
   --  the literal, correctly rounded to the type's precision (to nearest, a half to even),
   --  from the literal's exact value.

   --  the point in the mantissa, 0 for none
   function Point (S : Wide_Wide_String) return Natural is
     (if Hash_1 (S) = 0 then Find (S, Literal_After (S), Mantissa_Last (S), '.', '.')
      elsif Hash_2 (S) > Hash_1 (S) then Find (S, Hash_1 (S), Hash_2 (S) - 1, '.', '.')
      else 0)
     with Pre => From_One (S);

   --  a numeral of the base, or two with a point between them, S (After + 1 .. Last)
   function Is_Mantissa (S : Wide_Wide_String; After, Last : Natural; Base : Base_Range) return Boolean is
     (if Point (S) = 0 then Is_Run (S, After, Last, Base)
      else Point (S) - 1 > After and then Point (S) < Last
           and then Is_Run (S, After, Point (S) - 1, Base) and then Is_Run (S, Point (S), Last, Base))
     with Pre => From_One (S) and then Last <= S'Length;

   --  the syntax of a real (or integer) literal, after the spaces and the sign
   function Real_Syntax (S : Wide_Wide_String) return Boolean is
     (Literal_After (S) < Value_Last (S)
      and then (if Hash_1 (S) = 0 then Is_Mantissa (S, Literal_After (S), Mantissa_Last (S), 10)
                else Hash_1 (S) - 1 > Literal_After (S)
                     and then Is_Run (S, Literal_After (S), Hash_1 (S) - 1, 10)
                     and then Hash_2 (S) - 1 > Hash_1 (S)
                     and then Base_Number (S) in -16 .. -2
                     and then Is_Mantissa (S, Hash_1 (S), Hash_2 (S) - 1, Base_Range (-Base_Number (S))))
      and then Exponent_Form (S, Negative_Allowed => True))
     with Pre => From_One (S);

   --  S as Latin-1 (a character beyond it as '?': a literal has none)
   function Narrowed (S : Wide_Wide_String) return String is
     (String'(for I in 1 .. S'Length =>
                (if Wide_Wide_Character'Pos (S (I - 1 + S'First)) <= 255
                 then Character'Val (Wide_Wide_Character'Pos (S (I - 1 + S'First))) else '?')));

   --  the run time's reading of a real literal: whether its value is within the type's range
   --  (a single's, or a double's)
   function Literal_Finite (Literal : String; Single : Boolean) return Boolean
     with Import, Convention => Ada, External_Name => "verada_rt_literal_finite", Global => null;

   --  the literal's value, correctly rounded to a double
   function Literal_Double (Literal : String) return Long_Float
     with Import, Convention => Ada, External_Name => "verada_rt_literal_double", Global => null,
          Pre => Literal_Finite (Literal, False);

   --  the literal's value, correctly rounded to a single
   function Literal_Single (Literal : String) return Float
     with Import, Convention => Ada, External_Name => "verada_rt_literal_single", Global => null,
          Pre => Literal_Finite (Literal, True);

   --  the value of a literal, rounded to a single or a double
   function Literal_Value (Literal : String; Single : Boolean) return Long_Float is
     (if Single then Long_Float (Literal_Single (Literal)) else Literal_Double (Literal))
     with Pre => Literal_Finite (Literal, Single);

   --  whether S, from 1, is a real in First .. Last (of a type of single precision, or double)
   function Is_Real_From_One (S : Wide_Wide_String; First, Last : Long_Float; Single : Boolean) return Boolean is
     (Real_Syntax (S) and then Literal_Finite (Narrowed (S), Single)
      and then Literal_Value (Narrowed (S), Single) in First .. Last)
     with Pre => From_One (S);

   --  whether S is a real in First .. Last (of a type of single precision, or double)
   function Is_Real (S : String; First, Last : Long_Float; Single : Boolean := False) return Boolean is
     (Is_Real_From_One (Widened (S), First, Last, Single));

   --  (of a Wide_String)
   function Is_Real (S : Wide_String; First, Last : Long_Float; Single : Boolean := False) return Boolean is
     (Is_Real_From_One (Widened (S), First, Last, Single));

   --  (of a Wide_Wide_String)
   function Is_Real (S : Wide_Wide_String; First, Last : Long_Float; Single : Boolean := False) return Boolean is
     (Is_Real_From_One (Widened (S), First, Last, Single));

   --  its value
   function Real_Value (S : String; First, Last : Long_Float; Single : Boolean := False) return Long_Float is
     (Literal_Value (Narrowed (Widened (S)), Single))
     with Pre => Is_Real (S, First, Last, Single),
          Post => Real_Value'Result in First .. Last;

   --  (of a Wide_String)
   function Real_Value (S : Wide_String; First, Last : Long_Float; Single : Boolean := False) return Long_Float is
     (Literal_Value (Narrowed (Widened (S)), Single))
     with Pre => Is_Real (S, First, Last, Single),
          Post => Real_Value'Result in First .. Last;

   --  (of a Wide_Wide_String)
   function Real_Value (S : Wide_Wide_String; First, Last : Long_Float; Single : Boolean := False) return Long_Float is
     (Literal_Value (Narrowed (Widened (S)), Single))
     with Pre => Is_Real (S, First, Last, Single),
          Post => Real_Value'Result in First .. Last;

   --  Fixed point: a real's forms (or an integer's), whose value is a count of the type's
   --  small, Num / Den: the literal's exact value divided by the small, truncated toward
   --  zero as a conversion to a fixed-point type is (RM 4.6(32)). First and Last are the
   --  type's bounds as counts.

   --  the run time's count of smalls in a literal, exactly: whether it is in First .. Last
   function Literal_Smalls_Fit (Literal : String; Num, Den : Big; First, Last : Integer) return Boolean
     with Import, Convention => Ada, External_Name => "verada_rt_literal_smalls_fit", Global => null;

   --  the count
   function Literal_Smalls (Literal : String; Num, Den : Big; First, Last : Integer) return Integer
     with Import, Convention => Ada, External_Name => "verada_rt_literal_smalls", Global => null,
          Pre => Literal_Smalls_Fit (Literal, Num, Den, First, Last),
          Post => Literal_Smalls'Result in First .. Last;

   --  whether S, from 1, is a fixed-point value of First .. Last smalls
   function Is_Fixed_From_One (S : Wide_Wide_String; First, Last : Integer; Num, Den : Big) return Boolean is
     (Real_Syntax (S) and then Literal_Smalls_Fit (Narrowed (S), Num, Den, First, Last))
     with Pre => From_One (S);

   --  whether S is a fixed-point value of First .. Last smalls
   function Is_Fixed (S : String; First, Last : Integer; Num, Den : Big) return Boolean is
     (Is_Fixed_From_One (Widened (S), First, Last, Num, Den));

   --  (of a Wide_String)
   function Is_Fixed (S : Wide_String; First, Last : Integer; Num, Den : Big) return Boolean is
     (Is_Fixed_From_One (Widened (S), First, Last, Num, Den));

   --  (of a Wide_Wide_String)
   function Is_Fixed (S : Wide_Wide_String; First, Last : Integer; Num, Den : Big) return Boolean is
     (Is_Fixed_From_One (Widened (S), First, Last, Num, Den));

   --  its count of smalls
   function Fixed_Value (S : String; First, Last : Integer; Num, Den : Big) return Integer is
     (Literal_Smalls (Narrowed (Widened (S)), Num, Den, First, Last))
     with Pre => Is_Fixed (S, First, Last, Num, Den),
          Post => Fixed_Value'Result in First .. Last;

   --  (of a Wide_String)
   function Fixed_Value (S : Wide_String; First, Last : Integer; Num, Den : Big) return Integer is
     (Literal_Smalls (Narrowed (Widened (S)), Num, Den, First, Last))
     with Pre => Is_Fixed (S, First, Last, Num, Den),
          Post => Fixed_Value'Result in First .. Last;

   --  (of a Wide_Wide_String)
   function Fixed_Value (S : Wide_Wide_String; First, Last : Integer; Num, Den : Big) return Integer is
     (Literal_Smalls (Narrowed (Widened (S)), Num, Den, First, Last))
     with Pre => Is_Fixed (S, First, Last, Num, Den),
          Post => Fixed_Value'Result in First .. Last;
end Verada.Scanning;
