segment: New library for low-level phase of lexical syntax analysis.
[pspp-builds.git] / tests / language / lexer / segment-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2010, 2011 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
40 /* -a/--auto, -b/--batch, -i/--interactive: syntax mode. */
41 static enum segmenter_mode mode = SEG_MODE_AUTO;
42
43 /* -v, --verbose: Print row and column information. */
44 static bool verbose;
45
46 /* -1, --one-byte: Feed in one byte at a time? */
47 static bool one_byte;
48
49 static const char *parse_options (int argc, char **argv);
50 static void usage (void) NO_RETURN;
51
52 int
53 main (int argc, char *argv[])
54 {
55   size_t offset, line_number, line_offset;
56   const char *file_name;
57   char *input;
58   struct segmenter s;
59   size_t length;
60   int prev_type;
61
62   set_program_name (argv[0]);
63   file_name = parse_options (argc, argv);
64
65   /* Read from stdin into 'input'.  Ensure that 'input' ends in a new-line
66      followed by a null byte. */
67   input = (!strcmp (file_name, "-")
68            ? fread_file (stdin, &length)
69            : read_file (file_name, &length));
70   if (input == NULL)
71     error (EXIT_FAILURE, errno, "reading %s failed", file_name);
72   input = xrealloc (input, length + 3);
73   if (length == 0 || input[length - 1] != '\n')
74     input[length++] = '\n';
75   input[length++] = '\0';
76
77   segmenter_init (&s, mode);
78
79   line_number = 1;
80   line_offset = 0;
81   prev_type = -1;
82   for (offset = 0; offset < length; )
83     {
84       enum segment_type type;
85       const char *type_name, *p;
86       int n;
87
88       if (one_byte)
89         {
90           int n_newlines = 0;
91           int i;
92
93           for (i = 0; i <= length - offset; i++)
94             {
95               /* Make a copy to ensure that segmenter_push() isn't actually
96                  looking ahead. */
97               char *copy;
98
99               if (i > 0 && input[offset + i - 1] == '\n')
100                 n_newlines++;
101
102               copy = xmemdup (input + offset, i);
103               n = segmenter_push (&s, copy, i, &type);
104               free (copy);
105
106               if (n >= 0)
107                 break;
108             }
109           assert (n_newlines <= 2);
110         }
111       else
112         n = segmenter_push (&s, input + offset, length - offset, &type);
113
114       if (n < 0)
115         error (EXIT_FAILURE, 0, "segmenter_push returned -1 at offset %zu",
116                offset);
117       assert (offset + n <= length);
118
119       if (type == SEG_NEWLINE)
120         assert ((n == 1 && input[offset] == '\n')
121                 || (n == 2
122                     && input[offset] == '\r' && input[offset + 1] == '\n'));
123       else
124         assert (memchr (&input[offset], '\n', n) == NULL);
125
126       if (!verbose)
127         {
128           if (prev_type != SEG_SPACES && prev_type != -1
129               && type == SEG_SPACES && n == 1 && input[offset] == ' ')
130             {
131               printf ("    space\n");
132               offset++;
133               prev_type = -1;
134               continue;
135             }
136         }
137       if (prev_type != -1)
138         putchar ('\n');
139       prev_type = type;
140
141       if (verbose)
142         printf ("%2zu:%2zu: ", line_number, offset - line_offset);
143
144       type_name = segment_type_to_string (type);
145       for (p = type_name; *p != '\0'; p++)
146         putchar (tolower ((unsigned char) *p));
147       if (n > 0)
148         {
149           int i;
150
151           for (i = MIN (15, strlen (type_name)); i < 16; i++)
152             putchar (' ');
153           for (i = 0; i < n; )
154             {
155               const uint8_t *u_input = CHAR_CAST (const uint8_t *, input);
156               ucs4_t uc;
157               int mblen;
158
159               mblen = u8_mbtoucr (&uc, u_input + (offset + i), n - i);
160               if (mblen < 0)
161                 {
162                   int j;
163
164                   mblen = u8_mbtouc (&uc, u_input + (offset + i), n - i);
165                   putchar ('<');
166                   for (j = 0; j < mblen; j++)
167                     {
168                       if (j > 0)
169                         putchar (' ');
170                       printf ("%02x", input[offset + i + j]);
171                     }
172                   putchar ('>');
173                 }
174               else
175                 {
176                   switch (uc)
177                     {
178                     case ' ':
179                       printf ("_");
180                       break;
181
182                     case '_':
183                       printf ("\\_");
184                       break;
185
186                     case '\\':
187                       printf ("\\\\");
188                       break;
189
190                     case '\t':
191                       printf ("\\t");
192                       break;
193
194                     case '\r':
195                       printf ("\\r");
196                       break;
197
198                     case '\n':
199                       printf ("\\n");
200                       break;
201
202                     case '\v':
203                       printf ("\\v");
204                       break;
205
206                     default:
207                       if (uc < 0x20 || uc == 0x00a0)
208                         printf ("<U+%04X>", uc);
209                       else
210                         fwrite (input + offset + i, 1, mblen, stdout);
211                       break;
212                     }
213                 }
214
215               i += mblen;
216             }
217         }
218
219       offset += n;
220       if (type == SEG_NEWLINE)
221         {
222           enum prompt_style prompt;
223
224           line_number++;
225           line_offset = offset;
226
227           prompt = segmenter_get_prompt (&s);
228           printf (" (%s)\n", prompt_style_to_string (prompt));
229         }
230     }
231   putchar ('\n');
232
233   free (input);
234
235   return 0;
236 }
237
238 static const char *
239 parse_options (int argc, char **argv)
240 {
241   for (;;)
242     {
243       static const struct option options[] =
244         {
245           {"one-byte", no_argument, NULL, '1'},
246           {"auto", no_argument, NULL, 'a'},
247           {"batch", no_argument, NULL, 'b'},
248           {"interactive", no_argument, NULL, 'i'},
249           {"verbose", no_argument, NULL, 'v'},
250           {"help", no_argument, NULL, 'h'},
251           {NULL, 0, NULL, 0},
252         };
253
254       int c = getopt_long (argc, argv, "1abivh", options, NULL);
255       if (c == -1)
256         break;
257
258       switch (c)
259         {
260         case '1':
261           one_byte = true;
262           break;
263
264         case 'a':
265           mode = SEG_MODE_AUTO;
266           break;
267
268         case 'b':
269           mode = SEG_MODE_BATCH;
270           break;
271
272         case 'i':
273           mode = SEG_MODE_INTERACTIVE;
274           break;
275
276         case 'v':
277           verbose = true;
278           break;
279
280         case 'h':
281           usage ();
282
283         case 0:
284           break;
285
286         case '?':
287           exit (EXIT_FAILURE);
288           break;
289
290         default:
291           NOT_REACHED ();
292         }
293
294     }
295
296   if (optind + 1 != argc)
297     error (1, 0, "exactly one non-option argument required; "
298            "use --help for help");
299   return argv[optind];
300 }
301
302 static void
303 usage (void)
304 {
305   printf ("\
306 %s, to test breaking PSPP syntax into lexical segments\n\
307 usage: %s [OPTIONS] INPUT\n\
308 \n\
309 Options:\n\
310   -1, --one-byte      feed one byte at a time\n\
311   -a, --auto          use \"auto\" syntax mode\n\
312   -b, --batch         use \"batch\" syntax mode\n\
313   -i, --interactive   use \"interactive\" syntax mode (default)\n\
314   -v, --verbose       include rows and column numbers in output\n\
315   -h, --help          print this help message\n",
316           program_name, program_name);
317   exit (EXIT_SUCCESS);
318 }