]> git.draconx.ca Git - cdecl99.git/blob - src/parse.y
Make cdecl_free more robust by handling a NULL argument.
[cdecl99.git] / src / parse.y
1 /*
2  *  Parser for C declarations.
3  *  Copyright © 2011 Nick Bowler
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 %parse-param {struct cdecl **out}
20 %define api.pure
21 %error-verbose
22 %locations
23
24 %{
25 #include "scan.h"
26 #include "cdecl.h"
27
28 #define FAIL(msg) do { \
29         yyerror(&yylloc, NULL, msg); \
30         YYERROR; \
31 } while (0)
32
33 #define ALLOC(ptr, size) do { \
34         (ptr) = malloc(size); \
35         if (!(ptr)) \
36                 FAIL("failed to allocate memory"); \
37 } while (0)
38
39 #define ALLOC_STRUCT(ptr, type, ...) do { \
40         ALLOC(ptr, sizeof (type)); \
41         *(ptr) = (type) { __VA_ARGS__ }; \
42 } while (0)
43 %}
44
45 %code requires {
46 #include <inttypes.h>
47 }
48
49 %code provides {
50 void yyerror(YYLTYPE *, struct cdecl **, const char *);
51 int yyparse(struct cdecl **out);
52 }
53
54 %union {
55         uintmax_t uintval;
56         char *strval;
57         struct cdecl_declspec *declspec;
58         struct cdecl_declarator *declarator;
59         struct cdecl *decl;
60 }
61
62 %{
63 static void free_declspec(struct cdecl_declspec *x)
64 {
65         struct cdecl_declspec *p;
66         while (x) {
67                 p = x->next;
68                 free(x->ident);
69                 free(x);
70                 x = p;
71         }
72 }
73
74 static void free_declarator(struct cdecl_declarator *x)
75 {
76         struct cdecl_declarator *p;
77         while (x) {
78                 p = x->next;
79                 free(x->ident);
80                 free(x);
81                 x = p;
82         }
83 }
84
85 static void free_decl(struct cdecl *decl)
86 {
87         free_declspec(decl->specifiers);
88         free_declarator(decl->declarators);
89         free(decl);
90 }
91
92 void cdecl_free(struct cdecl *decl)
93 {
94         if (decl)
95                 free_decl(decl);
96 }
97 %}
98
99 %destructor { free($$); }            <strval>
100 %destructor { free_declspec($$); }   <declspec>
101 %destructor { free_declarator($$); } <declarator>
102 %destructor { free_decl($$); }       <decl>
103
104 %token T_LEX_ERROR
105
106 %token <strval> T_IDENT "identifier"
107 %token T_SEMICOLON ";"
108 %token T_ASTERISK  "*"
109 %token T_LPAREN    "("
110 %token T_RPAREN    ")"
111 %token T_LBRACKET  "["
112 %token T_RBRACKET  "]"
113 %token T_COMMA     ","
114
115 %token T_TYPEDEF  "typedef"
116 %token T_EXTERN   "extern"
117 %token T_STATIC   "static"
118 %token T_AUTO     "auto"
119 %token T_REGISTER "register"
120
121 %token T_INLINE   "inline"
122
123 %token T_RESTRICT "restrict"
124 %token T_VOLATILE "volatile"
125 %token T_CONST    "const"
126
127 %token T_VOID     "void"
128 %token T_CHAR     "char"
129 %token T_SHORT    "short"
130 %token T_INT      "int"
131 %token T_LONG     "long"
132 %token T_FLOAT    "float"
133 %token T_DOUBLE   "double"
134 %token T_SIGNED   "signed"
135 %token T_UNSIGNED "unsigned"
136 %token T_BOOL     "_Bool"
137 %token T_COMPLEX  "_Complex"
138
139 %token T_STRUCT   "struct"
140 %token T_UNION    "union"
141 %token T_ENUM     "enum"
142
143 %type <uintval>    declspec_simple
144 %type <declspec>   declspec declspecs
145 %type <declarator> declarator declarators
146 %type <decl>       declaration
147
148 %%
149
150 input: declaration {
151         *out = $1;
152 };
153
154 declaration: declspecs declarators T_SEMICOLON {
155         ALLOC_STRUCT($$, struct cdecl,
156                 .specifiers = $1,
157                 .declarators = $2);
158 };
159
160 declspecs: { $$ = NULL; } | declspecs declspec {
161         $$ = $2;
162         $$->next = $1;
163 }
164
165 declarators: declarator | declarator T_COMMA declarators {
166         $$ = $1;
167         $$->next = $3;
168 };
169
170 declspec_simple: T_VOID { $$ = CDECL_TYPE_VOID;     }
171         | T_CHAR        { $$ = CDECL_TYPE_CHAR;     }
172         | T_SHORT       { $$ = CDECL_TYPE_SHORT;    }
173         | T_INT         { $$ = CDECL_TYPE_INT;      }
174         | T_LONG        { $$ = CDECL_TYPE_LONG;     }
175         | T_FLOAT       { $$ = CDECL_TYPE_FLOAT;    }
176         | T_DOUBLE      { $$ = CDECL_TYPE_DOUBLE;   }
177         | T_SIGNED      { $$ = CDECL_TYPE_SIGNED;   }
178         | T_UNSIGNED    { $$ = CDECL_TYPE_UNSIGNED; }
179         | T_BOOL        { $$ = CDECL_TYPE_BOOL;     }
180         | T_COMPLEX     { $$ = CDECL_TYPE_COMPLEX;  }
181         | T_TYPEDEF     { $$ = CDECL_STOR_TYPEDEF;  }
182         | T_EXTERN      { $$ = CDECL_STOR_EXTERN;   }
183         | T_STATIC      { $$ = CDECL_STOR_STATIC;   }
184         | T_AUTO        { $$ = CDECL_STOR_AUTO;     }
185         | T_REGISTER    { $$ = CDECL_STOR_REGISTER; }
186         | T_RESTRICT    { $$ = CDECL_QUAL_RESTRICT; }
187         | T_VOLATILE    { $$ = CDECL_QUAL_VOLATILE; }
188         | T_CONST       { $$ = CDECL_QUAL_CONST;    }
189         | T_INLINE      { $$ = CDECL_FUNC_INLINE;   }
190         ;
191
192 declspec: declspec_simple  {
193         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
194 } | T_STRUCT T_IDENT {
195         ALLOC_STRUCT($$, struct cdecl_declspec,
196                 .type = CDECL_TYPE_STRUCT,
197                 .ident = $2);
198 } | T_UNION T_IDENT {
199         ALLOC_STRUCT($$, struct cdecl_declspec,
200                 .type = CDECL_TYPE_UNION,
201                 .ident = $2);
202 } | T_ENUM T_IDENT {
203         ALLOC_STRUCT($$, struct cdecl_declspec,
204                 .type = CDECL_TYPE_ENUM,
205                 .ident = $2);
206 } | T_IDENT {
207         ALLOC_STRUCT($$, struct cdecl_declspec,
208                 .type = CDECL_TYPE_IDENT,
209                 .ident = $1);
210 };
211
212 declarator: T_IDENT {
213         ALLOC_STRUCT($$, struct cdecl_declarator,
214                 .type = CDECL_DECL_IDENT,
215                 .ident = $1);
216 } | T_LPAREN declarator T_RPAREN {
217         $$ = $2;
218 };
219
220 %%
221 void yyerror(YYLTYPE *loc, struct cdecl **out, const char *err)
222 {
223         if (strstr(err, "T_LEX_ERROR"))
224                 return;
225
226         fprintf(stderr, "%s\n", err);
227 }