Natools

Check-in [0c604fe54e]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:s_expressions-templates-generic_integers: change prefix and suffix syntax to make shared values clearer
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 0c604fe54e4e2ffad24da3194bdb82186ffdcf1d
User & Date: nat 2014-09-27 16:30:56
Context
2014-09-28
14:25
s_expressions-templates-tests-integers: test the new prefix and suffix features check-in: 3f152ff81d user: nat tags: trunk
2014-09-27
16:30
s_expressions-templates-generic_integers: change prefix and suffix syntax to make shared values clearer check-in: 0c604fe54e user: nat tags: trunk
2014-09-26
21:13
s_expressions-templates-generic_integers: add prefixes and suffixes to rendered numbers check-in: b0204e546f user: nat tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/natools-s_expressions-templates-generic_integers.adb.

28
29
30
31
32
33
34






35
36
37
38
39
40
41


   procedure Insert_Image
     (State : in out Format;
      Context : in Meaningless_Type;
      Image : in Atom);







   procedure Update_Format
     (State : in out Format;
      Context : in Meaningless_Type;
      Name : in Atom;
      Arguments : in out Lockable.Descriptor'Class);









>
>
>
>
>
>







28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47


   procedure Insert_Image
     (State : in out Format;
      Context : in Meaningless_Type;
      Image : in Atom);

   procedure Update_Image
     (State : in out Format;
      Context : in Meaningless_Type;
      Name : in Atom;
      Arguments : in out Lockable.Descriptor'Class);

   procedure Update_Format
     (State : in out Format;
      Context : in Meaningless_Type;
      Name : in Atom;
      Arguments : in out Lockable.Descriptor'Class);


49
50
51
52
53
54
55





























56
57
58
59
60
61
62
      Image : in Atom)
   is
      pragma Unreferenced (Context);
   begin
      State.Append_Image (Image);
   end Insert_Image;































   procedure Update_Format
     (State : in out Format;
      Context : in Meaningless_Type;
      Name : in Atom;
      Arguments : in out Lockable.Descriptor'Class)
   is







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
      Image : in Atom)
   is
      pragma Unreferenced (Context);
   begin
      State.Append_Image (Image);
   end Insert_Image;


   procedure Update_Image
     (State : in out Format;
      Context : in Meaningless_Type;
      Name : in Atom;
      Arguments : in out Lockable.Descriptor'Class)
   is
      pragma Unreferenced (Context);
      Value : T;
   begin
      begin
         Value := T'Value (To_String (Name));
      exception
         when Constraint_Error =>
            return;
      end;

      case Arguments.Current_Event is
         when Events.Add_Atom =>
            State.Set_Image (Value, Arguments.Current_Atom);
         when others =>
            State.Remove_Image (Value);
      end case;
   end Update_Image;


   procedure Image_Interpreter is new Interpreter_Loop
     (Format, Meaningless_Type, Update_Image, Insert_Image);


   procedure Update_Format
     (State : in out Format;
      Context : in Meaningless_Type;
      Name : in Atom;
      Arguments : in out Lockable.Descriptor'Class)
   is
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
         when Commands.Align_Right =>
            State.Set_Align (Right_Aligned);

         when Commands.Base =>
            State.Set_Symbols (Arguments);

         when Commands.Images =>
            Parse (State.Images, Arguments);

         when Commands.Padding =>
            case Arguments.Current_Event is
               when Events.Add_Atom =>
                  State.Left_Padding := Create (Arguments.Current_Atom);
                  State.Right_Padding := State.Left_Padding;
               when others =>







|







131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
         when Commands.Align_Right =>
            State.Set_Align (Right_Aligned);

         when Commands.Base =>
            State.Set_Symbols (Arguments);

         when Commands.Images =>
            Image_Interpreter (Arguments, State, Meaningless_Value);

         when Commands.Padding =>
            case Arguments.Current_Event is
               when Events.Add_Atom =>
                  State.Left_Padding := Create (Arguments.Current_Atom);
                  State.Right_Padding := State.Left_Padding;
               when others =>
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

   procedure Include
     (Map : in out Atom_Maps.Map;
      Values : in Interval;
      Image : in Atom_Refs.Immutable_Reference) is
   begin
      Exclude (Map, Values);


      Map.Insert (Values, Image);

   end Include;


   procedure Parse
     (Map : in out Atom_Maps.Map;
      Expression : in out Lockable.Descriptor'Class)
   is
      Event : Events.Event := Expression.Current_Event;
      Lock : Lockable.Lock_State;
   begin
      loop
         case Event is
            when Events.Add_Atom =>
               declare
                  Index : constant T := Next_Index (Map);
               begin
                  Include
                    (Map,
                     (Index, Index),
                     Create (Expression.Current_Atom));
               end;

            when Events.Open_List =>
               Expression.Lock (Lock);

               begin
                  Expression.Next;
                  Parse_Single_Image (Map, Expression);
               exception
                  when others =>
                     Expression.Unlock (Lock, False);
                     raise;
               end;

               Expression.Unlock (Lock);
               Event := Expression.Current_Event;
               exit when Event in Events.Error | Events.End_Of_Input;

            when Events.Close_List | Events.Error | Events.End_Of_Input =>
               exit;
         end case;

         Expression.Next (Event);
      end loop;
   end Parse;


   procedure Parse_Single_Image
     (Map : in out Atom_Maps.Map;
      Expression : in out Lockable.Descriptor'Class)
   is

































      Event : Events.Event := Expression.Current_Event;
      Lock : Lockable.Lock_State;
      First, Last : T;
   begin
      case Event is
         when Events.Add_Atom =>
            begin
               First := T'Value (To_String (Expression.Current_Atom));
               Last := First;
            exception
               when Constraint_Error =>
                  return;
            end;

         when Events.Open_List =>
            Expression.Lock (Lock);

            begin
               Expression.Next (Event);
               if Event = Events.Add_Atom then
                  First := T'Value (To_String (Expression.Current_Atom));
               else
                  Expression.Unlock (Lock, False);
                  return;
               end if;

               Expression.Next (Event);
               if Event = Events.Add_Atom then
                  Last := T'Value (To_String (Expression.Current_Atom));
               else
                  Expression.Unlock (Lock, False);
                  return;
               end if;
            exception
               when Constraint_Error =>
                  Expression.Unlock (Lock, False);
                  return;
               when others =>
                  Expression.Unlock (Lock, False);
                  raise;
            end;

            Expression.Unlock (Lock);

         when others =>
            return;
      end case;



      if Last < First then


         return;






      end if;


      Expression.Next (Event);






      if Event = Events.Add_Atom then
         Include (Map, (First, Last), Create (Expression.Current_Atom));
      else

         Exclude (Map, (First, Last));




      end if;

   end Parse_Single_Image;



   ----------------------
   -- Public Interface --
   ----------------------








>
>
|
>













<
<
<
|
|
|
|
<






|



















|



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


|



<
<
<
<
<
<
<
|
<
<
|

<
<
|
<
<
<
<
|
<
<
<
<
<
<

<
<
<
<
<
<
<

<
<





>
>
|
>
>
|
>
>
>
>
>
>
|

>
|

>
>
>
>
>
|
|
|
>
|
>
>
>
>
|
>
|







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

   procedure Include
     (Map : in out Atom_Maps.Map;
      Values : in Interval;
      Image : in Atom_Refs.Immutable_Reference) is
   begin
      Exclude (Map, Values);

      if not Image.Is_Empty then
         Map.Insert (Values, Image);
      end if;
   end Include;


   procedure Parse
     (Map : in out Atom_Maps.Map;
      Expression : in out Lockable.Descriptor'Class)
   is
      Event : Events.Event := Expression.Current_Event;
      Lock : Lockable.Lock_State;
   begin
      loop
         case Event is
            when Events.Add_Atom =>



               Include
                 (Map,
                  (T'First, T'Last),
                  Create (Expression.Current_Atom));


            when Events.Open_List =>
               Expression.Lock (Lock);

               begin
                  Expression.Next;
                  Parse_Single_Affix (Map, Expression);
               exception
                  when others =>
                     Expression.Unlock (Lock, False);
                     raise;
               end;

               Expression.Unlock (Lock);
               Event := Expression.Current_Event;
               exit when Event in Events.Error | Events.End_Of_Input;

            when Events.Close_List | Events.Error | Events.End_Of_Input =>
               exit;
         end case;

         Expression.Next (Event);
      end loop;
   end Parse;


   procedure Parse_Single_Affix
     (Map : in out Atom_Maps.Map;
      Expression : in out Lockable.Descriptor'Class)
   is
      function Read_Interval (Exp : in out Descriptor'Class) return Interval;

      function Read_Interval (Exp : in out Descriptor'Class) return Interval is
         Event : Events.Event;
         First, Last : T;
      begin
         Exp.Next (Event);

         case Event is
            when Events.Add_Atom =>
               First := T'Value (To_String (Exp.Current_Atom));

            when others =>
               raise Constraint_Error with "Lower bound not an atom";
         end case;

         Exp.Next (Event);

         case Event is
            when Events.Add_Atom =>
               Last := T'Value (To_String (Exp.Current_Atom));

            when others =>
               raise Constraint_Error with "Upper bound not an atom";
         end case;

         if Last < First then
            raise Constraint_Error with "Invalid interval (Last < First)";
         end if;

         return (First, Last);
      end Read_Interval;

      Event : Events.Event := Expression.Current_Event;
      Lock : Lockable.Lock_State;
      Affix : Atom_Refs.Immutable_Reference;
   begin
      case Event is
         when Events.Add_Atom =>







            declare


               Current : constant Atom := Expression.Current_Atom;
            begin


               if Current'Length > 0 then




                  Affix := Create (Current);






               end if;







            end;



         when others =>
            return;
      end case;

      loop
         Expression.Next (Event);

         case Event is
            when Events.Add_Atom =>
               declare
                  Value : T;
               begin
                  Value := T'Value (To_String (Expression.Current_Atom));
                  Include (Map, (Value, Value), Affix);
               exception
                  when Constraint_Error => null;
               end;

            when Events.Open_List =>
               Expression.Lock (Lock);

               begin
                  Include (Map, Read_Interval (Expression), Affix);
               exception
                  when Constraint_Error =>
                     null;
                  when others =>
                     Expression.Unlock (Lock, False);
                     raise;
               end;

               Expression.Unlock (Lock);

            when others =>
               exit;
         end case;
      end loop;
   end Parse_Single_Affix;



   ----------------------
   -- Public Interface --
   ----------------------

828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846


   procedure Set_Prefix
     (Object : in out Format;
      Values : in Interval;
      Prefix : in Atom_Refs.Immutable_Reference) is
   begin
      if Prefix.Is_Empty then
         Exclude (Object.Prefix, Values);
      else
         Include (Object.Prefix, Values, Prefix);
      end if;
   end Set_Prefix;


   procedure Set_Prefix
     (Object : in out Format;
      Values : in Interval;
      Prefix : in Atom) is







<
<
<
|
<







887
888
889
890
891
892
893



894

895
896
897
898
899
900
901


   procedure Set_Prefix
     (Object : in out Format;
      Values : in Interval;
      Prefix : in Atom_Refs.Immutable_Reference) is
   begin



      Include (Object.Prefix, Values, Prefix);

   end Set_Prefix;


   procedure Set_Prefix
     (Object : in out Format;
      Values : in Interval;
      Prefix : in Atom) is
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902


   procedure Set_Suffix
     (Object : in out Format;
      Values : in Interval;
      Suffix : in Atom_Refs.Immutable_Reference) is
   begin
      if Suffix.Is_Empty then
         Exclude (Object.Suffix, Values);
      else
         Include (Object.Suffix, Values, Suffix);
      end if;
   end Set_Suffix;


   procedure Set_Suffix
     (Object : in out Format;
      Values : in Interval;
      Suffix : in Atom) is







<
<
<
|
<







939
940
941
942
943
944
945



946

947
948
949
950
951
952
953


   procedure Set_Suffix
     (Object : in out Format;
      Values : in Interval;
      Suffix : in Atom_Refs.Immutable_Reference) is
   begin



      Include (Object.Suffix, Values, Suffix);

   end Set_Suffix;


   procedure Set_Suffix
     (Object : in out Format;
      Values : in Interval;
      Suffix : in Atom) is

Changes to src/natools-s_expressions-templates-generic_integers.ads.

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
--   (base "symbol 0" "symbol 1" "symbol 2" ...)                            --
--   (left-padding "symbol")                                                --
--   (image (0 "symbol 0") (2 "symbol 2") ...)                              --
--   (max-width "max width" ["overflow text"])                              --
--   (min-width "min width")                                                --
--   (padding "left-symbol" "right-symbol")                                 --
--   (padding "symbol")                                                     --
--   (prefix (0 "prefix 0") (2 "prefix 2") ...)                             --
--   (right-padding "symbol")                                               --
--   (sign "plus sign" ["minus sign"])                                      --
--   (suffix (0 "suffix 0") (2 "suffix 2") ...)                             --
--   (width "fixed width")                                                  --
--   (width "min width" "max width" ["overflow text"])                      --
-- Top-level atoms are taken as the image for the next number.              --
------------------------------------------------------------------------------

with Ada.Containers.Ordered_Maps;
with Ada.Streams;







|


|







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
--   (base "symbol 0" "symbol 1" "symbol 2" ...)                            --
--   (left-padding "symbol")                                                --
--   (image (0 "symbol 0") (2 "symbol 2") ...)                              --
--   (max-width "max width" ["overflow text"])                              --
--   (min-width "min width")                                                --
--   (padding "left-symbol" "right-symbol")                                 --
--   (padding "symbol")                                                     --
--   (prefix ("prefix" 0 (10 20) ...) ("prefix" 2) ...)                     --
--   (right-padding "symbol")                                               --
--   (sign "plus sign" ["minus sign"])                                      --
--   (suffix ("suffix" 0 (10 20) ...) ("suffix" 2) ...)                     --
--   (width "fixed width")                                                  --
--   (width "min width" "max width" ["overflow text"])                      --
-- Top-level atoms are taken as the image for the next number.              --
------------------------------------------------------------------------------

with Ada.Containers.Ordered_Maps;
with Ada.Streams;
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
      Values : in Interval);
      --  Remove the given interval from the map

   procedure Include
     (Map : in out Atom_Maps.Map;
      Values : in Interval;
      Image : in Atom_Refs.Immutable_Reference);
      --  Add Image to the given interval, overwriting any existing values


   procedure Parse_Single_Image
     (Map : in out Atom_Maps.Map;
      Expression : in out Lockable.Descriptor'Class);
      --  Parse Expression to match `value image` or `(first last) image`,
      --  and include it to Map.

   procedure Parse
     (Map : in out Atom_Maps.Map;
      Expression : in out Lockable.Descriptor'Class);
      --  Parse Expression as a list of single image expression (see above)

   ---------------------
   -- Format Mutators --
   ---------------------

   procedure Set_Align (Object : in out Format; Value : in Alignment);








|
>

|


|
|




|







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
      Values : in Interval);
      --  Remove the given interval from the map

   procedure Include
     (Map : in out Atom_Maps.Map;
      Values : in Interval;
      Image : in Atom_Refs.Immutable_Reference);
      --  Add Image to the given interval, overwriting any existing values.
      --  If Image is empty, behave like Exclude.

   procedure Parse_Single_Affix
     (Map : in out Atom_Maps.Map;
      Expression : in out Lockable.Descriptor'Class);
      --  Parse Expression as an affix atom, followed by single numbers (atoms)
      --  or ranges (lists of two atoms).

   procedure Parse
     (Map : in out Atom_Maps.Map;
      Expression : in out Lockable.Descriptor'Class);
      --  Parse Expression as a list of single image expressions (see above)

   ---------------------
   -- Format Mutators --
   ---------------------

   procedure Set_Align (Object : in out Format; Value : in Alignment);