gnoga_2.1.2_5f127c56/deps/zanyblue/src/text/zanyblue-text-properties_parser.adb

  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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
--  -*- coding: utf-8 -*-
--
--  ZanyBlue, an Ada library and framework for finite element analysis.
--
--  Copyright (c) 2012, 2016, Michael Rohan <mrohan@zanyblue.com>
--  All rights reserved.
--
--  Redistribution and use in source and binary forms, with or without
--  modification, are permitted provided that the following conditions
--  are met:
--
--    * Redistributions of source code must retain the above copyright
--      notice, this list of conditions and the following disclaimer.
--
--    * Redistributions in binary form must reproduce the above copyright
--      notice, this list of conditions and the following disclaimer in the
--      documentation and/or other materials provided with the distribution.
--
--    * Neither the name of ZanyBlue nor the names of its contributors may
--      be used to endorse or promote products derived from this software
--      without specific prior written permission.
--
--  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
--  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
--  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
--  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
--  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
--  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
--  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
--  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
--  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
--  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
--  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--

with Ada.Exceptions;
with Ada.Characters.Latin_1;
with Ada.Characters.Handling;
with Ada.Containers.Indefinite_Hashed_Maps;
with UXStrings.Text_IO.Text_Streams;
with ZanyBlue.OS;
with ZanyBlue.Text.Utils;
with UXStrings.Hash;

package body ZanyBlue.Text.Properties_Parser is

   use Ada.Exceptions;
   use Ada.Characters.Latin_1;
   use Ada.Characters.Handling;
   use UXStrings.Text_IO.Text_Streams;
   use ZanyBlue.OS;
   use ZanyBlue.Text.Utils;

   ------------------
   -- Get_N_Errors --
   ------------------

   function Get_N_Errors
     (Handler : Parser_Handler_Type)
      return Natural
   is
   begin
      return Handler.N_Errors;
   end Get_N_Errors;

   --------------------
   -- Get_N_Messages --
   --------------------

   function Get_N_Messages
     (Handler : Parser_Handler_Type)
      return Natural
   is
   begin
      return Handler.N_Messages;
   end Get_N_Messages;

   ----------------------
   -- Increment_Errors --
   ----------------------

   procedure Increment_Errors
     (Handler   : in out Parser_Handler_Type;
      By_Amount :        Natural := 1)
   is
   begin
      Handler.N_Errors := Handler.N_Errors + By_Amount;
   end Increment_Errors;

   ------------------------
   -- Increment_Messages --
   ------------------------

   procedure Increment_Messages (Handler : in out Parser_Handler_Type) is
   begin
      Handler.N_Messages := Handler.N_Messages + 1;
   end Increment_Messages;

   -----------
   -- Parse --
   -----------

   procedure Parse
     (Handler   : in out Parser_Handler_Type'Class;
      File_Name :        String;
      Facility  :        String;
      Locale    :        Locale_Type)
   is
   begin
      Parse (Handler, File_Name, Facility, Locale, Locale);
   end Parse;

   -----------
   -- Parse --
   -----------

   procedure Parse
     (Handler       : in out Parser_Handler_Type'Class;
      File_Name     :        String;
      Facility      :        String;
      Locale        :        Locale_Type;
      Source_Locale :        Locale_Type)
   is
      Source_File : UXStrings.Text_IO.File_Type;
      Opened      : Boolean := False;
   begin
      Open (Source_File, UXStrings.Text_IO.In_File, File_Name);
      Opened := True;
      Parse (Handler, Source_File, File_Name, Facility, Locale, Source_Locale);
      UXStrings.Text_IO.Close (Source_File);
   exception
      when others =>
         if Opened then
            UXStrings.Text_IO.Close (Source_File);
         end if;
         raise;
   end Parse;

   -----------
   -- Parse --
   -----------

   procedure Parse
     (Handler     : in out Parser_Handler_Type'Class;
      Source_File : in out UXStrings.Text_IO.File_Type;
      File_Name   :        String;
      Facility    :        String;
      Locale      :        Locale_Type)
   is
   begin
      Parse (Handler, Source_File, File_Name, Facility, Locale, Locale);
   end Parse;

   -----------
   -- Parse --
   -----------

   procedure Parse
     (Handler       : in out Parser_Handler_Type'Class;
      Source_File   : in out UXStrings.Text_IO.File_Type;
      File_Name     :        String;
      Facility      :        String;
      Locale        :        Locale_Type;
      Source_Locale :        Locale_Type)
   is

      package Key_To_Line_Maps is new Ada.Containers.Indefinite_Hashed_Maps
        (Key_Type => String, Element_Type => Natural, Equivalent_Keys => "=",
         Hash     => UXStrings.Hash);

      Key_Definitions : Key_To_Line_Maps.Map;
      Source_Stream   : Stream_Access;
      Cur_Character   : Character;
      Cur_Key         : String;
      Cur_Value       : String;
      Cur_Line        : Natural  := 1;
      Def_Line        : Positive := 1;

      procedure Kill_Line;
      --  Kill, ignore, a line when an error has been encountered.

      procedure New_Message
        (Key   : String;
         Value : String);
      --  Perform the call back to define a new key/value pair.

      function Next
        (Require_ISO_646 : Boolean := True)
         return Character;
      --  Return the next character from the input stream.

      ---------------
      -- Kill_Line --
      ---------------

      procedure Kill_Line is
      begin
         Line_Characters :
         loop
            case Next (Require_ISO_646 => False) is
               when LF | CR | EOT =>
                  exit Line_Characters;
               when others =>
                  null;
            end case;
         end loop Line_Characters;
      end Kill_Line;

      -----------------
      -- New_Message --
      -----------------

      procedure New_Message
        (Key   : String;
         Value : String)
      is

         use Key_To_Line_Maps;

         Position : constant Cursor := Find (Key_Definitions, Key);
         WKey     : constant String := Key;
      begin
         if Position /= No_Element then
            Handler.Increment_Errors;
            Handler.Duplicate_Key
              (Facility, WKey, Locale, File_Name, Def_Line,
               Element (Position));
         end if;
         Handler.Increment_Messages;
         Handler.Add_Key_Value
           (Facility, WKey, Unescape_String (Value), Locale, Source_Locale,
            File_Name, Def_Line);
         Include (Key_Definitions, Key, Def_Line);
      end New_Message;

      ----------
      -- Next --
      ----------

      function Next
        (Require_ISO_646 : Boolean := True)
         return Character
      is
      begin
         if not UXStrings.Text_IO.End_Of_File (Source_File) then
            Character'Read (Source_Stream, Cur_Character);
            if Require_ISO_646 and then not Is_ISO_646 (Cur_Character) then
               Handler.Increment_Errors;
               Handler.Invalid_Character
                 (Facility, File_Name, Cur_Line, Cur_Character);
            end if;
         else
            Cur_Character := EOT;
         end if;
         if Cur_Character = LF then
            Cur_Line := Cur_Line + 1;
         end if;
         return Cur_Character;
      end Next;

   begin
      Source_Stream := Stream (Source_File);

      <<Start>>
      case Next is
         when EOT =>
            goto EOF;
         when Space | HT | VT | FF =>
            goto Start;
         when LF | CR =>
            goto Start;
         when '#' =>
            goto Comment;
         when others =>
            Cur_Key   := From_Latin_1 (Cur_Character);
            Cur_Value := "";
            goto Key;
      end case;

      <<Key>>
      Def_Line := Cur_Line;
      case Next is
         when '\' =>
            goto Key_Escape;
         when CR | LF | EOT =>
            goto Finish;
         when Space | HT | VT | FF =>
            goto Key_End_1;
         when ':' | '=' =>
            goto Key_End_2;
         when others =>
            Append (Cur_Key, From_Latin_1 (Cur_Character));
            goto Key;
      end case;

      <<Key_Escape>>
      case Next is
         when 'r' =>
            Append (Cur_Key, From_Latin_1 (CR));
            goto Key;
         when 'n' =>
            Append (Cur_Key, From_Latin_1 (LF));
            goto Key;
         when others =>
            Append (Cur_Key, From_Latin_1 (Cur_Character));
            goto Key;
      end case;

      <<Key_End_1>>
      case Next is
         when LF | CR | EOT =>
            goto Finish;
         when Space | HT | VT | FF =>
            goto Key_End_1;
         when '=' | ':' =>
            goto Key_End_2;
         when others =>
            goto Value;
      end case;

      <<Key_End_2>>
      case Next is
         when LF | CR | EOT =>
            goto Finish;
         when Space | HT | VT | FF =>
            goto Key_End_2;
         when '\' =>
            goto Value_Escape;
         when others =>
            goto Value;
      end case;

      <<Value>>
      Append (Cur_Value, From_Latin_1 (Cur_Character));
      case Next is
         when CR | LF | EOT =>
            goto Finish;
         when '\' =>
            goto Value_Escape;
         when others =>
            goto Value;
      end case;

      <<Value_Escape>>
      case Next is
         when CR =>
            goto Value_Escape_CR;
         when LF =>
            goto Value_Escape_LF;
         when others =>
            Append (Cur_Value, '\');
            goto Value;
      end case;

      <<Value_Escape_CR>>
      case Next is
         when LF | Space | HT =>
            goto Value_Escape_Space;
         when others =>
            goto Value;
      end case;

      <<Value_Escape_LF>>
      case Next is
         when CR | Space | HT =>
            goto Value_Escape_Space;
         when others =>
            goto Value;
      end case;

      <<Value_Escape_Space>>
      case Next is
         when Space | HT =>
            goto Value_Escape_Space;
         when LF | CR =>
            goto Finish;
         when others =>
            goto Value;
      end case;

      <<Comment>>
      Kill_Line;
      goto Start;

      <<Finish>>
      New_Message (Cur_Key, Cur_Value);
      goto Start;

      <<EOF>>
      return;

   exception
      when Error : Unicode_Format_Error =>
      --  Augment the exception with the file name ane line number
         Handler.Increment_Errors;
         Handler.Invalid_Definition
           (Facility, Locale, File_Name, Def_Line,
            From_Latin_1 (Exception_Message (Error)));
   end Parse;

   ----------------------
   -- Reset_N_Messages --
   ----------------------

   procedure Reset_N_Messages (Handler : in out Parser_Handler_Type) is
   begin
      Handler.N_Messages := 0;
   end Reset_N_Messages;

end ZanyBlue.Text.Properties_Parser;