]> git.saurik.com Git - bison.git/blob - src/scan-skel.l
Don't use m4_divert since it makes m4_divert_push and m4_divert_pop
[bison.git] / src / scan-skel.l
1 /* Scan Bison Skeletons. -*- C -*-
2
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4 Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
8 Bison is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 Bison is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to the Free
20 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 %option nodefault noyywrap nounput never-interactive debug
24 %option prefix="skel_" outfile="lex.yy.c"
25
26 %{
27 /* Work around a bug in flex 2.5.31. See Debian bug 333231
28 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */
29 #undef skel_wrap
30 #define skel_wrap() 1
31
32 #define FLEX_PREFIX(Id) skel_ ## Id
33 #include "flex-scanner.h"
34
35 #include <dirname.h>
36 #include <error.h>
37 #include <quotearg.h>
38
39 #include "complain.h"
40 #include "getargs.h"
41 #include "files.h"
42 #include "scan-skel.h"
43
44 #define YY_DECL static int skel_lex (void)
45 YY_DECL;
46
47 #define QPUTS(String) \
48 fputs (quotearg_style (c_quoting_style, String), yyout)
49
50 static void fail_for_at_directive_too_many_args (void);
51 static void fail_for_invalid_at (char const *at);
52
53 /* In SC_AT_DIRECTIVE_ARG context, the name of the directive. */
54 static char *at_directive_name;
55
56 /* Currently, only the @warn, @complain, and @fatal directives take multiple
57 arguments, and they already can't take more than 5. */
58 #define AT_DIRECTIVE_ARGC_MAX 5
59 static int at_directive_argc = 0;
60 static char *at_directive_argv[AT_DIRECTIVE_ARGC_MAX];
61 %}
62
63 %x SC_AT_DIRECTIVE_ARG
64 %x SC_AT_DIRECTIVE_SKIP_WS
65
66 %%
67
68 %{
69 int lineno IF_LINT (= 0);
70 char *outname = NULL;
71 %}
72
73 "@@" fputc ('@', yyout);
74 "@{" fputc ('[', yyout);
75 "@}" fputc (']', yyout);
76
77 "@oline@" fprintf (yyout, "%d", lineno + 1);
78 "@ofile@" QPUTS (outname);
79 "@dir_prefix@" QPUTS (dir_prefix);
80
81 @[a-z_]+"(" {
82 yytext[yyleng-1] = '\0';
83 at_directive_name = xstrdup (yytext);
84 BEGIN SC_AT_DIRECTIVE_ARG;
85 }
86
87 /* This pattern must not match more than the previous @ patterns. */
88 @[^@{}(\n]* fail_for_invalid_at (yytext);
89 \n lineno++; ECHO;
90 [^@\n]+ ECHO;
91
92 <INITIAL><<EOF>> {
93 if (outname)
94 {
95 free (outname);
96 xfclose (yyout);
97 }
98 return EOF;
99 }
100
101 <SC_AT_DIRECTIVE_ARG>{
102 [^@\n]+ { STRING_GROW; }
103 \n { ++lineno; STRING_GROW; }
104
105 "@@" { obstack_1grow (&obstack_for_string, '@'); }
106 "@{" { obstack_1grow (&obstack_for_string, '['); }
107 "@}" { obstack_1grow (&obstack_for_string, ']'); }
108 "@`" /* Emtpy. Useful for starting an argument
109 that begins with whitespace. */
110
111 @[,)] {
112 if (at_directive_argc >= AT_DIRECTIVE_ARGC_MAX)
113 fail_for_at_directive_too_many_args ();
114
115 obstack_1grow (&obstack_for_string, '\0');
116 at_directive_argv[at_directive_argc++] =
117 obstack_finish (&obstack_for_string);
118
119 /* Like M4, skip whitespace after a comma. */
120 if (yytext[1] == ',')
121 BEGIN SC_AT_DIRECTIVE_SKIP_WS;
122 else
123 {
124 if (0 == strcmp (at_directive_name, "@basename"))
125 {
126 if (at_directive_argc > 1)
127 fail_for_at_directive_too_many_args ();
128 fputs (last_component (at_directive_argv[0]), yyout);
129 }
130 else if (0 == strcmp (at_directive_name, "@warn")
131 || 0 == strcmp (at_directive_name, "@complain")
132 || 0 == strcmp (at_directive_name, "@fatal"))
133 {
134 void (*func)(char const *, ...);
135 switch (at_directive_name[1])
136 {
137 case 'w': func = warn; break;
138 case 'c': func = complain; break;
139 case 'f': func = fatal; break;
140 default: aver (false); func = NULL; break;
141 }
142 switch (at_directive_argc)
143 {
144 case 1:
145 func (_(at_directive_argv[0]));
146 break;
147 case 2:
148 func (_(at_directive_argv[0]), at_directive_argv[1]);
149 break;
150 case 3:
151 func (_(at_directive_argv[0]), at_directive_argv[1],
152 at_directive_argv[2]);
153 break;
154 case 4:
155 func (_(at_directive_argv[0]), at_directive_argv[1],
156 at_directive_argv[2], at_directive_argv[3]);
157 break;
158 case 5:
159 func (_(at_directive_argv[0]), at_directive_argv[1],
160 at_directive_argv[2], at_directive_argv[3],
161 at_directive_argv[4]);
162 break;
163 default:
164 fail_for_at_directive_too_many_args ();
165 break;
166 }
167 }
168 else if (0 == strcmp (at_directive_name, "@output"))
169 {
170 if (at_directive_argc > 1)
171 fail_for_at_directive_too_many_args ();
172 if (outname)
173 {
174 free (outname);
175 xfclose (yyout);
176 }
177 outname = xstrdup (at_directive_argv[0]);
178 output_file_name_check (outname);
179 yyout = xfopen (outname, "w");
180 lineno = 1;
181 }
182 else
183 fail_for_invalid_at (at_directive_name);
184
185 obstack_free (&obstack_for_string, at_directive_argv[0]);
186 at_directive_argc = 0;
187 free (at_directive_name);
188 BEGIN INITIAL;
189 }
190 }
191
192 @.? { fail_for_invalid_at (yytext); }
193 }
194
195 <SC_AT_DIRECTIVE_SKIP_WS>{
196 [ \t\r]
197 \n { ++lineno; }
198 . { yyless (0); BEGIN SC_AT_DIRECTIVE_ARG; }
199 }
200
201 <SC_AT_DIRECTIVE_ARG,SC_AT_DIRECTIVE_SKIP_WS>{
202 <<EOF>> {
203 fatal (_("unclosed %s directive in skeleton"), at_directive_name);
204 }
205 }
206
207 %%
208
209 /*------------------------.
210 | Scan a Bison skeleton. |
211 `------------------------*/
212
213 void
214 scan_skel (FILE *in)
215 {
216 static bool initialized = false;
217 if (!initialized)
218 {
219 initialized = true;
220 obstack_init (&obstack_for_string);
221 }
222 skel_in = in;
223 skel__flex_debug = trace_flag & trace_skeleton;
224 skel_lex ();
225 /* Reclaim Flex's buffers. */
226 yylex_destroy ();
227 }
228
229 static void
230 fail_for_at_directive_too_many_args (void)
231 {
232 fatal (_("too many arguments for %s directive in skeleton"),
233 at_directive_name);
234 }
235
236 static void
237 fail_for_invalid_at (char const *at)
238 {
239 fatal ("invalid @ in skeleton: %s", at);
240 }
241
242 void
243 skel_scanner_free (void)
244 {
245 obstack_free (&obstack_for_string, 0);
246 }