Natools

Diff
Login

Differences From Artifact [b778534f67]:

To Artifact [b5a4a6c160]:


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
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







+

















-
+






-
-
+
+


-
+
-












-
-
+
+







      end case;
   end Is_Newline;


   procedure Newline (Output : in out Printer) is
      Data : Atom (0 .. 1);
      Length : Count;
      Writer : Printer'Class renames Printer'Class (Output);
   begin
      case Output.Param.Newline is
         when CR =>
            Data (0) := Encodings.CR;
            Length := 1;
         when LF =>
            Data (0) := Encodings.LF;
            Length := 1;
         when CR_LF =>
            Data (0) := Encodings.CR;
            Data (1) := Encodings.LF;
            Length := 2;
         when LF_CR =>
            Data (0) := Encodings.LF;
            Data (1) := Encodings.CR;
            Length := 2;
      end case;
      Output.Stream.Write (Data (0 .. Length - 1));
      Writer.Write_Raw (Data (0 .. Length - 1));

      if Output.Indent_Level > 0 and Output.Param.Indentation > 0 then
         case Output.Param.Indent is
            when Spaces =>
               Output.Cursor := Output.Param.Indentation * Output.Indent_Level
                                + 1;
               Output.Stream.Write ((1 .. Count (Output.Cursor) - 1
                                     => Encodings.Space));
               Writer.Write_Raw
                 ((1 .. Count (Output.Cursor) - 1 => Encodings.Space));
            when Tabs =>
               Output.Cursor := Output.Param.Indentation * Output.Indent_Level;
               Output.Stream.Write ((1 .. Count (Output.Cursor)
               Writer.Write_Raw ((1 .. Count (Output.Cursor) => Encodings.HT));
                                     => Encodings.HT));
               Output.Cursor := Output.Cursor * Output.Param.Tab_Stop;
            when Tabs_And_Spaces =>
               Output.Cursor := Output.Param.Indentation * Output.Indent_Level
                                + 1;
               declare
                  Tab_Count : constant Count
                    := (Count (Output.Cursor) - 1)
                        / Count (Output.Param.Tab_Stop);
                  Space_Count : constant Count
                    := (Count (Output.Cursor) - 1)
                        mod Count (Output.Param.Tab_Stop);
               begin
                  Output.Stream.Write ((1 .. Tab_Count => Encodings.HT));
                  Output.Stream.Write ((1 .. Space_Count => Encodings.Space));
                  Writer.Write_Raw ((1 .. Tab_Count => Encodings.HT));
                  Writer.Write_Raw ((1 .. Space_Count => Encodings.Space));
               end;
         end case;
      else
         Output.Cursor := 1;
      end if;
   end Newline;

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
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







+


-
-
-
+
+
+

-
+







-
-
+
+




-
-
+
+



-
-
-
+
+
+













+


-
-
-
-
+
+
+
+

-
+







-
+




+
-
-
-
+
+
+



+
-
-
-
-
+
+
+
+







   end Single_Line_Quoted_Width;


   procedure Write_Base64 (Output : in out Printer; Data : in Atom) is
      Available : Screen_Offset;
      I : Offset := Data'First;
      Chunk_Size : Count;
      Writer : Printer'Class renames Printer'Class (Output);
   begin
      if Output.Param.Width = 0 then
         Output.Stream.Write ((0 => Encodings.Base64_Atom_Begin));
         Output.Stream.Write (Encodings.Encode_Base64 (Data));
         Output.Stream.Write ((0 => Encodings.Base64_Atom_End));
         Writer.Write_Raw ((0 => Encodings.Base64_Atom_Begin));
         Writer.Write_Raw (Encodings.Encode_Base64 (Data));
         Writer.Write_Raw ((0 => Encodings.Base64_Atom_End));
      else
         Output.Stream.Write ((0 => Encodings.Base64_Atom_Begin));
         Writer.Write_Raw ((0 => Encodings.Base64_Atom_Begin));
         Output.Cursor := Output.Cursor + 1;

         loop
            Available := Output.Param.Width + 1 - Output.Cursor;
            Chunk_Size := Count'Max (1, Count (Available) / 4) * 3;

            if Available mod 4 /= 0 and then I in Data'Range then
               Output.Stream.Write
                 ((1 .. Count (Available mod 4) => Encodings.Space));
               Writer.Write_Raw
                 (((1 .. Count (Available mod 4) => Encodings.Space)));
               Output.Cursor := Output.Cursor + (Available mod 4);
            end if;

            if I + Chunk_Size - 1 in Data'Range then
               Output.Stream.Write (Encodings.Encode_Base64
                                      (Data (I .. I + Chunk_Size - 1)));
               Writer.Write_Raw
                 (Encodings.Encode_Base64 (Data (I .. I + Chunk_Size - 1)));
               Newline (Output);
               I := I + Chunk_Size;
            else
               Output.Stream.Write (Encodings.Encode_Base64
                                      (Data (I .. Data'Last)));
               Output.Stream.Write ((0 => Encodings.Base64_Atom_End));
               Writer.Write_Raw
                 (Encodings.Encode_Base64 (Data (I .. Data'Last)));
               Writer.Write_Raw ((0 => Encodings.Base64_Atom_End));
               Output.Cursor := Output.Cursor
                 + Screen_Offset (Data'Last - I + 2) / 3 * 4 + 1;
               exit;
            end if;
         end loop;
      end if;
   end Write_Base64;


   procedure Write_Hex (Output : in out Printer; Data : in Atom) is
      Available : Screen_Offset;
      I : Offset := Data'First;
      Chunk_Size : Count;
      Writer : Printer'Class renames Printer'Class (Output);
   begin
      if Output.Param.Width = 0 then
         Output.Stream.Write ((0 => Encodings.Hex_Atom_Begin));
         Output.Stream.Write (Encodings.Encode_Hex (Data,
                                                    Output.Param.Hex_Casing));
         Output.Stream.Write ((0 => Encodings.Hex_Atom_End));
         Writer.Write_Raw ((0 => Encodings.Hex_Atom_Begin));
         Writer.Write_Raw
           (Encodings.Encode_Hex (Data, Output.Param.Hex_Casing));
         Writer.Write_Raw ((0 => Encodings.Hex_Atom_End));
      else
         Output.Stream.Write ((0 => Encodings.Hex_Atom_Begin));
         Writer.Write_Raw ((0 => Encodings.Hex_Atom_Begin));
         Output.Cursor := Output.Cursor + 1;

         loop
            Available := Output.Param.Width + 1 - Output.Cursor;
            Chunk_Size := Count'Max (1, Count (Available) / 2);

            if Available mod 2 = 1 and then I in Data'Range then
               Output.Stream.Write ((0 => Encodings.Space));
               Writer.Write_Raw ((0 => Encodings.Space));
               Output.Cursor := Output.Cursor + 1;
            end if;

            if I + Chunk_Size - 1 in Data'Range then
               Writer.Write_Raw
               Output.Stream.Write (Encodings.Encode_Hex
                                      (Data (I .. I + Chunk_Size - 1),
                                       Output.Param.Hex_Casing));
                 (Encodings.Encode_Hex
                    (Data (I .. I + Chunk_Size - 1),
                     Output.Param.Hex_Casing));
               Newline (Output);
               I := I + Chunk_Size;
            else
               Writer.Write_Raw
               Output.Stream.Write (Encodings.Encode_Hex
                                      (Data (I .. Data'Last),
                                       Output.Param.Hex_Casing));
               Output.Stream.Write ((0 => Encodings.Hex_Atom_End));
                 (Encodings.Encode_Hex
                    (Data (I .. Data'Last),
                     Output.Param.Hex_Casing));
               Writer.Write_Raw ((0 => Encodings.Hex_Atom_End));
               Output.Cursor := Output.Cursor
                 + Screen_Offset (Data'Last - I + 1) * 2 + 1;
               exit;
            end if;
         end loop;
      end if;
   end Write_Hex;
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
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







-
+














-
-
+
+







               Output.Cursor := New_Cursor;
            end if;
         end loop;

         pragma Assert (O = Result'Last);
         Result (O) := Encodings.Quoted_Atom_End;

         Output.Stream.Write (Result);
         Write_Raw (Printer'Class (Output), Result);
      end;
   end Write_Quoted;


   procedure Write_Verbatim (Output : in out Printer; Data : in Atom) is
      Length_Image : constant String := Count'Image (Data'Length);
      Prefix : Atom (0 .. Length_Image'Length - 1);
   begin
      for I in Length_Image'First + 1 .. Length_Image'Last loop
         Prefix (Count (I) - Count (Length_Image'First + 1)) :=
           Character'Pos (Length_Image (I));
      end loop;
      Prefix (Prefix'Last) := Encodings.Verbatim_Begin;

      Output.Stream.Write (Prefix);
      Output.Stream.Write (Data);
      Write_Raw (Printer'Class (Output), Prefix);
      Write_Raw (Printer'Class (Output), Data);
      Output.Cursor := Output.Cursor + Screen_Offset (Prefix'Length)
        + Screen_Offset (Data'Length);
   end Write_Verbatim;



   -----------------------
792
793
794
795
796
797
798
799

800
801
802
803
804
805
806

807
808
809
810
811
812
813
796
797
798
799
800
801
802

803
804
805
806
807
808
809

810
811
812
813
814
815
816
817







-
+






-
+







         Output.First := True;  --  inhibit extra space or newline
      end if;

      if not Output.First then
         if Output.Param.Newline_At (Output.Previous, Opening) then
            Newline (Output);
         elsif Output.Param.Space_At (Output.Previous, Opening) then
            Output.Stream.Write ((0 => Encodings.Space));
            Write_Raw (Printer'Class (Output), (0 => Encodings.Space));
            Output.Cursor := Output.Cursor + 1;
         end if;
      else
         Output.First := False;
      end if;

      Output.Stream.Write ((0 => Encodings.List_Begin));
      Write_Raw (Printer'Class (Output), (0 => Encodings.List_Begin));
      Output.Cursor := Output.Cursor + 1;
      Output.Indent_Level := Output.Indent_Level + 1;
      Output.Previous := Opening;
      Output.Need_Blank := False;
   end Open_List;


851
852
853
854
855
856
857
858

859
860
861

862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877

878
879
880
881
882
883
884
855
856
857
858
859
860
861

862
863
864

865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880

881
882
883
884
885
886
887
888







-
+


-
+















-
+







                 - 2;
         begin
            if not At_Origin
              and then not Fit_In_Line (Output, Blank_Width + Width)
            then
               Newline (Output);
            elsif Output.Need_Blank then
               Output.Stream.Write ((0 => Encodings.Space));
               Write_Raw (Printer'Class (Output), (0 => Encodings.Space));
               Output.Cursor := Output.Cursor + 1;
            end if;
            Output.Stream.Write (Data);
            Write_Raw (Printer'Class (Output), Data);
            Output.Cursor := Output.Cursor + Width;
            Output.Need_Blank := True;
            return;
         end;
      end if;

      --  Single-line quoted string if requested and possible

      if Output.Param.Quoted = Single_Line then
         declare
            Width : constant Screen_Offset
              := Single_Line_Quoted_Width (Data, Output.Param.Char_Encoding);
         begin
            if Fit_In_Line (Output, Blank_Width + Width) then
               if Output.Need_Blank then
                  Output.Stream.Write ((0 => Encodings.Space));
                  Write_Raw (Printer'Class (Output), (0 => Encodings.Space));
                  Output.Cursor := Output.Cursor + 1;
               end if;
               Write_Quoted (Output, Data, True);
               Output.Need_Blank := False;
               return;
            end if;

913
914
915
916
917
918
919
920

921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940

941
942
943
944
945
946
947
917
918
919
920
921
922
923

924
925
926
927
928
929
930
931






932
933
934
935
936
937

938
939
940
941
942
943
944
945







-
+







-
-
-
-
-
-






-
+







               end;
         end case;

         if Output.Param.Quoted = When_Shorter
           and then Multi_Line_Quoted_Size (Output, Data) <= Size
         then
            if Output.Need_Blank then
               Output.Stream.Write ((0 => Encodings.Space));
               Write_Raw (Printer'Class (Output), (0 => Encodings.Space));
               Output.Cursor := Output.Cursor + 1;
            end if;
            Write_Quoted (Output, Data, False);
            Output.Need_Blank := False;
            return;
         end if;

--       if Output.Param.Quoted = When_Shorter then
--          String'Write (Output.Stream,
--            "{" & Count'Image (Size) & "|"
--            & Count'Image (Multi_Line_Quoted_Size (Output, Data)) & "}");
--       end if;

         if not At_Origin
           and then
           not Fit_In_Line (Output, Blank_Width + Screen_Offset (Size))
         then
            Newline (Output);
         elsif Output.Need_Blank then
            Output.Stream.Write ((0 => Encodings.Space));
            Write_Raw (Printer'Class (Output), (0 => Encodings.Space));
            Output.Cursor := Output.Cursor + 1;
         end if;

         case Output.Param.Fallback is
            when Base64 =>
               Write_Base64 (Output, Data);
            when Hexadecimal =>
966
967
968
969
970
971
972
973

974
975
976
977
978
979
980

981
982
983
984
985
986
987
964
965
966
967
968
969
970

971
972
973
974
975
976
977

978
979
980
981
982
983
984
985







-
+






-
+







         Output.First := True;  --  inhibit extra space or newline
      end if;

      if not Output.First then
         if Output.Param.Newline_At (Output.Previous, Closing) then
            Newline (Output);
         elsif Output.Param.Space_At (Output.Previous, Closing) then
            Output.Stream.Write ((0 => Encodings.Space));
            Write_Raw (Printer'Class (Output), (0 => Encodings.Space));
            Output.Cursor := Output.Cursor + 1;
         end if;
      else
         Output.First := False;
      end if;

      Output.Stream.Write ((0 => Encodings.List_End));
      Write_Raw (Printer'Class (Output), (0 => Encodings.List_End));
      Output.Cursor := Output.Cursor + 1;
      Output.Previous := Closing;
      Output.Need_Blank := False;
   end Close_List;



1100
1101
1102
1103
1104
1105
1106








1107
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113







+
+
+
+
+
+
+
+

   procedure Set_Newline
     (Output : in out Printer;
      Newline : in Newline_Encoding) is
   begin
      Output.Param.Newline := Newline;
   end Set_Newline;


   overriding procedure Write_Raw
     (Output : in out Stream_Printer;
      Data : in Ada.Streams.Stream_Element_Array) is
   begin
      Output.Stream.Write (Data);
   end Write_Raw;

end Natools.S_Expressions.Printers.Pretty;