1 /* Scan Bison Skeletons. -*- C -*-
3 Copyright (C) 2001-2015 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 <http://www.gnu.org/licenses/>. */
20 %option nodefault noyywrap noinput nounput never-interactive debug
21 %option prefix="skel_" outfile="lex.yy.c"
24 /* Work around a bug in flex 2.5.31. See Debian bug 333231
25 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */
29 #define FLEX_PREFIX(Id) skel_ ## Id
30 #include <src/flex-scanner.h>
36 #include <src/complain.h>
37 #include <src/getargs.h>
38 #include <src/files.h>
39 #include <src/scan-skel.h>
41 #define YY_DECL static int skel_lex (void)
44 typedef void (*at_directive)(int, char**, char **, int*);
45 static void at_init (int *argc, char *argv[], at_directive *at_ptr, at_directive fun);
46 static void at_basename (int argc, char *argv[], char**, int*);
47 static void at_complain (int argc, char *argv[], char**, int*);
48 static void at_output (int argc, char *argv[], char **name, int *lineno);
49 static void fail_for_at_directive_too_many_args (char const *at_directive_name);
50 static void fail_for_at_directive_too_few_args (char const *at_directive_name);
51 static void fail_for_invalid_at (char const *at);
54 %x SC_AT_DIRECTIVE_ARGS
55 %x SC_AT_DIRECTIVE_SKIP_WS
60 int out_lineno PACIFY_CC (= 0);
61 char *out_name = NULL;
63 /* Currently, only the @complain directive takes multiple arguments, and
64 never more than 7, with argv[0] being the directive name and argv[1]
65 being the type of complaint to dispatch. */
69 at_directive at_ptr = NULL;
72 "@@" fputc ('@', yyout);
73 "@{" fputc ('[', yyout);
74 "@}" fputc (']', yyout);
75 "@'" continue; /* Used by b4_cat in ../data/bison.m4. */
78 "@oline@" fprintf (yyout, "%d", out_lineno + 1);
79 "@ofile@" fputs (quotearg_style (c_quoting_style, out_name), yyout);
81 "@basename(" at_init (&argc, argv, &at_ptr, &at_basename);
82 "@complain(" at_init (&argc, argv, &at_ptr, &at_complain);
83 "@output(" at_init (&argc, argv, &at_ptr, &at_output);
85 /* This pattern must not match more than the previous @ patterns. */
86 @[^@{}\'(\n]* fail_for_invalid_at (yytext);
87 \n out_lineno++; ECHO;
99 <SC_AT_DIRECTIVE_ARGS>
103 "@@" obstack_1grow (&obstack_for_string, '@');
104 "@{" obstack_1grow (&obstack_for_string, '[');
105 "@}" obstack_1grow (&obstack_for_string, ']');
106 "@'" continue; /* For starting an argument that begins with whitespace. */
110 if (argc >= ARGC_MAX)
111 fail_for_at_directive_too_many_args (argv[0]);
113 argv[argc++] = obstack_finish0 (&obstack_for_string);
115 /* Like M4, skip whitespace after a comma. */
116 if (yytext[1] == ',')
117 BEGIN SC_AT_DIRECTIVE_SKIP_WS;
121 at_ptr (argc, argv, &out_name, &out_lineno);
122 obstack_free (&obstack_for_string, argv[0]);
128 @.? fail_for_invalid_at (yytext);
131 <SC_AT_DIRECTIVE_SKIP_WS>
134 . yyless (0); BEGIN SC_AT_DIRECTIVE_ARGS;
137 <SC_AT_DIRECTIVE_ARGS,SC_AT_DIRECTIVE_SKIP_WS>
139 <<EOF>> complain (NULL, fatal, _("unclosed %s directive in skeleton"), argv[0]);
145 at_init (int *argc, char *argv[], at_directive *at_ptr, at_directive fun)
148 yytext[yyleng-1] = '\0';
149 obstack_grow (&obstack_for_string, yytext, yyleng);
150 argv[(*argc)++] = obstack_finish (&obstack_for_string);
151 BEGIN SC_AT_DIRECTIVE_ARGS;
154 /*------------------------.
155 | Scan a Bison skeleton. |
156 `------------------------*/
161 static bool initialized = false;
165 obstack_init (&obstack_for_string);
168 skel__flex_debug = trace_flag & trace_skeleton;
173 skel_scanner_free (void)
175 obstack_free (&obstack_for_string, 0);
176 /* Reclaim Flex's buffers. */
180 static inline warnings
181 flag (const char *arg)
183 /* compare with values issued from b4_error */
184 if (STREQ (arg, "complain"))
186 else if (STREQ (arg, "deprecated"))
188 else if (STREQ (arg, "fatal"))
190 else if (STREQ (arg, "note"))
191 return silent | complaint | no_caret;
192 else if (STREQ (arg, "warn"))
199 at_basename (int argc, char *argv[], char **out_namep, int *out_linenop)
204 fail_for_at_directive_too_many_args (argv[0]);
205 fputs (last_component (argv[1]), yyout);
209 at_complain (int argc, char *argv[], char **out_namep, int *out_linenop)
211 static unsigned indent;
212 warnings w = flag (argv[1]);
214 location *locp = NULL;
220 fail_for_at_directive_too_few_args (argv[0]);
221 if (argv[2] && argv[2][0])
223 boundary_set_from_string (&loc.start, argv[2]);
224 boundary_set_from_string (&loc.end, argv[3]);
228 indent += SUB_INDENT;
231 complain_args (locp, w, &indent, argc - 4, argv + 4);
233 indent -= SUB_INDENT;
237 at_output (int argc, char *argv[], char **out_namep, int *out_linenop)
240 fail_for_at_directive_too_many_args (argv[0]);
246 *out_namep = xstrdup (argv[1]);
247 output_file_name_check (out_namep, true);
248 /* If there were errors, do not generate the output. */
249 yyout = xfopen (complaint_status ? "/dev/null" : *out_namep, "w");
254 fail_for_at_directive_too_few_args (char const *at_directive_name)
256 complain (NULL, fatal, _("too few arguments for %s directive in skeleton"),
261 fail_for_at_directive_too_many_args (char const *at_directive_name)
263 complain (NULL, fatal, _("too many arguments for %s directive in skeleton"),
268 fail_for_invalid_at (char const *at)
270 complain (NULL, fatal, "invalid @ in skeleton: %s", at);