Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 1 | %{ |
| 2 | |
| 3 | #include "AST.h" |
| 4 | #include "ArrayType.h" |
| 5 | #include "CompoundType.h" |
| 6 | #include "Constant.h" |
| 7 | #include "EnumType.h" |
| 8 | #include "Interface.h" |
| 9 | #include "Method.h" |
| 10 | #include "RefType.h" |
| 11 | #include "TypeDef.h" |
| 12 | #include "VectorType.h" |
| 13 | |
| 14 | #include "hidl-gen_y.h" |
| 15 | |
| 16 | #include <stdio.h> |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 17 | #include <utils/String8.h> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 18 | |
| 19 | using namespace android; |
| 20 | |
| 21 | extern int yylex(YYSTYPE *yylval_param, void *yyscanner); |
| 22 | extern int column; |
| 23 | |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 24 | int yyerror(AST *, const char *s) { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 25 | fflush(stdout); |
| 26 | printf("\n%*s\n%*s\n", column, "^", column, s); |
| 27 | |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | #define scanner ast->scanner() |
| 32 | |
| 33 | %} |
| 34 | |
| 35 | %parse-param { android::AST *ast } |
| 36 | %lex-param { void *scanner } |
| 37 | %pure-parser |
| 38 | |
| 39 | %token<str> CONST |
| 40 | %token<str> ENUM |
| 41 | %token<str> EXTENDS |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 42 | %token<str> FQNAME |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 43 | %token<str> GENERATES |
| 44 | %token<str> IDENTIFIER |
| 45 | %token<str> IMPORT |
| 46 | %token<str> INTEGER |
| 47 | %token<str> INTERFACE |
| 48 | %token<str> PACKAGE |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 49 | %token<type> SCALAR |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 50 | %token<str> STRUCT |
| 51 | %token<str> STRING_LITERAL |
| 52 | %token<str> TYPEDEF |
| 53 | %token<str> UNION |
| 54 | %token<str> VEC |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 55 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 56 | %type<str> optIdentifier package |
| 57 | %type<str> const_value |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 58 | %type<type> fqname |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 59 | |
| 60 | %type<type> type opt_storage_type |
| 61 | %type<type> enum_declaration |
| 62 | %type<type> struct_or_union_declaration |
| 63 | %type<type> opt_extends |
| 64 | |
| 65 | %type<field> field_declaration |
| 66 | %type<fields> field_declarations struct_or_union_body |
| 67 | %type<enumValue> enum_value |
| 68 | %type<enumValues> enum_values |
| 69 | %type<typedVars> typed_vars |
| 70 | %type<typedVar> typed_var |
| 71 | %type<method> method_declaration |
| 72 | %type<compoundStyle> struct_or_union_keyword |
| 73 | |
| 74 | %start program |
| 75 | |
| 76 | %union { |
| 77 | const char *str; |
| 78 | android::Type *type; |
| 79 | android::CompoundType *compoundType; |
| 80 | android::CompoundField *field; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 81 | std::vector<android::CompoundField *> *fields; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 82 | android::EnumValue *enumValue; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 83 | std::vector<android::EnumValue *> *enumValues; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 84 | android::TypedVar *typedVar; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 85 | std::vector<android::TypedVar *> *typedVars; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 86 | android::Method *method; |
| 87 | android::CompoundType::Style compoundStyle; |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 88 | android::Vector<std::string> *stringVec; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | %% |
| 92 | |
| 93 | program |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 94 | : package imports body |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 95 | ; |
| 96 | |
| 97 | fqname |
| 98 | : FQNAME |
| 99 | { |
| 100 | $$ = ast->lookupType($1); |
| 101 | if ($$ == NULL) { |
| 102 | yyerror(ast, |
| 103 | android::String8::format( |
| 104 | "Failed to lookup type '%s'.", $1).string()); |
| 105 | YYERROR; |
| 106 | } |
| 107 | } |
| 108 | | IDENTIFIER |
| 109 | { |
| 110 | $$ = ast->lookupType($1); |
| 111 | if ($$ == NULL) { |
| 112 | yyerror(ast, |
| 113 | android::String8::format( |
| 114 | "Failed to lookup type '%s'.", $1).string()); |
| 115 | YYERROR; |
| 116 | } |
| 117 | } |
| 118 | | SCALAR |
| 119 | ; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 120 | |
| 121 | package |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 122 | : PACKAGE FQNAME ';' |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 123 | { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 124 | if (!ast->setPackage($2)) { |
| 125 | yyerror(ast, "Malformed package identifier."); |
| 126 | YYERROR; |
| 127 | } |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 128 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 129 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 130 | imports |
| 131 | : /* empty */ |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 132 | | imports IMPORT FQNAME ';' |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 133 | { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 134 | if (!ast->addImport($3)) { |
| 135 | yyerror(ast, |
| 136 | android::String8::format( |
| 137 | "Unable to import '%s'.", $3)); |
| 138 | |
| 139 | YYERROR; |
| 140 | } |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 141 | } |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 142 | | imports IMPORT IDENTIFIER ';' |
| 143 | { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 144 | if (!ast->addImport($3)) { |
| 145 | yyerror(ast, |
| 146 | android::String8::format( |
| 147 | "Unable to import '%s'.", $3)); |
| 148 | |
| 149 | YYERROR; |
| 150 | } |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 151 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 152 | ; |
| 153 | |
| 154 | opt_extends |
| 155 | : /* empty */ { $$ = NULL; } |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 156 | | EXTENDS fqname { $$ = $2; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 157 | |
| 158 | body |
| 159 | : INTERFACE IDENTIFIER opt_extends |
| 160 | { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 161 | Interface *iface = new Interface($3); |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 162 | |
| 163 | // Register interface immediately so it can be referenced inside |
| 164 | // definition. |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame^] | 165 | if (!ast->addScopedType($2, iface)) { |
| 166 | YYERROR; |
| 167 | } |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 168 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 169 | ast->enterScope(iface); |
| 170 | } |
| 171 | '{' interface_declarations '}' ';' |
| 172 | { |
| 173 | Interface *iface = static_cast<Interface *>(ast->scope()); |
| 174 | |
| 175 | ast->leaveScope(); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 176 | } |
| 177 | | type_declarations |
| 178 | ; |
| 179 | |
| 180 | interface_declarations |
| 181 | : /* empty */ |
| 182 | | interface_declarations type_declaration |
| 183 | | interface_declarations method_declaration |
| 184 | { |
| 185 | Interface *iface = static_cast<Interface *>(ast->scope()); |
| 186 | iface->addMethod($2); |
| 187 | } |
| 188 | ; |
| 189 | |
| 190 | type_declarations |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 191 | : /* empty */ |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 192 | | type_declarations type_declaration |
| 193 | ; |
| 194 | |
| 195 | type_declaration |
| 196 | : named_struct_or_union_declaration ';' |
| 197 | | named_enum_declaration ';' |
| 198 | | typedef_declaration ';' |
| 199 | | const_declaration ';' |
| 200 | ; |
| 201 | |
| 202 | typedef_declaration |
| 203 | : TYPEDEF type IDENTIFIER |
| 204 | { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 205 | TypeDef *def = new TypeDef($2); |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame^] | 206 | if (!ast->addScopedType($3, def)) { |
| 207 | YYERROR; |
| 208 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 209 | } |
| 210 | ; |
| 211 | |
| 212 | const_value |
| 213 | : INTEGER |
| 214 | | STRING_LITERAL ; |
| 215 | |
| 216 | const_declaration |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 217 | : CONST fqname IDENTIFIER '=' const_value |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 218 | { |
| 219 | Constant *constant = new Constant($3, $2, $5); |
| 220 | ast->scope()->addConstant(constant); |
| 221 | } |
| 222 | ; |
| 223 | |
| 224 | method_declaration |
| 225 | : IDENTIFIER '(' typed_vars ')' ';' |
| 226 | { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 227 | $$ = new Method($1, $3, new std::vector<TypedVar *>); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 228 | } |
| 229 | | IDENTIFIER '(' typed_vars ')' GENERATES '(' typed_vars ')' ';' |
| 230 | { |
| 231 | $$ = new Method($1, $3, $7); |
| 232 | } |
| 233 | ; |
| 234 | |
| 235 | typed_vars |
| 236 | : /* empty */ |
| 237 | { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 238 | $$ = new std::vector<TypedVar *>; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 239 | } |
| 240 | | typed_var |
| 241 | { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 242 | $$ = new std::vector<TypedVar *>; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 243 | $$->push_back($1); |
| 244 | } |
| 245 | | typed_vars ',' typed_var |
| 246 | { |
| 247 | $$ = $1; |
| 248 | $$->push_back($3); |
| 249 | } |
| 250 | ; |
| 251 | |
| 252 | typed_var : type IDENTIFIER { $$ = new TypedVar($2, $1); } |
| 253 | ; |
| 254 | |
| 255 | |
| 256 | struct_or_union_keyword |
| 257 | : STRUCT { $$ = CompoundType::STYLE_STRUCT; } |
| 258 | | UNION { $$ = CompoundType::STYLE_UNION; } |
| 259 | ; |
| 260 | |
| 261 | named_struct_or_union_declaration |
| 262 | : struct_or_union_keyword IDENTIFIER |
| 263 | { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 264 | CompoundType *container = new CompoundType($1); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 265 | ast->enterScope(container); |
| 266 | } |
| 267 | struct_or_union_body |
| 268 | { |
| 269 | CompoundType *container = static_cast<CompoundType *>(ast->scope()); |
| 270 | |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame^] | 271 | if (!container->setFields($4)) { |
| 272 | YYERROR; |
| 273 | } |
| 274 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 275 | ast->leaveScope(); |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame^] | 276 | if (!ast->addScopedType($2, container)) { |
| 277 | YYERROR; |
| 278 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 279 | } |
| 280 | ; |
| 281 | |
| 282 | struct_or_union_declaration |
| 283 | : struct_or_union_keyword optIdentifier |
| 284 | { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 285 | CompoundType *container = new CompoundType($1); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 286 | ast->enterScope(container); |
| 287 | } |
| 288 | struct_or_union_body |
| 289 | { |
| 290 | CompoundType *container = static_cast<CompoundType *>(ast->scope()); |
| 291 | |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame^] | 292 | if (!container->setFields($4)) { |
| 293 | YYERROR; |
| 294 | } |
| 295 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 296 | ast->leaveScope(); |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame^] | 297 | if (!ast->addScopedType($2, container)) { |
| 298 | YYERROR; |
| 299 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 300 | |
Andreas Huber | 867fcb6 | 2016-08-02 16:26:34 -0700 | [diff] [blame] | 301 | $$ = new RefType(container); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 302 | } |
| 303 | ; |
| 304 | |
| 305 | struct_or_union_body |
| 306 | : '{' field_declarations '}' { $$ = $2; } |
| 307 | ; |
| 308 | |
| 309 | field_declarations |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 310 | : /* empty */ { $$ = new std::vector<CompoundField *>; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 311 | | field_declarations field_declaration |
| 312 | { |
| 313 | $$ = $1; |
| 314 | |
| 315 | if ($2 != NULL) { |
| 316 | $$->push_back($2); |
| 317 | } |
| 318 | } |
| 319 | ; |
| 320 | |
| 321 | field_declaration |
| 322 | : type IDENTIFIER ';' { $$ = new CompoundField($2, $1); } |
| 323 | | struct_or_union_declaration ';' { $$ = NULL; } |
| 324 | | enum_declaration ';' { $$ = NULL; } |
| 325 | ; |
| 326 | |
| 327 | opt_storage_type |
| 328 | : /* empty */ { $$ = NULL; } |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 329 | | ':' fqname { $$ = $2; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 330 | ; |
| 331 | |
| 332 | opt_comma |
| 333 | : /* empty */ |
| 334 | | ',' |
| 335 | ; |
| 336 | |
| 337 | named_enum_declaration |
| 338 | : ENUM IDENTIFIER opt_storage_type '{' enum_values opt_comma '}' |
| 339 | { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 340 | EnumType *enumType = new EnumType($5, $3); |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame^] | 341 | if (!ast->addScopedType($2, enumType)) { |
| 342 | YYERROR; |
| 343 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 344 | } |
| 345 | ; |
| 346 | |
| 347 | enum_declaration |
| 348 | : ENUM '{' enum_values opt_comma '}' |
| 349 | { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 350 | EnumType *enumType = new EnumType($3); |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame^] | 351 | if (!ast->addScopedType(NULL /* localName */, enumType)) { |
| 352 | YYERROR; |
| 353 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 354 | |
Andreas Huber | 867fcb6 | 2016-08-02 16:26:34 -0700 | [diff] [blame] | 355 | $$ = new RefType(enumType); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 356 | } |
| 357 | | ENUM IDENTIFIER opt_storage_type '{' enum_values opt_comma '}' |
| 358 | { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 359 | EnumType *enumType = new EnumType($5, $3); |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame^] | 360 | if (!ast->addScopedType($2, enumType)) { |
| 361 | YYERROR; |
| 362 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 363 | |
Andreas Huber | 867fcb6 | 2016-08-02 16:26:34 -0700 | [diff] [blame] | 364 | $$ = new RefType(enumType); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 365 | } |
| 366 | ; |
| 367 | |
| 368 | enum_value |
| 369 | : IDENTIFIER { $$ = new EnumValue($1); } |
| 370 | | IDENTIFIER '=' INTEGER { $$ = new EnumValue($1, $3); } |
| 371 | | IDENTIFIER '=' IDENTIFIER { $$ = new EnumValue($1, $3); } |
Andreas Huber | 006189f | 2016-07-29 15:51:17 -0700 | [diff] [blame] | 372 | | IDENTIFIER '=' IDENTIFIER '+' INTEGER |
| 373 | { |
| 374 | $$ = new EnumValue( |
| 375 | $1, strdup( |
| 376 | android::String8::format("%s + %s", $3, $5).string())); |
| 377 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 378 | ; |
| 379 | |
| 380 | enum_values |
| 381 | : /* empty */ |
| 382 | { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 383 | $$ = new std::vector<EnumValue *>; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 384 | } |
| 385 | | enum_value |
| 386 | { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 387 | $$ = new std::vector<EnumValue *>; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 388 | $$->push_back($1); |
| 389 | } |
| 390 | | enum_values ',' enum_value |
| 391 | { |
| 392 | $$ = $1; |
| 393 | $$->push_back($3); |
| 394 | } |
| 395 | ; |
| 396 | |
| 397 | type |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 398 | : fqname { $$ = $1; } |
| 399 | | fqname '[' INTEGER ']' { $$ = new ArrayType($1, $3); } |
| 400 | | VEC '<' fqname '>' { $$ = new VectorType($3); } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 401 | | struct_or_union_declaration { $$ = $1; } |
| 402 | | enum_declaration { $$ = $1; } |
| 403 | ; |
| 404 | |
| 405 | optIdentifier |
| 406 | : /* empty */ { $$ = NULL; } |
| 407 | | IDENTIFIER { $$ = $1; } |
| 408 | ; |
| 409 | |
| 410 | %% |