]> git.saurik.com Git - bison.git/blob - src/scan-skel.l
662a00b2f98ab2caf7257f4f7a1050959c4e3dbf
[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_directive_perform (int argc, char *argv[],
45 char **outnamep, int *out_linenop);
46 static void fail_for_at_directive_too_many_args (char const *at_directive_name);
47 static void fail_for_at_directive_too_few_args (char const *at_directive_name);
48 static void fail_for_invalid_at (char const *at);
49 %}
50
51 %x SC_AT_DIRECTIVE_ARGS
52 %x SC_AT_DIRECTIVE_SKIP_WS
53
54 %%
55
56 %{
57 int out_lineno PACIFY_CC (= 0);
58 char *outname = NULL;
59
60 /* Currently, only the @complain directive takes multiple arguments, and
61 never more than 7, with argv[0] being the directive name and argv[1]
62 being the type of complaint to dispatch. */
63 #define ARGC_MAX 9
64 int argc = 0;
65 char *argv[ARGC_MAX];
66 %}
67
68 "@@" fputc ('@', yyout);
69 "@{" fputc ('[', yyout);
70 "@}" fputc (']', yyout);
71 "@`" continue; /* Used by b4_cat in ../data/bison.m4. */
72 @\n continue;
73
74 "@oline@" fprintf (yyout, "%d", out_lineno + 1);
75 "@ofile@" fputs (quotearg_style (c_quoting_style, outname), yyout);
76
77 @[a-z_]+"(" {
78 yytext[yyleng-1] = '\0';
79 obstack_grow (&obstack_for_string, yytext, yyleng);
80 argv[argc++] = obstack_finish (&obstack_for_string);
81 BEGIN SC_AT_DIRECTIVE_ARGS;
82 }
83
84 /* This pattern must not match more than the previous @ patterns. */
85 @[^@{}`(\n]* fail_for_invalid_at (yytext);
86 \n out_lineno++; ECHO;
87 [^@\n]+ ECHO;
88
89 <INITIAL><<EOF>> {
90 if (outname)
91 {
92 free (outname);
93 xfclose (yyout);
94 }
95 return EOF;
96 }
97
98 <SC_AT_DIRECTIVE_ARGS>
99 {
100 [^@]+ STRING_GROW;
101
102 "@@" obstack_1grow (&obstack_for_string, '@');
103 "@{" obstack_1grow (&obstack_for_string, '[');
104 "@}" obstack_1grow (&obstack_for_string, ']');
105 "@`" continue; /* For starting an argument that begins with whitespace. */
106 @\n continue;
107
108 @[,)] {
109 if (argc >= ARGC_MAX)
110 fail_for_at_directive_too_many_args (argv[0]);
111
112 argv[argc++] = obstack_finish0 (&obstack_for_string);
113
114 /* Like M4, skip whitespace after a comma. */
115 if (yytext[1] == ',')
116 BEGIN SC_AT_DIRECTIVE_SKIP_WS;
117 else
118 {
119 at_directive_perform (argc, argv, &outname, &out_lineno);
120 obstack_free (&obstack_for_string, argv[0]);
121 argc = 0;
122 BEGIN INITIAL;
123 }
124 }
125
126 @.? fail_for_invalid_at (yytext);
127 }
128
129 <SC_AT_DIRECTIVE_SKIP_WS>
130 {
131 [ \t\r\n] continue;
132 . yyless (0); BEGIN SC_AT_DIRECTIVE_ARGS;
133 }
134
135 <SC_AT_DIRECTIVE_ARGS,SC_AT_DIRECTIVE_SKIP_WS>
136 {
137 <<EOF>> complain (NULL, fatal, _("unclosed %s directive in skeleton"), argv[0]);
138 }
139
140 %%
141
142 /*------------------------.
143 | Scan a Bison skeleton. |
144 `------------------------*/
145
146 void
147 scan_skel (FILE *in)
148 {
149 static bool initialized = false;
150 if (!initialized)
151 {
152 initialized = true;
153 obstack_init (&obstack_for_string);
154 }
155 skel_in = in;
156 skel__flex_debug = trace_flag & trace_skeleton;
157 skel_lex ();
158 }
159
160 void
161 skel_scanner_free (void)
162 {
163 obstack_free (&obstack_for_string, 0);
164 /* Reclaim Flex's buffers. */
165 yylex_destroy ();
166 }
167
168 static inline warnings
169 flag (const char *arg)
170 {
171 /* compare with values issued from b4_error */
172 if (STREQ (arg, "warn"))
173 return Wother;
174 else if (STREQ (arg, "complain"))
175 return complaint;
176 else if (STREQ (arg, "fatal"))
177 return fatal;
178 else
179 aver (false);
180 }
181
182 static void
183 at_directive_perform (int argc, char *argv[], char **outnamep, int *out_linenop)
184 {
185 if (STREQ (argv[0], "@basename"))
186 {
187 if (argc > 2)
188 fail_for_at_directive_too_many_args (argv[0]);
189 fputs (last_component (argv[1]), yyout);
190 }
191 else if (STREQ (argv[0], "@complain"))
192 {
193 if (argc < 4)
194 fail_for_at_directive_too_few_args (argv[0]);
195 warnings w = flag (argv[1]);
196 location loc;
197 location *locp = NULL;
198 if (argv[2] && argv[2][0])
199 {
200 boundary_set_from_string (&loc.start, argv[2]);
201 boundary_set_from_string (&loc.end, argv[3]);
202 locp = &loc;
203 }
204 complain_args (locp, w, argc - 3, argv + 3);
205 }
206 else if (STREQ (argv[0], "@output"))
207 {
208 if (argc > 2)
209 fail_for_at_directive_too_many_args (argv[0]);
210 if (*outnamep)
211 {
212 free (*outnamep);
213 xfclose (yyout);
214 }
215 *outnamep = xstrdup (argv[1]);
216 output_file_name_check (outnamep);
217 yyout = xfopen (*outnamep, "w");
218 *out_linenop = 1;
219 }
220 else
221 fail_for_invalid_at (argv[0]);
222 }
223
224 static void
225 fail_for_at_directive_too_few_args (char const *at_directive_name)
226 {
227 complain (NULL, fatal, _("too few arguments for %s directive in skeleton"),
228 at_directive_name);
229 }
230
231 static void
232 fail_for_at_directive_too_many_args (char const *at_directive_name)
233 {
234 complain (NULL, fatal, _("too many arguments for %s directive in skeleton"),
235 at_directive_name);
236 }
237
238 static void
239 fail_for_invalid_at (char const *at)
240 {
241 complain (NULL, fatal, "invalid @ in skeleton: %s", at);
242 }