summaryrefslogtreecommitdiff
path: root/misc/scanners.ads
blob: 4168fef39d04caefafd5ba93da0468f724b150f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

with Ada.Characters.Latin_1;
private with Ada.Strings.Unbounded, Ada.Strings.Maps;

package Scanners is

    --  This package roughly corresponds to
    --  the Java StringTokenizer class.

    --  At the moment the most significant difference
    --  is that the Next_Token function does not raise
    --  an exception if there are no tokens to return.
    --- Instead, it returns an empty string.

    type Scanner is tagged private;

    Default_Delimiters : constant String :=
        Ada.Characters.Latin_1.HT &
        Ada.Characters.Latin_1.LF &
        Ada.Characters.Latin_1.FF &
        Ada.Characters.Latin_1.CR & " ";

    function Create
           (Input         : in String;
            Delimiters    : in String  := Default_Delimiters;
            Return_Delims : in Boolean := False)
        return Scanner;

    function Has_More_Tokens
           (This : in Scanner)
        return Boolean;

    function Next_Token
           (This : in out Scanner)
        return String;

    function Remaining_Token_Count
           (This : in Scanner)
        return Natural;

    procedure Reset
           (This : in out Scanner);

private

    package UB renames Ada.Strings.Unbounded;
    package SM renames Ada.Strings.Maps;

    Default_Delimiter_Set : constant SM.Character_Set :=
        SM.To_Set (Default_Delimiters);

    type Scanner is tagged record
        Text          : UB.Unbounded_String := UB.Null_Unbounded_String;
        Position      : Positive            := 1;
        Next_Start    : Positive            := 1;
        Next_Finish   : Natural             := 0;
        Delimiters    : SM.Character_Set    := SM.Null_Set;
        Return_Delims : Boolean             := False;
    end record;

end Scanners;