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
38 #include "muscle_tab.h"
40 /* Pass the control structure to YYPARSE but not YYLEX (yet?). */
41 #define YYPARSE_PARAM skel_control
42 /* YYPARSE receives SKEL_CONTROL as a void *. Provide a correctly
43 typed access to it. */
44 #define yycontrol ((skel_control_t *) skel_control)
52 static void merror PARAMS ((const char* error));
54 /* Request detailed parse error messages, and pass them to
57 #define yyerror(Msg) \
58 skel_error (yycontrol, &yylloc, Msg)
60 /* When debugging our pure parser, we want to see values and locations
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);
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. */
82 %token <string> BLANKS
83 /* Raw data, but char by char. */
84 %token <character> CHARACTER
96 %type <boolean> section.yacc
102 { LOCATION_RESET (yylloc) } skeleton
105 skeleton : /* Empty. */ { }
106 | section skeleton { }
109 section : section.header section.body { }
112 section.header : SECTION BLANKS MUSCLE BLANKS STRING BLANKS section.yacc '\n'
118 /* Close the previous parser. */
120 parser = (xfclose (parser), NULL);
122 /* If the following section should be named with the yacc-style, and it's
123 suffix is of the form 'something.h' or 'something.c', then add '.tab' in
124 the middle of the suffix. */
125 if (tab_extension && $7 && (strsuffix (suffix, ".h") ||
126 strsuffix (suffix, ".c")))
128 size_t prefix_len = strlen (prefix);
129 size_t suffix_len = strlen (suffix);
131 /* Allocate enough space to insert '.tab'. */
132 name = XMALLOC (char, prefix_len + suffix_len + 5);
133 limit = strrchr (suffix, '.');
137 /* Prefix is 'X', suffix is 'Y.Z'. Name will be 'XY.tab.Z'. */
140 cp = stpcpy (name, prefix);
141 cp = stpncpy (cp, suffix, limit - suffix);
142 cp = stpcpy (cp, ".tab");
143 cp = stpcpy (cp, limit);
147 name = stringappend (prefix, suffix);
149 /* Prepare the next parser to be output. */
150 parser = xfopen (name, "w");
151 MUSCLE_INSERT_STRING ("parser-file-name", name);
158 section.yacc : /* Empty. */ { $$ = 0; }
164 | section.body '\n' { fputc ('\n', parser); ++output_line; ++skeleton_line; }
165 | section.body LINE { fprintf (parser, "%d", output_line); }
166 | section.body SLINE { fprintf (parser, "%d", skeleton_line); }
167 | section.body GUARDS { guards_output (parser, &output_line); }
168 | section.body TOKENS { token_definitions_output (parser, &output_line); }
169 | section.body ACTIONS { actions_output (parser, &output_line); }
170 | section.body CHARACTER { fputc ($2, parser); }
171 | section.body RAW { fputs ($2, parser); }
172 | section.body BLANKS { fputs ($2, parser); }
173 | section.body MUSCLE {
174 const char* value = muscle_find ($2);
177 fputs (value, parser);
178 output_line += get_lines_number (value);
182 fprintf (parser, "%%{%s}", $2);
188 /*------------------------------------------------------------------.
189 | When debugging the parser, display tokens' locations and values. |
190 `------------------------------------------------------------------*/
194 const yyltype *loc, int type, const yystype *value)
197 LOCATION_PRINT (file, *loc);
205 fprintf (file, " = %s", quotearg_style (c_quoting_style,
210 fprintf (file, " = '%c'", value->character);
214 fprintf (file, " = %s", value->boolean ? "true" : "false");
221 merror (const char* error)
223 printf ("line %d: %%{%s} undeclared.\n", skeleton_line, error);
227 skel_error (skel_control_t *control,
228 const yyltype *loc, const char *msg)
230 /* Neutralize GCC warnings for unused parameters. */
231 skel_control_t *c = control;
233 LOCATION_PRINT (stderr, *loc);
234 fprintf (stderr, "%s\n", msg);
238 process_skeleton (const char* skel)
240 /* Compute prefix. Actually, it seems that the processing I need here is
241 done in compute_base_names, and the result stored in short_base_name. */
242 prefix = short_base_name;
244 /* Prepare a few things. */
249 skel_in = fopen (skel, "r");
250 skel__flex_debug = 0;
251 skel_debug = trace_flag ? 1 : 0;
254 /* Close the last parser. */
256 parser = (xfclose (parser), NULL);