Suppress GCC 12.1.1 warnings.
[pspp] / tests / language / lexer / segment-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2010, 2011, 2013 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <ctype.h>
20 #include <errno.h>
21 #include <getopt.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistr.h>
27
28 #include "libpspp/assertion.h"
29 #include "libpspp/cast.h"
30 #include "libpspp/compiler.h"
31 #include "libpspp/misc.h"
32 #include "language/lexer/segment.h"
33
34 #include "gl/error.h"
35 #include "gl/minmax.h"
36 #include "gl/progname.h"
37 #include "gl/read-file.h"
38 #include "gl/xalloc.h"
39 #include "gl/xmemdup0.h"
40
41 /* -a/--auto, -b/--batch, -i/--interactive: syntax mode. */
42 static enum segmenter_mode mode = SEG_MODE_AUTO;
43
44 /* -v, --verbose: Print row and column information. */
45 static bool verbose;
46
47 /* -1, --one-byte: Feed in one byte at a time? */
48 static bool one_byte;
49
50 /* -0, --truncations: Check that every truncation of input yields a result. */
51 static bool check_truncations;
52
53 /* -s, --strip-trailing-newline: Strip trailing newline from last line of
54     input. */
55 static bool strip_trailing_newline;
56
57 static const char *parse_options (int argc, char **argv);
58 static void usage (void) NO_RETURN;
59
60 static void check_segmentation (const char *input, size_t length,
61                                 bool print_segments);
62
63 int
64 main (int argc, char *argv[])
65 {
66   const char *file_name;
67   size_t length;
68   char *input;
69
70   set_program_name (argv[0]);
71   file_name = parse_options (argc, argv);
72
73   setvbuf (stdout, NULL, _IONBF, 0);
74
75   /* Read from stdin into 'input'.  Ensure that 'input' ends in a new-line
76      followed by a null byte. */
77   input = (!strcmp (file_name, "-")
78            ? fread_file (stdin, 0, &length)
79            : read_file (file_name, 0, &length));
80   if (input == NULL)
81     error (EXIT_FAILURE, errno, "reading %s failed", file_name);
82
83   if (strip_trailing_newline && length && input[length - 1] == '\n')
84     {
85       length--;
86       if (length && input[length - 1] == '\r')
87         length--;
88     }
89
90   if (!check_truncations)
91     check_segmentation (input, length, true);
92   else
93     {
94       size_t test_len;
95
96       for (test_len = 0; test_len <= length; test_len++)
97         {
98           char *copy = xmemdup (input, test_len);
99           check_segmentation (copy, test_len, false);
100           free (copy);
101         }
102     }
103   free (input);
104
105   return 0;
106 }
107
108 static void
109 check_segmentation (const char *input, size_t length, bool print_segments)
110 {
111   struct segmenter s = segmenter_init (mode, false);
112
113   size_t line_number = 1;
114   size_t line_offset = 0;
115   int prev_type = -1;
116   size_t offset = 0;
117   enum segment_type type;
118   do
119     {
120       const char *type_name, *p;
121       int n;
122
123       if (one_byte)
124         {
125           int n_newlines = 0;
126           int i;
127
128           for (i = 0; i <= length - offset; i++)
129             {
130               /* Make a copy to ensure that segmenter_push() isn't actually
131                  looking ahead. */
132               char *copy;
133
134               if (i > 0 && input[offset + i - 1] == '\n')
135                 n_newlines++;
136
137               copy = xmemdup (input + offset, i);
138               n = segmenter_push (&s, copy, i, i + offset >= length, &type);
139               free (copy);
140
141               if (n >= 0)
142                 break;
143             }
144           assert (n_newlines <= 2);
145         }
146       else
147         n = segmenter_push (&s, input + offset, length - offset, true, &type);
148
149       if (n < 0)
150         {
151           if (!print_segments)
152             check_segmentation (input, length, true);
153           else
154             error (EXIT_FAILURE, 0, "segmenter_push returned -1 at offset %zu",
155                    offset);
156         }
157       assert (offset + n <= length);
158       assert (offset <= length);
159
160       if (type == SEG_NEWLINE)
161         {
162           if (n == 1 ? input[offset] != '\n'
163               : n == 2 ? input[offset] != '\r' || input[offset + 1] != '\n'
164               : false)
165             error (EXIT_FAILURE, 0, "NEWLINE segment at offset %zu contains "
166                    "non-newline content \"%.*s\"", offset, n, &input[offset]);
167         }
168       else if (memchr (&input[offset], '\n', n))
169         error (EXIT_FAILURE, 0, "%s segment \"%.*s\" contains new-line",
170                segment_type_to_string (type), n, &input[offset]);
171
172       if (!print_segments)
173         {
174           offset += n;
175           continue;
176         }
177
178       if (!verbose)
179         {
180           if (prev_type != SEG_SPACES && prev_type != -1
181               && type == SEG_SPACES && n == 1 && input[offset] == ' ')
182             {
183               printf ("    space\n");
184               offset++;
185               prev_type = -1;
186               continue;
187             }
188         }
189       if (prev_type != -1)
190         putchar ('\n');
191       prev_type = type;
192
193       if (verbose)
194         printf ("%2zu:%2zu: ", line_number, offset - line_offset);
195
196       type_name = segment_type_to_string (type);
197       for (p = type_name; *p != '\0'; p++)
198         putchar (tolower ((unsigned char) *p));
199       if (n > 0)
200         {
201           int i;
202
203           for (i = MIN (15, strlen (type_name)); i < 16; i++)
204             putchar (' ');
205           for (i = 0; i < n;)
206             {
207               const uint8_t *u_input = CHAR_CAST (const uint8_t *, input);
208               ucs4_t uc;
209               int mblen;
210
211               mblen = u8_mbtoucr (&uc, u_input + (offset + i), n - i);
212               if (mblen < 0)
213                 {
214                   int j;
215
216                   mblen = u8_mbtouc (&uc, u_input + (offset + i), n - i);
217                   putchar ('<');
218                   for (j = 0; j < mblen; j++)
219                     {
220                       if (j > 0)
221                         putchar (' ');
222                       printf ("%02x", input[offset + i + j]);
223                     }
224                   putchar ('>');
225                 }
226               else
227                 {
228                   switch (uc)
229                     {
230                     case ' ':
231                       printf ("_");
232                       break;
233
234                     case '_':
235                       printf ("\\_");
236                       break;
237
238                     case '\\':
239                       printf ("\\\\");
240                       break;
241
242                     case '\t':
243                       printf ("\\t");
244                       break;
245
246                     case '\r':
247                       printf ("\\r");
248                       break;
249
250                     case '\n':
251                       printf ("\\n");
252                       break;
253
254                     case '\v':
255                       printf ("\\v");
256                       break;
257
258                     default:
259                       if (uc < 0x20 || uc == 0x00a0)
260                         printf ("<U+%04X>", uc);
261                       else
262                         fwrite (input + offset + i, 1, mblen, stdout);
263                       break;
264                     }
265                 }
266
267               i += mblen;
268             }
269         }
270
271       offset += n;
272       if (type == SEG_NEWLINE)
273         {
274           enum prompt_style prompt;
275
276           line_number++;
277           line_offset = offset;
278
279           prompt = segmenter_get_prompt (&s);
280           printf (" (%s)\n", prompt_style_to_string (prompt));
281         }
282       fflush (stdout);
283     }
284   while (type != SEG_END);
285
286   if (print_segments)
287     putchar ('\n');
288 }
289
290 static const char *
291 parse_options (int argc, char **argv)
292 {
293   for (;;)
294     {
295       static const struct option options[] =
296         {
297           {"one-byte", no_argument, NULL, '1'},
298           {"truncations", no_argument, NULL, '0'},
299           {"strip-trailing-newline", no_argument, NULL, 's'},
300           {"auto", no_argument, NULL, 'a'},
301           {"batch", no_argument, NULL, 'b'},
302           {"interactive", no_argument, NULL, 'i'},
303           {"verbose", no_argument, NULL, 'v'},
304           {"help", no_argument, NULL, 'h'},
305           {NULL, 0, NULL, 0},
306         };
307
308       int c = getopt_long (argc, argv, "01abivhs", options, NULL);
309       if (c == -1)
310         break;
311
312       switch (c)
313         {
314         case '1':
315           one_byte = true;
316           break;
317
318         case '0':
319           check_truncations = true;
320           break;
321
322         case 's':
323           strip_trailing_newline = true;
324           break;
325
326         case 'a':
327           mode = SEG_MODE_AUTO;
328           break;
329
330         case 'b':
331           mode = SEG_MODE_BATCH;
332           break;
333
334         case 'i':
335           mode = SEG_MODE_INTERACTIVE;
336           break;
337
338         case 'v':
339           verbose = true;
340           break;
341
342         case 'h':
343           usage ();
344
345         case 0:
346           break;
347
348         case '?':
349           exit (EXIT_FAILURE);
350           break;
351
352         default:
353           NOT_REACHED ();
354         }
355
356     }
357
358   if (optind + 1 != argc)
359     error (1, 0, "exactly one non-option argument required; "
360            "use --help for help");
361   return argv[optind];
362 }
363
364 static void
365 usage (void)
366 {
367   printf ("\
368 %s, to test breaking PSPP syntax into lexical segments\n\
369 usage: %s [OPTIONS] INPUT\n\
370 \n\
371 Options:\n\
372   -1, --one-byte      feed one byte at a time\n\
373   -0, --truncations   check null truncation of each prefix of input\n\
374   -s, --strip-trailing-newline  remove newline from end of input\n\
375   -a, --auto          use \"auto\" syntax mode\n\
376   -b, --batch         use \"batch\" syntax mode\n\
377   -i, --interactive   use \"interactive\" syntax mode (default)\n\
378   -v, --verbose       include rows and column numbers in output\n\
379   -h, --help          print this help message\n",
380           program_name, program_name);
381   exit (EXIT_SUCCESS);
382 }