summaryrefslogtreecommitdiff
path: root/src/packrat-errors.adb
blob: 82f02f986ce0345c2ed258cb15dd462b98eb11fd (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316


with

    Ada.Strings.Fixed,
    Ada.Characters.Handling,
    Ada.Characters.Latin_1;


separate (Packrat)
package body Errors is


    package SU renames Ada.Strings.Unbounded;
    package SF renames Ada.Strings.Fixed;
    package CH renames Ada.Characters.Handling;
    package Latin renames Ada.Characters.Latin_1;





    function Image
           (Num : in Natural)
        return String
    is
        Raw : String := Integer'Image (Num);
    begin
        return Raw (Raw'First + 1 .. Raw'Last);
    end Image;


    function Is_Upper
           (Char : in Character)
        return Boolean is
    begin
        return Char >= 'A' and Char <= 'Z';
    end Is_Upper;


    function Is_Lower
           (Char : in Character)
        return Boolean is
    begin
        return Char >= 'a' and Char <= 'z';
    end Is_Lower;


    function Is_Letter
           (Char : in Character)
        return Boolean is
    begin
        return Is_Upper (Char) or Is_Lower (Char);
    end Is_Letter;


    function Is_Digit
           (Char : in Character)
        return Boolean is
    begin
        return Char >= '0' and Char <= '9';
    end Is_Digit;





    generic
        Message : in String;
    package Parts is

        function Length
            return Natural;

        procedure Reset_Position;

        procedure Set_Position
               (To : in Positive);

        procedure Next_ID_Token
               (Start  : out Positive;
                Finish : out Natural);

        procedure Next_Pos_Token
               (Start  : out Positive;
                Finish : out Natural);

        procedure Next_Full_Token
               (Start  : out Positive;
                Finish : out Natural);

    end Parts;
    package body Parts is separate;





    function Valid_Identifier
           (Check : in String)
        return Boolean
    is
        All_Digit : Boolean;
    begin
        if Check'Length < 1 then
            return False;
        end if;

        --  Have to relax requirements to allow Integers and such
        --  since there is no way to require a generic parameter to
        --  *only* be an enumeration.
        All_Digit := True;
        for N in Integer range Check'First .. Check'Last loop
            if not Is_Digit (Check (N)) then
                All_Digit := False;
                exit;
            end if;
        end loop;
        if All_Digit then
            return True;
        end if;

        --  Regular checks for a valid identifier.
        if not Is_Letter (Check (Check'First)) then
            return False;
        end if;
        for N in Integer range Check'First + 1 .. Check'Last - 1 loop
            if  not Is_Letter (Check (N)) and
                not Is_Digit (Check (N)) and
                Check (N) /= '_'
            then
                return False;
            end if;
            if  Check (N) = '_' and
                ((not Is_Letter (Check (N - 1)) and not Is_Digit (Check (N - 1))) or
                 (not Is_Letter (Check (N + 1)) and not Is_Digit (Check (N + 1))))
            then
                return False;
            end if;
        end loop;
        if not Is_Letter (Check (Check'Last)) and not Is_Digit (Check (Check'Last)) then
            return False;
        end if;
        return True;
    end Valid_Identifier;


    function Valid_Identifier
           (Check : in SU.Unbounded_String)
        return Boolean is
    begin
        return Valid_Identifier (-Check);
    end Valid_Identifier;


    function Valid_Identifier_Array
           (Check : in Error_Info_Array)
        return Boolean is
    begin
        for EI of Check loop
            if not Valid_Identifier (-EI.Symbol) then
                return False;
            end if;
        end loop;
        return True;
    end Valid_Identifier_Array;


    function Valid_Message
           (Check : in String)
        return Boolean
    is
        S_Place, P_Place : Natural := 1;
    begin
        while S_Place <= Check'Length loop
            if Check (S_Place) /= 's' then
                return False;
            end if;
            P_Place := SF.Index (Check, "p", S_Place);
            if (P_Place < S_Place) then
                return False;
            end if;
            declare
                ID : String := Check (S_Place + 1 .. P_Place - 1);
            begin
                if not Valid_Identifier (ID) or ID /= CH.To_Upper (ID) then
                    return False;
                end if;
            end;
            P_Place := P_Place + 1;
            if P_Place > Check'Last or else not Is_Digit (Check (P_Place)) then
                return False;
            end if;
            loop
                P_Place := P_Place + 1;
                exit when P_Place > Check'Last or else not Is_Digit (Check (P_Place));
            end loop;
            S_Place := P_Place;
        end loop;
        return True;
    end Valid_Message;


    function Debug_String
           (This : in Error_Message)
        return String
    is
        Result : SU.Unbounded_String := +"";
        Info : Error_Info_Array := Decode (This);
    begin
        for E of Info loop
            SU.Append (Result, "Expected " & (-E.Symbol) &
                " at input position" & Integer'Image (E.Position) & Latin.LF);
        end loop;
        return -Result;
    end Debug_String;





    function Join
           (Left, Right : in Error_Message)
        return Error_Message
    is
        package Left_Parts is new Parts (Left);
        package Right_Parts is new Parts (Right);

        LS, LF, RS, RF : Natural;
        Add : Boolean;

        Result : SU.Unbounded_String := +Left;
    begin
        for R in Integer range 1 .. Right_Parts.Length loop
            Add := True;
            Right_Parts.Next_Full_Token (RS, RF);
            for L in Integer range 1 .. Left_Parts.Length loop
                Left_Parts.Next_Full_Token (LS, LF);
                if Left (LS .. LF) = Right (RS .. RF) then
                    Add := False;
                    exit;
                end if;
            end loop;
            if Add then
                SU.Append (Result, +Right (RS .. RF));
            end if;
            Left_Parts.Reset_Position;
        end loop;
        return -Result;
    end Join;





    function Encode
           (Name : in String;
            Pos  : in Natural)
        return Error_Message is
    begin
        return "s" & CH.To_Upper (Name) & "p" & Image (Pos);
    end Encode;


    function Encode
           (Name : in SU.Unbounded_String;
            Pos  : in Natural)
        return Error_Message is
    begin
        return Encode (-Name, Pos);
    end Encode;


    function Encode
           (Info : in Error_Info)
        return Error_Message is
    begin
        return Encode (-Info.Symbol, Info.Position);
    end Encode;


    function Encode_Array
           (Info : in Error_Info_Array)
        return Error_Message
    is
        Result : SU.Unbounded_String := SU.To_Unbounded_String (0);
    begin
        for EI of Info loop
            SU.Append (Result, (Encode (-EI.Symbol, EI.Position)));
        end loop;
        return -Result;
    end Encode_Array;


    function Decode
           (Msg : in Error_Message)
        return Error_Info_Array
    is
        package Msg_Parts is new Parts (Msg);

        Result : Error_Info_Array (1 .. Msg_Parts.Length);
        Start, Finish : Natural;
    begin
        for EI of Result loop
            Msg_Parts.Next_ID_Token (Start, Finish);
            EI.Symbol := +Msg (Start .. Finish);
            Msg_Parts.Next_Pos_Token (Start, Finish);
            EI.Position := Integer'Value (Msg (Start .. Finish));
        end loop;
        return Result;
    end Decode;


end Errors;