]>
Commit | Line | Data |
---|---|---|
1 | /* -*- C -*- */ | |
2 | /* Parse Bison Skeletons. | |
3 | Copyright (C) 2001 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of Bison, the GNU Compiler Compiler. | |
6 | ||
7 | Bison is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | Bison is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with Bison; see the file COPYING. If not, write to the Free | |
19 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, USA. */ | |
21 | ||
22 | %debug | |
23 | %defines | |
24 | %verbose | |
25 | %locations | |
26 | %name-prefix="skel_" | |
27 | %pure-parser | |
28 | ||
29 | %{ | |
30 | #include "system.h" | |
31 | #include "obstack.h" | |
32 | #include "quotearg.h" | |
33 | #include "files.h" | |
34 | #include "getargs.h" | |
35 | #include "output.h" | |
36 | #include "skeleton.h" | |
37 | #include "muscle_tab.h" | |
38 | ||
39 | #define YYERROR_VERBOSE 1 | |
40 | ||
41 | /* Pass the control structure to YYPARSE but not YYLEX (yet?). */ | |
42 | #define YYPARSE_PARAM skel_control | |
43 | /* YYPARSE receives SKEL_CONTROL as a void *. Provide a correctly | |
44 | typed access to it. */ | |
45 | #define yycontrol ((skel_control_t *) skel_control) | |
46 | ||
47 | FILE* parser = NULL; | |
48 | ||
49 | size_t output_line; | |
50 | size_t skeleton_line; | |
51 | ||
52 | static void merror PARAMS ((const char* error)); | |
53 | ||
54 | /* Request detailed parse error messages, and pass them to | |
55 | YLEVAL_ERROR. */ | |
56 | #undef yyerror | |
57 | #define yyerror(Msg) \ | |
58 | skel_error (yycontrol, &yylloc, Msg) | |
59 | ||
60 | /* When debugging our pure parser, we want to see values and locations | |
61 | of the tokens. */ | |
62 | #define YYPRINT(File, Type, Value) \ | |
63 | yyprint (File, &yylloc, Type, &Value) | |
64 | static void yyprint (FILE *file, const yyltype *loc, | |
65 | int type, const yystype *value); | |
66 | %} | |
67 | ||
68 | %union | |
69 | { | |
70 | char *string; | |
71 | char character; | |
72 | int boolean; | |
73 | } | |
74 | ||
75 | /* Name of a muscle. */ | |
76 | %token <string> MUSCLE | |
77 | /* A string dedicated to Bison (%%"foo"). */ | |
78 | %token <string> STRING | |
79 | /* Raw data, to output directly. */ | |
80 | %token <string> RAW | |
81 | /* Spaces. */ | |
82 | %token <string> BLANKS | |
83 | /* Raw data, but char by char. */ | |
84 | %token <character> CHARACTER | |
85 | ||
86 | %token LINE | |
87 | %token SLINE | |
88 | ||
89 | %token SECTION | |
90 | ||
91 | %token GUARDS | |
92 | %token TOKENS | |
93 | %token ACTIONS | |
94 | ||
95 | %type <string> string.1 string | |
96 | ||
97 | %start input | |
98 | ||
99 | %% | |
100 | input: | |
101 | { LOCATION_RESET (yylloc) } skeleton | |
102 | ; | |
103 | ||
104 | skeleton : /* Empty. */ { } | |
105 | | section skeleton { } | |
106 | ; | |
107 | ||
108 | section : section.header section.body { } | |
109 | ; | |
110 | ||
111 | section.header : SECTION BLANKS string '\n' | |
112 | { | |
113 | char *name = $3; | |
114 | ||
115 | /* Close the previous parser. */ | |
116 | if (parser) | |
117 | parser = (xfclose (parser), NULL); | |
118 | ||
119 | /* Prepare the next parser to be output. */ | |
120 | parser = xfopen (name, "w"); | |
121 | MUSCLE_INSERT_STRING ("parser-file-name", name); | |
122 | XFREE (name); | |
123 | ||
124 | ++skeleton_line; | |
125 | } | |
126 | ; | |
127 | ||
128 | /* Either a literal string, or a muscle value. */ | |
129 | string.1: | |
130 | STRING { $$ = $1; } | |
131 | | MUSCLE { $$ = xstrdup (muscle_find ($1)); } | |
132 | ; | |
133 | ||
134 | /* Either a literal string, or a muscle value, or the concatenation of | |
135 | them. */ | |
136 | string: | |
137 | string.1 | |
138 | { $$ = $1; } | |
139 | | string BLANKS string.1 | |
140 | { $$ = stringappend ($1, $3); free ($1); free ($3); } | |
141 | ; | |
142 | ||
143 | section.body | |
144 | : /* Empty. */ { } | |
145 | | section.body '\n' { fputc ('\n', parser); ++output_line; ++skeleton_line; } | |
146 | | section.body LINE { fprintf (parser, "%d", output_line); } | |
147 | | section.body SLINE { fprintf (parser, "%d", skeleton_line); } | |
148 | | section.body GUARDS { guards_output (parser, &output_line); } | |
149 | | section.body TOKENS { token_definitions_output (parser, &output_line); } | |
150 | | section.body ACTIONS { actions_output (parser, &output_line); } | |
151 | | section.body CHARACTER { fputc ($2, parser); } | |
152 | | section.body RAW { fputs ($2, parser); } | |
153 | | section.body BLANKS { fputs ($2, parser); } | |
154 | | section.body MUSCLE { | |
155 | const char* value = muscle_find ($2); | |
156 | if (value) | |
157 | { | |
158 | fputs (value, parser); | |
159 | output_line += get_lines_number (value); | |
160 | } | |
161 | else | |
162 | { | |
163 | fprintf (parser, "%%{%s}", $2); | |
164 | merror ($2); | |
165 | } | |
166 | } | |
167 | ; | |
168 | %% | |
169 | /*------------------------------------------------------------------. | |
170 | | When debugging the parser, display tokens' locations and values. | | |
171 | `------------------------------------------------------------------*/ | |
172 | ||
173 | static void | |
174 | yyprint (FILE *file, | |
175 | const yyltype *loc, int type, const yystype *value) | |
176 | { | |
177 | fputs (" (", file); | |
178 | LOCATION_PRINT (file, *loc); | |
179 | fputs (")", file); | |
180 | switch (type) | |
181 | { | |
182 | case MUSCLE: | |
183 | case STRING: | |
184 | case RAW: | |
185 | case BLANKS: | |
186 | fprintf (file, " = %s", quotearg_style (c_quoting_style, | |
187 | value->string)); | |
188 | break; | |
189 | ||
190 | case CHARACTER: | |
191 | fprintf (file, " = '%c'", value->character); | |
192 | break; | |
193 | } | |
194 | } | |
195 | ||
196 | ||
197 | static void | |
198 | merror (const char* error) | |
199 | { | |
200 | printf ("line %d: %%{%s} undeclared.\n", skeleton_line, error); | |
201 | } | |
202 | ||
203 | void | |
204 | skel_error (skel_control_t *control, | |
205 | const yyltype *loc, const char *msg) | |
206 | { | |
207 | /* Neutralize GCC warnings for unused parameters. */ | |
208 | skel_control_t *c = control; | |
209 | c++; | |
210 | LOCATION_PRINT (stderr, *loc); | |
211 | fprintf (stderr, "%s\n", msg); | |
212 | } | |
213 | ||
214 | void | |
215 | process_skeleton (const char* skel) | |
216 | { | |
217 | /* Prepare a few things. */ | |
218 | output_line = 1; | |
219 | skeleton_line = 1; | |
220 | ||
221 | /* Output. */ | |
222 | skel_in = fopen (skel, "r"); | |
223 | /* FIXME: This is not acceptable for a release. */ | |
224 | skel__flex_debug = getenv ("BISON_TRACE_SCAN") ? 1 : 0; | |
225 | skel_debug = getenv ("BISON_TRACE_PARSE") ? 1 : 0; | |
226 | skel_parse (NULL); | |
227 | ||
228 | /* Close the last parser. */ | |
229 | if (parser) | |
230 | parser = (xfclose (parser), NULL); | |
231 | } |