scan-test: Print nonintegers with limited precision to avoid test failures.
[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 static const char *parse_options (int argc, char **argv);
43 static void usage (void) NO_RETURN;
44
45 int
46 main (int argc, char *argv[])
47 {
48   const char *file_name;
49   size_t length;
50   char *input;
51
52   struct string_lexer slex;
53   bool more;
54
55   set_program_name (argv[0]);
56   file_name = parse_options (argc, argv);
57
58   /* Read from stdin into 'input'.  Ensure that 'input' ends in a new-line
59      followed by a null byte. */
60   input = (!strcmp (file_name, "-")
61            ? fread_file (stdin, &length)
62            : read_file (file_name, &length));
63   if (input == NULL)
64     error (EXIT_FAILURE, errno, "reading %s failed", file_name);
65   input = xrealloc (input, length + 3);
66   if (length == 0 || input[length - 1] != '\n')
67     input[length++] = '\n';
68   input[length++] = '\0';
69
70   string_lexer_init (&slex, input, mode);
71   do
72     {
73       struct token token;
74
75       more = string_lexer_next (&slex, &token);
76
77       printf ("%s", scan_type_to_string (token.type));
78       if (token.number != 0.0)
79         {
80           double x = token.number;
81
82           if (x > LONG_MIN && x <= LONG_MAX && floor (x) == x)
83             printf (" %ld", (long int) x);
84           else
85             printf (" %.3g", x);
86         }
87       if (token.string.string != NULL || token.string.length > 0)
88         printf (" \"%.*s\"", (int) token.string.length, token.string.string);
89       printf ("\n");
90
91       token_destroy (&token);
92     }
93   while (more);
94
95   free (input);
96
97   return 0;
98 }
99
100 static const char *
101 parse_options (int argc, char **argv)
102 {
103   for (;;)
104     {
105       static const struct option options[] =
106         {
107           {"auto", no_argument, NULL, 'a'},
108           {"batch", no_argument, NULL, 'b'},
109           {"interactive", no_argument, NULL, 'i'},
110           {"help", no_argument, NULL, 'h'},
111           {NULL, 0, NULL, 0},
112         };
113
114       int c = getopt_long (argc, argv, "abih", options, NULL);
115       if (c == -1)
116         break;
117
118       switch (c)
119         {
120         case 'a':
121           mode = SEG_MODE_AUTO;
122           break;
123
124         case 'b':
125           mode = SEG_MODE_BATCH;
126           break;
127
128         case 'i':
129           mode = SEG_MODE_INTERACTIVE;
130           break;
131
132         case 'h':
133           usage ();
134
135         case 0:
136           break;
137
138         case '?':
139           exit (EXIT_FAILURE);
140           break;
141
142         default:
143           NOT_REACHED ();
144         }
145
146     }
147
148   if (optind + 1 != argc)
149     error (1, 0, "exactly one non-option argument required; "
150            "use --help for help");
151   return argv[optind];
152 }
153
154 static void
155 usage (void)
156 {
157   printf ("\
158 %s, to test breaking PSPP syntax into tokens\n\
159 usage: %s [OPTIONS] INPUT\n\
160 \n\
161 Options:\n\
162   -1, --one-segment   feed one segment at a time\n\
163   -a, --auto          use \"auto\" syntax mode\n\
164   -b, --batch         use \"batch\" syntax mode\n\
165   -i, --interactive   use \"interactive\" syntax mode (default)\n\
166   -v, --verbose       include rows and column numbers in output\n\
167   -h, --help          print this help message\n",
168           program_name, program_name);
169   exit (EXIT_SUCCESS);
170 }