summaryrefslogtreecommitdiffstats
path: root/aidl_language_y.yy
blob: 888e6b909323300df630f55bb05abaa9ed98f714 (plain)
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
%{
#include "aidl_language.h"
#include "aidl_language_y.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *);

#define lex_scanner ps->Scanner()

%}

%parse-param { Parser* ps }
%lex-param { void *lex_scanner }

%pure-parser
%skeleton "glr.cc"

%union {
    AidlToken* token;
    int integer;
    std::string *str;
    AidlType::Annotation annotation;
    AidlType::Annotation annotation_list;
    AidlType* type;
    AidlType* unannotated_type;
    AidlArgument* arg;
    AidlArgument::Direction direction;
    std::vector<std::unique_ptr<AidlArgument>>* arg_list;
    AidlMethod* method;
    AidlMember* constant;
    std::vector<std::unique_ptr<AidlMember>>* members;
    AidlQualifiedName* qname;
    AidlInterface* interface_obj;
    AidlParcelable* parcelable;
    AidlDocument* parcelable_list;
}

%token<token> IDENTIFIER INTERFACE ONEWAY C_STR
%token<integer> INTVALUE

%token '(' ')' ',' '=' '[' ']' '<' '>' '.' '{' '}' ';'
%token IN OUT INOUT PACKAGE IMPORT PARCELABLE CPP_HEADER CONST INT STRING
%token ANNOTATION_NULLABLE ANNOTATION_UTF8 ANNOTATION_UTF8_CPP

%type<parcelable_list> parcelable_decls
%type<parcelable> parcelable_decl
%type<members> members
%type<interface_obj> interface_decl
%type<method> method_decl
%type<constant> constant_decl
%type<annotation> annotation
%type<annotation_list>annotation_list
%type<type> type
%type<unannotated_type> unannotated_type
%type<arg_list> arg_list
%type<arg> arg
%type<direction> direction
%type<str> generic_list
%type<qname> qualified_name

%type<token> identifier error
%%
document
 : package imports parcelable_decls
  { ps->SetDocument($3); }
 | package imports interface_decl
  { ps->SetDocument(new AidlDocument($3)); };

/* A couple of tokens that are keywords elsewhere are identifiers when
 * occurring in the identifier position. Therefore identifier is a
 * non-terminal, which is either an IDENTIFIER token, or one of the
 * aforementioned keyword tokens.
 */
identifier
 : IDENTIFIER
  { $$ = $1; }
 | CPP_HEADER
  { $$ = new AidlToken("cpp_header", ""); }
 | INT
  { $$ = new AidlToken("int", ""); }
 | STRING
  { $$ = new AidlToken("String", ""); }
 ;

package
 : {}
 | PACKAGE qualified_name ';'
  { ps->SetPackage($2); };

imports
 : {}
 | import imports {};

import
 : IMPORT qualified_name ';'
  { ps->AddImport($2, @1.begin.line); };

qualified_name
 : identifier {
    $$ = new AidlQualifiedName($1->GetText(), $1->GetComments());
    delete $1;
  }
 | qualified_name '.' identifier
  { $$ = $1;
    $$->AddTerm($3->GetText());
  };

parcelable_decls
 :
  { $$ = new AidlDocument(); }
 | parcelable_decls parcelable_decl {
   $$ = $1;
   $$->AddParcelable($2);
  }
 | parcelable_decls error {
    fprintf(stderr, "%s:%d: syntax error don't know what to do with \"%s\"\n",
            ps->FileName().c_str(),
            @2.begin.line, $2->GetText().c_str());
    $$ = $1;
  };

parcelable_decl
 : PARCELABLE qualified_name ';' {
    $$ = new AidlParcelable($2, @2.begin.line, ps->Package());
  }
 | PARCELABLE qualified_name CPP_HEADER C_STR ';' {
    $$ = new AidlParcelable($2, @2.begin.line, ps->Package(), $4->GetText());
  }
 | PARCELABLE ';' {
    fprintf(stderr, "%s:%d syntax error in parcelable declaration. Expected type name.\n",
            ps->FileName().c_str(), @1.begin.line);
    $$ = NULL;
  }
 | PARCELABLE error ';' {
    fprintf(stderr, "%s:%d syntax error in parcelable declaration. Expected type name, saw \"%s\".\n",
            ps->FileName().c_str(), @2.begin.line, $2->GetText().c_str());
    $$ = NULL;
  };

interface_decl
 : annotation_list INTERFACE identifier '{' members '}' {
    $$ = new AidlInterface($3->GetText(), @2.begin.line, $2->GetComments(),
                           false, $5, ps->Package());
    $$->Annotate($1);
    delete $2;
    delete $3;
  }
 | annotation_list ONEWAY INTERFACE identifier '{' members '}' {
    $$ = new AidlInterface($4->GetText(), @4.begin.line, $2->GetComments(),
                           true, $6, ps->Package());
    $$->Annotate($1);
    delete $2;
    delete $3;
    delete $4;
  }
 | annotation_list INTERFACE error '{' members '}' {
    fprintf(stderr, "%s:%d: syntax error in interface declaration.  Expected "
                    "type name, saw \"%s\"\n",
            ps->FileName().c_str(), @3.begin.line, $3->GetText().c_str());
    $$ = NULL;
    delete $2;
    delete $3;
    delete $5;
  }
 | annotation_list INTERFACE error '}' {
    fprintf(stderr, "%s:%d: syntax error in interface declaration.  Expected "
                    "type name, saw \"%s\"\n",
            ps->FileName().c_str(), @3.begin.line, $3->GetText().c_str());
    $$ = NULL;
    delete $2;
    delete $3;
  };

members
 :
  { $$ = new std::vector<std::unique_ptr<AidlMember>>(); }
 | members method_decl
  { $1->push_back(std::unique_ptr<AidlMember>($2)); }
 | members constant_decl
  { $1->push_back(std::unique_ptr<AidlMember>($2)); }
 | members error ';' {
    fprintf(stderr, "%s:%d: syntax error before ';' "
                    "(expected method or constant declaration)\n",
            ps->FileName().c_str(), @3.begin.line);
    $$ = $1;
  };

constant_decl
 : CONST INT identifier '=' INTVALUE ';' {
    $$ = new AidlIntConstant($3->GetText(), $5);
    delete $3;
   }
 | CONST STRING identifier '=' C_STR ';' {
    $$ = new AidlStringConstant($3->GetText(), $5->GetText(), @5.begin.line);
    delete $3;
    delete $5;
   }
 ;

method_decl
 : type identifier '(' arg_list ')' ';' {
    $$ = new AidlMethod(false, $1, $2->GetText(), $4, @2.begin.line,
                        $1->GetComments());
    delete $2;
  }
 | ONEWAY type identifier '(' arg_list ')' ';' {
    $$ = new AidlMethod(true, $2, $3->GetText(), $5, @3.begin.line,
                        $1->GetComments());
    delete $1;
    delete $3;
  }
 | type identifier '(' arg_list ')' '=' INTVALUE ';' {
    $$ = new AidlMethod(false, $1, $2->GetText(), $4, @2.begin.line,
                        $1->GetComments(), $7);
    delete $2;
  }
 | ONEWAY type identifier '(' arg_list ')' '=' INTVALUE ';' {
    $$ = new AidlMethod(true, $2, $3->GetText(), $5, @3.begin.line,
                        $1->GetComments(), $8);
    delete $1;
    delete $3;
  };

arg_list
 :
  { $$ = new std::vector<std::unique_ptr<AidlArgument>>(); }
 | arg {
    $$ = new std::vector<std::unique_ptr<AidlArgument>>();
    $$->push_back(std::unique_ptr<AidlArgument>($1));
  }
 | arg_list ',' arg {
    $$ = $1;
    $$->push_back(std::unique_ptr<AidlArgument>($3));
  }
 | error {
    fprintf(stderr, "%s:%d: syntax error in parameter list\n",
            ps->FileName().c_str(), @1.begin.line);
    $$ = new std::vector<std::unique_ptr<AidlArgument>>();
  };

arg
 : direction type identifier {
    $$ = new AidlArgument($1, $2, $3->GetText(), @3.begin.line);
    delete $3;
  };
 | type identifier {
    $$ = new AidlArgument($1, $2->GetText(), @2.begin.line);
    delete $2;
  };

unannotated_type
 : qualified_name {
    $$ = new AidlType($1->GetDotName(), @1.begin.line, $1->GetComments(), false);
    delete $1;
  }
 | qualified_name '[' ']' {
    $$ = new AidlType($1->GetDotName(), @1.begin.line, $1->GetComments(),
                      true);
    delete $1;
  }
 | qualified_name '<' generic_list '>' {
    $$ = new AidlType($1->GetDotName() + "<" + *$3 + ">", @1.begin.line,
                      $1->GetComments(), false);
    delete $1;
    delete $3;
  };

type
 : annotation_list unannotated_type {
    $$ = $2;
    $2->Annotate($1);
  };

generic_list
 : qualified_name {
    $$ = new std::string($1->GetDotName());
    delete $1;
  }
 | generic_list ',' qualified_name {
    $$ = new std::string(*$1 + "," + $3->GetDotName());
    delete $1;
    delete $3;
  };

annotation_list
 :
  { $$ = AidlType::AnnotationNone; }
 | annotation_list annotation
  { $$ = static_cast<AidlType::Annotation>($1 | $2); };

annotation
 : ANNOTATION_NULLABLE
  { $$ = AidlType::AnnotationNullable; }
 | ANNOTATION_UTF8
  { $$ = AidlType::AnnotationUtf8; }
 | ANNOTATION_UTF8_CPP
  { $$ = AidlType::AnnotationUtf8InCpp; };

direction
 : IN
  { $$ = AidlArgument::IN_DIR; }
 | OUT
  { $$ = AidlArgument::OUT_DIR; }
 | INOUT
  { $$ = AidlArgument::INOUT_DIR; };

%%

#include <ctype.h>
#include <stdio.h>

void yy::parser::error(const yy::parser::location_type& l,
                       const std::string& errstr) {
  ps->ReportError(errstr, l.begin.line);
}