blob: ce12aa7ddbfd395a290032134ccde29211b32af7 [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"
34#include "CompoundType.h"
Yifan Hong52165692016-08-12 18:06:40 -070035#include "ConstantExpression.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070036#include "EnumType.h"
37#include "HandleType.h"
38#include "Method.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070039#include "ScalarType.h"
40#include "StringType.h"
Yifan Hongbf459bc2016-08-23 16:50:37 -070041#include "VectorType.h"
42#include "RefType.h"
Hridya Valsarajucd91bf62016-10-25 12:41:04 -070043#include "PredefinedType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070044
45#include "hidl-gen_y.h"
46
47#include <assert.h>
48
49using namespace android;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070050using token = yy::parser::token;
Andreas Huberc9410c72016-07-28 12:18:40 -070051
Andreas Huberc9410c72016-07-28 12:18:40 -070052int check_type(yyscan_t yyscanner, struct yyguts_t *yyg);
53
54#define SCALAR_TYPE(kind) \
55 do { \
Andreas Huberc9410c72016-07-28 12:18:40 -070056 yylval->type = new ScalarType(ScalarType::kind); \
Hridya Valsarajucd91bf62016-10-25 12:41:04 -070057 return token::TYPE; \
Andreas Huberc9410c72016-07-28 12:18:40 -070058 } while (0)
59
Andreas Huber0d0f9a22016-08-17 10:26:11 -070060#define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng);
61
62#pragma clang diagnostic push
63#pragma clang diagnostic ignored "-Wunused-parameter"
Steven Moreland191c1fe2016-09-13 13:05:46 -070064#pragma clang diagnostic ignored "-Wdeprecated-register"
Andreas Huber0d0f9a22016-08-17 10:26:11 -070065
Andreas Huberc9410c72016-07-28 12:18:40 -070066%}
67
Andreas Huber0d0f9a22016-08-17 10:26:11 -070068%option yylineno
69%option noyywrap
Andreas Huberc9410c72016-07-28 12:18:40 -070070%option reentrant
71%option bison-bridge
Andreas Huber0d0f9a22016-08-17 10:26:11 -070072%option bison-locations
Andreas Huberc9410c72016-07-28 12:18:40 -070073
Steven Moreland605c4212016-09-06 10:19:53 -070074%x COMMENT_STATE
75
Andreas Huberc9410c72016-07-28 12:18:40 -070076%%
77
Steven Moreland605c4212016-09-06 10:19:53 -070078"/*" { 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 Huberc9410c72016-07-28 12:18:40 -070084
Andreas Huber0d0f9a22016-08-17 10:26:11 -070085"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 Hongbf459bc2016-08-23 16:50:37 -070094"vec" { yylval->templatedType = new VectorType; return token::TEMPLATED; }
95"ref" { yylval->templatedType = new RefType; return token::TEMPLATED; }
Andreas Huber0d0f9a22016-08-17 10:26:11 -070096"oneway" { return token::ONEWAY; }
Andreas Huberc9410c72016-07-28 12:18:40 -070097
Andreas Huberc9410c72016-07-28 12:18:40 -070098"bool" { SCALAR_TYPE(KIND_BOOL); }
Andreas Huberc9410c72016-07-28 12:18:40 -070099"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 Valsarajucd91bf62016-10-25 12:41:04 -0700110"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 Huberc9410c72016-07-28 12:18:40 -0700115
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700116"(" { 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 Huberc9410c72016-07-28 12:18:40 -0700149
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700150{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 Huberc9410c72016-07-28 12:18:40 -0700155
Yifan Hongb44a6c82016-09-22 15:50:18 -0700156{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 Huber0d0f9a22016-08-17 10:26:11 -07001600[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
1610{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
162{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
163L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; }
Yifan Hong52165692016-08-12 18:06:40 -0700164
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700165{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 Hong52165692016-08-12 18:06:40 -0700168
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700169[\n] { yylloc->lines(); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700170. { /* ignore bad characters */ }
171
172%%
173
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700174#pragma clang diagnostic pop
Andreas Huberc9410c72016-07-28 12:18:40 -0700175
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700176status_t parseFile(AST *ast) {
177 FILE *file = fopen(ast->getFilename().c_str(), "rb");
Andreas Huberc9410c72016-07-28 12:18:40 -0700178
Andreas Huber68f24592016-07-29 14:53:48 -0700179 if (file == NULL) {
180 return -errno;
181 }
182
Andreas Huberc9410c72016-07-28 12:18:40 -0700183 yyscan_t scanner;
184 yylex_init_extra(ast, &scanner);
185 ast->setScanner(scanner);
186
187 yyset_in(file, scanner);
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700188 int res = yy::parser(ast).parse();
Andreas Huberc9410c72016-07-28 12:18:40 -0700189
190 yylex_destroy(scanner);
191 ast->setScanner(NULL);
192
193 fclose(file);
194 file = NULL;
Andreas Huber68f24592016-07-29 14:53:48 -0700195
196 if (res != 0) {
197 return UNKNOWN_ERROR;
198 }
199
200 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700201}