lexer: Change the pipeline to allow more flexible use of macros.
[pspp] / tests / language / lexer / scan-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
27 #include "libpspp/assertion.h"
28 #include "libpspp/compiler.h"
29 #include "libpspp/misc.h"
30 #include "language/lexer/scan.h"
31 #include "language/lexer/token.h"
32
33 #include "gl/error.h"
34 #include "gl/ftoastr.h"
35 #include "gl/progname.h"
36 #include "gl/read-file.h"
37 #include "gl/xalloc.h"
38
39 /* -a/--auto, -b/--batch, -i/--interactive: syntax mode. */
40 static enum segmenter_mode mode = SEG_MODE_AUTO;
41
42 /* -s, --strip-trailing-newline: Strip trailing newline from last line of
43     input. */
44 static bool strip_trailing_newline;
45
46 static const char *parse_options (int argc, char **argv);
47 static void usage (void) NO_RETURN;
48
49 int
50 main (int argc, char *argv[])
51 {
52   const char *file_name;
53   size_t length;
54   char *input;
55
56   struct string_lexer slex;
57
58   set_program_name (argv[0]);
59   file_name = parse_options (argc, argv);
60
61   /* Read from stdin into 'input'. */
62   input = (!strcmp (file_name, "-")
63            ? fread_file (stdin, 0, &length)
64            : read_file (file_name, 0, &length));
65   if (input == NULL)
66     error (EXIT_FAILURE, errno, "reading %s failed", file_name);
67
68   if (strip_trailing_newline && length && input[length - 1] == '\n')
69     {
70       length--;
71       if (length && input[length - 1] == '\r')
72         length--;
73     }
74
75   struct token *tokens = NULL;
76   size_t n_tokens = 0;
77   size_t allocated_tokens = 0;
78   string_lexer_init (&slex, input, length, mode, false);
79   for (;;)
80     {
81       if (n_tokens >= allocated_tokens)
82         tokens = x2nrealloc (tokens, &allocated_tokens, sizeof *tokens);
83       enum string_lexer_result result
84         = string_lexer_next (&slex, &tokens[n_tokens]);
85
86       if (result == SLR_ERROR)
87         tokens[n_tokens].type = T_STOP;
88       n_tokens++;
89
90       if (result == SLR_END)
91         break;
92     }
93
94   for (size_t i = 0; i < n_tokens; )
95     {
96       struct merger m = MERGER_INIT;
97       int retval;
98       struct token out;
99       for (size_t j = i; ; j++)
100         {
101           assert (j < n_tokens);
102           retval = merger_add (&m, &tokens[j], &out);
103           if (retval != -1)
104             break;
105         }
106
107       const struct token *t = retval ? &out : &tokens[i];
108
109       fputs (token_type_to_name (t->type), stdout);
110       if (t->number != 0.0)
111         {
112           double x = t->number;
113
114           if (x > LONG_MIN && x <= LONG_MAX && floor (x) == x)
115             printf (" %ld", (long int) x);
116           else
117             printf (" %.3g", x);
118         }
119       if (t->string.string != NULL || t->string.length > 0)
120         printf (" \"%.*s\"", (int) t->string.length, t->string.string);
121       printf ("\n");
122
123       if (retval)
124         {
125           i += retval;
126           token_uninit (&out);
127         }
128       else
129         i++;
130     }
131
132   for (size_t i = 0; i < n_tokens; i++)
133     token_uninit (&tokens[i]);
134   free (tokens);
135   free (input);
136
137   return 0;
138 }
139
140 static const char *
141 parse_options (int argc, char **argv)
142 {
143   for (;;)
144     {
145       static const struct option options[] =
146         {
147           {"auto", no_argument, NULL, 'a'},
148           {"batch", no_argument, NULL, 'b'},
149           {"interactive", no_argument, NULL, 'i'},
150           {"strip-trailing-newline", no_argument, NULL, 's'},
151           {"help", no_argument, NULL, 'h'},
152           {NULL, 0, NULL, 0},
153         };
154
155       int c = getopt_long (argc, argv, "sabih", options, NULL);
156       if (c == -1)
157         break;
158
159       switch (c)
160         {
161         case 'a':
162           mode = SEG_MODE_AUTO;
163           break;
164
165         case 'b':
166           mode = SEG_MODE_BATCH;
167           break;
168
169         case 'i':
170           mode = SEG_MODE_INTERACTIVE;
171           break;
172
173         case 's':
174           strip_trailing_newline = true;
175           break;
176
177         case 'h':
178           usage ();
179
180         case 0:
181           break;
182
183         case '?':
184           exit (EXIT_FAILURE);
185           break;
186
187         default:
188           NOT_REACHED ();
189         }
190
191     }
192
193   if (optind + 1 != argc)
194     error (1, 0, "exactly one non-option argument required; "
195            "use --help for help");
196   return argv[optind];
197 }
198
199 static void
200 usage (void)
201 {
202   printf ("\
203 %s, to test breaking PSPP syntax into tokens\n\
204 usage: %s [OPTIONS] INPUT\n\
205 \n\
206 Options:\n\
207   -a, --auto          use \"auto\" syntax mode\n\
208   -b, --batch         use \"batch\" syntax mode\n\
209   -i, --interactive   use \"interactive\" syntax mode (default)\n\
210   -s, --strip-trailing-newline  remove newline from end of input\n\
211   -v, --verbose       include rows and column numbers in output\n\
212   -h, --help          print this help message\n",
213           program_name, program_name);
214   exit (EXIT_SUCCESS);
215 }