emacs_ada_mode_8.1.0_114ab44a/dump_wisitoken_corrected.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
--  Abstract :
--
--  Parse a file with the WisiToken lalr parser, output the corrected token stream.
--
--  Copyright (C) 2020 Stephen Leake All Rights Reserved.
--
--  This library is free software;  you can redistribute it and/or modify it
--  under terms of the  GNU General Public License  as published by the Free
--  Software  Foundation;  either version 3,  or (at your  option) any later
--  version. This library is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN-
--  TABILITY or FITNESS FOR A PARTICULAR PURPOSE.

pragma License (GPL);

with Ada.Characters.Handling;
with Ada.Command_Line;
with Ada.Directories;
with Ada.Exceptions;
with Ada.IO_Exceptions;
with Ada.Strings.Unbounded;
with Ada.Text_IO; use Ada.Text_IO;
with Ada_Annex_P_Process_Actions;
with Ada_Annex_P_Process_LALR_Main;
with Ada_Annex_P_Process_LR1_Main;
with GNAT.Traceback.Symbolic;
with WisiToken.Parse.LR.McKenzie_Recover.Ada;
with WisiToken.Parse.LR.Parser;
with WisiToken.Syntax_Trees;
with WisiToken.Text_IO_Trace;
procedure Dump_WisiToken_Corrected
is
   use WisiToken;
   use all type Ada_Annex_P_Process_Actions.Token_Enum_ID;

   procedure Put_Usage
   is begin
      Put_Line (Standard_Error, "dump_wisitoken_corrected <alg> <file> [trace_parse trace_mckenzie]");
      Put_Line ("alg {LR1 | LALR}");
   end Put_Usage;

   File_Name : Ada.Strings.Unbounded.Unbounded_String;

   Trace    : aliased WisiToken.Text_IO_Trace.Trace;
   Log_File : Ada.Text_IO.File_Type;
   Parser   : WisiToken.Parse.LR.Parser.Parser;

   function Image (Node : in WisiToken.Syntax_Trees.Valid_Node_Access) return String
   is
      use Ada_Annex_P_Process_Actions;
      use WisiToken.Syntax_Trees;

      Punctuation_Image : constant Token_ID_Array_String (+LEFT_PAREN_ID .. +TICK_2_ID) :=
        (new String'("("),
         new String'("["),
         new String'(")"),
         new String'("]"),
         new String'("&"),
         new String'("@"),
         new String'("|"),
         new String'("<>"),
         new String'(":"),
         new String'(":="),
         new String'(","),
         new String'("."),
         new String'(".."),
         new String'("="),
         new String'("=>"),
         new String'(">"),
         new String'(">="),
         new String'(">>"),
         new String'("<"),
         new String'("<="),
         new String'("<<"),
         new String'("-"),
         new String'("+"),
         new String'(";"),
         new String'("/"),
         new String'("/="),
         new String'("*"),
         new String'("**"),
         new String'("'"),
         new String'("'"));

      Tree : WisiToken.Syntax_Trees.Tree renames Parser.Tree;
      ID   : constant Token_ID := Tree.ID (Node);
   begin
      if Tree.Label (Node) = Source_Terminal then
         declare
            Token : constant WisiToken.Base_Token := Tree.Base_Token (Node);
         begin
            case To_Token_Enum (ID) is
            when IDENTIFIER_ID =>
               return "IDENTIFIER " & Parser.Tree.Lexer.Buffer_Text (Token.Byte_Region);
            when CHARACTER_LITERAL_ID =>
               return "CHARACTER_LITERAL " & Parser.Tree.Lexer.Buffer_Text (Token.Byte_Region);
            when NUMERIC_LITERAL_ID =>
               return "NUMERIC_LITERAL";
            when STRING_LITERAL_ID =>
               return "STRING_LITERAL";
            when others =>
               return Parser.Tree.Lexer.Buffer_Text (Token.Byte_Region);
            end case;
         end;
      else
         if -ID in RIGHT_PAREN_ID .. TICK_2_ID then
            return Punctuation_Image (ID).all;
         elsif -ID = IDENTIFIER_ID then
            return "IDENTIFIER";
         elsif -ID = CHARACTER_LITERAL_ID then
            return "CHARACTER_LITERAL";
         else
            return Ada.Characters.Handling.To_Lower (Ada_Annex_P_Process_Actions.Descriptor.Image (ID).all);
         end if;
      end if;
   end Image;

begin
   declare
      use Ada.Command_Line;
      use Ada.Directories;
   begin
      Set_Exit_Status (Success);
      if Argument_Count < 2 then
         Put_Usage;
         Set_Exit_Status (Failure);
         return;
      end if;

      if Argument (1) = "LR1" then
         WisiToken.Parse.LR.Parser.New_Parser
           (Parser,
            Trace'Unrestricted_Access,
            Ada_Annex_P_Process_LR1_Main.Create_Lexer,
            Ada_Annex_P_Process_LR1_Main.Create_Parse_Table
              (Text_Rep_File_Name => Containing_Directory (Command_Name) & "/ada_lr1_parse_table.txt"),
            WisiToken.Parse.LR.McKenzie_Recover.Ada.Language_Fixes'Access,
            WisiToken.Parse.LR.McKenzie_Recover.Ada.Matching_Begin_Tokens'Access,
            WisiToken.Parse.LR.McKenzie_Recover.Ada.String_ID_Set'Access,
            User_Data => null);
      else
         WisiToken.Parse.LR.Parser.New_Parser
           (Parser,
            Trace'Unrestricted_Access,
            Ada_Annex_P_Process_LALR_Main.Create_Lexer,
            Ada_Annex_P_Process_LALR_Main.Create_Parse_Table,
            WisiToken.Parse.LR.McKenzie_Recover.Ada.Language_Fixes'Access,
            WisiToken.Parse.LR.McKenzie_Recover.Ada.Matching_Begin_Tokens'Access,
            WisiToken.Parse.LR.McKenzie_Recover.Ada.String_ID_Set'Access,
            User_Data => null);
      end if;

      File_Name := +Argument (2);
      begin
         Parser.Tree.Lexer.Reset_With_File (-File_Name);
      exception
      when Ada.IO_Exceptions.Name_Error =>
         Put_Line (Standard_Error, "'" & (-File_Name) & "' cannot be opened");
         Set_Exit_Status (Failure);
         return;
      end;

      if Argument_Count >= 3 then
         WisiToken.Trace_Parse := Integer'Value (Argument (3));
      end if;

      if Argument_Count >= 4 then
         WisiToken.Trace_McKenzie := Integer'Value (Argument (4));
      end if;
   end;

   Parser.Trace.Set_Prefix (";; "); -- so we get the same debug messages as Emacs_Wisi_Common_Parse

   Parser.Table.McKenzie_Param.Task_Count := 1; -- minimize race conditions

   Parser.Parse (Log_File);

   if Trace_Parse > 0 then
      Parser.Put_Errors;
   end if;

   declare
      Terminals : constant WisiToken.Syntax_Trees.Valid_Node_Access_Array :=
        Parser.Tree.Get_Terminals (Parser.Tree.Root);
   begin
      for T of Terminals loop
         Put_Line (Image (T));
      end loop;
   end;

exception
when E : others =>
   Put_Line (Standard_Error, -File_Name & ":1:1");
   Put_Line (Standard_Error,
             "exception " & Ada.Exceptions.Exception_Name (E) & ": " & Ada.Exceptions.Exception_Message (E));
   Put_Line (Standard_Error, GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
   Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
end Dump_WisiToken_Corrected;