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 | -- Abstract :
--
-- Parse the production rules from Input_File, add to List.
--
-- Copyright (C) 2012 - 2015 Stephen Leake. All Rights Reserved.
--
-- This program 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 program is distributed in the
-- hope that it will be useful, but WITHOUT ANY WARRANTY; without even
-- the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
-- PURPOSE. See the GNU General Public License for more details. You
-- should have received a copy of the GNU General Public License
-- distributed with this program; see file COPYING. If not, write to
-- the Free Software Foundation, 51 Franklin Street, Suite 500, Boston,
-- MA 02110-1335, USA.
pragma License (GPL);
with Ada.Directories;
with Ada.Exceptions;
with Ada.Strings.Fixed;
with Ada.Strings.Maps;
with Ada.Text_IO; use Ada.Text_IO;
with Wisi.Utils; use Wisi.Utils;
procedure Wisi.Rules
(Input_File : in Standard.Ada.Text_IO.File_Type;
Rule_List : in out Rule_Lists.List;
Rule_Count : out Integer;
Action_Count : out Integer)
is
use Standard.Ada.Strings;
use Standard.Ada.Strings.Fixed;
type State_Type is (Left_Hand_Side, Production, Action);
State : State_Type := Left_Hand_Side;
Paren_Count : Integer; -- For reporting unbalanced parens
Bracket_Count : Integer; -- ""
Token_Count : Integer; -- for reporting out of range tokens
Rule : Rule_Type;
RHS : RHS_Type;
Error : Boolean := False;
procedure Check_Numbers (Line : in String)
is
Number : Integer := 0;
First : Integer := 0;
Last_Char : Character := Line (Line'First);
begin
for I in Line'Range loop
case Line (I) is
when '0' .. '9' =>
if Last_Char = ' ' then
First := I;
end if;
when ' ' =>
if First > 0 then
Number := Integer'Value (Line (First .. I));
if Number > Token_Count then
raise Syntax_Error with "token number " & Line (First .. I) &
" out of range 1 .." & Integer'Image (Token_Count);
end if;
First := 0;
end if;
when others =>
null;
end case;
Last_Char := Line (I);
end loop;
end Check_Numbers;
procedure Update_Paren_Count (Line : in String)
is begin
for I in Line'Range loop
case Line (I) is
when '(' => Paren_Count := Paren_Count + 1;
when ')' => Paren_Count := Paren_Count - 1;
when '[' => Bracket_Count := Bracket_Count + 1;
when ']' => Bracket_Count := Bracket_Count - 1;
when others => null;
end case;
end loop;
end Update_Paren_Count;
begin
-- We assume actions start on a new line starting with either ` or
-- (, and are terminated by ; on a new line.
Rule_Count := 0;
Action_Count := 0;
loop
declare
Line : constant String := Skip_Comments (Input_File);
Cursor : Integer := Line'First;
Need_New_Line : Boolean := False;
procedure Parse_Production
is
use Standard.Ada.Strings.Maps;
Last : Integer;
begin
Last := -1 + Index (Set => To_Set (" |;"), Source => Line, From => Cursor);
if Last = -1 then Last := Line'Last; end if;
RHS.Production.Append (Line (Cursor .. Last));
if Last = Line'Last then
Need_New_Line := True;
else
Cursor := Index_Non_Blank (Source => Line, From => Last + 1);
end if;
end Parse_Production;
procedure Parse_State
is begin
case State is
when Left_Hand_Side =>
Rule_Count := Rule_Count + 1;
Cursor := -1 + Index (Set => Standard.Ada.Strings.Maps.To_Set (" :"), Source => Line);
if Cursor = -1 then Cursor := Line'Last; end if;
Rule.Left_Hand_Side := +Line (Line'First .. Cursor);
Rule.Source_Line := Standard.Ada.Text_IO.Line (Input_File) - 1;
Token_Count := 0;
State := Production;
Cursor := Index (Pattern => ":", Source => Line);
Need_New_Line := Cursor = 0;
when Production =>
case Line (Cursor) is
when '`' | '(' =>
State := Action;
RHS.Action := RHS.Action + Line;
Need_New_Line := True;
Paren_Count := 0;
Bracket_Count := 0;
Check_Numbers (Line);
Update_Paren_Count (Line);
Action_Count := Action_Count + 1;
when ';' =>
Rule.Right_Hand_Sides.Append (RHS);
Rule_List.Append (Rule);
State := Left_Hand_Side;
Need_New_Line := True;
when '|' =>
Rule.Right_Hand_Sides.Append (RHS);
RHS.Production.Clear;
RHS.Action.Clear;
Token_Count := 0;
Cursor := Index_Non_Blank (Line, From => Cursor + 1);
Need_New_Line := Cursor = 0;
when ':' =>
Rule.Right_Hand_Sides.Clear;
RHS.Production.Clear;
RHS.Action.Clear;
Cursor := Index_Non_Blank (Line, From => Cursor + 1);
Need_New_Line := Cursor = 0;
when others =>
Parse_Production;
Token_Count := Token_Count + 1;
end case;
when Action =>
case Line (Cursor) is
when ';' =>
Rule.Right_Hand_Sides.Append (RHS);
Rule_List.Append (Rule);
State := Left_Hand_Side;
Need_New_Line := True;
if Paren_Count /= 0 then
raise Syntax_Error with "unbalanced parens in action";
end if;
if Bracket_Count /= 0 then
raise Syntax_Error with "unbalanced brackets in action";
end if;
when '|' =>
Rule.Right_Hand_Sides.Append (RHS);
State := Production;
RHS.Production.Clear;
RHS.Action.Clear;
Cursor := Index_Non_Blank (Line, From => Cursor + 1);
Need_New_Line := Cursor = 0;
if Paren_Count /= 0 then
raise Syntax_Error with "unbalanced parens in action";
end if;
if Bracket_Count /= 0 then
raise Syntax_Error with "unbalanced brackets in action";
end if;
when others =>
RHS.Action := RHS.Action + Line;
Need_New_Line := True;
Check_Numbers (Line);
Update_Paren_Count (Line);
end case;
end case;
end Parse_State;
begin
exit when Line = "%%";
loop
Parse_State;
exit when Need_New_Line;
end loop;
exception
when E : Syntax_Error =>
Error := True;
declare
use Standard.Ada.Exceptions;
use Standard.Ada.Directories;
begin
Standard.Ada.Text_IO.Put_Line
(Simple_Name (Name (Input_File)) & ":" &
Trim (Standard.Ada.Text_IO.Count'Image (Standard.Ada.Text_IO.Line (Input_File)), Left) & ":0: " &
Exception_Message (E));
end;
when E : others =>
declare
use Standard.Ada.Exceptions;
use Standard.Ada.Directories;
begin
Standard.Ada.Text_IO.Put_Line
(Simple_Name (Name (Input_File)) & ":" &
Trim (Standard.Ada.Text_IO.Count'Image (Standard.Ada.Text_IO.Line (Input_File)), Left) &
":0: unhandled exception " & Exception_Name (E));
end;
raise Syntax_Error;
end;
end loop;
if Error then
raise Syntax_Error;
end if;
end Wisi.Rules;
|