Natools

Diff
Login

Differences From Artifact [0a7601bdad]:

To Artifact [d59de17b25]:


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







-
-
+
-
-
-

-
-
-
-
-
-
-
+
+
+
-
-
+
-
-
+
-
-
+

-
-
+
-
-
+

-
-
-
+
+
+


-
+



-
-
+
+


-
+

-

-
+



-
+



-
-
+
+
+
+








with Natools.S_Expressions.Atom_Buffers;
with Natools.S_Expressions.Lockable;

package Natools.S_Expressions.Parsers is
   pragma Preelaborate (Natools.S_Expressions.Parsers);

   type Parser is tagged private;

   type Parser is abstract limited new Lockable.Descriptor with private;
   function Current_Event (P : in Parser) return Events.Event;
   function Current_Atom (P : in Parser) return Atom;
   function Current_Level (P : in Parser) return Natural;

   procedure Query_Atom
     (P : in Parser;
      Process : not null access procedure (Data : in Atom));

   procedure Read_Atom
     (P      : in Parser;
      Data   : out Atom;
   procedure Read_More
     (Self : in out Parser;
      Buffer : out Atom_Buffers.Atom_Buffer)
      Length : out Count);

     is abstract;
   procedure Next_Event
     (P     : in out Parser;
      --  Read data to be parsed.
      Input : not null access Ada.Streams.Root_Stream_Type'Class);

      --  Leaving the buffer empty signals end of input stream.

   type Subparser
     (Backend : access Parser;
   procedure Reset (Self : in out Parser; Hard : in Boolean := False);
      Input   : access Ada.Streams.Root_Stream_Type'Class)
   is new Lockable.Descriptor with private;
      --  Reset internal state, and free internal memory if Hard

   overriding function Current_Event (P : in Subparser) return Events.Event;
   overriding function Current_Atom (P : in Subparser) return Atom;
   overriding function Current_Level (P : in Subparser) return Natural;
   overriding function Current_Event (Self : Parser) return Events.Event;
   overriding function Current_Atom (Self : Parser) return Atom;
   overriding function Current_Level (Self : Parser) return Natural;

   overriding procedure Query_Atom
     (P : in Subparser;
     (Self : in Parser;
      Process : not null access procedure (Data : in Atom));

   overriding procedure Read_Atom
     (P      : in Subparser;
      Data   : out Atom;
     (Self : in Parser;
      Data : out Atom;
      Length : out Count);

   overriding procedure Next (P : in out Subparser; Event : out Events.Event);
   overriding procedure Next (Self : in out Parser; Event : out Events.Event);


   overriding procedure Lock
     (Object : in out Subparser;
     (Self : in out Parser;
      State : out Lockable.Lock_State);

   overriding procedure Unlock
     (Object : in out Subparser;
     (Self : in out Parser;
      State : in out Lockable.Lock_State;
      Finish : in Boolean := True);

   procedure Finish (P : in out Subparser);
      --  Read enough data to exhaust intial nesting level


   type Stream_Parser (Input : access Ada.Streams.Root_Stream_Type'Class) is
     limited new Lockable.Descriptor with private;

private

   type Internal_State is
     (Waiting,          --  waiting for a marker
      Base64_Atom,      --  reading an atom encoded in base 64
      Base64_Expr,      --  reading an expression encoded in base 64
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
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







-
+

-
+
-

+


+
+


-
-
-
-
-
-
-
-
+
+
+
+
+
+


         when Quoted_Atom =>
            Escape : Read_Buffer;
         when Verbatim_Atom =>
            Size : Count;
      end case;
   end record;

   type Parser is tagged record
   type Parser is abstract limited new Lockable.Descriptor with record
      Internal     : State_Data;
      Pending      : Events.Event := Events.End_Of_Input;
      Next_Event : Events.Event := Events.End_Of_Input;
      Override     : Atom_Buffers.Atom_Buffer;
      Latest       : Events.Event := Events.Error;
      Pending : Atom_Buffers.Atom_Buffer;
      Buffer       : Atom_Buffers.Atom_Buffer;
      Level        : Natural := 0;
      Lock_Stack : Lockable.Lock_Stack;
      Locked : Boolean := False;
   end record;

   type Subparser
     (Backend : access Parser;
      Input   : access Ada.Streams.Root_Stream_Type'Class)
   is new Lockable.Descriptor with record
      Levels      : Lockable.Lock_Stack;
      Initialized : Boolean := False;
      Terminated  : Boolean := False;
   end record;
   type Stream_Parser (Input : access Ada.Streams.Root_Stream_Type'Class) is
     new Parser with null record;

   overriding procedure Read_More
     (Self : in out Stream_Parser;
      Buffer : out Atom_Buffers.Atom_Buffer);

end Natools.S_Expressions.Parsers;