]> git.saurik.com Git - bison.git/blame - src/parse-skel.y
Really add m4sugar to the repo.
[bison.git] / src / parse-skel.y
CommitLineData
1239777d
AD
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
a4b36db4 24%verbose
aed7fd9b
AD
25%locations
26%name-prefix="skel_"
27%pure-parser
9b3add5b 28
1239777d 29%{
9b3add5b
RA
30#include "system.h"
31#include "obstack.h"
aed7fd9b 32#include "quotearg.h"
9b3add5b 33#include "files.h"
aed7fd9b 34#include "getargs.h"
1239777d
AD
35#include "output.h"
36#include "skeleton.h"
9b3add5b
RA
37#include "muscle_tab.h"
38
a67cef01
TVH
39#define YYERROR_VERBOSE 1
40
aed7fd9b
AD
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)
9b3add5b 46
9b3add5b
RA
47FILE* parser = NULL;
48
49size_t output_line;
50size_t skeleton_line;
51
aed7fd9b 52static void merror PARAMS ((const char* error));
9b3add5b 53
aed7fd9b
AD
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)
64static void yyprint (FILE *file, const yyltype *loc,
65 int type, const yystype *value);
9b3add5b
RA
66%}
67
68%union
69{
a4b36db4 70 char *string;
9b3add5b 71 char character;
aed7fd9b 72 int boolean;
9b3add5b
RA
73}
74
24fad99e 75/* Name of a muscle. */
aed7fd9b 76%token <string> MUSCLE
24fad99e 77/* A string dedicated to Bison (%%"foo"). */
1239777d 78%token <string> STRING
24fad99e 79/* Raw data, to output directly. */
aed7fd9b 80%token <string> RAW
24fad99e 81/* Spaces. */
aed7fd9b 82%token <string> BLANKS
24fad99e 83/* Raw data, but char by char. */
1239777d 84%token <character> CHARACTER
9b3add5b
RA
85
86%token LINE
87%token SLINE
88
9b3add5b
RA
89%token SECTION
90
91%token GUARDS
92%token TOKENS
93%token ACTIONS
94
b85810ae 95%type <string> string.1 string
9b3add5b 96
aed7fd9b 97%start input
9b3add5b
RA
98
99%%
aed7fd9b
AD
100input:
101 { LOCATION_RESET (yylloc) } skeleton
102 ;
9b3add5b
RA
103
104skeleton : /* Empty. */ { }
105 | section skeleton { }
106;
107
108section : section.header section.body { }
109;
110
b85810ae 111section.header : SECTION BLANKS string '\n'
9b3add5b 112{
b85810ae 113 char *name = $3;
9b3add5b
RA
114
115 /* Close the previous parser. */
116 if (parser)
117 parser = (xfclose (parser), NULL);
118
9b3add5b
RA
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
b85810ae
AD
128/* Either a literal string, or a muscle value. */
129string.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. */
136string:
137 string.1
138 { $$ = $1; }
139 | string BLANKS string.1
140 { $$ = stringappend ($1, $3); free ($1); free ($3); }
141 ;
9b3add5b 142
1239777d 143section.body
9b3add5b
RA
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); }
24fad99e
AD
152| section.body RAW { fputs ($2, parser); }
153| section.body BLANKS { fputs ($2, parser); }
1239777d 154| section.body MUSCLE {
9b3add5b
RA
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;
9b3add5b 168%%
aed7fd9b
AD
169/*------------------------------------------------------------------.
170| When debugging the parser, display tokens' locations and values. |
171`------------------------------------------------------------------*/
172
173static void
174yyprint (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;
aed7fd9b
AD
193 }
194}
9b3add5b 195
aed7fd9b
AD
196
197static void
9b3add5b
RA
198merror (const char* error)
199{
200 printf ("line %d: %%{%s} undeclared.\n", skeleton_line, error);
9b3add5b
RA
201}
202
aed7fd9b
AD
203void
204skel_error (skel_control_t *control,
205 const yyltype *loc, const char *msg)
9b3add5b 206{
aed7fd9b
AD
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);
9b3add5b
RA
212}
213
1239777d
AD
214void
215process_skeleton (const char* skel)
9b3add5b 216{
9b3add5b
RA
217 /* Prepare a few things. */
218 output_line = 1;
219 skeleton_line = 1;
220
221 /* Output. */
aed7fd9b 222 skel_in = fopen (skel, "r");
1109455c
AD
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;
aed7fd9b 226 skel_parse (NULL);
9b3add5b
RA
227
228 /* Close the last parser. */
229 if (parser)
230 parser = (xfclose (parser), NULL);
231}