anagram_1.0.0_49233f56/sources/generated/ag.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
with Ada.Wide_Wide_Text_Io;
with Anagram.String_Sources;
with Anagram.Grammars.Scanners;
with Anagram.Grammars.Scanner_Handler;
with League.String_Vectors;
with Anagram.Grammars.Parser_Utils;
-- 3
with Ag.Goto_Table;
use  Ag.Goto_Table;
with Ag_Tokens;
use  Ag_Tokens;
with Ag.Shift_Reduce;
use  Ag.Shift_Reduce;

package body Ag is
   package Ag_Goto renames Ag.Goto_Table;
   package Ag_Shift_Reduce  renames Ag.Shift_Reduce;
   function Grammar (Self : in out Parser) return Anagram.Grammars.Grammar is
   begin
      return Anagram.Grammars.Constructors.Complete (Self.Constructor);
   end Grammar;

   procedure Read
     (Self : in out Parser;
      Text : League.Strings.Universal_String;
      Tail_List : Boolean := False)
   is
      Scanner     : aliased Anagram.Grammars.Scanners.Scanner;
      Handler     : aliased Anagram.Grammars.Scanner_Handler.Handler;
      Context     : PU.Context_Node;

      procedure yyerror (X : Wide_Wide_String) is
      begin
        Ada.Wide_Wide_Text_IO.Put_Line
          (X & " on line" & Positive'Wide_Wide_Image (Handler.Get_Line));
      end;

      function YYLex return Token is
         Result : Token;
      begin
         Scanner.Get_Token (Result);
         return Result;
      end YYLex;

procedure YYParse is

   -- Rename User Defined Packages to Internal Names.
    package yy_goto_tables         renames
      Ag.Goto_Table;
    package yy_shift_reduce_tables renames
      Ag.Shift_Reduce;
    package yy_tokens              renames
      Ag_Tokens;

   use yy_tokens, yy_goto_tables, yy_shift_reduce_tables;

   procedure yyerrok;
   procedure yyclearin;


   package yy is

       -- the size of the value and state stacks
       stack_size : constant Natural := 300;

       -- subtype rule         is natural;
       subtype parse_state  is natural;
       -- subtype nonterminal  is integer;

       -- encryption constants
       default           : constant := -1;
       first_shift_entry : constant :=  0;
       accept_code       : constant := -3001;
       error_code        : constant := -3000;

       -- stack data used by the parser
       tos                : natural := 0;
       value_stack        : array(0..stack_size) of yy_tokens.yystype;
       state_stack        : array(0..stack_size) of parse_state;

       -- current input symbol and action the parser is on
       action             : integer;
       rule_id            : rule;
       input_symbol       : yy_tokens.token;


       -- error recovery flag
       error_flag : natural := 0;
          -- indicates  3 - (number of valid shifts after an error occurs)

       look_ahead : boolean := true;
       index      : integer;

       -- Is Debugging option on or off
        DEBUG : constant boolean := FALSE;

    end yy;


    function goto_state
      (state : yy.parse_state;
       sym   : nonterminal) return yy.parse_state;

    function parse_action
      (state : yy.parse_state;
       t     : yy_tokens.token) return integer;

    pragma inline(goto_state, parse_action);


    function goto_state(state : yy.parse_state;
                        sym   : nonterminal) return yy.parse_state is
        index : integer;
    begin
        index := goto_offset(state);
        while  integer(goto_matrix(index).nonterm) /= sym loop
            index := index + 1;
        end loop;
        return integer(goto_matrix(index).newstate);
    end goto_state;


    function parse_action(state : yy.parse_state;
                          t     : yy_tokens.token) return integer is
        index      : integer;
        tok_pos    : integer;
        default    : constant integer := -1;
    begin
        tok_pos := yy_tokens.token'pos(t);
        index   := shift_reduce_offset(state);
        while integer(shift_reduce_matrix(index).t) /= tok_pos and then
              integer(shift_reduce_matrix(index).t) /= default
        loop
            index := index + 1;
        end loop;
        return integer(shift_reduce_matrix(index).act);
    end parse_action;

-- error recovery stuff

    procedure handle_error is
      temp_action : integer;
    begin

      if yy.error_flag = 3 then -- no shift yet, clobber input.
      if yy.debug then
          Ada.Wide_Wide_Text_Io.Put_Line ("Ayacc.YYParse: Error Recovery Clobbers " &
                   yy_tokens.token'Wide_Wide_Image (yy.input_symbol));
      end if;
        if yy.input_symbol = yy_tokens.end_of_input then  -- don't discard,
        if yy.debug then
            Ada.Wide_Wide_Text_IO.Put_Line ("Ayacc.YYParse: Can't discard END_OF_INPUT, quiting...");
        end if;
        raise yy_tokens.syntax_error;
        end if;

            yy.look_ahead := true;   -- get next token
        return;                  -- and try again...
    end if;

    if yy.error_flag = 0 then -- brand new error
        yyerror("Syntax Error");
    end if;

    yy.error_flag := 3;

    -- find state on stack where error is a valid shift --

    if yy.debug then
        Ada.Wide_Wide_Text_IO.Put_Line ("Ayacc.YYParse: Looking for state with error as valid shift");
    end if;

    loop
        if yy.debug then
          Ada.Wide_Wide_Text_IO.Put_Line ("Ayacc.YYParse: Examining State " &
               yy.parse_state'Wide_Wide_Image (yy.state_stack(yy.tos)));
        end if;
        temp_action := parse_action(yy.state_stack(yy.tos), error);

            if temp_action >= yy.first_shift_entry then
                if yy.tos = yy.stack_size then
                    Ada.Wide_Wide_Text_IO.Put_Line (" Stack size exceeded on state_stack");
                    raise yy_Tokens.syntax_error;
                end if;
                yy.tos := yy.tos + 1;
                yy.state_stack(yy.tos) := temp_action;
                exit;
            end if;

        Decrement_Stack_Pointer :
        begin
          yy.tos := yy.tos - 1;
        exception
          when Constraint_Error =>
            yy.tos := 0;
        end Decrement_Stack_Pointer;

        if yy.tos = 0 then
          if yy.debug then
            Ada.Wide_Wide_Text_IO.Put_Line ("Ayacc.YYParse: Error recovery popped entire stack, aborting...");
          end if;
          raise yy_tokens.syntax_error;
        end if;
    end loop;

    if yy.debug then
        Ada.Wide_Wide_Text_IO.Put_Line ("Ayacc.YYParse: Shifted error token in state " &
              yy.parse_state'Wide_Wide_Image (yy.state_stack(yy.tos)));
    end if;

    end handle_error;

   -- print debugging information for a shift operation
   procedure shift_debug(state_id: yy.parse_state; lexeme: yy_tokens.token) is
   begin
       Ada.Wide_Wide_Text_IO.Put_Line ("Ayacc.YYParse: Shift "& yy.parse_state'Wide_Wide_Image (state_id)&" on input symbol "&
               yy_tokens.token'Wide_Wide_Image (lexeme));
   end;

   -- print debugging information for a reduce operation
   procedure reduce_debug(rule_id: rule; state_id: yy.parse_state) is
   begin
       Ada.Wide_Wide_Text_IO.Put_Line ("Ayacc.YYParse: Reduce by rule "&rule'Wide_Wide_Image (rule_id)&" goto state "&
               yy.parse_state'Wide_Wide_Image (state_id));
   end;

   -- make the parser believe that 3 valid shifts have occured.
   -- used for error recovery.
   procedure yyerrok is
   begin
       yy.error_flag := 0;
   end yyerrok;

   -- called to clear input symbol that caused an error.
   procedure yyclearin is
   begin
       -- yy.input_symbol := yylex;
       yy.look_ahead := true;
   end yyclearin;


begin
    -- initialize by pushing state 0 and getting the first input symbol
    yy.state_stack(yy.tos) := 0;


    loop

        yy.index := shift_reduce_offset(yy.state_stack(yy.tos));
        if integer(shift_reduce_matrix(yy.index).t) = yy.default then
            yy.action := integer(shift_reduce_matrix(yy.index).act);
        else
            if yy.look_ahead then
                yy.look_ahead   := false;

                yy.input_symbol := yylex;
            end if;
            yy.action :=
             parse_action(yy.state_stack(yy.tos), yy.input_symbol);
        end if;


        if yy.action >= yy.first_shift_entry then  -- SHIFT

            if yy.debug then
                shift_debug(yy.action, yy.input_symbol);
            end if;

            -- Enter new state
            if yy.tos = yy.stack_size then
                Ada.Wide_Wide_Text_IO.Put_Line (" Stack size exceeded on state_stack");
                raise yy_Tokens.syntax_error;
            end if;
            yy.tos := yy.tos + 1;
            yy.state_stack(yy.tos) := yy.action;
              yy.value_stack(yy.tos) := yylval;

        if yy.error_flag > 0 then  -- indicate a valid shift
            yy.error_flag := yy.error_flag - 1;
        end if;

            -- Advance lookahead
            yy.look_ahead := true;

        elsif yy.action = yy.error_code then       -- ERROR

            handle_error;

        elsif yy.action = yy.accept_code then
            if yy.debug then
                Ada.Wide_Wide_Text_IO.Put_Line ("Ayacc.YYParse: Accepting Grammar...");
            end if;
            exit;

        else -- Reduce Action

            -- Convert action into a rule
            yy.rule_id  := -1 * yy.action;

            -- Execute User Action
            -- user_action(yy.rule_id);


                case yy.rule_id is

when 12 =>
--# line 72 "../ag.y"
 Context.Add_Token (YY.Value_Stack (YY.TOS -  1).Image); 

when 13 =>
--# line 75 "../ag.y"
 Context.Add_Token (YY.Value_Stack (YY.TOS -  4).Image, YY.Value_Stack (YY.TOS -  2).Image, YY.Value_Stack (YY.TOS -  1).Image); 

when 14 =>
--# line 79 "../ag.y"
 Context.Add_With (YY.Value_Stack (YY.TOS -  1).Image); 

when 15 =>
--# line 83 "../ag.y"

  Context.Add_Non_Terminal (YY.Value_Stack (YY.TOS -  3).Image, YY.Value_Stack (YY.TOS -  1).Production_List);


when 16 =>
--# line 89 "../ag.y"

 YYVal := (Production_List,
        Context.New_Production_List (YY.Value_Stack (YY.TOS).Named_Production));


when 17 =>
--# line 95 "../ag.y"

 YYVal := (Production_List,
        Context.Add_Production (YY.Value_Stack (YY.TOS -  2).Production_List, YY.Value_Stack (YY.TOS).Named_Production));


when 18 =>
--# line 102 "../ag.y"

  YYVal := (Named_Production,
         Context.To_Named_Production (YY.Value_Stack (YY.TOS).Production, YY.Value_Stack (YY.TOS -  2).Image));


when 19 =>
--# line 108 "../ag.y"

  YYVal := (Named_Production,
         Context.To_Named_Production (YY.Value_Stack (YY.TOS).Production));


when 20 =>
--# line 115 "../ag.y"
 YYVal := (Production, Context.New_Production (YY.Value_Stack (YY.TOS).Named_Part));

when 21 =>
--# line 118 "../ag.y"
 YYVal := (Production, Context.Add_Part (YY.Value_Stack (YY.TOS -  1).Production, YY.Value_Stack (YY.TOS).Named_Part));

when 22 =>
--# line 122 "../ag.y"
 YYVal := (Named_Part, Context.To_Named_Part (YY.Value_Stack (YY.TOS).Part)); 

when 23 =>
--# line 125 "../ag.y"
 YYVal := (Named_Part, Context.To_Named_Part (YY.Value_Stack (YY.TOS -  3).Part, YY.Value_Stack (YY.TOS -  1).Image)); 

when 24 =>
--# line 129 "../ag.y"
 YYVal := (Part, Context.Add_Reference (YY.Value_Stack (YY.TOS).Image)); 

when 25 =>
--# line 132 "../ag.y"
 YYVal := (Part, Context.Add_List (YY.Value_Stack (YY.TOS -  1).Production_List)); 

when 26 =>
--# line 135 "../ag.y"
 YYVal := (Part, Context.Add_Option ( YY.Value_Stack (YY.TOS -  1).Production_List)); 

when 30 =>
--# line 152 "../ag.y"

  YYVal := (Kind => None);
  Context.Add_Inherited_Attr
   (Target => YY.Value_Stack (YY.TOS -  5).Vector,
    Tipe   => YY.Value_Stack (YY.TOS -  3).Image,
    Names  => YY.Value_Stack (YY.TOS -  1).Vector);


when 31 =>
--# line 163 "../ag.y"

  YYVal := (Kind => None);
  Context.Add_Synthesized_Attr
   (Target => YY.Value_Stack (YY.TOS -  5).Vector,
    Tipe   => YY.Value_Stack (YY.TOS -  3).Image,
    Names  => YY.Value_Stack (YY.TOS -  1).Vector);


when 32 =>
--# line 174 "../ag.y"

  YYVal := (Kind => None);
  Context.Add_Local_Attr
   (Target => YY.Value_Stack (YY.TOS -  5).Vector,
    Tipe   => YY.Value_Stack (YY.TOS -  3).Image,
    Names  => YY.Value_Stack (YY.TOS -  1).Vector);


when 34 =>
--# line 187 "../ag.y"

  YYVal := (Kind => None);
  Context.Add_Rule (YY.Value_Stack (YY.TOS -  4).Vector, YY.Value_Stack (YY.TOS -  1).Image);


when 35 =>
--# line 194 "../ag.y"
 YYVal := (Image, Scanner.Get_Text); 

when 36 =>
--# line 198 "../ag.y"

  Context.Add_Priority (YY.Value_Stack (YY.TOS -  3).Image, YY.Value_Stack (YY.TOS -  2).Image, YY.Value_Stack (YY.TOS -  1).Image);


when 37 =>
--# line 204 "../ag.y"

   YYVal := (Vector, League.String_Vectors.Empty_Universal_String_Vector);
   YYVal.Vector.Append (YY.Value_Stack (YY.TOS).Image);


when 38 =>
--# line 210 "../ag.y"

   YYVal :=(Vector, YY.Value_Stack (YY.TOS -  2).Vector);
   YYVal.Vector.Append (YY.Value_Stack (YY.TOS).Image);


when 39 =>
--# line 217 "../ag.y"
 YYVal := (Image, Scanner.Get_Text); 

when 40 =>
--# line 221 "../ag.y"
 YYVal := (Image, Scanner.Get_Text); 

                    when others => null;
                end case;


            -- Pop RHS states and goto next state
            yy.tos      := yy.tos - rule_length(yy.rule_id) + 1;
            if yy.tos > yy.stack_size then
                Ada.Wide_Wide_Text_IO.Put_Line (" Stack size exceeded on state_stack");
                raise yy_Tokens.syntax_error;
            end if;
            yy.state_stack(yy.tos) := goto_state(yy.state_stack(yy.tos-1) ,
                                 get_lhs_rule(yy.rule_id));

              yy.value_stack(yy.tos) := yyval;

            if yy.debug then
                reduce_debug(yy.rule_id,
                    goto_state(yy.state_stack(yy.tos - 1),
                               get_lhs_rule(yy.rule_id)));
            end if;

        end if;


    end loop;


end yyparse;

      Source   : aliased Anagram.String_Sources.String_Source;
   begin
      Source.Create (Text);
      Scanner.Set_Source (Source'Unchecked_Access);
      Scanner.Set_Handler (Handler'Unchecked_Access);
      YYParse;
      Context.Complete (Self.Constructor, Tail_List);
   end Read;
end Ag;