scan: Get rid of scan token types in favor of new scan result state.
[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   string_lexer_init (&slex, input, length, mode, false);
76   enum string_lexer_result result;
77   do
78     {
79       struct token token;
80       result = string_lexer_next (&slex, &token);
81
82       printf ("%s", result == SLR_ERROR ? "error" : token_type_to_name (token.type));
83       if (token.number != 0.0)
84         {
85           double x = token.number;
86
87           if (x > LONG_MIN && x <= LONG_MAX && floor (x) == x)
88             printf (" %ld", (long int) x);
89           else
90             printf (" %.3g", x);
91         }
92       if (token.string.string != NULL || token.string.length > 0)
93         printf (" \"%.*s\"", (int) token.string.length, token.string.string);
94       printf ("\n");
95
96       token_uninit (&token);
97     }
98   while (result != SLR_END);
99
100   free (input);
101
102   return 0;
103 }
104
105 static const char *
106 parse_options (int argc, char **argv)
107 {
108   for (;;)
109     {
110       static const struct option options[] =
111         {
112           {"auto", no_argument, NULL, 'a'},
113           {"batch", no_argument, NULL, 'b'},
114           {"interactive", no_argument, NULL, 'i'},
115           {"strip-trailing-newline", no_argument, NULL, 's'},
116           {"help", no_argument, NULL, 'h'},
117           {NULL, 0, NULL, 0},
118         };
119
120       int c = getopt_long (argc, argv, "sabih", options, NULL);
121       if (c == -1)
122         break;
123
124       switch (c)
125         {
126         case 'a':
127           mode = SEG_MODE_AUTO;
128           break;
129
130         case 'b':
131           mode = SEG_MODE_BATCH;
132           break;
133
134         case 'i':
135           mode = SEG_MODE_INTERACTIVE;
136           break;
137
138         case 's':
139           strip_trailing_newline = true;
140           break;
141
142         case 'h':
143           usage ();
144
145         case 0:
146           break;
147
148         case '?':
149           exit (EXIT_FAILURE);
150           break;
151
152         default:
153           NOT_REACHED ();
154         }
155
156     }
157
158   if (optind + 1 != argc)
159     error (1, 0, "exactly one non-option argument required; "
160            "use --help for help");
161   return argv[optind];
162 }
163
164 static void
165 usage (void)
166 {
167   printf ("\
168 %s, to test breaking PSPP syntax into tokens\n\
169 usage: %s [OPTIONS] INPUT\n\
170 \n\
171 Options:\n\
172   -a, --auto          use \"auto\" syntax mode\n\
173   -b, --batch         use \"batch\" syntax mode\n\
174   -i, --interactive   use \"interactive\" syntax mode (default)\n\
175   -s, --strip-trailing-newline  remove newline from end of input\n\
176   -v, --verbose       include rows and column numbers in output\n\
177   -h, --help          print this help message\n",
178           program_name, program_name);
179   exit (EXIT_SUCCESS);
180 }