Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Steven Moreland | d13d08f | 2017-09-07 10:56:43 -0700 | [diff] [blame] | 17 | D [0-9] |
| 18 | L [a-zA-Z_] |
| 19 | H [a-fA-F0-9] |
| 20 | E [Ee][+-]?{D}+ |
| 21 | FS (f|F|l|L) |
| 22 | IS (u|U|l|L)* |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 23 | |
Steven Moreland | d13d08f | 2017-09-07 10:56:43 -0700 | [diff] [blame] | 24 | COMPONENT {L}({L}|{D})* |
| 25 | DOT [.] |
| 26 | AT [@] |
| 27 | VERSION {AT}{D}+{DOT}{D}+ |
| 28 | FQNAME ({COMPONENT}|{VERSION})(({DOT}|":"+){COMPONENT}|{VERSION})* |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 29 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 30 | %{ |
| 31 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 32 | #include "Annotation.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 33 | #include "AST.h" |
Yifan Hong | bd33e38 | 2016-11-02 13:30:17 -0700 | [diff] [blame] | 34 | #include "ArrayType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 35 | #include "CompoundType.h" |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 36 | #include "ConstantExpression.h" |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 37 | #include "DeathRecipientType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 38 | #include "EnumType.h" |
| 39 | #include "HandleType.h" |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 40 | #include "MemoryType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 41 | #include "Method.h" |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 42 | #include "PointerType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 43 | #include "ScalarType.h" |
Timur Iskhakov | 63f3990 | 2017-08-29 15:47:29 -0700 | [diff] [blame] | 44 | #include "Scope.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 45 | #include "StringType.h" |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 46 | #include "VectorType.h" |
| 47 | #include "RefType.h" |
Hridya Valsaraju | a32bde8 | 2016-12-27 11:47:46 -0800 | [diff] [blame] | 48 | #include "FmqType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 49 | |
| 50 | #include "hidl-gen_y.h" |
| 51 | |
| 52 | #include <assert.h> |
| 53 | |
| 54 | using namespace android; |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 55 | using token = yy::parser::token; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 56 | |
Timur Iskhakov | 63f3990 | 2017-08-29 15:47:29 -0700 | [diff] [blame] | 57 | #define SCALAR_TYPE(kind) \ |
| 58 | { \ |
| 59 | yylval->type = new ScalarType(ScalarType::kind, *scope); \ |
| 60 | return token::TYPE; \ |
| 61 | } |
| 62 | |
| 63 | #define YY_DECL int yylex(YYSTYPE* yylval_param, YYLTYPE* yylloc_param, \ |
| 64 | yyscan_t yyscanner, android::Scope** const scope) |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 65 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 66 | #define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng); |
| 67 | |
| 68 | #pragma clang diagnostic push |
| 69 | #pragma clang diagnostic ignored "-Wunused-parameter" |
Steven Moreland | 191c1fe | 2016-09-13 13:05:46 -0700 | [diff] [blame] | 70 | #pragma clang diagnostic ignored "-Wdeprecated-register" |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 71 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 72 | %} |
| 73 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 74 | %option yylineno |
| 75 | %option noyywrap |
Steven Moreland | 6081863 | 2017-02-04 00:33:42 -0800 | [diff] [blame] | 76 | %option nounput |
| 77 | %option noinput |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 78 | %option reentrant |
| 79 | %option bison-bridge |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 80 | %option bison-locations |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 81 | |
Steven Moreland | 605c421 | 2016-09-06 10:19:53 -0700 | [diff] [blame] | 82 | %x COMMENT_STATE |
| 83 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 84 | %% |
| 85 | |
Steven Moreland | d13d08f | 2017-09-07 10:56:43 -0700 | [diff] [blame] | 86 | "/*" { BEGIN(COMMENT_STATE); } |
| 87 | <COMMENT_STATE>"*/" { BEGIN(INITIAL); } |
| 88 | <COMMENT_STATE>[\n] { yylloc->lines(); } |
| 89 | <COMMENT_STATE>. { } |
Steven Moreland | 605c421 | 2016-09-06 10:19:53 -0700 | [diff] [blame] | 90 | |
Steven Moreland | d13d08f | 2017-09-07 10:56:43 -0700 | [diff] [blame] | 91 | "//"[^\r\n]* { /* skip C++ style comment */ } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 92 | |
Steven Moreland | d13d08f | 2017-09-07 10:56:43 -0700 | [diff] [blame] | 93 | "enum" { return token::ENUM; } |
| 94 | "extends" { return token::EXTENDS; } |
| 95 | "generates" { return token::GENERATES; } |
| 96 | "import" { return token::IMPORT; } |
| 97 | "interface" { return token::INTERFACE; } |
| 98 | "package" { return token::PACKAGE; } |
| 99 | "struct" { return token::STRUCT; } |
| 100 | "typedef" { return token::TYPEDEF; } |
| 101 | "union" { return token::UNION; } |
| 102 | "bitfield" { yylval->templatedType = new BitFieldType(*scope); return token::TEMPLATED; } |
| 103 | "vec" { yylval->templatedType = new VectorType(*scope); return token::TEMPLATED; } |
| 104 | "ref" { yylval->templatedType = new RefType(*scope); return token::TEMPLATED; } |
| 105 | "oneway" { return token::ONEWAY; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 106 | |
Steven Moreland | d13d08f | 2017-09-07 10:56:43 -0700 | [diff] [blame] | 107 | "bool" { SCALAR_TYPE(KIND_BOOL); } |
| 108 | "int8_t" { SCALAR_TYPE(KIND_INT8); } |
| 109 | "uint8_t" { SCALAR_TYPE(KIND_UINT8); } |
| 110 | "int16_t" { SCALAR_TYPE(KIND_INT16); } |
| 111 | "uint16_t" { SCALAR_TYPE(KIND_UINT16); } |
| 112 | "int32_t" { SCALAR_TYPE(KIND_INT32); } |
| 113 | "uint32_t" { SCALAR_TYPE(KIND_UINT32); } |
| 114 | "int64_t" { SCALAR_TYPE(KIND_INT64); } |
| 115 | "uint64_t" { SCALAR_TYPE(KIND_UINT64); } |
| 116 | "float" { SCALAR_TYPE(KIND_FLOAT); } |
| 117 | "double" { SCALAR_TYPE(KIND_DOUBLE); } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 118 | |
Steven Moreland | d13d08f | 2017-09-07 10:56:43 -0700 | [diff] [blame] | 119 | "death_recipient" { yylval->type = new DeathRecipientType(*scope); return token::TYPE; } |
| 120 | "handle" { yylval->type = new HandleType(*scope); return token::TYPE; } |
| 121 | "memory" { yylval->type = new MemoryType(*scope); return token::TYPE; } |
| 122 | "pointer" { yylval->type = new PointerType(*scope); return token::TYPE; } |
| 123 | "string" { yylval->type = new StringType(*scope); return token::TYPE; } |
Hridya Valsaraju | cd91bf6 | 2016-10-25 12:41:04 -0700 | [diff] [blame] | 124 | |
Steven Moreland | d13d08f | 2017-09-07 10:56:43 -0700 | [diff] [blame] | 125 | "fmq_sync" { yylval->type = new FmqType("::android::hardware", "MQDescriptorSync", *scope); return token::TEMPLATED; } |
| 126 | "fmq_unsync" { yylval->type = new FmqType("::android::hardware", "MQDescriptorUnsync", *scope); return token::TEMPLATED; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 127 | |
Steven Moreland | d13d08f | 2017-09-07 10:56:43 -0700 | [diff] [blame] | 128 | "(" { return('('); } |
| 129 | ")" { return(')'); } |
| 130 | "<" { return('<'); } |
| 131 | ">" { return('>'); } |
| 132 | "{" { return('{'); } |
| 133 | "}" { return('}'); } |
| 134 | "[" { return('['); } |
| 135 | "]" { return(']'); } |
| 136 | ":" { return(':'); } |
| 137 | ";" { return(';'); } |
| 138 | "," { return(','); } |
| 139 | "." { return('.'); } |
| 140 | "=" { return('='); } |
| 141 | "+" { return('+'); } |
| 142 | "-" { return('-'); } |
| 143 | "*" { return('*'); } |
| 144 | "/" { return('/'); } |
| 145 | "%" { return('%'); } |
| 146 | "&" { return('&'); } |
| 147 | "|" { return('|'); } |
| 148 | "^" { return('^'); } |
| 149 | "<<" { return(token::LSHIFT); } |
| 150 | ">>" { return(token::RSHIFT); } |
| 151 | "&&" { return(token::LOGICAL_AND); } |
| 152 | "||" { return(token::LOGICAL_OR); } |
| 153 | "!" { return('!'); } |
| 154 | "~" { return('~'); } |
| 155 | "<=" { return(token::LEQ); } |
| 156 | ">=" { return(token::GEQ); } |
| 157 | "==" { return(token::EQUALITY); } |
| 158 | "!=" { return(token::NEQ); } |
| 159 | "?" { return('?'); } |
| 160 | "@" { return('@'); } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 161 | |
Steven Moreland | d13d08f | 2017-09-07 10:56:43 -0700 | [diff] [blame] | 162 | {COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; } |
| 163 | {FQNAME} { yylval->str = strdup(yytext); return token::FQNAME; } |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 164 | |
Steven Moreland | d13d08f | 2017-09-07 10:56:43 -0700 | [diff] [blame] | 165 | 0[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; } |
| 166 | 0{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; } |
| 167 | {D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; } |
| 168 | L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; } |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 169 | |
Steven Moreland | d13d08f | 2017-09-07 10:56:43 -0700 | [diff] [blame] | 170 | {D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; } |
| 171 | {D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; } |
| 172 | {D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; } |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 173 | |
Steven Moreland | d13d08f | 2017-09-07 10:56:43 -0700 | [diff] [blame] | 174 | \n|\r\n { yylloc->lines(); } |
| 175 | [ \t\f\v] { /* ignore all other whitespace */ } |
Steven Moreland | 7a4c662 | 2017-09-07 10:32:20 -0700 | [diff] [blame] | 176 | |
Steven Moreland | d13d08f | 2017-09-07 10:56:43 -0700 | [diff] [blame] | 177 | . { yylval->str = strdup(yytext); return token::UNKNOWN; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 178 | |
| 179 | %% |
| 180 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 181 | #pragma clang diagnostic pop |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 182 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 183 | status_t parseFile(AST *ast) { |
| 184 | FILE *file = fopen(ast->getFilename().c_str(), "rb"); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 185 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 186 | if (file == nullptr) { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 187 | return -errno; |
| 188 | } |
| 189 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 190 | yyscan_t scanner; |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 191 | yylex_init(&scanner); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 192 | |
| 193 | yyset_in(file, scanner); |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 194 | |
| 195 | Scope* scopeStack = ast->getRootScope(); |
| 196 | int res = yy::parser(scanner, ast, &scopeStack).parse(); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 197 | |
| 198 | yylex_destroy(scanner); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 199 | |
| 200 | fclose(file); |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 201 | file = nullptr; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 202 | |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 203 | if (res != 0 || ast->syntaxErrors() != 0) { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 204 | return UNKNOWN_ERROR; |
| 205 | } |
| 206 | |
| 207 | return OK; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 208 | } |