aflex_1.7.0_77dbb4da/src/ascan_io.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
--  Warning: This file is automatically generated by AFLEX.
--           It is useless to modify it. Change the ".Y" & ".L" files instead.
--  Template: templates/body-io.adb
package body ascan_IO is

   --  gets input and stuffs it into 'buf'.  number of characters read, or YY_NULL,
   --  is returned in 'result'.

   procedure YY_INPUT (buf      : out unbounded_character_array;
                       result   : out Integer;
                       max_size : in Integer) is
      c   : Character;
      i   : Integer := 1;
      loc : Integer := buf'First;
   begin
      if Ada.Text_IO.Is_Open (user_input_file) then
         while i <= max_size loop
            --  Ada ate our newline, put it back on the end.
            if Ada.Text_IO.End_Of_Line (user_input_file) then
               buf (loc) := ASCII.LF;
               Ada.Text_IO.Skip_Line (user_input_file, 1);
            else
               --  UCI CODES CHANGED:
               --    The following codes are modified. Previous codes is commented out.
               --    The purpose of doing this is to make it possible to set Temp_Line
               --    in Ayacc-extension specific codes. Definitely, we can read the character
               --    into the Temp_Line and then set the buf. But Temp_Line will only
               --    be used in Ayacc-extension specific codes which makes
               --    this approach impossible.
               Ada.Text_IO.Get (user_input_file, c);
               buf (loc) := c;
--             Ada.Text_IO.Get (user_input_file, buf (loc));
            end if;

            loc := loc + 1;
            i := i + 1;
         end loop;
      else
         while i <= max_size loop
            if Ada.Text_IO.End_Of_Line then -- Ada ate our newline, put it back on the end.
               buf (loc) := ASCII.LF;
               Ada.Text_IO.Skip_Line (1);

            else
               --  The following codes are modified. Previous codes is commented out.
               --  The purpose of doing this is to make it possible to set Temp_Line
               --  in Ayacc-extension specific codes. Definitely, we can read the character
               --  into the Temp_Line and then set the buf. But Temp_Line will only
               --  be used in Ayacc-extension specific codes which makes this approach impossible.
               Ada.Text_IO.Get (c);
               buf (loc) := c;
               --         get (buf (loc));
            end if;

            loc := loc + 1;
            i := i + 1;
         end loop;
      end if; --  for input file being standard input
      result := i - 1;

   exception
      when Ada.Text_IO.End_Error =>
         result := i - 1;
         --  when we hit EOF we need to set yy_eof_has_been_seen
         yy_eof_has_been_seen := True;

   end YY_INPUT;

   --  yy_get_next_buffer - try to read in new buffer
   --
   --  returns a code representing an action
   --     EOB_ACT_LAST_MATCH -
   --     EOB_ACT_RESTART_SCAN - restart the scanner
   --     EOB_ACT_END_OF_FILE - end of file

   function yy_get_next_buffer return eob_action_type is
      dest           : Integer := 0;
      source         : Integer := yytext_ptr - 1; -- copy prev. char, too
      number_to_move : Integer;
      ret_val        : eob_action_type;
      num_to_read    : Integer;
   begin
      if yy_c_buf_p > yy_n_chars + 1 then
         raise NULL_IN_INPUT;
      end if;

      --  try to read more data

      --  first move last chars to start of buffer
      number_to_move := yy_c_buf_p - yytext_ptr;

      for i in 0 .. number_to_move - 1 loop
         yy_ch_buf (dest) := yy_ch_buf (source);
         dest := dest + 1;
         source := source + 1;
      end loop;

      if yy_eof_has_been_seen then
         --  don't do the read, it's not guaranteed to return an EOF,
         --  just force an EOF

         yy_n_chars := 0;
      else
         num_to_read := YY_BUF_SIZE - number_to_move - 1;

         if num_to_read > YY_READ_BUF_SIZE then
            num_to_read := YY_READ_BUF_SIZE;
         end if;

         --  read in more data
         YY_INPUT (yy_ch_buf (number_to_move .. yy_ch_buf'Last), yy_n_chars, num_to_read);
      end if;
      if yy_n_chars = 0 then
         if number_to_move = 1 then
            ret_val := EOB_ACT_END_OF_FILE;
         else
            ret_val := EOB_ACT_LAST_MATCH;
         end if;

         yy_eof_has_been_seen := True;
      else
         ret_val := EOB_ACT_RESTART_SCAN;
      end if;

      yy_n_chars := yy_n_chars + number_to_move;
      yy_ch_buf (yy_n_chars) := YY_END_OF_BUFFER_CHAR;
      yy_ch_buf (yy_n_chars + 1) := YY_END_OF_BUFFER_CHAR;

      --  yytext begins at the second character in
      --  yy_ch_buf; the first character is the one which
      --  preceded it before reading in the latest buffer;
      --  it needs to be kept around in case it's a
      --  newline, so yy_get_previous_state() will have
      --  with '^' rules active

      yytext_ptr := 1;

      return ret_val;
   end yy_get_next_buffer;

   procedure yyUnput (c : Character; yy_bp : in out Integer) is
      number_to_move : Integer;
      dest : Integer;
      source : Integer;
      tmp_yy_cp : Integer;
   begin
      tmp_yy_cp := yy_c_buf_p;
      yy_ch_buf (tmp_yy_cp) := yy_hold_char; --  undo effects of setting up yytext

      if tmp_yy_cp < 2 then
         --  need to shift things up to make room
         number_to_move := yy_n_chars + 2; --  +2 for EOB chars
         dest := YY_BUF_SIZE + 2;
         source := number_to_move;

         while source > 0 loop
            dest := dest - 1;
            source := source - 1;
            yy_ch_buf (dest) := yy_ch_buf (source);
         end loop;

         tmp_yy_cp := tmp_yy_cp + dest - source;
         yy_bp := yy_bp + dest - source;
         yy_n_chars := YY_BUF_SIZE;

         if tmp_yy_cp < 2 then
            raise PUSHBACK_OVERFLOW;
         end if;
      end if;

      if tmp_yy_cp > yy_bp and then yy_ch_buf (tmp_yy_cp - 1) = ASCII.LF then
         yy_ch_buf (tmp_yy_cp - 2) := ASCII.LF;
      end if;

      tmp_yy_cp := tmp_yy_cp - 1;
      yy_ch_buf (tmp_yy_cp) := c;

      --  Note:  this code is the text of YY_DO_BEFORE_ACTION, only
      --         here we get different yy_cp and yy_bp's
      yytext_ptr := yy_bp;
      yy_hold_char := yy_ch_buf (tmp_yy_cp);
      yy_ch_buf (tmp_yy_cp) := ASCII.NUL;
      yy_c_buf_p := tmp_yy_cp;
   end yyUnput;

   procedure Unput (c : Character) is
   begin
      yyUnput (c, yy_bp);
   end Unput;

   function Input return Character is
      c : Character;
   begin
      yy_ch_buf (yy_c_buf_p) := yy_hold_char;

      if yy_ch_buf (yy_c_buf_p) = YY_END_OF_BUFFER_CHAR then
         --  need more input
         yytext_ptr := yy_c_buf_p;
         yy_c_buf_p := yy_c_buf_p + 1;

         case yy_get_next_buffer is
            --  this code, unfortunately, is somewhat redundant with
            --  that above

         when EOB_ACT_END_OF_FILE =>
            if yyWrap then
               yy_c_buf_p := yytext_ptr;
               return ASCII.NUL;
            end if;

            yy_ch_buf (0) := ASCII.LF;
            yy_n_chars := 1;
            yy_ch_buf (yy_n_chars) := YY_END_OF_BUFFER_CHAR;
            yy_ch_buf (yy_n_chars + 1) := YY_END_OF_BUFFER_CHAR;
            yy_eof_has_been_seen := False;
            yy_c_buf_p := 1;
            yytext_ptr := yy_c_buf_p;
            yy_hold_char := yy_ch_buf (yy_c_buf_p);

            return Input;
         when EOB_ACT_RESTART_SCAN =>
            yy_c_buf_p := yytext_ptr;

         when EOB_ACT_LAST_MATCH =>
            raise UNEXPECTED_LAST_MATCH;
         end case;
      end if;

      c := yy_ch_buf (yy_c_buf_p);
      yy_c_buf_p := yy_c_buf_p + 1;
      yy_hold_char := yy_ch_buf (yy_c_buf_p);

      return c;
   end Input;

   procedure Output (c : Character) is
   begin
      if Ada.Text_IO.Is_Open (user_output_file) then
         Ada.Text_IO.Put (user_output_file, c);
      else
         Ada.Text_IO.Put (c);
      end if;
   end Output;

   procedure Output_New_Line is
   begin
      if Ada.Text_IO.Is_Open (user_output_file) then
         Ada.Text_IO.New_Line (user_output_file);
      else
         Ada.Text_IO.New_Line;
      end if;
   end Output_New_Line;

   function Output_Column return Ada.Text_IO.Count is
   begin
      if Ada.Text_IO.Is_Open (user_output_file) then
         return Ada.Text_IO.Col (user_output_file);
      else
         return Ada.Text_IO.Col;
      end if;
   end Output_Column;

   --  default yywrap function - always treat EOF as an EOF
   function yyWrap return Boolean is
   begin
      return True;
   end yyWrap;

   procedure Open_Input (fname : in String) is
   begin
      yy_init := True;
      Ada.Text_IO.Open (user_input_file, Ada.Text_IO.In_File, fname);
      yylineno  := 1;
   end Open_Input;

   procedure Create_Output (fname : in String := "") is
   begin
      if fname /= "" then
         Ada.Text_IO.Create (user_output_file, Ada.Text_IO.Out_File, fname);
      end if;
   end Create_Output;

   procedure Close_Input is
   begin
      if Ada.Text_IO.Is_Open (user_input_file) then
         Ada.Text_IO.Close (user_input_file);
      end if;
   end Close_Input;

   procedure Close_Output is
   begin
      if Ada.Text_IO.Is_Open (user_output_file) then
         Ada.Text_IO.Close (user_output_file);
      end if;
   end Close_Output;

end ascan_IO;