]> git.saurik.com Git - bison.git/blob - src/scan-skel.l
c0df0ed00da456362f745a83bc4e07521b941194
[bison.git] / src / scan-skel.l
1 /* Scan Bison Skeletons. -*- C -*-
2
3 Copyright (C) 2001-2012 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
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.
11
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.
16
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/>. */
19
20 %option nodefault noyywrap noinput nounput never-interactive debug
21 %option prefix="skel_" outfile="lex.yy.c"
22
23 %{
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>. */
26 #undef skel_wrap
27 #define skel_wrap() 1
28
29 #define FLEX_PREFIX(Id) skel_ ## Id
30 #include <src/flex-scanner.h>
31
32 #include <dirname.h>
33 #include <error.h>
34 #include <quotearg.h>
35
36 #include <src/complain.h>
37 #include <src/getargs.h>
38 #include <src/files.h>
39 #include <src/scan-skel.h>
40
41 #define YY_DECL static int skel_lex (void)
42 YY_DECL;
43
44 static void at_basename (int argc, char *argv[]);
45 static void at_complain (int argc, char *argv[]);
46 static void at_output (int argc, char *argv[], char **name, int *lineno);
47 static void fail_for_at_directive_too_many_args (char const *at_directive_name);
48 static void fail_for_at_directive_too_few_args (char const *at_directive_name);
49 static void fail_for_invalid_at (char const *at);
50 %}
51
52 %x SC_AT_DIRECTIVE_ARGS
53 %x SC_AT_DIRECTIVE_SKIP_WS
54
55 %%
56
57 %{
58 int out_lineno PACIFY_CC (= 0);
59 char *outname = NULL;
60
61 /* Currently, only the @complain directive takes multiple arguments, and
62 never more than 7, with argv[0] being the directive name and argv[1]
63 being the type of complaint to dispatch. */
64 #define ARGC_MAX 9
65 int argc = 0;
66 char *argv[ARGC_MAX];
67 %}
68
69 "@@" fputc ('@', yyout);
70 "@{" fputc ('[', yyout);
71 "@}" fputc (']', yyout);
72 "@`" continue; /* Used by b4_cat in ../data/bison.m4. */
73 @\n continue;
74
75 "@oline@" fprintf (yyout, "%d", out_lineno + 1);
76 "@ofile@" fputs (quotearg_style (c_quoting_style, outname), yyout);
77
78 @[a-z_]+"(" {
79 yytext[yyleng-1] = '\0';
80 obstack_grow (&obstack_for_string, yytext, yyleng);
81 argv[argc++] = obstack_finish (&obstack_for_string);
82 BEGIN SC_AT_DIRECTIVE_ARGS;
83 }
84
85 /* This pattern must not match more than the previous @ patterns. */
86 @[^@{}`(\n]* fail_for_invalid_at (yytext);
87 \n out_lineno++; ECHO;
88 [^@\n]+ ECHO;
89
90 <INITIAL><<EOF>> {
91 if (outname)
92 {
93 free (outname);
94 xfclose (yyout);
95 }
96 return EOF;
97 }
98
99 <SC_AT_DIRECTIVE_ARGS>
100 {
101 [^@]+ STRING_GROW;
102
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. */
107 @\n continue;
108
109 @[,)] {
110 if (argc >= ARGC_MAX)
111 fail_for_at_directive_too_many_args (argv[0]);
112
113 argv[argc++] = obstack_finish0 (&obstack_for_string);
114
115 /* Like M4, skip whitespace after a comma. */
116 if (yytext[1] == ',')
117 BEGIN SC_AT_DIRECTIVE_SKIP_WS;
118 else
119 {
120 if (STREQ (argv[0], "@basename"))
121 at_basename (argc, argv);
122 else if (STREQ (argv[0], "@complain"))
123 at_complain (argc, argv);
124 else if (STREQ (argv[0], "@output"))
125 at_output (argc, argv, &outname, &out_lineno);
126 else
127 fail_for_invalid_at (argv[0]);
128
129 obstack_free (&obstack_for_string, argv[0]);
130 argc = 0;
131 BEGIN INITIAL;
132 }
133 }
134
135 @.? fail_for_invalid_at (yytext);
136 }
137
138 <SC_AT_DIRECTIVE_SKIP_WS>
139 {
140 [ \t\r\n] continue;
141 . yyless (0); BEGIN SC_AT_DIRECTIVE_ARGS;
142 }
143
144 <SC_AT_DIRECTIVE_ARGS,SC_AT_DIRECTIVE_SKIP_WS>
145 {
146 <<EOF>> complain (NULL, fatal, _("unclosed %s directive in skeleton"), argv[0]);
147 }
148
149 %%
150
151 /*------------------------.
152 | Scan a Bison skeleton. |
153 `------------------------*/
154
155 void
156 scan_skel (FILE *in)
157 {
158 static bool initialized = false;
159 if (!initialized)
160 {
161 initialized = true;
162 obstack_init (&obstack_for_string);
163 }
164 skel_in = in;
165 skel__flex_debug = trace_flag & trace_skeleton;
166 skel_lex ();
167 }
168
169 void
170 skel_scanner_free (void)
171 {
172 obstack_free (&obstack_for_string, 0);
173 /* Reclaim Flex's buffers. */
174 yylex_destroy ();
175 }
176
177 static inline warnings
178 flag (const char *arg)
179 {
180 /* compare with values issued from b4_error */
181 if (STREQ (arg, "complain"))
182 return complaint;
183 else if (STREQ (arg, "fatal"))
184 return fatal;
185 else if (STREQ (arg, "note"))
186 return silent;
187 else if (STREQ (arg, "warn"))
188 return Wother;
189 else
190 aver (false);
191 }
192
193 static void
194 at_basename (int argc, char *argv[])
195 {
196 if (2 < argc)
197 fail_for_at_directive_too_many_args (argv[0]);
198 fputs (last_component (argv[1]), yyout);
199 }
200
201 static void
202 at_complain (int argc, char *argv[])
203 {
204 static unsigned indent;
205 warnings w = flag (argv[1]);
206 location loc;
207 location *locp = NULL;
208
209 if (argc < 4)
210 fail_for_at_directive_too_few_args (argv[1]);
211 if (argv[2] && argv[2][0])
212 {
213 boundary_set_from_string (&loc.start, argv[2]);
214 boundary_set_from_string (&loc.end, argv[3]);
215 locp = &loc;
216 }
217 if (w & silent)
218 indent += SUB_INDENT;
219 else
220 indent = 0;
221 complain_args (locp, w, &indent, argc - 3, argv + 3);
222 if (w & silent)
223 indent -= SUB_INDENT;
224 }
225
226 static void
227 at_output (int argc, char *argv[], char **outnamep, int *out_linenop)
228 {
229 if (2 < argc)
230 fail_for_at_directive_too_many_args (argv[0]);
231 if (*outnamep)
232 {
233 free (*outnamep);
234 xfclose (yyout);
235 }
236 *outnamep = xstrdup (argv[1]);
237 output_file_name_check (outnamep);
238 yyout = xfopen (*outnamep, "w");
239 *out_linenop = 1;
240 }
241
242 static void
243 fail_for_at_directive_too_few_args (char const *at_directive_name)
244 {
245 complain (NULL, fatal, _("too few arguments for %s directive in skeleton"),
246 at_directive_name);
247 }
248
249 static void
250 fail_for_at_directive_too_many_args (char const *at_directive_name)
251 {
252 complain (NULL, fatal, _("too many arguments for %s directive in skeleton"),
253 at_directive_name);
254 }
255
256 static void
257 fail_for_invalid_at (char const *at)
258 {
259 complain (NULL, fatal, "invalid @ in skeleton: %s", at);
260 }