blob: 5dd3c85e3a6bbf6bc1c9daeaf5864353575e0ffb [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"
Martijn Coenen115d4282016-12-19 05:14:04 +010037#include "DeathRecipientType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070038#include "EnumType.h"
39#include "HandleType.h"
Martijn Coenen99e6beb2016-12-01 15:48:42 +010040#include "MemoryType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070041#include "Method.h"
Martijn Coenen99e6beb2016-12-01 15:48:42 +010042#include "PointerType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070043#include "ScalarType.h"
44#include "StringType.h"
Yifan Hongbf459bc2016-08-23 16:50:37 -070045#include "VectorType.h"
46#include "RefType.h"
Hridya Valsarajua32bde82016-12-27 11:47:46 -080047#include "FmqType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070048
49#include "hidl-gen_y.h"
50
51#include <assert.h>
52
53using namespace android;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070054using token = yy::parser::token;
Andreas Huberc9410c72016-07-28 12:18:40 -070055
Andreas Huberc9410c72016-07-28 12:18:40 -070056int check_type(yyscan_t yyscanner, struct yyguts_t *yyg);
57
58#define SCALAR_TYPE(kind) \
59 do { \
Andreas Huberc9410c72016-07-28 12:18:40 -070060 yylval->type = new ScalarType(ScalarType::kind); \
Hridya Valsarajucd91bf62016-10-25 12:41:04 -070061 return token::TYPE; \
Andreas Huberc9410c72016-07-28 12:18:40 -070062 } while (0)
63
Andreas Huber0d0f9a22016-08-17 10:26:11 -070064#define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng);
65
66#pragma clang diagnostic push
67#pragma clang diagnostic ignored "-Wunused-parameter"
Steven Moreland191c1fe2016-09-13 13:05:46 -070068#pragma clang diagnostic ignored "-Wdeprecated-register"
Andreas Huber0d0f9a22016-08-17 10:26:11 -070069
Andreas Huberc9410c72016-07-28 12:18:40 -070070%}
71
Andreas Huber0d0f9a22016-08-17 10:26:11 -070072%option yylineno
73%option noyywrap
Steven Moreland60818632017-02-04 00:33:42 -080074%option nounput
75%option noinput
Andreas Huberc9410c72016-07-28 12:18:40 -070076%option reentrant
77%option bison-bridge
Andreas Huber0d0f9a22016-08-17 10:26:11 -070078%option bison-locations
Andreas Huberc9410c72016-07-28 12:18:40 -070079
Steven Moreland605c4212016-09-06 10:19:53 -070080%x COMMENT_STATE
81
Andreas Huberc9410c72016-07-28 12:18:40 -070082%%
83
Steven Moreland605c4212016-09-06 10:19:53 -070084"/*" { BEGIN(COMMENT_STATE); }
85<COMMENT_STATE>"*/" { BEGIN(INITIAL); }
86<COMMENT_STATE>[\n] { yylloc->lines(); }
87<COMMENT_STATE>. { }
88
89"//"[^\r\n]* { /* skip C++ style comment */ }
Andreas Huberc9410c72016-07-28 12:18:40 -070090
Andreas Huber0d0f9a22016-08-17 10:26:11 -070091"enum" { return token::ENUM; }
92"extends" { return token::EXTENDS; }
93"generates" { return token::GENERATES; }
94"import" { return token::IMPORT; }
95"interface" { return token::INTERFACE; }
96"package" { return token::PACKAGE; }
97"struct" { return token::STRUCT; }
98"typedef" { return token::TYPEDEF; }
99"union" { return token::UNION; }
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800100"bitfield" { yylval->templatedType = new BitFieldType; return token::TEMPLATED; }
Yifan Hongbf459bc2016-08-23 16:50:37 -0700101"vec" { yylval->templatedType = new VectorType; return token::TEMPLATED; }
102"ref" { yylval->templatedType = new RefType; return token::TEMPLATED; }
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700103"oneway" { return token::ONEWAY; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700104
Andreas Huberc9410c72016-07-28 12:18:40 -0700105"bool" { SCALAR_TYPE(KIND_BOOL); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700106"int8_t" { SCALAR_TYPE(KIND_INT8); }
107"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
108"int16_t" { SCALAR_TYPE(KIND_INT16); }
109"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
110"int32_t" { SCALAR_TYPE(KIND_INT32); }
111"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
112"int64_t" { SCALAR_TYPE(KIND_INT64); }
113"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
114"float" { SCALAR_TYPE(KIND_FLOAT); }
115"double" { SCALAR_TYPE(KIND_DOUBLE); }
116
Martijn Coenen115d4282016-12-19 05:14:04 +0100117"death_recipient" { yylval->type = new DeathRecipientType; return token::TYPE; }
Hridya Valsarajucd91bf62016-10-25 12:41:04 -0700118"handle" { yylval->type = new HandleType; return token::TYPE; }
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100119"memory" { yylval->type = new MemoryType; return token::TYPE; }
120"pointer" { yylval->type = new PointerType; return token::TYPE; }
Hridya Valsarajucd91bf62016-10-25 12:41:04 -0700121"string" { yylval->type = new StringType; return token::TYPE; }
122
Hridya Valsarajua32bde82016-12-27 11:47:46 -0800123"fmq_sync" { yylval->type = new FmqType("::android::hardware", "MQDescriptorSync"); return token::TEMPLATED; }
124"fmq_unsync" { yylval->type = new FmqType("::android::hardware", "MQDescriptorUnsync"); return token::TEMPLATED; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700125
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700126"(" { 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('/'); }
143"%" { return('%'); }
144"&" { return('&'); }
145"|" { return('|'); }
146"^" { return('^'); }
147"<<" { return(token::LSHIFT); }
148">>" { return(token::RSHIFT); }
149"&&" { return(token::LOGICAL_AND); }
150"||" { return(token::LOGICAL_OR); }
151"!" { return('!'); }
152"~" { return('~'); }
153"<=" { return(token::LEQ); }
154">=" { return(token::GEQ); }
155"==" { return(token::EQUALITY); }
156"!=" { return(token::NEQ); }
157"?" { return('?'); }
158"@" { return('@'); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700159
Yifan Hong333a6d22017-01-12 12:28:02 -0800160{PATH}{VERSION}"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700161{VERSION}"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
162{PATH}{VERSION} { yylval->str = strdup(yytext); return token::FQNAME; }
163{COMPONENT}({DOT}{COMPONENT})+ { yylval->str = strdup(yytext); return token::FQNAME; }
164{COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700165
Yifan Hong333a6d22017-01-12 12:28:02 -0800166{PATH}{VERSION}"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
Yifan Hongb44a6c82016-09-22 15:50:18 -0700167{VERSION}"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
168{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
169
Andreas Huber0d0f9a22016-08-17 10:26:11 -07001700[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
1710{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
172{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
173L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; }
Yifan Hong52165692016-08-12 18:06:40 -0700174
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700175{D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
176{D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
177{D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
Yifan Hong52165692016-08-12 18:06:40 -0700178
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700179[\n] { yylloc->lines(); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700180. { /* ignore bad characters */ }
181
182%%
183
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700184#pragma clang diagnostic pop
Andreas Huberc9410c72016-07-28 12:18:40 -0700185
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700186status_t parseFile(AST *ast) {
187 FILE *file = fopen(ast->getFilename().c_str(), "rb");
Andreas Huberc9410c72016-07-28 12:18:40 -0700188
Andreas Huber68f24592016-07-29 14:53:48 -0700189 if (file == NULL) {
190 return -errno;
191 }
192
Andreas Huberc9410c72016-07-28 12:18:40 -0700193 yyscan_t scanner;
194 yylex_init_extra(ast, &scanner);
195 ast->setScanner(scanner);
196
197 yyset_in(file, scanner);
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700198 int res = yy::parser(ast).parse();
Andreas Huberc9410c72016-07-28 12:18:40 -0700199
200 yylex_destroy(scanner);
201 ast->setScanner(NULL);
202
203 fclose(file);
204 file = NULL;
Andreas Huber68f24592016-07-29 14:53:48 -0700205
Yifan Hongbe627b32016-10-28 18:38:56 -0700206 if (res != 0 || ast->syntaxErrors() != 0) {
Andreas Huber68f24592016-07-29 14:53:48 -0700207 return UNKNOWN_ERROR;
208 }
209
210 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700211}