2 /* Parse Bison Skeletons.
3 Copyright (C) 2001 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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
37 #include "muscle_tab.h"
39 #define YYERROR_VERBOSE 1
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)
53 static void merror PARAMS ((const char* error));
55 /* Request detailed parse error messages, and pass them to
58 #define yyerror(Msg) \
59 skel_error (yycontrol, &yylloc, Msg)
61 /* When debugging our pure parser, we want to see values and locations
63 #define YYPRINT(File, Type, Value) \
64 yyprint (File, &yylloc, Type, &Value)
65 static void yyprint (FILE *file, const yyltype *loc,
66 int type, const yystype *value);
76 /* Name of a muscle. */
77 %token <string> MUSCLE
78 /* A string dedicated to Bison (%%"foo"). */
79 %token <string> STRING
80 /* Raw data, to output directly. */
83 %token <string> BLANKS
84 /* Raw data, but char by char. */
85 %token <character> CHARACTER
97 %type <boolean> section.yacc
103 { LOCATION_RESET (yylloc) } skeleton
106 skeleton : /* Empty. */ { }
107 | section skeleton { }
110 section : section.header section.body { }
113 section.header : SECTION BLANKS MUSCLE BLANKS STRING BLANKS section.yacc '\n'
119 /* Close the previous parser. */
121 parser = (xfclose (parser), NULL);
123 /* If the following section should be named with the yacc-style, and it's
124 suffix is of the form 'something.h' or 'something.c', then add '.tab' in
125 the middle of the suffix. */
126 if (tab_extension && $7 && (strsuffix (suffix, ".h") ||
127 strsuffix (suffix, ".c")))
129 size_t prefix_len = strlen (prefix);
130 size_t suffix_len = strlen (suffix);
132 /* Allocate enough space to insert '.tab'. */
133 name = XMALLOC (char, prefix_len + suffix_len + 5);
134 limit = strrchr (suffix, '.');
138 /* Prefix is 'X', suffix is 'Y.Z'. Name will be 'XY.tab.Z'. */
141 cp = stpcpy (name, prefix);
142 cp = stpncpy (cp, suffix, limit - suffix);
143 cp = stpcpy (cp, ".tab");
144 cp = stpcpy (cp, limit);
148 name = stringappend (prefix, suffix);
150 /* Prepare the next parser to be output. */
151 parser = xfopen (name, "w");
152 MUSCLE_INSERT_STRING ("parser-file-name", name);
159 section.yacc : /* Empty. */ { $$ = 0; }
165 | section.body '\n' { fputc ('\n', parser); ++output_line; ++skeleton_line; }
166 | section.body LINE { fprintf (parser, "%d", output_line); }
167 | section.body SLINE { fprintf (parser, "%d", skeleton_line); }
168 | section.body GUARDS { guards_output (parser, &output_line); }
169 | section.body TOKENS { token_definitions_output (parser, &output_line); }
170 | section.body ACTIONS { actions_output (parser, &output_line); }
171 | section.body CHARACTER { fputc ($2, parser); }
172 | section.body RAW { fputs ($2, parser); }
173 | section.body BLANKS { fputs ($2, parser); }
174 | section.body MUSCLE {
175 const char* value = muscle_find ($2);
178 fputs (value, parser);
179 output_line += get_lines_number (value);
183 fprintf (parser, "%%{%s}", $2);
189 /*------------------------------------------------------------------.
190 | When debugging the parser, display tokens' locations and values. |
191 `------------------------------------------------------------------*/
195 const yyltype *loc, int type, const yystype *value)
198 LOCATION_PRINT (file, *loc);
206 fprintf (file, " = %s", quotearg_style (c_quoting_style,
211 fprintf (file, " = '%c'", value->character);
215 fprintf (file, " = %s", value->boolean ? "true" : "false");
222 merror (const char* error)
224 printf ("line %d: %%{%s} undeclared.\n", skeleton_line, error);
228 skel_error (skel_control_t *control,
229 const yyltype *loc, const char *msg)
231 /* Neutralize GCC warnings for unused parameters. */
232 skel_control_t *c = control;
234 LOCATION_PRINT (stderr, *loc);
235 fprintf (stderr, "%s\n", msg);
239 process_skeleton (const char* skel)
241 /* Compute prefix. Actually, it seems that the processing I need here is
242 done in compute_base_names, and the result stored in short_base_name. */
243 prefix = short_base_name;
245 /* Prepare a few things. */
250 skel_in = fopen (skel, "r");
251 /* FIXME: This is not acceptable for a release. */
252 skel__flex_debug = getenv ("BISON_TRACE_SCAN") ? 1 : 0;
253 skel_debug = getenv ("BISON_TRACE_PARSE") ? 1 : 0;
256 /* Close the last parser. */
258 parser = (xfclose (parser), NULL);