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