1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
%{
#include "aidl_language.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int yyerror(char* errstr);
int yylex(void);
extern int yylineno;
static int count_brackets(const char*);
%}
%token IMPORT
%token PACKAGE
%token IDENTIFIER
%token GENERIC
%token ARRAY
%token PARCELABLE
%token INTERFACE
%token FLATTENABLE
%token RPC
%token IN
%token OUT
%token INOUT
%token ONEWAY
%%
document:
document_items { g_callbacks->document($1.document_item); }
| headers document_items { g_callbacks->document($2.document_item); }
;
headers:
package { }
| imports { }
| package imports { }
;
package:
PACKAGE { }
;
imports:
IMPORT { g_callbacks->import(&($1.buffer)); }
| IMPORT imports { g_callbacks->import(&($1.buffer)); }
;
document_items:
{ $$.document_item = NULL; }
| document_items declaration {
if ($2.document_item == NULL) {
// error cases only
$$ = $1;
} else {
document_item_type* p = $1.document_item;
while (p && p->next) {
p=p->next;
}
if (p) {
p->next = (document_item_type*)$2.document_item;
$$ = $1;
} else {
$$.document_item = (document_item_type*)$2.document_item;
}
}
}
| document_items error {
fprintf(stderr, "%s:%d: syntax error don't know what to do with \"%s\"\n", g_currentFilename,
$2.buffer.lineno, $2.buffer.data);
$$ = $1;
}
;
declaration:
parcelable_decl { $$.document_item = (document_item_type*)$1.user_data; }
| interface_decl { $$.document_item = (document_item_type*)$1.interface_item; }
;
parcelable_decl:
PARCELABLE IDENTIFIER ';' {
user_data_type* b = (user_data_type*)malloc(sizeof(user_data_type));
b->document_item.item_type = USER_DATA_TYPE;
b->document_item.next = NULL;
b->keyword_token = $1.buffer;
b->name = $2.buffer;
b->package = g_currentPackage ? strdup(g_currentPackage) : NULL;
b->semicolon_token = $3.buffer;
b->flattening_methods = PARCELABLE_DATA;
$$.user_data = b;
}
| PARCELABLE ';' {
fprintf(stderr, "%s:%d syntax error in parcelable declaration. Expected type name.\n",
g_currentFilename, $1.buffer.lineno);
$$.user_data = NULL;
}
| PARCELABLE error ';' {
fprintf(stderr, "%s:%d syntax error in parcelable declaration. Expected type name, saw \"%s\".\n",
g_currentFilename, $2.buffer.lineno, $2.buffer.data);
$$.user_data = NULL;
}
| FLATTENABLE IDENTIFIER ';' {
user_data_type* b = (user_data_type*)malloc(sizeof(user_data_type));
b->document_item.item_type = USER_DATA_TYPE;
b->document_item.next = NULL;
b->keyword_token = $1.buffer;
b->name = $2.buffer;
b->package = g_currentPackage ? strdup(g_currentPackage) : NULL;
b->semicolon_token = $3.buffer;
b->flattening_methods = PARCELABLE_DATA | RPC_DATA;
$$.user_data = b;
}
| FLATTENABLE ';' {
fprintf(stderr, "%s:%d syntax error in flattenable declaration. Expected type name.\n",
g_currentFilename, $1.buffer.lineno);
$$.user_data = NULL;
}
| FLATTENABLE error ';' {
fprintf(stderr, "%s:%d syntax error in flattenable declaration. Expected type name, saw \"%s\".\n",
g_currentFilename, $2.buffer.lineno, $2.buffer.data);
$$.user_data = NULL;
}
;
interface_header:
INTERFACE {
interface_type* c = (interface_type*)malloc(sizeof(interface_type));
c->document_item.item_type = INTERFACE_TYPE_BINDER;
c->document_item.next = NULL;
c->interface_token = $1.buffer;
c->oneway = false;
memset(&c->oneway_token, 0, sizeof(buffer_type));
c->comments_token = &c->interface_token;
$$.interface_obj = c;
}
| ONEWAY INTERFACE {
interface_type* c = (interface_type*)malloc(sizeof(interface_type));
c->document_item.item_type = INTERFACE_TYPE_BINDER;
c->document_item.next = NULL;
c->interface_token = $2.buffer;
c->oneway = true;
c->oneway_token = $1.buffer;
c->comments_token = &c->oneway_token;
$$.interface_obj = c;
}
| RPC {
interface_type* c = (interface_type*)malloc(sizeof(interface_type));
c->document_item.item_type = INTERFACE_TYPE_RPC;
c->document_item.next = NULL;
c->interface_token = $1.buffer;
c->oneway = false;
memset(&c->oneway_token, 0, sizeof(buffer_type));
c->comments_token = &c->interface_token;
$$.interface_obj = c;
}
;
interface_keywords:
INTERFACE
| RPC
;
interface_decl:
interface_header IDENTIFIER '{' interface_items '}' {
interface_type* c = $1.interface_obj;
c->name = $2.buffer;
c->package = g_currentPackage ? strdup(g_currentPackage) : NULL;
c->open_brace_token = $3.buffer;
c->interface_items = $4.interface_item;
c->close_brace_token = $5.buffer;
$$.interface_obj = c;
}
| interface_keywords error '{' interface_items '}' {
fprintf(stderr, "%s:%d: syntax error in interface declaration. Expected type name, saw \"%s\"\n",
g_currentFilename, $2.buffer.lineno, $2.buffer.data);
$$.document_item = NULL;
}
| interface_keywords error '}' {
fprintf(stderr, "%s:%d: syntax error in interface declaration. Expected type name, saw \"%s\"\n",
g_currentFilename, $2.buffer.lineno, $2.buffer.data);
$$.document_item = NULL;
}
;
interface_items:
{ $$.interface_item = NULL; }
| interface_items method_decl {
interface_item_type* p=$1.interface_item;
while (p && p->next) {
p=p->next;
}
if (p) {
p->next = (interface_item_type*)$2.method;
$$ = $1;
} else {
$$.interface_item = (interface_item_type*)$2.method;
}
}
| interface_items error ';' {
fprintf(stderr, "%s:%d: syntax error before ';' (expected method declaration)\n",
g_currentFilename, $3.buffer.lineno);
$$ = $1;
}
;
method_decl:
type IDENTIFIER '(' arg_list ')' ';' {
method_type *method = (method_type*)malloc(sizeof(method_type));
method->interface_item.item_type = METHOD_TYPE;
method->interface_item.next = NULL;
method->type = $1.type;
method->oneway = false;
memset(&method->oneway_token, 0, sizeof(buffer_type));
method->name = $2.buffer;
method->open_paren_token = $3.buffer;
method->args = $4.arg;
method->close_paren_token = $5.buffer;
method->semicolon_token = $6.buffer;
method->comments_token = &method->type.type;
$$.method = method;
}
| ONEWAY type IDENTIFIER '(' arg_list ')' ';' {
method_type *method = (method_type*)malloc(sizeof(method_type));
method->interface_item.item_type = METHOD_TYPE;
method->interface_item.next = NULL;
method->oneway = true;
method->oneway_token = $1.buffer;
method->type = $2.type;
method->name = $3.buffer;
method->open_paren_token = $4.buffer;
method->args = $5.arg;
method->close_paren_token = $6.buffer;
method->semicolon_token = $7.buffer;
method->comments_token = &method->oneway_token;
$$.method = method;
}
;
arg_list:
{ $$.arg = NULL; }
| arg { $$ = $1; }
| arg_list ',' arg {
if ($$.arg != NULL) {
// only NULL on error
$$ = $1;
arg_type *p = $1.arg;
while (p && p->next) {
p=p->next;
}
$3.arg->comma_token = $2.buffer;
p->next = $3.arg;
}
}
| error {
fprintf(stderr, "%s:%d: syntax error in parameter list\n", g_currentFilename, $1.buffer.lineno);
$$.arg = NULL;
}
;
arg:
direction type IDENTIFIER {
arg_type* arg = (arg_type*)malloc(sizeof(arg_type));
memset(&arg->comma_token, 0, sizeof(buffer_type));
arg->direction = $1.buffer;
arg->type = $2.type;
arg->name = $3.buffer;
arg->next = NULL;
$$.arg = arg;
}
;
type:
IDENTIFIER {
$$.type.type = $1.buffer;
init_buffer_type(&$$.type.array_token, yylineno);
$$.type.dimension = 0;
}
| IDENTIFIER ARRAY {
$$.type.type = $1.buffer;
$$.type.array_token = $2.buffer;
$$.type.dimension = count_brackets($2.buffer.data);
}
| GENERIC {
$$.type.type = $1.buffer;
init_buffer_type(&$$.type.array_token, yylineno);
$$.type.dimension = 0;
}
;
direction:
{ init_buffer_type(&$$.buffer, yylineno); }
| IN { $$.buffer = $1.buffer; }
| OUT { $$.buffer = $1.buffer; }
| INOUT { $$.buffer = $1.buffer; }
;
%%
#include <ctype.h>
#include <stdio.h>
int g_error = 0;
int yyerror(char* errstr)
{
fprintf(stderr, "%s:%d: %s\n", g_currentFilename, yylineno, errstr);
g_error = 1;
return 1;
}
void init_buffer_type(buffer_type* buf, int lineno)
{
buf->lineno = lineno;
buf->token = 0;
buf->data = NULL;
buf->extra = NULL;
}
static int count_brackets(const char* s)
{
int n=0;
while (*s) {
if (*s == '[') n++;
s++;
}
return n;
}
|