tests: grep -E is not portable
[bison.git] / src / scan-skel.l
blob60232a2ebdf0a1204b041e427303127368cf1bd5
1 /* Scan Bison Skeletons.                                       -*- C -*-
3    Copyright (C) 2001-2015, 2018-2021 Free Software Foundation, Inc.
5    This file is part of Bison, the GNU Compiler Compiler.
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
20 %option nodefault noyywrap noinput nounput never-interactive debug
21 %option prefix="skel_" outfile="lex.yy.c"
24 #include <dirname.h>
25 #include <error.h>
26 #include <path-join.h>
27 #include <quotearg.h>
29 #include "src/complain.h"
30 #include "src/files.h"
31 #include "src/getargs.h"
32 #include "src/scan-skel.h"
34 #define FLEX_PREFIX(Id) skel_ ## Id
35 #include "src/flex-scanner.h"
37 /* Work around a bug in flex 2.5.31.  See Debian bug 333231
38    <https://bugs.debian.org/333231>.  */
39 #undef skel_wrap
40 #define skel_wrap() 1
42 #define YY_DECL static int skel_lex (void)
43 YY_DECL;
45 typedef void (*at_directive)(int, char**, char **, int*);
46 static void at_init (int *argc, char *argv[], at_directive *at_ptr, at_directive fun);
47 static void at_basename (int argc, char *argv[], char**, int*);
48 static void at_complain (int argc, char *argv[], char**, int*);
49 static void at_output (int argc, char *argv[], char **name, int *lineno);
50 static void fail_for_at_directive_too_many_args (char const *at_directive_name);
51 static void fail_for_at_directive_too_few_args (char const *at_directive_name);
52 static void fail_for_invalid_at (char const *at);
53 static void output_mapped_file (char const *name);
56 /* Identifiers of our M4 macros.  */
57 macro    [bm]4_[a-zA-Z_0-9]*
59 %x SC_AT_DIRECTIVE_ARGS
60 %x SC_AT_DIRECTIVE_SKIP_WS
65   int out_lineno PACIFY_CC (= 0);
66   char *out_name = NULL;
68   /* Currently, only the @complain directive takes multiple arguments, and
69      never more than 7, with argv[0] being the directive name and argv[1]
70      being the type of complaint to dispatch. */
71 #define ARGC_MAX 9
72   int argc = 0;
73   char *argv[ARGC_MAX];
74   at_directive at_ptr = NULL;
77 "@@" fputc ('@', yyout);
78 "@{" fputc ('[', yyout);
79 "@}" fputc (']', yyout);
80 "@'" continue;  /* Used by b4_cat in ../data/bison.m4.  */
81 @\n  continue;
83 "@oline@"  fprintf (yyout, "%d", out_lineno + 1);
84 "@ofile@"  output_mapped_file (out_name);
86 "@basename("    at_init (&argc, argv, &at_ptr, &at_basename);
87 "@complain("    at_init (&argc, argv, &at_ptr, &at_complain);
88 "@output("      at_init (&argc, argv, &at_ptr, &at_output);
90   /* This pattern must not match more than the previous @ patterns. */
91 @[^@{}''(\n]*   fail_for_invalid_at (yytext);
92 \n              out_lineno++; ECHO;
93 [^bm@\n]+       ECHO;
95   /* If there are still identifiers that look like macros, such as
96      b4_synbol, this probably an error, say a typo in M4, or
97      overquotation.  */
98 {macro}         {
99                   location loc = empty_loc;
100                   loc.start.file = map_file_name (out_name);
101                   loc.start.line = out_lineno;
102                   loc.end = loc.start;
103                   complain (&loc, Wother,
104                             "suspicious sequence in the output: %s", yytext);
105                   ECHO;
106                 }
107 .               ECHO;
109 <INITIAL><<EOF>> {
110   if (out_name)
111     {
112       free (out_name);
113       xfclose (yyout);
114     }
115   return EOF;
118 <SC_AT_DIRECTIVE_ARGS>
120   [^@]+  STRING_GROW ();
122   "@@"   STRING_1GROW ('@');
123   "@{"   STRING_1GROW ('[');
124   "@}"   STRING_1GROW (']');
125   "@'"   continue; /* For starting an argument that begins with whitespace. */
126   @\n    continue;
128   @[,)] {
129     if (argc >= ARGC_MAX)
130       fail_for_at_directive_too_many_args (argv[0]);
132     argv[argc++] = obstack_finish0 (&obstack_for_string);
134     /* Like M4, skip whitespace after a comma.  */
135     if (yytext[1] == ',')
136       BEGIN SC_AT_DIRECTIVE_SKIP_WS;
137     else
138       {
139         aver (at_ptr);
140         at_ptr (argc, argv, &out_name, &out_lineno);
141         obstack_free (&obstack_for_string, argv[0]);
142         argc = 0;
143         BEGIN INITIAL;
144       }
145   }
147   @.?  fail_for_invalid_at (yytext);
150 <SC_AT_DIRECTIVE_SKIP_WS>
152   [ \t\r\n]    continue;
153   .            yyless (0); BEGIN SC_AT_DIRECTIVE_ARGS;
156 <SC_AT_DIRECTIVE_ARGS,SC_AT_DIRECTIVE_SKIP_WS>
158   <<EOF>>  complain (NULL, fatal, _("unclosed %s directive in skeleton"), argv[0]);
163 static void
164 at_init (int *argc, char *argv[], at_directive *at_ptr, at_directive fun)
166   *at_ptr = fun;
167   yytext[yyleng-1] = '\0';
168   obstack_grow (&obstack_for_string, yytext, yyleng);
169   argv[(*argc)++] = obstack_finish (&obstack_for_string);
170   BEGIN SC_AT_DIRECTIVE_ARGS;
173 /*------------------------.
174 | Scan a Bison skeleton.  |
175 `------------------------*/
177 void
178 scan_skel (FILE *in)
180   static bool initialized = false;
181   if (!initialized)
182     {
183       initialized = true;
184       obstack_init (&obstack_for_string);
185     }
186   skel_in = in;
187   skel__flex_debug = trace_flag & trace_skeleton;
188   skel_lex ();
191 void
192 skel_scanner_free (void)
194   obstack_free (&obstack_for_string, 0);
195   /* Reclaim Flex's buffers.  */
196   yylex_destroy ();
199 static inline warnings
200 flag (const char *arg)
202   /* compare with values issued from b4_error */
203   if (STREQ (arg, "complain"))
204     return complaint;
205   else if (STREQ (arg, "deprecated"))
206     return Wdeprecated;
207   else if (STREQ (arg, "fatal"))
208     return fatal;
209   else if (STREQ (arg, "note"))
210     return silent | complaint | no_caret | note;
211   else if (STREQ (arg, "warn"))
212     return Wother;
213   else
214     abort ();
217 static void
218 at_basename (int argc, char *argv[], char **out_namep, int *out_linenop)
220   (void) out_namep;
221   (void) out_linenop;
222   if (2 < argc)
223     fail_for_at_directive_too_many_args (argv[0]);
224   fputs (last_component (argv[1]), yyout);
227 static void
228 at_complain (int argc, char *argv[], char **out_namep, int *out_linenop)
230   if (argc < 4)
231     fail_for_at_directive_too_few_args (argv[0]);
233   (void) out_namep;
234   (void) out_linenop;
236   warnings w = flag (argv[1]);
238   location loc;
239   location *locp = NULL;
240   if (argv[2] && argv[2][0])
241     {
242       boundary_set_from_string (&loc.start, argv[2]);
243       boundary_set_from_string (&loc.end, argv[3]);
244       locp = &loc;
245     }
246   complain_args (locp, w, argc - 4, argv + 4);
249 static void
250 at_output (int argc, char *argv[], char **out_namep, int *out_linenop)
252   if (3 < argc)
253     fail_for_at_directive_too_many_args (argv[0]);
254   if (*out_namep)
255     {
256       free (*out_namep);
257       xfclose (yyout);
258     }
259   *out_namep = xpath_join (argv[1], 2 < argc ? argv[2] : NULL);
260   output_file_name_check (out_namep, true);
261   /* If there were errors, do not generate the output.  */
262   yyout = xfopen (complaint_status ? "/dev/null" : *out_namep, "w");
263   *out_linenop = 1;
266 static void
267 fail_for_at_directive_too_few_args (char const *at_directive_name)
269   complain (NULL, fatal, _("too few arguments for %s directive in skeleton"),
270             at_directive_name);
273 static void
274 fail_for_at_directive_too_many_args (char const *at_directive_name)
276   complain (NULL, fatal, _("too many arguments for %s directive in skeleton"),
277             at_directive_name);
280 static void
281 fail_for_invalid_at (char const *at)
283   complain (NULL, fatal, "invalid @ in skeleton: %s", at);
286 static void
287 output_mapped_file (char const *name)
289   fputs (quotearg_style (c_quoting_style, map_file_name (name)), yyout);