Update URLs to prefer https: to http:
[bison.git] / src / scan-skel.l
blob1ec125af59edcaceba17639a5c1dc019695df65e
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 %x SC_AT_DIRECTIVE_ARGS
57 %x SC_AT_DIRECTIVE_SKIP_WS
62   int out_lineno PACIFY_CC (= 0);
63   char *out_name = NULL;
65   /* Currently, only the @complain directive takes multiple arguments, and
66      never more than 7, with argv[0] being the directive name and argv[1]
67      being the type of complaint to dispatch. */
68 #define ARGC_MAX 9
69   int argc = 0;
70   char *argv[ARGC_MAX];
71   at_directive at_ptr = NULL;
74 "@@" fputc ('@', yyout);
75 "@{" fputc ('[', yyout);
76 "@}" fputc (']', yyout);
77 "@'" continue;  /* Used by b4_cat in ../data/bison.m4.  */
78 @\n  continue;
80 "@oline@"  fprintf (yyout, "%d", out_lineno + 1);
81 "@ofile@"  output_mapped_file (out_name);
83 "@basename("    at_init (&argc, argv, &at_ptr, &at_basename);
84 "@complain("    at_init (&argc, argv, &at_ptr, &at_complain);
85 "@output("      at_init (&argc, argv, &at_ptr, &at_output);
87   /* This pattern must not match more than the previous @ patterns. */
88 @[^@{}''(\n]*   fail_for_invalid_at (yytext);
89 \n              out_lineno++; ECHO;
90 [^@\n]+         ECHO;
92 <INITIAL><<EOF>> {
93   if (out_name)
94     {
95       free (out_name);
96       xfclose (yyout);
97     }
98   return EOF;
101 <SC_AT_DIRECTIVE_ARGS>
103   [^@]+  STRING_GROW ();
105   "@@"   STRING_1GROW ('@');
106   "@{"   STRING_1GROW ('[');
107   "@}"   STRING_1GROW (']');
108   "@'"   continue; /* For starting an argument that begins with whitespace. */
109   @\n    continue;
111   @[,)] {
112     if (argc >= ARGC_MAX)
113       fail_for_at_directive_too_many_args (argv[0]);
115     argv[argc++] = obstack_finish0 (&obstack_for_string);
117     /* Like M4, skip whitespace after a comma.  */
118     if (yytext[1] == ',')
119       BEGIN SC_AT_DIRECTIVE_SKIP_WS;
120     else
121       {
122         aver (at_ptr);
123         at_ptr (argc, argv, &out_name, &out_lineno);
124         obstack_free (&obstack_for_string, argv[0]);
125         argc = 0;
126         BEGIN INITIAL;
127       }
128   }
130   @.?  fail_for_invalid_at (yytext);
133 <SC_AT_DIRECTIVE_SKIP_WS>
135   [ \t\r\n]    continue;
136   .            yyless (0); BEGIN SC_AT_DIRECTIVE_ARGS;
139 <SC_AT_DIRECTIVE_ARGS,SC_AT_DIRECTIVE_SKIP_WS>
141   <<EOF>>  complain (NULL, fatal, _("unclosed %s directive in skeleton"), argv[0]);
146 static void
147 at_init (int *argc, char *argv[], at_directive *at_ptr, at_directive fun)
149   *at_ptr = fun;
150   yytext[yyleng-1] = '\0';
151   obstack_grow (&obstack_for_string, yytext, yyleng);
152   argv[(*argc)++] = obstack_finish (&obstack_for_string);
153   BEGIN SC_AT_DIRECTIVE_ARGS;
156 /*------------------------.
157 | Scan a Bison skeleton.  |
158 `------------------------*/
160 void
161 scan_skel (FILE *in)
163   static bool initialized = false;
164   if (!initialized)
165     {
166       initialized = true;
167       obstack_init (&obstack_for_string);
168     }
169   skel_in = in;
170   skel__flex_debug = trace_flag & trace_skeleton;
171   skel_lex ();
174 void
175 skel_scanner_free (void)
177   obstack_free (&obstack_for_string, 0);
178   /* Reclaim Flex's buffers.  */
179   yylex_destroy ();
182 static inline warnings
183 flag (const char *arg)
185   /* compare with values issued from b4_error */
186   if (STREQ (arg, "complain"))
187     return complaint;
188   else if (STREQ (arg, "deprecated"))
189     return Wdeprecated;
190   else if (STREQ (arg, "fatal"))
191     return fatal;
192   else if (STREQ (arg, "note"))
193     return silent | complaint | no_caret | note;
194   else if (STREQ (arg, "warn"))
195     return Wother;
196   else
197     abort ();
200 static void
201 at_basename (int argc, char *argv[], char **out_namep, int *out_linenop)
203   (void) out_namep;
204   (void) out_linenop;
205   if (2 < argc)
206     fail_for_at_directive_too_many_args (argv[0]);
207   fputs (last_component (argv[1]), yyout);
210 static void
211 at_complain (int argc, char *argv[], char **out_namep, int *out_linenop)
213   if (argc < 4)
214     fail_for_at_directive_too_few_args (argv[0]);
216   (void) out_namep;
217   (void) out_linenop;
219   warnings w = flag (argv[1]);
221   location loc;
222   location *locp = NULL;
223   if (argv[2] && argv[2][0])
224     {
225       boundary_set_from_string (&loc.start, argv[2]);
226       boundary_set_from_string (&loc.end, argv[3]);
227       locp = &loc;
228     }
229   complain_args (locp, w, argc - 4, argv + 4);
232 static void
233 at_output (int argc, char *argv[], char **out_namep, int *out_linenop)
235   if (3 < argc)
236     fail_for_at_directive_too_many_args (argv[0]);
237   if (*out_namep)
238     {
239       free (*out_namep);
240       xfclose (yyout);
241     }
242   *out_namep = xpath_join (argv[1], 2 < argc ? argv[2] : NULL);
243   output_file_name_check (out_namep, true);
244   /* If there were errors, do not generate the output.  */
245   yyout = xfopen (complaint_status ? "/dev/null" : *out_namep, "w");
246   *out_linenop = 1;
249 static void
250 fail_for_at_directive_too_few_args (char const *at_directive_name)
252   complain (NULL, fatal, _("too few arguments for %s directive in skeleton"),
253             at_directive_name);
256 static void
257 fail_for_at_directive_too_many_args (char const *at_directive_name)
259   complain (NULL, fatal, _("too many arguments for %s directive in skeleton"),
260             at_directive_name);
263 static void
264 fail_for_invalid_at (char const *at)
266   complain (NULL, fatal, "invalid @ in skeleton: %s", at);
269 static void
270 output_mapped_file (char const *name)
272   char *f = map_file_name (name);
273   fputs (quotearg_style (c_quoting_style, f), yyout);
274   free (f);