matreshka_league_21.0.0_0c8f4d47/tools/aflex/src/sym.adb

  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
-- Copyright (c) 1990 Regents of the University of California.
-- All rights reserved.
--
-- This software was developed by John Self of the Arcadia project
-- at the University of California, Irvine.
--
-- Redistribution and use in source and binary forms are permitted
-- provided that the above copyright notice and this paragraph are
-- duplicated in all such forms and that any documentation,
-- advertising materials, and other materials related to such
-- distribution and use acknowledge that the software was developed
-- by the University of California, Irvine.  The name of the
-- University may not be used to endorse or promote products derived
-- from this software without specific prior written permission.
-- THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
-- IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
-- WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

-- TITLE symbol table routines
-- AUTHOR: John Self (UCI)
-- DESCRIPTION implements only a simple symbol table using open hashing
-- NOTES could be faster, but it isn't used much
-- $Header: /co/ua/self/arcadia/aflex/ada/src/RCS/symB.a,v 1.6 90/01/12 15:20:39 self Exp Locker: self $
with Ada.Integer_Wide_Wide_Text_IO;
with Ada.Strings.Wide_Wide_Unbounded.Wide_Wide_Text_IO;
with Ada.Wide_Wide_Text_IO;

with MISC, NFA;

package body SYM is

   use Ada.Integer_Wide_Wide_Text_IO;
   use Ada.Strings.Wide_Wide_Unbounded;
   use Ada.Strings.Wide_Wide_Unbounded.Wide_Wide_Text_IO;
   use Ada.Wide_Wide_Text_IO;

   ------------
   -- ADDSYM --
   ------------

   -- addsym - add symbol and definitions to symbol table
   --
   -- true is returned if the symbol already exists, and the change not made.

   procedure ADDSYM
     (SYM        : Ada.Strings.Wide_Wide_Unbounded.Unbounded_Wide_Wide_String;
      STR_DEF    : Ada.Strings.Wide_Wide_Unbounded.Unbounded_Wide_Wide_String;
      INT_DEF    : INTEGER;
      TABLE      : in out HASH_TABLE;
      TABLE_SIZE : INTEGER;
      RESULT     : out BOOLEAN)
   is
      HASH_VAL             : constant INTEGER := HASHFUNCT (SYM, TABLE_SIZE);
      SYM_ENTRY            : HASH_LINK := TABLE(HASH_VAL);
      NEW_ENTRY, SUCCESSOR : HASH_LINK;
   begin
      while (SYM_ENTRY /= null) loop
         if (SYM = SYM_ENTRY.NAME) then

            -- entry already exists
            RESULT := TRUE;

            return;
         end if;

         SYM_ENTRY := SYM_ENTRY.NEXT;
      end loop;

      -- create new entry
      NEW_ENTRY := new HASH_ENTRY;

      SUCCESSOR := TABLE(HASH_VAL);
      if ((SUCCESSOR /= null)) then
         NEW_ENTRY.NEXT := SUCCESSOR;
         SUCCESSOR.PREV := NEW_ENTRY;
      else
         NEW_ENTRY.NEXT := null;
      end if;

      NEW_ENTRY.PREV := null;
      NEW_ENTRY.NAME := SYM;
      NEW_ENTRY.STR_VAL := STR_DEF;
      NEW_ENTRY.INT_VAL := INT_DEF;

      TABLE(HASH_VAL) := NEW_ENTRY;

      RESULT := FALSE;

      return;

   exception
      when STORAGE_ERROR =>
         Misc.Aflex_Fatal ("symbol table memory allocation failed");
   end ADDSYM;


   ---------------
   -- CCLINSTAL --
   ---------------

   -- cclinstal - save the text of a character class

   procedure CCLINSTAL
     (CCLTXT : Ada.Strings.Wide_Wide_Unbounded.Unbounded_Wide_Wide_String;
      CCLNUM : in INTEGER)
   is
      -- we don't bother checking the return status because we are not called
      -- unless the symbol is new
      DUMMY : BOOLEAN;

   begin
      ADDSYM
        (CCLTXT,
         Null_Unbounded_Wide_Wide_String,
         CCLNUM,
         CCLTAB,
         CCL_HASH_SIZE,
         DUMMY);
   end CCLINSTAL;

   ---------------
   -- CCLLOOKUP --
   ---------------

   -- ccllookup - lookup the number associated with character class text

   function CCLLOOKUP
     (CCLTXT : Ada.Strings.Wide_Wide_Unbounded.Unbounded_Wide_Wide_String)
      return INTEGER is
   begin
      return FINDSYM (CCLTXT, CCLTAB, CCL_HASH_SIZE).INT_VAL;
   end CCLLOOKUP;

   -------------
   -- FINDSYM --
   -------------

   -- findsym - find symbol in symbol table

   function FINDSYM
     (SYMBOL     : Ada.Strings.Wide_Wide_Unbounded.Unbounded_Wide_Wide_String;
      TABLE      : in HASH_TABLE;
      TABLE_SIZE : in INTEGER) return HASH_LINK
   is
      SYM_ENTRY   : HASH_LINK := TABLE (HASHFUNCT (SYMBOL, TABLE_SIZE));
      EMPTY_ENTRY : HASH_LINK;

   begin
      while (SYM_ENTRY /= null) loop
         if (SYMBOL = SYM_ENTRY.NAME) then
            return SYM_ENTRY;
         end if;
         SYM_ENTRY := SYM_ENTRY.NEXT;
      end loop;
      EMPTY_ENTRY := new HASH_ENTRY;
      EMPTY_ENTRY.all :=
        (null,
         null,
         Null_Unbounded_Wide_Wide_String,
         Null_Unbounded_Wide_Wide_String,
         0);

      return EMPTY_ENTRY;

   exception
      when STORAGE_ERROR =>
         Misc.Aflex_Fatal ("dynamic memory failure in findsym()");

         return EMPTY_ENTRY;
   end FINDSYM;

   ---------------
   -- HASHFUNCT --
   ---------------

   -- hashfunct - compute the hash value for "str" and hash size "hash_size"

   function HASHFUNCT
     (STR       : Ada.Strings.Wide_Wide_Unbounded.Unbounded_Wide_Wide_String;
      HASH_SIZE : INTEGER) return INTEGER
   is
      HASHVAL, LOCSTR : INTEGER;
   begin
      HASHVAL := 0;
      LOCSTR := 1;

      while LOCSTR <= Length (STR) loop
         HASHVAL :=
           ((HASHVAL*2) + Wide_Wide_Character'Pos(Element (STR, LOCSTR)))
              mod HASH_SIZE;
         LOCSTR := LOCSTR + 1;
      end loop;

      return HASHVAL;
   end HASHFUNCT;

   --------------
   -- NDINSTAL --
   --------------

   --  ndinstal - install a name definition

   procedure NDINSTAL
     (ND, DEF : Ada.Strings.Wide_Wide_Unbounded.Unbounded_Wide_Wide_String)
   is
      RESULT : BOOLEAN;
   begin
      ADDSYM (ND, DEF, 0, NDTBL, NAME_TABLE_HASH_SIZE, RESULT);

      if RESULT then
         MISC.SYNERR("name defined twice");
      end if;
   end NDINSTAL;

   --------------
   -- NDLOOKUP --
   --------------

   -- ndlookup - lookup a name definition

   function NDLOOKUP
     (ND : Ada.Strings.Wide_Wide_Unbounded.Unbounded_Wide_Wide_String)
      return Ada.Strings.Wide_Wide_Unbounded.Unbounded_Wide_Wide_String is
   begin
      return FINDSYM (ND, NDTBL, NAME_TABLE_HASH_SIZE).STR_VAL;
   end NDLOOKUP;

   --------------
   -- SCINSTAL --
   --------------

   -- scinstal - make a start condition
   --
   -- NOTE
   --    the start condition is Exclusive if xcluflg is true

   procedure SCINSTAL
     (STR     : in Unbounded_Wide_Wide_String;
      XCLUFLG : in BOOLEAN)
   is
      -- bit of a hack.  We know how the default start-condition is
      -- declared, and don't put out a define for it, because it
      -- would come out as "#define 0 1"

      -- actually, this is no longer the case.  The default start-condition
      -- is now called "INITIAL".  But we keep the following for the sake
      -- of future robustness.

      RESULT : BOOLEAN;

   begin
      if STR /= "0" then
         PUT (DEF_FILE, "   ");
         PUT (DEF_FILE, STR);
         PUT (DEF_FILE, " : constant YY_State_Type := ");
         PUT (DEF_FILE, LASTSC, 1);
         PUT_LINE (DEF_FILE, ";");
    end if;

    LASTSC := LASTSC + 1;
    if (LASTSC >= CURRENT_MAX_SCS) then
      CURRENT_MAX_SCS := CURRENT_MAX_SCS + MAX_SCS_INCREMENT;

      NUM_REALLOCS := NUM_REALLOCS + 1;

      REALLOCATE_INTEGER_ARRAY(SCSET, CURRENT_MAX_SCS);
      REALLOCATE_INTEGER_ARRAY(SCBOL, CURRENT_MAX_SCS);
      REALLOCATE_BOOLEAN_ARRAY(SCXCLU, CURRENT_MAX_SCS);
      REALLOCATE_BOOLEAN_ARRAY(SCEOF, CURRENT_MAX_SCS);
      REALLOCATE_VSTRING_ARRAY(SCNAME, CURRENT_MAX_SCS);
      REALLOCATE_INTEGER_ARRAY(ACTVSC, CURRENT_MAX_SCS);
    end if;

      SCNAME (LASTSC) := STR;

      ADDSYM
        (SCNAME(LASTSC),
         Null_Unbounded_Wide_Wide_String,
         LASTSC,
         SCTBL,
         START_COND_HASH_SIZE,
         RESULT);
    if (RESULT) then
      Misc.Aflex_Error ("start condition " & STR & " declared twice");
    end if;

    SCSET(LASTSC) := NFA.MKSTATE(SYM_EPSILON);
    SCBOL(LASTSC) := NFA.MKSTATE(SYM_EPSILON);
    SCXCLU(LASTSC) := XCLUFLG;
    SCEOF(LASTSC) := FALSE;
  end SCINSTAL;


  -- sclookup - lookup the number associated with a start condition

   function SCLOOKUP
     (STR : Ada.Strings.Wide_Wide_Unbounded.Unbounded_Wide_Wide_String)
      return Integer is
   begin
      return FINDSYM (STR, SCTBL, START_COND_HASH_SIZE).INT_VAL;
   end SCLOOKUP;

end SYM;