1------------------------------------------------------------------------------
2--                                                                          --
3--                            Matreshka Project                             --
4--                                                                          --
5--                               XML Processor                              --
6--                                                                          --
7--                        Runtime Library Component                         --
8--                                                                          --
9------------------------------------------------------------------------------
10--                                                                          --
11-- Copyright © 2010, Vadim Godunko <vgodunko@gmail.com>                     --
12-- All rights reserved.                                                     --
13--                                                                          --
14-- Redistribution and use in source and binary forms, with or without       --
15-- modification, are permitted provided that the following conditions       --
16-- are met:                                                                 --
17--                                                                          --
18--  * Redistributions of source code must retain the above copyright        --
19--    notice, this list of conditions and the following disclaimer.         --
20--                                                                          --
21--  * Redistributions in binary form must reproduce the above copyright     --
22--    notice, this list of conditions and the following disclaimer in the   --
23--    documentation and/or other materials provided with the distribution.  --
24--                                                                          --
25--  * Neither the name of the Vadim Godunko, IE nor the names of its        --
26--    contributors may be used to endorse or promote products derived from  --
27--    this software without specific prior written permission.              --
28--                                                                          --
29-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      --
30-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT        --
31-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR    --
32-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT     --
33-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,   --
34-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED --
35-- TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   --
36-- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF   --
37-- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     --
38-- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS       --
39-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.             --
40--                                                                          --
41------------------------------------------------------------------------------
42--  $Revision: 2026 $ $Date: 2011-07-23 00:16:17 +0400 (Sat, 23 Jul 2011) $
43------------------------------------------------------------------------------
44--  This package provides symbol table to store frequently used strings,
45--  allocate identifier for them and reduce number of memory allocations by
46--  reusing shared strings.
47------------------------------------------------------------------------------
48with League.Strings;
49with Matreshka.Internals.Strings;
50with Matreshka.Internals.Utf16;
51
52package Matreshka.Internals.XML.Symbol_Tables is
53
54   pragma Preelaborate;
55
56   type Qualified_Name_Errors is
57    (Valid,
58     Colon_At_Start,
59     Colon_At_End,
60     Multiple_Colons,
61     First_Character_Is_Not_NS_Name_Start_Char);
62
63   type Symbol_Table is limited private;
64
65   procedure Insert
66    (Self        : in out Symbol_Table;
67     String      : not null Matreshka.Internals.Strings.Shared_String_Access;
68     First       : Matreshka.Internals.Utf16.Utf16_String_Index;
69     Size        : Matreshka.Internals.Utf16.Utf16_String_Index;
70     Length      : Natural;
71     Namespaces  : Boolean;
72     Qname_Error : out Qualified_Name_Errors;
73     Identifier  : out Symbol_Identifier);
74   --  Lookup symbol table for name and returns its identifier if present,
75   --  otherwise add new name and returns allocated identifier.
76
77   procedure Insert
78    (Self       : in out Symbol_Table;
79     String     : not null Matreshka.Internals.Strings.Shared_String_Access;
80     Identifier : out Symbol_Identifier);
81   --  Lookup symbol table for name and returns its identifier if present,
82   --  otherwise add new name and returns allocated identifier.
83
84   function Name
85    (Self       : Symbol_Table;
86     Identifier : Symbol_Identifier)
87       return not null Matreshka.Internals.Strings.Shared_String_Access;
88   --  Returns name of the identifier. Reference counter is not incremented.
89
90   function Name
91    (Self       : Symbol_Table;
92     Identifier : Symbol_Identifier) return League.Strings.Universal_String;
93   --  Returns name of the identifier.
94
95   function Local_Name
96    (Self       : Symbol_Table;
97     Identifier : Symbol_Identifier) return Symbol_Identifier;
98   --  Returns local name component of the identifier.
99
100   function Local_Name
101    (Self       : Symbol_Table;
102     Identifier : Symbol_Identifier)
103       return not null Matreshka.Internals.Strings.Shared_String_Access;
104   --  Returns local name component of the identifier. Reference counter is not
105   --  incremented.
106
107   function Prefix_Name
108    (Self       : Symbol_Table;
109     Identifier : Symbol_Identifier) return Symbol_Identifier;
110   --  Returns prefix name component of the identifier.
111
112   function Parameter_Entity
113    (Self       : Symbol_Table;
114     Identifier : Symbol_Identifier) return Entity_Identifier;
115   --  Returns parameter entity associated with the name.
116
117   procedure Set_Parameter_Entity
118    (Self       : in out Symbol_Table;
119     Identifier : Symbol_Identifier;
120     Entity     : Entity_Identifier);
121   --  Associates parameter entity with the name.
122
123   function General_Entity
124    (Self       : Symbol_Table;
125     Identifier : Symbol_Identifier) return Entity_Identifier;
126   --  Returns general entity associated with the name.
127
128   procedure Set_General_Entity
129    (Self       : in out Symbol_Table;
130     Identifier : Symbol_Identifier;
131     Entity     : Entity_Identifier);
132   --  Associates general entity with the name.
133
134   function Element
135    (Self       : Symbol_Table;
136     Identifier : Symbol_Identifier) return Element_Identifier;
137   --  Returns element declaration associated with the name.
138
139   procedure Set_Element
140    (Self       : in out Symbol_Table;
141     Identifier : Symbol_Identifier;
142     Element    : Element_Identifier);
143   --  Associates element declaration with the name.
144
145   function Notation
146    (Self       : Symbol_Table;
147     Identifier : Symbol_Identifier) return Notation_Identifier;
148   --  Returns notation declaration associated with the name.
149
150   procedure Set_Notation
151    (Self       : in out Symbol_Table;
152     Identifier : Symbol_Identifier;
153     Notation   : Notation_Identifier);
154   --  Associates notation declaration with the name.
155
156   procedure Initialize (Self : in out Symbol_Table);
157   --  Initialize internal structures and register predefined general entities.
158
159   procedure Finalize (Self : in out Symbol_Table);
160   --  Finalize internal structures.
161
162   procedure Reset (Self : in out Symbol_Table);
163   --  Resets internal structures to initial state.
164
165private
166
167   type Symbol_Record is record
168      String              : Matreshka.Internals.Strings.Shared_String_Access;
169      --  Name of the symbol.
170      Namespace_Processed : Boolean;
171      Prefix_Name         : Symbol_Identifier;
172      Local_Name          : Symbol_Identifier;
173
174      Element             : Element_Identifier;
175      Notation            : Notation_Identifier;
176      Parameter_Entity    : Entity_Identifier;
177      General_Entity      : Entity_Identifier;
178   end record;
179
180   type Symbol_Record_Array is
181     array (Symbol_Identifier range <>) of Symbol_Record;
182   type Symbol_Record_Array_Access is access all Symbol_Record_Array;
183
184   type Symbol_Table is record
185      Table : Symbol_Record_Array_Access;
186      Last  : Symbol_Identifier;
187   end record;
188
189   pragma Inline (Element);
190   pragma Inline (General_Entity);
191   pragma Inline (Local_Name);
192   pragma Inline (Name);
193   pragma Inline (Notation);
194   pragma Inline (Parameter_Entity);
195   pragma Inline (Prefix_Name);
196   pragma Inline (Set_Element);
197   pragma Inline (Set_General_Entity);
198   pragma Inline (Set_Notation);
199   pragma Inline (Set_Parameter_Entity);
200
201end Matreshka.Internals.XML.Symbol_Tables;
202