gnulib: update
[bison.git] / src / scan-skel.l
blobd647c079f4017f441e5ed20e783bbeb382592bcd
1 /* Scan Bison Skeletons.                                       -*- C -*-
3    Copyright (C) 2001-2015, 2018-2022 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]*
58 /* Safe sequence of word-constituent characters.  */
59 identifier [A-Za-z_0-9]+
61 %x SC_AT_DIRECTIVE_ARGS
62 %x SC_AT_DIRECTIVE_SKIP_WS
67   int out_lineno PACIFY_CC (= 0);
68   char *out_name = NULL;
70   /* Currently, only the @complain directive takes multiple arguments, and
71      never more than 7, with argv[0] being the directive name and argv[1]
72      being the type of complaint to dispatch. */
73 #define ARGC_MAX 9
74   int argc = 0;
75   char *argv[ARGC_MAX];
76   at_directive at_ptr = NULL;
79 "@@" fputc ('@', yyout);
80 "@{" fputc ('[', yyout);
81 "@}" fputc (']', yyout);
82 "@'" continue;  /* Used by b4_cat in ../data/bison.m4.  */
83 @\n  continue;
85 "@oline@"  fprintf (yyout, "%d", out_lineno + 1);
86 "@ofile@"  output_mapped_file (out_name);
88 "@basename("    at_init (&argc, argv, &at_ptr, &at_basename);
89 "@complain("    at_init (&argc, argv, &at_ptr, &at_complain);
90 "@output("      at_init (&argc, argv, &at_ptr, &at_output);
92   /* This pattern must not match more than the previous @ patterns. */
93 @[^@{}''(\n]*   fail_for_invalid_at (yytext);
94 \n              out_lineno++; ECHO;
95 [^a-z@\n]+      ECHO;
97   /* If there are still identifiers that look like macros, such as
98      b4_synbol, this probably an error, say a typo in M4, or
99      overquotation.  */
100 {macro}         {
101                   location loc = empty_loc;
102                   loc.start.file = map_file_name (out_name);
103                   loc.start.line = out_lineno;
104                   loc.end = loc.start;
105                   complain (&loc, Wother,
106                             "suspicious sequence in the output: %s", yytext);
107                   ECHO;
108                 }
109 {identifier}    ECHO;
110 .               ECHO;
112 <INITIAL><<EOF>> {
113   if (out_name)
114     {
115       free (out_name);
116       xfclose (yyout);
117     }
118   return EOF;
121 <SC_AT_DIRECTIVE_ARGS>
123   [^@]+  STRING_GROW ();
125   "@@"   STRING_1GROW ('@');
126   "@{"   STRING_1GROW ('[');
127   "@}"   STRING_1GROW (']');
128   "@'"   continue; /* For starting an argument that begins with whitespace. */
129   @\n    continue;
131   @[,)] {
132     if (argc >= ARGC_MAX)
133       fail_for_at_directive_too_many_args (argv[0]);
135     argv[argc++] = obstack_finish0 (&obstack_for_string);
137     /* Like M4, skip whitespace after a comma.  */
138     if (yytext[1] == ',')
139       BEGIN SC_AT_DIRECTIVE_SKIP_WS;
140     else
141       {
142         aver (at_ptr);
143         at_ptr (argc, argv, &out_name, &out_lineno);
144         obstack_free (&obstack_for_string, argv[0]);
145         argc = 0;
146         BEGIN INITIAL;
147       }
148   }
150   @.?  fail_for_invalid_at (yytext);
153 <SC_AT_DIRECTIVE_SKIP_WS>
155   [ \t\r\n]    continue;
156   .            yyless (0); BEGIN SC_AT_DIRECTIVE_ARGS;
159 <SC_AT_DIRECTIVE_ARGS,SC_AT_DIRECTIVE_SKIP_WS>
161   <<EOF>>  complain (NULL, fatal, _("unclosed %s directive in skeleton"), argv[0]);
166 static void
167 at_init (int *argc, char *argv[], at_directive *at_ptr, at_directive fun)
169   *at_ptr = fun;
170   yytext[yyleng-1] = '\0';
171   obstack_grow (&obstack_for_string, yytext, yyleng);
172   argv[(*argc)++] = obstack_finish (&obstack_for_string);
173   BEGIN SC_AT_DIRECTIVE_ARGS;
176 /*------------------------.
177 | Scan a Bison skeleton.  |
178 `------------------------*/
180 void
181 scan_skel (FILE *in)
183   static bool initialized = false;
184   if (!initialized)
185     {
186       initialized = true;
187       obstack_init (&obstack_for_string);
188     }
189   skel_in = in;
190   skel__flex_debug = trace_flag & trace_skeleton;
191   skel_lex ();
194 void
195 skel_scanner_free (void)
197   obstack_free (&obstack_for_string, 0);
198   /* Reclaim Flex's buffers.  */
199   yylex_destroy ();
202 static inline warnings
203 flag (const char *arg)
205   /* compare with values issued from b4_error */
206   if (STREQ (arg, "complain"))
207     return complaint;
208   else if (STREQ (arg, "deprecated"))
209     return Wdeprecated;
210   else if (STREQ (arg, "fatal"))
211     return fatal;
212   else if (STREQ (arg, "note"))
213     return silent | complaint | no_caret | note;
214   else if (STREQ (arg, "warn"))
215     return Wother;
216   else
217     abort ();
220 static void
221 at_basename (int argc, char *argv[], char **out_namep, int *out_linenop)
223   (void) out_namep;
224   (void) out_linenop;
225   if (2 < argc)
226     fail_for_at_directive_too_many_args (argv[0]);
227   fputs (last_component (argv[1]), yyout);
230 static void
231 at_complain (int argc, char *argv[], char **out_namep, int *out_linenop)
233   if (argc < 4)
234     fail_for_at_directive_too_few_args (argv[0]);
236   (void) out_namep;
237   (void) out_linenop;
239   warnings w = flag (argv[1]);
241   location loc;
242   location *locp = NULL;
243   if (argv[2] && argv[2][0])
244     {
245       boundary_set_from_string (&loc.start, argv[2]);
246       boundary_set_from_string (&loc.end, argv[3]);
247       locp = &loc;
248     }
249   complain_args (locp, w, argc - 4, argv + 4);
252 static void
253 at_output (int argc, char *argv[], char **out_namep, int *out_linenop)
255   if (3 < argc)
256     fail_for_at_directive_too_many_args (argv[0]);
257   if (*out_namep)
258     {
259       free (*out_namep);
260       xfclose (yyout);
261     }
262   *out_namep = xpath_join (argv[1], 2 < argc ? argv[2] : NULL);
263   output_file_name_check (out_namep, true);
264   /* If there were errors, do not generate the output.  */
265   yyout = xfopen (complaint_status ? "/dev/null" : *out_namep, "w");
266   *out_linenop = 1;
269 static void
270 fail_for_at_directive_too_few_args (char const *at_directive_name)
272   complain (NULL, fatal, _("too few arguments for %s directive in skeleton"),
273             at_directive_name);
276 static void
277 fail_for_at_directive_too_many_args (char const *at_directive_name)
279   complain (NULL, fatal, _("too many arguments for %s directive in skeleton"),
280             at_directive_name);
283 static void
284 fail_for_invalid_at (char const *at)
286   complain (NULL, fatal, "invalid @ in skeleton: %s", at);
289 static void
290 output_mapped_file (char const *name)
292   fputs (quotearg_style (c_quoting_style, map_file_name (name)), yyout);