matreshka_league_21.0.0_0c8f4d47/tools/ayacc/src/lexical_analyzer.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
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
-- Copyright (c) 1990 Regents of the University of California.
-- All rights reserved.
--
--    The primary authors of ayacc were David Taback and Deepak Tolani.
--    Enhancements were made by Ronald J. Schmalz.
--
--    Send requests for ayacc information to ayacc-info@ics.uci.edu
--    Send bug reports for ayacc to ayacc-bugs@ics.uci.edu
--
-- 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.

-- Module       : lexical_analyzer_body.ada
-- Component of : ayacc
-- Version      : 1.2
-- Date         : 11/21/86  12:30:42
-- SCCS File    : disk21~/rschm/hasee/sccs/ayacc/sccs/sxlexical_analyzer_body.ada

-- $Header: lexical_analyzer_body.a,v 0.1 86/04/01 15:05:27 ada Exp $
-- $Log:	lexical_analyzer_body.a,v $
-- Revision 0.1  86/04/01  15:05:27  ada
--  This version fixes some minor bugs with empty grammars
--  and $$ expansion. It also uses vads5.1b enhancements
--  such as pragma inline.
--
--
-- Revision 0.0  86/02/19  18:37:05  ada
--
-- These files comprise the initial version of Ayacc
-- designed and implemented by David Taback and Deepak Tolani.
-- Ayacc has been compiled and tested under the Verdix Ada compiler
-- version 4.06 on a vax 11/750 running Unix 4.2BSD.
--
with Ada.Characters.Handling;
with Ada.Strings.Unbounded;
with Ada.Text_IO;

with Ayacc_File_Names;
with Actions_File, Source_File, Tokens_File;
use  Actions_File, Source_File, Tokens_File;

package body Lexical_Analyzer is

   use Ada.Characters.Handling;
   use Ada.Strings.Unbounded;
   use Ada.Text_IO;
   use Ayacc_File_Names;

   function "+" (Item : String) return Unbounded_String
     renames To_Unbounded_String;

   function "+" (Item : Unbounded_String) return String
     renames To_String;

   Current_Line_Number : Natural := 1;
   Lexeme_Text         : Unbounded_String;

   ---------------------
   -- Get_Lexeme_Text --
   ---------------------

   function Get_Lexeme_Text return String is
   begin
      return +Lexeme_Text;
   end Get_Lexeme_Text;


    function Line_Number return Natural is
    begin
	return Source_Line_Number;
    end Line_Number;


    procedure Skip_Comment is
	Ch: Character;
    begin
	loop
	    Get_Char(Ch);
	    if Ch = Eof then
		Unget_Char(Ch);
		exit;
	    elsif Ch = Eoln then
		Current_Line_Number := Current_Line_Number + 1;
		exit;
	    end if;
	end loop;
    end Skip_Comment;

   ---------------
   -- Get_Token --
   ---------------

   function Get_Token return Ayacc_Token is
      Ch: Character;
   begin
      loop
         Lexeme_Text := Null_Unbounded_String;

         loop
            Get_Char (Ch);

            if Ch = Eoln then
               Current_Line_Number := Current_Line_Number + 1;
            end if;

            exit when Ch /= ' '
              and then Ch /= Eoln
              and then Ch /= Ascii.Ht;
         end loop;

         case Ch is
            when '-' =>
               Get_Char (Ch);
               if Ch = '-' then
                  Skip_Comment;

               else
                  raise Illegal_Token;
               end if;

            when ':' =>
               Append (Lexeme_Text, Ch);

               return Colon;

            when ';' =>
               Append (Lexeme_Text, Ch);

               return Semicolon;

            when ',' =>
               Append (Lexeme_Text, Ch);

               return Comma;

            when '%' =>
               Append (Lexeme_Text, Ch);

               if Peek_Next_Char = '%' then
                  Get_Char (Ch);
                  Append (Lexeme_Text, Ch);

                  return Mark;

               else
                  loop
                     Get_Char(Ch);

                     if Ch not in 'A'..'Z'
                       and then Ch not in 'a'..'z'
                     then
                        Unget_Char(Ch);

                        exit;
                     end if;

                     Append (Lexeme_Text, Ch);
                  end loop;

                  Lexeme_Text := +To_Upper (+Lexeme_Text);

                  if Lexeme_Text = "%TOKEN" then
                     return Token;

                  elsif Lexeme_Text = "%START" then
                     return Start;

                  elsif Lexeme_Text = "%LEFT" then
                     return Left;

                  elsif Lexeme_Text = "%RIGHT" then
                     return Right;

                  elsif Lexeme_Text = "%NONASSOC" then
                     return Nonassoc;

                  elsif Lexeme_Text = "%PREC" then
                     return Prec;

                  elsif Lexeme_Text = "%WITH" then
                     return With_Clause;

                  elsif Lexeme_Text = "%USE" then
                     return Use_Clause;

                  else
                     raise Illegal_Token;
                  end if;
               end if;

            when '|' =>
               Append (Lexeme_Text, Ch);

               return Vertical_Bar;

            when '{' =>
               Append (Lexeme_Text, Ch);

               return Left_Brace;

            when Eof =>
               return Eof_Token;

            when ''' =>
               Append (Lexeme_Text, Ch);
               Get_Char(Ch);

               if Ch /= Eof and Ch /= Eoln then
                  Append(Lexeme_Text, Ch);
                  Get_Char(Ch);

                  if Ch /= ''' then
                     raise Illegal_Token;

                  else
                     Append (Lexeme_Text, Ch);
                  end if;

                  return Character_Literal;
               end if;

            when  'A'..'Z' | 'a'..'z' | '.' | '_' =>
               Append (Lexeme_Text, Ch);

               loop
                  Get_Char(Ch);

                  if Ch in 'a'..'z'
                    or else Ch in 'A'..'Z'
                    or else Ch in '0'..'9'
                    or else Ch = '.'
                    or else Ch = '_'
                  then
                     Append (Lexeme_Text, Ch);

                  else
                     Unget_Char (Ch);

                     exit;
                  end if;
               end loop;

               Lexeme_Text := +To_Upper (+Lexeme_Text);

               return Identifier;

            when others =>
               raise Illegal_Token;
         end case;
      end loop;
   end Get_Token;



    procedure Handle_Action(Rule, Rule_Length : Integer) is
        Char   : Character;
        Base   : Integer;
    begin
	Actions_File.Writeln;
        Actions_File.Write("when" & Integer'Image (Rule) & " =>");
	Actions_File.Writeln;
	Actions_File.Write("--# line" & Integer'Image(Current_Line_Number));
        Actions_File.Write(" """ & Get_Source_File_Name & """");
	Actions_File.Writeln;
        loop
            Get_Char(Char);
            if Char = '-' and then Peek_Next_Char = '-' then
		loop
		    Actions_File.Write(Char);
		    Get_Char(Char);
		    exit when Char = Eoln;
		end loop;
	    end if;

            case Char is
                when '"' =>
                    Actions_File.Write(Char);
                    loop
                        Get_Char(Char);
                        Actions_File.Write(Char);
                        exit when Char = '"';
                    end loop;

		-- handle special case '"' where there is no matching ".
		when ''' =>
                    Actions_File.Write(Char);
		    if Peek_Next_Char= '"' then
		      Get_Char(Char);
		      if Peek_Next_Char = ''' then
                        Actions_File.Write(Char); -- '"'
			Get_Char(Char);   -- '''
                        Actions_File.Write(Char);
		      else
			UnGet_Char(Char);  -- '"'
		      end if;
		    end if;
                when '$' =>
                    Get_Char(Char);
                    if Char = '$' then
                        Actions_File.Write("YYVal");
                    elsif Char in '0'..'9' then
                        Base := Character'Pos(Char) - Character'Pos('0');
                        while Peek_Next_Char in '0'..'9' loop
                            Get_Char(Char);
                            Base := Base * 10 +
                                    Character'Pos(Char) - Character'Pos('0');
                        end loop;
                        if Base > Rule_Length then
                            Put_Line("Ayacc: Illegal use of $"&Integer'Image(Base));
                            raise Illegal_Token;
                        end if;
                        Base := Base - Rule_Length;
                        if Base = 0 then
                            Actions_File.Write("YY.Value_Stack (YY.TOS)");
                        else
                            Actions_File.Write
                             ("YY.Value_Stack (YY.TOS - "
                                & Integer'Image (-Base) & ")");
                        end if;
                    else
                        Put_Line("Ayacc: Illegal symbol following $");
                        raise Illegal_Token;
                    end if;

                when Eoln =>
                    Actions_File.Writeln;
                    Current_Line_Number := Current_Line_Number + 1;

                when '}'  =>
                    exit;

                when others =>
                    Actions_File.Write(Char);
            end case;

        end loop;
        Actions_File.Writeln;
    end Handle_Action;

   -----------------------
   -- Dump_Declarations --
   -----------------------

   procedure Dump_Declarations is
      Ch   : Character;
      Text : Unbounded_String;

   begin
      Text := Null_Unbounded_String;

      loop
         Get_Char (Ch);

         exit when Ch = '}';

         case Ch is
            when '-' =>
               Append (Text, Ch);

               if Peek_Next_Char = '-' then
                  loop
                     Get_Char (Ch);
                     Append (Text, Ch);

                     exit when Peek_Next_Char = Eoln
                       or Peek_Next_Char = Eof;
                  end loop;
               end if;

            when '"' =>
               Append (Text, Ch);

               loop
                  Get_Char(Ch);

                  if Ch = Eoln or Ch = Eof then
                     raise Illegal_Token;
                  end if;

                  Append (Text, Ch);

                  exit when Ch = '"';
               end loop;

            when Eoln =>
               Tokens_File.Writeln (+Text);
               Text := Null_Unbounded_String;
               Current_Line_Number := Current_Line_Number + 1;

            when Eof =>
               exit;

            when others =>
               Append (Text, Ch);
         end case;
      end loop;

      Tokens_File.Writeln (+Text);
   end Dump_Declarations;

end Lexical_Analyzer;