summaryrefslogtreecommitdiff
path: root/src/packrat-tokens.adb
blob: 005cba61b420594c0e4d0ab7c2b01abc6d8d0b97 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196


--  This source is licensed under the Sunset License v1.0


with

    Ada.Characters.Latin_1;


package body Packrat.Tokens is


    package SU renames Ada.Strings.Unbounded;
    package Latin renames Ada.Characters.Latin_1;





    function "<"
           (Left, Right : in Value_Holders.Holder)
        return Boolean is
    begin
        return Left.Element < Right.Element;
    end "<";


    function "<"
           (Left, Right : in Token_Type)
        return Boolean is
    begin
        if Left.Start_At = Right.Start_At then
            if Left.Identifier = Right.Identifier then
                return Left.Token_Value < Right.Token_Value;
            else
                return Left.Identifier < Right.Identifier;
            end if;
        else
            return Left.Start_At < Right.Start_At;
        end if;
    end "<";


    function "<"
           (Left, Right : in Token_Array)
        return Boolean
    is
        function LT is new Array_Less_Than
           (Base_Type  => Token_Type,
            Array_Type => Token_Array);
    begin
        return LT (Left, Right);
    end "<";


    function "<"
           (Left, Right : Finished_Token_Type)
        return Boolean
    is
        function LT is new Array_Less_Than
           (Base_Type  => Element_Type,
            Array_Type => Element_Array);
    begin
        if Start (Left.Token) = Start (Right.Token) then
            if Left.Finish = Right.Finish then
                if Label (Left.Token) = Label (Right.Token) then
                    return Value (Left.Token) < Value (Right.Token);
                else
                    return Label (Left.Token) < Label (Right.Token);
                end if;
            else
                return Left.Finish < Right.Finish;
            end if;
        else
            return Start (Left.Token) < Start (Right.Token);
        end if;
    end "<";


    function "<"
           (Left, Right : in Finished_Token_Array)
        return Boolean
    is
        function LT is new Array_Less_Than
           (Base_Type  => Finished_Token_Type,
            Array_Type => Finished_Token_Array);
    begin
        return LT (Left, Right);
    end "<";




    function Create
           (Ident  : in Label_Enum;
            Start  : in Positive;
            Value  : in Element_Array)
        return Token_Type is
    begin
        return This : Token_Type do
            This.Identifier := Ident;
            This.Start_At := Start;
            This.Token_Value := Value_Holders.To_Holder (Value);
        end return;
    end Create;





    function Debug_String
           (This : in Token_Type)
        return String
    is
        Result : SU.Unbounded_String := +"";
    begin
        SU.Append (Result, "Token " & Label_Enum'Image (This.Identifier) &
            " at input position" & Integer'Image (This.Start_At) & " with value length" &
            Integer'Image (This.Token_Value.Constant_Reference.Element'Length) & Latin.LF);
        return -Result;
    end Debug_String;


    function Debug_String
           (This : in Finished_Token_Type)
        return String
    is
        Result : SU.Unbounded_String := +"";
    begin
        SU.Append (Result, "Token " & Label_Enum'Image (This.Token.Identifier) &
            " at input position" & Integer'Image (This.Token.Start_At) & " to" &
            Integer'Image (This.Finish) & " with value length" &
            Integer'Image (This.Token.Token_Value.Constant_Reference.Element'Length) &
            Latin.LF);
        return -Result;
    end Debug_String;





    function Label
           (This : in Token_Type)
        return Label_Enum is
    begin
        return This.Identifier;
    end Label;


    function Start
           (This : in Token_Type)
        return Positive is
    begin
        return This.Start_At;
    end Start;


    function Value
           (This : in Token_Type)
        return Element_Array is
    begin
        return This.Token_Value.Element;
    end Value;





    function Is_Label
           (This : in Token_Type)
        return Boolean is
    begin
        return This.Identifier = Ident;
    end Is_Label;


    function Is_Start
           (This : in Token_Type)
        return Boolean is
    begin
        return This.Start_At = Start;
    end Is_Start;


    function Is_Value
           (This : in Token_Type)
        return Boolean is
    begin
        return This.Token_Value.Element = Value;
    end Is_Value;


end Packrat.Tokens;