blob: 05a91fd363e3770f93354b91e3e87dae741cf0e1 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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 Huberc9410c72016-07-28 12:18:40 -070017D [0-9]
18L [a-zA-Z_]
19H [a-fA-F0-9]
20E [Ee][+-]?{D}+
21FS (f|F|l|L)
22IS (u|U|l|L)*
23
Andreas Huber84f89de2016-07-28 15:39:51 -070024COMPONENT {L}({L}|{D})*
25DOT [.]
26PATH {COMPONENT}({DOT}{COMPONENT})*
27AT [@]
28VERSION {AT}{D}+{DOT}{D}+
29
Andreas Huberc9410c72016-07-28 12:18:40 -070030%{
31
Andreas Huber3599d922016-08-09 10:42:57 -070032#include "Annotation.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070033#include "AST.h"
Yifan Hongbd33e382016-11-02 13:30:17 -070034#include "ArrayType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070035#include "CompoundType.h"
Yifan Hong52165692016-08-12 18:06:40 -070036#include "ConstantExpression.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070037#include "EnumType.h"
38#include "HandleType.h"
Martijn Coenen99e6beb2016-12-01 15:48:42 +010039#include "MemoryType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070040#include "Method.h"
Martijn Coenen99e6beb2016-12-01 15:48:42 +010041#include "PointerType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070042#include "ScalarType.h"
43#include "StringType.h"
Yifan Hongbf459bc2016-08-23 16:50:37 -070044#include "VectorType.h"
45#include "RefType.h"
Hridya Valsarajucd91bf62016-10-25 12:41:04 -070046#include "PredefinedType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070047
48#include "hidl-gen_y.h"
49
50#include <assert.h>
51
52using namespace android;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070053using token = yy::parser::token;
Andreas Huberc9410c72016-07-28 12:18:40 -070054
Andreas Huberc9410c72016-07-28 12:18:40 -070055int check_type(yyscan_t yyscanner, struct yyguts_t *yyg);
56
57#define SCALAR_TYPE(kind) \
58 do { \
Andreas Huberc9410c72016-07-28 12:18:40 -070059 yylval->type = new ScalarType(ScalarType::kind); \
Hridya Valsarajucd91bf62016-10-25 12:41:04 -070060 return token::TYPE; \
Andreas Huberc9410c72016-07-28 12:18:40 -070061 } while (0)
62
Andreas Huber0d0f9a22016-08-17 10:26:11 -070063#define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng);
64
65#pragma clang diagnostic push
66#pragma clang diagnostic ignored "-Wunused-parameter"
Steven Moreland191c1fe2016-09-13 13:05:46 -070067#pragma clang diagnostic ignored "-Wdeprecated-register"
Andreas Huber0d0f9a22016-08-17 10:26:11 -070068
Andreas Huberc9410c72016-07-28 12:18:40 -070069%}
70
Andreas Huber0d0f9a22016-08-17 10:26:11 -070071%option yylineno
72%option noyywrap
Andreas Huberc9410c72016-07-28 12:18:40 -070073%option reentrant
74%option bison-bridge
Andreas Huber0d0f9a22016-08-17 10:26:11 -070075%option bison-locations
Andreas Huberc9410c72016-07-28 12:18:40 -070076
Steven Moreland605c4212016-09-06 10:19:53 -070077%x COMMENT_STATE
78
Andreas Huberc9410c72016-07-28 12:18:40 -070079%%
80
Steven Moreland605c4212016-09-06 10:19:53 -070081"/*" { BEGIN(COMMENT_STATE); }
82<COMMENT_STATE>"*/" { BEGIN(INITIAL); }
83<COMMENT_STATE>[\n] { yylloc->lines(); }
84<COMMENT_STATE>. { }
85
86"//"[^\r\n]* { /* skip C++ style comment */ }
Andreas Huberc9410c72016-07-28 12:18:40 -070087
Andreas Huber0d0f9a22016-08-17 10:26:11 -070088"enum" { return token::ENUM; }
89"extends" { return token::EXTENDS; }
90"generates" { return token::GENERATES; }
91"import" { return token::IMPORT; }
92"interface" { return token::INTERFACE; }
93"package" { return token::PACKAGE; }
94"struct" { return token::STRUCT; }
95"typedef" { return token::TYPEDEF; }
96"union" { return token::UNION; }
Yifan Hongbf459bc2016-08-23 16:50:37 -070097"vec" { yylval->templatedType = new VectorType; return token::TEMPLATED; }
98"ref" { yylval->templatedType = new RefType; return token::TEMPLATED; }
Andreas Huber0d0f9a22016-08-17 10:26:11 -070099"oneway" { return token::ONEWAY; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700100
Andreas Huberc9410c72016-07-28 12:18:40 -0700101"bool" { SCALAR_TYPE(KIND_BOOL); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700102"int8_t" { SCALAR_TYPE(KIND_INT8); }
103"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
104"int16_t" { SCALAR_TYPE(KIND_INT16); }
105"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
106"int32_t" { SCALAR_TYPE(KIND_INT32); }
107"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
108"int64_t" { SCALAR_TYPE(KIND_INT64); }
109"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
110"float" { SCALAR_TYPE(KIND_FLOAT); }
111"double" { SCALAR_TYPE(KIND_DOUBLE); }
112
Hridya Valsarajucd91bf62016-10-25 12:41:04 -0700113"handle" { yylval->type = new HandleType; return token::TYPE; }
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100114"memory" { yylval->type = new MemoryType; return token::TYPE; }
115"pointer" { yylval->type = new PointerType; return token::TYPE; }
Hridya Valsarajucd91bf62016-10-25 12:41:04 -0700116"string" { yylval->type = new StringType; return token::TYPE; }
117
Yifan Hong244e82d2016-11-11 11:13:57 -0800118"MQDescriptorSync" { yylval->type = new PredefinedType("::android::hardware", "MQDescriptorSync"); return token::TYPE; }
119"MQDescriptorUnsync" { yylval->type = new PredefinedType("::android::hardware", "MQDescriptorUnsync"); return token::TYPE; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700120
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700121"(" { 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('/'); }
138"%" { return('%'); }
139"&" { return('&'); }
140"|" { return('|'); }
141"^" { return('^'); }
142"<<" { return(token::LSHIFT); }
143">>" { return(token::RSHIFT); }
144"&&" { return(token::LOGICAL_AND); }
145"||" { return(token::LOGICAL_OR); }
146"!" { return('!'); }
147"~" { return('~'); }
148"<=" { return(token::LEQ); }
149">=" { return(token::GEQ); }
150"==" { return(token::EQUALITY); }
151"!=" { return(token::NEQ); }
152"?" { return('?'); }
153"@" { return('@'); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700154
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700155{PATH}{VERSION}?"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
156{VERSION}"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
157{PATH}{VERSION} { yylval->str = strdup(yytext); return token::FQNAME; }
158{COMPONENT}({DOT}{COMPONENT})+ { yylval->str = strdup(yytext); return token::FQNAME; }
159{COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700160
Yifan Hongb44a6c82016-09-22 15:50:18 -0700161{PATH}{VERSION}?"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
162{VERSION}"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
163{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
164
Andreas Huber0d0f9a22016-08-17 10:26:11 -07001650[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
1660{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
167{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
168L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; }
Yifan Hong52165692016-08-12 18:06:40 -0700169
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700170{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 Hong52165692016-08-12 18:06:40 -0700173
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700174[\n] { yylloc->lines(); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700175. { /* ignore bad characters */ }
176
177%%
178
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700179#pragma clang diagnostic pop
Andreas Huberc9410c72016-07-28 12:18:40 -0700180
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700181status_t parseFile(AST *ast) {
182 FILE *file = fopen(ast->getFilename().c_str(), "rb");
Andreas Huberc9410c72016-07-28 12:18:40 -0700183
Andreas Huber68f24592016-07-29 14:53:48 -0700184 if (file == NULL) {
185 return -errno;
186 }
187
Andreas Huberc9410c72016-07-28 12:18:40 -0700188 yyscan_t scanner;
189 yylex_init_extra(ast, &scanner);
190 ast->setScanner(scanner);
191
192 yyset_in(file, scanner);
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700193 int res = yy::parser(ast).parse();
Andreas Huberc9410c72016-07-28 12:18:40 -0700194
195 yylex_destroy(scanner);
196 ast->setScanner(NULL);
197
198 fclose(file);
199 file = NULL;
Andreas Huber68f24592016-07-29 14:53:48 -0700200
Yifan Hongbe627b32016-10-28 18:38:56 -0700201 if (res != 0 || ast->syntaxErrors() != 0) {
Andreas Huber68f24592016-07-29 14:53:48 -0700202 return UNKNOWN_ERROR;
203 }
204
205 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700206}