]> git.saurik.com Git - bison.git/blob - src/parse-skel.y
* src/system.h: We don't need nor want bcopy.
[bison.git] / src / parse-skel.y
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 %error-verbose
26
27 %{
28 #include "system.h"
29 #include "obstack.h"
30 #include "files.h"
31 #include "output.h"
32 #include "skeleton.h"
33 #include "muscle_tab.h"
34
35 extern FILE* yyin;
36 extern int yylineno;
37
38 char* prefix = NULL;
39 FILE* parser = NULL;
40
41 size_t output_line;
42 size_t skeleton_line;
43
44 static int merror PARAMS ((const char* error));
45 static int yyerror PARAMS ((const char* error));
46
47 %}
48
49 %union
50 {
51 char *muscle;
52 char *string;
53 char character;
54 int yacc;
55 }
56
57 %token <muscle> MUSCLE
58 %token <string> STRING
59 %token <character> CHARACTER
60
61 %token LINE
62 %token SLINE
63
64 %token YACC
65 %token SECTION
66
67 %token GUARDS
68 %token TOKENS
69 %token ACTIONS
70
71 %type <yacc> section.yacc
72
73 %start skeleton
74
75 %%
76
77 skeleton : /* Empty. */ { }
78 | section skeleton { }
79 ;
80
81 section : section.header section.body { }
82 ;
83
84 section.header : SECTION gb MUSCLE gb STRING gb section.yacc gb '\n'
85 {
86 char *name = 0;
87 char *limit = 0;
88 char *suffix = $5;
89
90 /* Close the previous parser. */
91 if (parser)
92 parser = (xfclose (parser), NULL);
93
94 /* If the following section should be named with the yacc-style, and it's
95 suffix is of the form 'something.h' or 'something.c', then add '.tab' in
96 the middle of the suffix. */
97 if (tab_extension && $7 && (strsuffix (suffix, ".h") ||
98 strsuffix (suffix, ".c")))
99 {
100 size_t prefix_len = strlen (prefix);
101 size_t suffix_len = strlen (suffix);
102
103 /* Allocate enough space to insert '.tab'. */
104 name = XMALLOC (char, prefix_len + suffix_len + 5);
105 limit = strrchr (suffix, '.');
106 if (!limit)
107 limit = suffix;
108
109 /* Prefix is 'X', suffix is 'Y.Z'. Name will be 'XY.tab.Z'. */
110 {
111 char* cp = 0;
112 cp = stpcpy (name, prefix);
113 cp = stpncpy (cp, suffix, limit - suffix);
114 cp = stpcpy (cp, ".tab");
115 cp = stpcpy (cp, limit);
116 }
117 }
118 else
119 name = stringappend (prefix, suffix);
120
121 /* Prepare the next parser to be output. */
122 parser = xfopen (name, "w");
123 MUSCLE_INSERT_STRING ("parser-file-name", name);
124 XFREE (name);
125
126 ++skeleton_line;
127 }
128 ;
129
130 section.yacc : /* Empty. */ { $$ = 0; }
131 | YACC { $$ = 1; }
132 ;
133
134 section.body
135 : /* Empty. */ { }
136 | section.body '\n' { fputc ('\n', parser); ++output_line; ++skeleton_line; }
137 | section.body LINE { fprintf (parser, "%d", output_line); }
138 | section.body SLINE { fprintf (parser, "%d", skeleton_line); }
139 | section.body GUARDS { guards_output (parser, &output_line); }
140 | section.body TOKENS { token_definitions_output (parser, &output_line); }
141 | section.body ACTIONS { actions_output (parser, &output_line); }
142 | section.body CHARACTER { fputc ($2, parser); }
143 | section.body MUSCLE {
144 const char* value = muscle_find ($2);
145 if (value)
146 {
147 fputs (value, parser);
148 output_line += get_lines_number (value);
149 }
150 else
151 {
152 fprintf (parser, "%%{%s}", $2);
153 merror ($2);
154 }
155 }
156 ;
157
158 gb : /* Empty. */ { }
159 | gb CHARACTER { /* Do not echo garbage characters. */ }
160 ;
161
162 %%
163
164 static int
165 merror (const char* error)
166 {
167 printf ("line %d: %%{%s} undeclared.\n", skeleton_line, error);
168 return 0;
169 }
170
171 static int
172 yyerror (const char* error)
173 {
174 fprintf (stderr, "%s\n", error);
175 return 0;
176 }
177
178 void
179 process_skeleton (const char* skel)
180 {
181 /* Compute prefix. Actually, it seems that the processing I need here is
182 done in compute_base_names, and the result stored in short_base_name. */
183 prefix = short_base_name;
184
185 /* Prepare a few things. */
186 output_line = 1;
187 skeleton_line = 1;
188
189 /* Output. */
190 yyin = fopen (skel, "r");
191 yydebug = 0;
192 yyparse ();
193
194 /* Close the last parser. */
195 if (parser)
196 parser = (xfclose (parser), NULL);
197 }