File: ini_parser.e

package info (click to toggle)
smarteiffel 1.1-11
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,288 kB
  • ctags: 40,785
  • sloc: ansic: 35,791; lisp: 4,036; sh: 1,783; java: 895; ruby: 613; python: 209; makefile: 115; csh: 78; cpp: 50
file content (847 lines) | stat: -rw-r--r-- 21,503 bytes parent folder | download | duplicates (2)
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
-- This file is part of SmartEiffel The GNU Eiffel Compiler Tools and Libraries
--
-- SmartEiffel is  free software;  you can redistribute it and/or  modify it
-- under  the terms of the  GNU General Public License, as published by  the
-- Free Software Foundation; either version 2, or (at your option) any later
-- version.
-- SmartEiffel is distributed in the hope that it will be useful but WITHOUT 
-- ANY WARRANTY;  without  even the implied warranty  of MERCHANTABILITY  or
-- FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
-- more details.  You should have received a copy of  the GNU General Public
-- License along with SmartEiffel;  see the file COPYING.  If not,  write to
-- the Free Software Foundation,  Inc., 59 Temple Place - Suite 330,  Boston, 
-- MA 02111-1307, USA.
--
-- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P.
--			   - University of Nancy 1 - FRANCE
-- Copyright(C) 2003:      INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne
--			   - University of Nancy 2 - FRANCE
--
--		 Dominique COLNET, Suzanne COLLIN, Olivier ZENDRA,
--			   Philippe RIBET, Cyril ADRIAN
--
-- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
--
class INI_PARSER
   --
   -- .INI-like files parser
   --
   -- inifile -> key* [section]*
   --
   -- key -> identifier ':' value '%N'
   --
   -- section -> '[' identifier ']' '%N' key*
   --
   -- Eiffel-like comments are allowed at ends of lines and on empty lines
   --
inherit
   PARSER
      redefine
         skip1, fcp
      end

feature {INI_PARSER_VISITOR}

   accept(visitor: INI_PARSER_VISITOR) is
      do
         visitor.visit_ini_parser(Current)
      end

feature

   file_path: STRING

feature {SYSTEM_TOOLS, INSTALL} -- Parsing:

   a_inifile is
      require
         parser_buffer.is_ready
      do
         debug
            echo.put_string(once "<inifile>%N")
         end

         last_section := Void
         last_key := Void

	 line := 1
	 column := 1
	 current_line := parser_buffer.item(line)
         file_path := parser_buffer.path
	 if current_line.count = 0 then
	    cc := '%N'
	 else
	    cc := current_line.first
	 end
         skip_comments
         if cc /= '[' then
            a_keys
         end
         a_sections
         goto_next_line
         if cc /= end_of_text then
            error_handler.add_position(pos(line, column))
            fcp("Unexpected text continuation")
         elseif default_section = Void and then sections = Void then
            error_handler.add_position(pos(line, column))
            fcp("Invalid empty file")
         end

         debug
            echo.put_string(once "</inifile>%N")
         end

         parser_buffer.release
      ensure
         not parser_buffer.is_ready
      end

feature {SYSTEM_TOOLS, INSTALL}

   is_invalid: BOOLEAN

feature -- Values in the default section:

   has(key: STRING): BOOLEAN is
      do
         Result := default_section /= Void and then default_section.has(key)
      end

   item(key: STRING): STRING is
      require
         has(key)
      do
         Result := default_section.at(key)
      end

   key_iterator: ITERATOR[STRING] is
      do
         if default_section /= Void then
            Result := default_section.get_new_iterator_on_keys
         end
      end

feature -- Values in sections:

   section_has(section, key: STRING): BOOLEAN is
      local
         sec: like default_section
      do
         if sections /= Void then
            sec := sections.reference_at(section)
            Result := sec /= Void and then sec.has(key)
         end
      end

   section_item(section, key: STRING): STRING is
      require
         section_has(section, key)
      do
         Result := sections.at(section).at(key)
      end

   sections_iterator: ITERATOR[STRING] is
      do
         if sections /= Void then
            Result := sections.get_new_iterator_on_keys
         end
      end

   section_key_iterator(section: STRING): ITERATOR[STRING] is
      local
         sec: DICTIONARY[STRING, STRING]
      do
         if sections /= Void then
            sec := sections.reference_at(section)
            if sec /= Void then
               Result := sec.get_new_iterator_on_keys
            end
         end
      end

feature {NONE} -- Parser:

   last_section: STRING
   last_key: STRING

   a_sections is
      local
         stop: BOOLEAN
      do
         debug
            echo.put_string(once "<sections>%N")
         end

         from
            goto_next_line
         until
            stop
         loop
            stop := not a_section
         end

         debug
            echo.put_string(once "</sections>%N")
         end
      end

   a_section: BOOLEAN is
      do
         debug
            echo.put_string(once "<section>%N")
         end

         if skip_spaces and then skip1('[') and then skip_spaces then
            Result := a_identifier and then skip_spaces and then skip1(']') and then skip_spaces
            if Result then
               goto_next_line
               last_section := tmp_name.case_sensitive_aliased_string
               if sections = Void then
                  create sections.make
               end
               if not sections.has(last_section) then
                  sections.add(create {ORDERED_DICTIONARY[STRING, STRING]}.make, last_section)
               end
               a_keys
            end
         end

         debug
            echo.put_string(once "</section result='")
            echo.put_string(Result.out)
            echo.put_string(once "'>%N")
         end
      end

   a_keys is
      local
         stop: BOOLEAN
      do
         debug
            echo.put_string(once "<keys>%N")
         end

         from
            goto_next_line
         until
            stop
         loop
            stop := not a_key
         end

         debug
            echo.put_string(once "</keys>%N")
         end
      end

   a_key: BOOLEAN is
      local
         dummy: BOOLEAN
      do
         debug
            echo.put_string(once "<key>%N")
         end

         if a_identifier and then skip_spaces then
            if not skip1(':') then
               error_handler.add_position(pos(line, column))
               error_handler.append(once "Inserted ':'")
               error_handler.print_as_warning
            end
            dummy := skip_spaces
            last_key := tmp_name.aliased_string

            if last_value /= Void then
               echo.w_put_string(once "The key '")
               echo.w_put_string(last_key)
               echo.w_put_string(once "' is defined more than once")
               if last_section /= Void then
                  echo.w_put_string(once " in the section [")
                  echo.w_put_string(last_section)
                  echo.w_put_character(']')
               end
               echo.w_put_string(once ".%NIts retained value will be the last read.%N")
            end

            if cc = '%N' or else cc = end_of_text then
               Result := True
               set_value(once "")
            elseif cc = '"' and then a_manifest_string then
               Result := True
               set_value(last_manifest_string.to_string)
            elseif a_string then
               Result := True
               set_value(tmp_name.aliased_string)
            end
            if Result then
               goto_next_line
            end
         end

         debug
            echo.put_string(once "</key result='")
            echo.put_string(Result.out)
            echo.put_string(once "'>%N")
         end
      end

   goto_next_line is
      do
         from
         until
            not skip1('%N')
         loop
            skip_comments
         end
      end

   set_value(value: STRING) is
      do
         if last_section = Void then
            if default_section = Void then
               create default_section.make
            end
            default_section.add(value, last_key)
         else
            sections.at(last_section).add(value, last_key)
         end
      end

   last_value: STRING is
      do
         if last_section = Void then
            if default_section /= Void then
               Result := default_section.reference_at(last_key)
            end
         elseif sections.has(last_section) then
            Result := sections.at(last_section).reference_at(last_key)
         end
      end

feature {NONE}

   last_manifest_string: MANIFEST_STRING

   a_identifier: BOOLEAN is
	 -- Case-sensitive identifiers, used for section names and key
	 -- names. The dot is not eiffel-like, but useful in many cases.
      local
	 stop: BOOLEAN
      do
         debug
            echo.put_string(once "<a_identifier>%N")
         end

	 if cc.is_letter then
	    from
	       tmp_name.reset(pos(line,column))
	       tmp_name.extend(cc)
	    until
	       stop
	    loop
	       next_char
	       inspect
		  cc
	       when 'a'..'z' then
		  tmp_name.extend(cc)
	       when 'A'..'Z' then
		  tmp_name.extend(cc)
	       when '0'..'9','_','.' then
		  tmp_name.extend(cc)
	       else
		  stop := True
	       end
	    end
            Result := True
	 end

         debug
            echo.put_string(once "</a_identifier result='")
            echo.put_string(Result.out)
            if Result then
               echo.put_string(once "' identifier='")
               echo.put_string(tmp_name.aliased_string)
            end
            echo.put_string(once "'>%N")
         end
      end

   a_string: BOOLEAN is
	 -- Some string
      local
         state, l, c: INTEGER
      do
         debug
            echo.put_string(once "<a_string>%N")
         end

         from
            tmp_name.reset(pos(line,column))
            state := 0
         until
            cc = '%N' or else cc = end_of_text
         loop
            Result := True
            inspect
               state
            when 0 then
               -- reading "normal" characters
               inspect
                  cc
               when ' ', '%T' then
                  buffer.extend(cc)
                  state := 1
               when '-' then
                  l := line
                  c := column
                  state := 2
               else
                  tmp_name.extend(cc)
               end
            when 1 then
               -- reading spaces. They will be appended if some normal
               -- characters are still to be found after
               inspect
                  cc
               when ' ', '%T' then
                  buffer.extend(cc)
               when '-' then
                  l := line
                  c := column
                  state := 2
               else
                  tmp_name.append(buffer)
                  buffer.clear
                  tmp_name.extend(cc)
                  state := 0
               end
            when 2 then
               -- just read a '-'. If a second is read, then it is a comment;
               -- the string is finished
               inspect cc
               when '-'then
                  state := 3
               when ' ', '%T' then
                  tmp_name.append(buffer)
                  tmp_name.extend('-')
                  buffer.clear
                  buffer.extend(cc)
                  state := 1
               else
                  tmp_name.append(buffer)
                  tmp_name.extend('-')
                  tmp_name.extend(cc)
                  buffer.clear
                  state := 0
               end
            when 3 then
               -- reading a comment
            end
            next_char
         end

         debug
            echo.put_string(once "</a_string result='")
            echo.put_string(Result.out)
            if Result then
               echo.put_string(once "' string='")
               echo.put_string(tmp_name.aliased_string)
            end
            echo.put_string(once "'>%N")
         end
      end

   skip_spaces: BOOLEAN is
         -- returns always True
      local
         stop: BOOLEAN
      do
         from
         until
            stop
         loop
            inspect cc
            when ' ', '%T' then
               next_char
            else
               stop := True
            end
         end
         Result := True
      ensure
         Result
      end

   skip1(char: CHARACTER): BOOLEAN is
         -- Redefined to not skip comments
      do
	 if char = cc then
	    start_line := line
	    start_column := column
	    Result := true
	    next_char
	 end
      end

   skip2(c1, c2: CHARACTER): BOOLEAN is
      do
	 if c1 = cc then
	    start_line := line
	    start_column := column
	    next_char
	    if c2 = cc then
	       Result := True
	       next_char
	       skip_comments
	    else
	       cc := c1
	       column := start_column
	    end
	 end
      end

   a_manifest_string: BOOLEAN is
      local
	 state, l, c: INTEGER; once_flag, unicode_flag, stop: BOOLEAN
	 ascii_code, unicode, digit_count: INTEGER
	 line_view: STRING
	 multi_line_end: STRING; left_alignment, current_alignment: INTEGER
      do
         debug
            echo.put_string(once "<a_manifest_string>%N")
         end

	 l := line; c := column
	 if a_keyword(fz_once) then once_flag := True end
	 if cc = 'U' then
	    unicode_flag := True; next_char
	 end
	 if cc = '%"' then
	    Result := True
	    if smart_eiffel.short_flag or else smart_eiffel.pretty_flag then
	       create line_view.make(32)
	    end
	    from buffer.clear until
	       state > 9
	    loop
	       next_char
	       inspect
		  state
	       when 0 then -- Somewhere inside the manifest string.
		  inspect
		     cc
		  when '%N' then
		     from until	multi_line_end /= Void
		     loop
			if buffer.is_empty then
			   fcp(em8)
			else
			   inspect
			      buffer.last
			   when ' ', '%T' then
			      buffer.remove_last(1)
			      wcp(em42)
			   when '{', '[' then
			      multi_line_end := once "]foo%""
			      multi_line_end.clear
			      if buffer.last = '{' then
				 multi_line_end.extend('}')
				 left_alignment := 1
				 state := 8
			      else
				 multi_line_end.extend(']')
				 state := 7
			      end
			      multi_line_end.append(buffer)
			      current_alignment := 1
			      multi_line_end.remove_last(1)
			      multi_line_end.extend('%"')
			      buffer.clear
			   else
			      fcp(em8)
			   end
			end
		     end
		  when '%"' then
		     state := 11
		  when '%%' then
		     state := 1
		  else
		     buffer.extend(cc)
		  end
	       when 1 then -- Just after a '%%'.
		  state := 0
		  inspect
		     cc
		  when '%N' then
		     state := 3
		  when 'A' then buffer.extend('%A')
		  when 'B' then buffer.extend('%B')
		  when 'C' then buffer.extend('%C')
		  when 'D' then buffer.extend('%D')
		  when 'F' then buffer.extend('%F')
		  when 'H' then buffer.extend('%H')
		  when 'L' then buffer.extend('%L')
		  when 'N' then buffer.extend('%N')
		  when 'Q' then buffer.extend('%Q')
		  when 'R' then buffer.extend('%R')
		  when 'S' then buffer.extend('%S')
		  when 'T' then buffer.extend('%T')
		  when 'U' then buffer.extend('%U')
		  when 'V' then buffer.extend('%V')
		  when '%%' then buffer.extend('%%')
		  when '%'' then buffer.extend('%'')
		  when '%"' then buffer.extend('%"')
		  when '(' then buffer.extend('%(')
		  when ')' then buffer.extend('%)')
		  when '<' then buffer.extend('%<')
		  when '>' then buffer.extend('%>')
		  when '/' then
		     ascii_code := 0
		     digit_count := 0
		     state := 4
		  when ' ','%T' then
		     state := 2
		  else
		     fcp(em37)
		     state := 0
		  end
	       when 2 then -- Extended form. At end of line.
		  inspect
		     cc
		  when '%N' then
		     state := 3
		  when ' ','%T' then
		     wcp(em42)
		  else
		     fcp("In extended form of manifest string.%
			 %Bad character after '%%'.")
		  end
	       when 3 then  -- Extended form. At beginning of line.
		  inspect
		     cc
		  when ' ','%T' then
		  when '%%' then
		     state := 0
		  when '%N' then
		     fcp(em8)
		  else
		     fcp("In extended form of manifest string.%
			 % Bad character before '%%'.")
		  end
	       when 4 then  -- Inside ascii code. After opening '/'.
		  inspect
		     cc
		  when '0' .. '9' then
		     digit_count := digit_count + 1
		     ascii_code := ascii_code * 10 + cc.decimal_value
		  when 'x' then
		     if digit_count = 1 and ascii_code = 0 then
			digit_count := 0
			state := 5
		     else
			fcp(em38)
		     end
		  when 'U' then
		     if digit_count = 0 then
			next_char
			if cc = 'x' then
			   state := 6
			   unicode := 0
			   digit_count := 0
			else fcp(em38) end
		     else
			fcp(em38)
		     end
		  when '/' then
		     buffer.extend(ascii_code.to_character)
		     state := 0
		     if digit_count = 0 then
			ecp(em39)
		     elseif ascii_code > Maximum_character_code then
			ascii_code := 0
			ecp(em40)
		     end
		  else
		     fcp(em38)
		  end
	       when 5 then  -- Inside ascii code. After /0x
		  if cc.is_hexadecimal_digit then
		     ascii_code := ascii_code * 16 + cc.hexadecimal_value
		     digit_count := digit_count + 1
		     if digit_count.even then
			buffer.extend(ascii_code.to_character)
			ascii_code := 0
		     end
		  elseif cc = '/' then
		     state := 0
		     if digit_count = 0 then
			ecp(em45)
		     elseif digit_count.odd then
			ecp("You must use an even number of hexadecimal %
                            %digits to denote a sequence of CHARACTERs.")
		     end
		  else
		     fcp(em45)
		  end
	       when 6 then  -- Inside unicode code. After /Ux
		  if cc.is_hexadecimal_digit then
		     unicode := unicode * 16 + cc.hexadecimal_value
		     digit_count := digit_count + 1
		  elseif cc = '/' then
		     state := 0
		     if digit_count = 0 then
			ecp(em46)
		     elseif digit_count > 8 then
			ecp("Too long hexadecimal sequence %
                            %for a single unicode value.")
		     else
			unicode_string_buffer.clear
			if unicode_string_buffer.valid_unicode(unicode) then
			   unicode_string_buffer.add_last(unicode)
			   unicode_string_buffer.utf8_encode_in(buffer)
			else
			   ecp("Invalid unicode notation (see also http://%
			       %www.unicode.org as well as feature %
			       %{UNICODE_STRING}.valid_unicode).")
			end
		     end
		  else
		     fcp(em46)
		  end
	       when 7 then -- Just after the multi-line opener, in left
		  -- alignment mode.
		  check left_alignment = 0 end
		  inspect
		     cc
		  when ' ', '%T' then
		     current_alignment := current_alignment + 1
		  when '%N' then
		     buffer.extend('%N'); current_alignment := 1
		  when end_of_text then
		     error_handler.add_position(pos(l,c))
		     fcp(em41)
		  else
		     buffer.extend(cc)
		     if left_alignment = 0 then
			left_alignment := current_alignment
		     end
		     state := 8
		  end
	       when 8 then -- Inside multi-line manifest string.
		  current_alignment := current_alignment + 1
		  inspect
		     cc
		  when ' ', '%T' then
		     if current_alignment >= left_alignment then
			buffer.extend(cc)
		     end
		  when '%N' then
		     buffer.extend(cc); current_alignment := 0
		  when end_of_text then
		     error_handler.add_position(pos(l,c))
		     fcp(em41)
		  when '%"' then
		     buffer.extend(cc)
		     if buffer.has_suffix(multi_line_end) then
			state := 10
		     end
		  else
		     if current_alignment < left_alignment then
			if cc = multi_line_end.first then
			   buffer.extend(cc)
			   state := 9
			else
			   error_handler.add_position(pos(l,c))
			   fcp(em41)
			end
		     else
			buffer.extend(cc)
		     end
		  end
	       when 9 then -- Inside multi-line closer.
		  inspect
		     cc
		  when end_of_text then
		     error_handler.add_position(pos(l,c))
		     fcp(em41)
		  when '%"' then
		     buffer.extend(cc)
		     if buffer.has_suffix(multi_line_end) then
			state := 10
		     end
		  else
		     buffer.extend(cc)
		  end
	       end
	    end
	    if state = 10 then -- Good verbatim string.
	       buffer.remove_suffix(multi_line_end)
	       from
	       until stop
	       loop
		  if buffer.is_empty then
		     stop := True
		  else
		     stop := buffer.last = '%N'
		     buffer.remove_last(1)
		  end
	       end
	    end
	    create last_manifest_string.make(pos(l,c),
	                                     once_flag, unicode_flag, buffer)
	    next_char
	    skip_comments
	 end

         debug
            echo.put_string(once "</a_manifest_string result='")
            echo.put_string(Result.out)
            if Result then
               echo.put_string(once "' string='")
               echo.put_string(last_manifest_string.to_string)
            end
            echo.put_string(once "'>%N")
         end
      end

   buffer: STRING is
      once
	 create Result.make(80)
      end

   unicode_string_buffer: UNICODE_STRING is
      once
	 create Result.make_empty
      end

   ecp(msg: STRING) is
	 -- Error at current position.
      do
	 error(current_position, msg)
      end

   fcp(msg: STRING) is
      do
         if is_install.item then
            is_invalid := True
         else
            Precursor(msg)
         end
      end

   pos(l, c: INTEGER): POSITION is
      do
	 Result.ini_file(l,c)
      end

feature {NONE} -- Sections:

   default_section: ORDERED_DICTIONARY[STRING, STRING]
   sections: ORDERED_DICTIONARY[ORDERED_DICTIONARY[STRING, STRING], STRING]

invariant

   is_invalid implies is_install.item

end