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 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -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)* |
| 23 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 24 | COMPONENT {L}({L}|{D})* |
| 25 | DOT [.] |
| 26 | PATH {COMPONENT}({DOT}{COMPONENT})* |
| 27 | AT [@] |
| 28 | VERSION {AT}{D}+{DOT}{D}+ |
| 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" |
| 34 | #include "CompoundType.h" |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 35 | #include "ConstantExpression.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 36 | #include "EnumType.h" |
| 37 | #include "HandleType.h" |
| 38 | #include "Method.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 39 | #include "ScalarType.h" |
| 40 | #include "StringType.h" |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 41 | #include "VectorType.h" |
| 42 | #include "RefType.h" |
Hridya Valsaraju | cd91bf6 | 2016-10-25 12:41:04 -0700 | [diff] [blame^] | 43 | #include "PredefinedType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 44 | |
| 45 | #include "hidl-gen_y.h" |
| 46 | |
| 47 | #include <assert.h> |
| 48 | |
| 49 | using namespace android; |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 50 | using token = yy::parser::token; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 51 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 52 | int check_type(yyscan_t yyscanner, struct yyguts_t *yyg); |
| 53 | |
| 54 | #define SCALAR_TYPE(kind) \ |
| 55 | do { \ |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 56 | yylval->type = new ScalarType(ScalarType::kind); \ |
Hridya Valsaraju | cd91bf6 | 2016-10-25 12:41:04 -0700 | [diff] [blame^] | 57 | return token::TYPE; \ |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 58 | } while (0) |
| 59 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 60 | #define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng); |
| 61 | |
| 62 | #pragma clang diagnostic push |
| 63 | #pragma clang diagnostic ignored "-Wunused-parameter" |
Steven Moreland | 191c1fe | 2016-09-13 13:05:46 -0700 | [diff] [blame] | 64 | #pragma clang diagnostic ignored "-Wdeprecated-register" |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 65 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 66 | %} |
| 67 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 68 | %option yylineno |
| 69 | %option noyywrap |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 70 | %option reentrant |
| 71 | %option bison-bridge |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 72 | %option bison-locations |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 73 | |
Steven Moreland | 605c421 | 2016-09-06 10:19:53 -0700 | [diff] [blame] | 74 | %x COMMENT_STATE |
| 75 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 76 | %% |
| 77 | |
Steven Moreland | 605c421 | 2016-09-06 10:19:53 -0700 | [diff] [blame] | 78 | "/*" { BEGIN(COMMENT_STATE); } |
| 79 | <COMMENT_STATE>"*/" { BEGIN(INITIAL); } |
| 80 | <COMMENT_STATE>[\n] { yylloc->lines(); } |
| 81 | <COMMENT_STATE>. { } |
| 82 | |
| 83 | "//"[^\r\n]* { /* skip C++ style comment */ } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 84 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 85 | "enum" { return token::ENUM; } |
| 86 | "extends" { return token::EXTENDS; } |
| 87 | "generates" { return token::GENERATES; } |
| 88 | "import" { return token::IMPORT; } |
| 89 | "interface" { return token::INTERFACE; } |
| 90 | "package" { return token::PACKAGE; } |
| 91 | "struct" { return token::STRUCT; } |
| 92 | "typedef" { return token::TYPEDEF; } |
| 93 | "union" { return token::UNION; } |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 94 | "vec" { yylval->templatedType = new VectorType; return token::TEMPLATED; } |
| 95 | "ref" { yylval->templatedType = new RefType; return token::TEMPLATED; } |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 96 | "oneway" { return token::ONEWAY; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 97 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 98 | "bool" { SCALAR_TYPE(KIND_BOOL); } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 99 | "int8_t" { SCALAR_TYPE(KIND_INT8); } |
| 100 | "uint8_t" { SCALAR_TYPE(KIND_UINT8); } |
| 101 | "int16_t" { SCALAR_TYPE(KIND_INT16); } |
| 102 | "uint16_t" { SCALAR_TYPE(KIND_UINT16); } |
| 103 | "int32_t" { SCALAR_TYPE(KIND_INT32); } |
| 104 | "uint32_t" { SCALAR_TYPE(KIND_UINT32); } |
| 105 | "int64_t" { SCALAR_TYPE(KIND_INT64); } |
| 106 | "uint64_t" { SCALAR_TYPE(KIND_UINT64); } |
| 107 | "float" { SCALAR_TYPE(KIND_FLOAT); } |
| 108 | "double" { SCALAR_TYPE(KIND_DOUBLE); } |
| 109 | |
Hridya Valsaraju | cd91bf6 | 2016-10-25 12:41:04 -0700 | [diff] [blame^] | 110 | "handle" { yylval->type = new HandleType; return token::TYPE; } |
| 111 | "string" { yylval->type = new StringType; return token::TYPE; } |
| 112 | |
| 113 | "MQDescriptorSync" { yylval->type = new PredefinedType("::android::hardware::MQDescriptorSync"); return token::TYPE; } |
| 114 | "MQDescriptorUnsync" { yylval->type = new PredefinedType("::android::hardware::MQDescriptorUnsync"); return token::TYPE; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 115 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 116 | "(" { return('('); } |
| 117 | ")" { return(')'); } |
| 118 | "<" { return('<'); } |
| 119 | ">" { return('>'); } |
| 120 | "{" { return('{'); } |
| 121 | "}" { return('}'); } |
| 122 | "[" { return('['); } |
| 123 | "]" { return(']'); } |
| 124 | ":" { return(':'); } |
| 125 | ";" { return(';'); } |
| 126 | "," { return(','); } |
| 127 | "." { return('.'); } |
| 128 | "=" { return('='); } |
| 129 | "+" { return('+'); } |
| 130 | "-" { return('-'); } |
| 131 | "*" { return('*'); } |
| 132 | "/" { return('/'); } |
| 133 | "%" { return('%'); } |
| 134 | "&" { return('&'); } |
| 135 | "|" { return('|'); } |
| 136 | "^" { return('^'); } |
| 137 | "<<" { return(token::LSHIFT); } |
| 138 | ">>" { return(token::RSHIFT); } |
| 139 | "&&" { return(token::LOGICAL_AND); } |
| 140 | "||" { return(token::LOGICAL_OR); } |
| 141 | "!" { return('!'); } |
| 142 | "~" { return('~'); } |
| 143 | "<=" { return(token::LEQ); } |
| 144 | ">=" { return(token::GEQ); } |
| 145 | "==" { return(token::EQUALITY); } |
| 146 | "!=" { return(token::NEQ); } |
| 147 | "?" { return('?'); } |
| 148 | "@" { return('@'); } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 149 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 150 | {PATH}{VERSION}?"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; } |
| 151 | {VERSION}"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; } |
| 152 | {PATH}{VERSION} { yylval->str = strdup(yytext); return token::FQNAME; } |
| 153 | {COMPONENT}({DOT}{COMPONENT})+ { yylval->str = strdup(yytext); return token::FQNAME; } |
| 154 | {COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 155 | |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 156 | {PATH}{VERSION}?"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; } |
| 157 | {VERSION}"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; } |
| 158 | {PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; } |
| 159 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 160 | 0[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; } |
| 161 | 0{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; } |
| 162 | {D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; } |
| 163 | L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; } |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 164 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 165 | {D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; } |
| 166 | {D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; } |
| 167 | {D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; } |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 168 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 169 | [\n] { yylloc->lines(); } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 170 | . { /* ignore bad characters */ } |
| 171 | |
| 172 | %% |
| 173 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 174 | #pragma clang diagnostic pop |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 175 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 176 | status_t parseFile(AST *ast) { |
| 177 | FILE *file = fopen(ast->getFilename().c_str(), "rb"); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 178 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 179 | if (file == NULL) { |
| 180 | return -errno; |
| 181 | } |
| 182 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 183 | yyscan_t scanner; |
| 184 | yylex_init_extra(ast, &scanner); |
| 185 | ast->setScanner(scanner); |
| 186 | |
| 187 | yyset_in(file, scanner); |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 188 | int res = yy::parser(ast).parse(); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 189 | |
| 190 | yylex_destroy(scanner); |
| 191 | ast->setScanner(NULL); |
| 192 | |
| 193 | fclose(file); |
| 194 | file = NULL; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 195 | |
| 196 | if (res != 0) { |
| 197 | return UNKNOWN_ERROR; |
| 198 | } |
| 199 | |
| 200 | return OK; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 201 | } |