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"
26 #include <path-join.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>. */
42 #define YY_DECL static int skel_lex (void)
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. */
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. */
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;
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
99 location loc = empty_loc;
100 loc.start.file = map_file_name (out_name);
101 loc.start.line = out_lineno;
103 complain (&loc, Wother,
104 "suspicious sequence in the output: %s", yytext);
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. */
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;
140 at_ptr (argc, argv, &out_name, &out_lineno);
141 obstack_free (&obstack_for_string, argv[0]);
147 @.? fail_for_invalid_at (yytext);
150 <SC_AT_DIRECTIVE_SKIP_WS>
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]);
164 at_init (int *argc, char *argv[], at_directive *at_ptr, at_directive 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 `------------------------*/
180 static bool initialized = false;
184 obstack_init (&obstack_for_string);
187 skel__flex_debug = trace_flag & trace_skeleton;
192 skel_scanner_free (void)
194 obstack_free (&obstack_for_string, 0);
195 /* Reclaim Flex's buffers. */
199 static inline warnings
200 flag (const char *arg)
202 /* compare with values issued from b4_error */
203 if (STREQ (arg, "complain"))
205 else if (STREQ (arg, "deprecated"))
207 else if (STREQ (arg, "fatal"))
209 else if (STREQ (arg, "note"))
210 return silent | complaint | no_caret | note;
211 else if (STREQ (arg, "warn"))
218 at_basename (int argc, char *argv[], char **out_namep, int *out_linenop)
223 fail_for_at_directive_too_many_args (argv[0]);
224 fputs (last_component (argv[1]), yyout);
228 at_complain (int argc, char *argv[], char **out_namep, int *out_linenop)
231 fail_for_at_directive_too_few_args (argv[0]);
236 warnings w = flag (argv[1]);
239 location *locp = NULL;
240 if (argv[2] && argv[2][0])
242 boundary_set_from_string (&loc.start, argv[2]);
243 boundary_set_from_string (&loc.end, argv[3]);
246 complain_args (locp, w, argc - 4, argv + 4);
250 at_output (int argc, char *argv[], char **out_namep, int *out_linenop)
253 fail_for_at_directive_too_many_args (argv[0]);
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");
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"),
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"),
281 fail_for_invalid_at (char const *at)
283 complain (NULL, fatal, "invalid @ in skeleton: %s", at);
287 output_mapped_file (char const *name)
289 fputs (quotearg_style (c_quoting_style, map_file_name (name)), yyout);