blob: 13b69bf09cb0b83e03734cfc8c0f5141f86c60ce [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 [.]
Andreas Huber84f89de2016-07-28 15:39:51 -070026AT [@]
27VERSION {AT}{D}+{DOT}{D}+
Steven Moreland3effa832017-06-16 16:17:41 -070028FQNAME ({COMPONENT}|{VERSION})(({DOT}|":"+){COMPONENT}|{VERSION})*
Andreas Huber84f89de2016-07-28 15:39:51 -070029
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 -070056#define SCALAR_TYPE(kind) \
57 do { \
Andreas Huberc9410c72016-07-28 12:18:40 -070058 yylval->type = new ScalarType(ScalarType::kind); \
Hridya Valsarajucd91bf62016-10-25 12:41:04 -070059 return token::TYPE; \
Andreas Huberc9410c72016-07-28 12:18:40 -070060 } while (0)
61
Andreas Huber0d0f9a22016-08-17 10:26:11 -070062#define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng);
63
64#pragma clang diagnostic push
65#pragma clang diagnostic ignored "-Wunused-parameter"
Steven Moreland191c1fe2016-09-13 13:05:46 -070066#pragma clang diagnostic ignored "-Wdeprecated-register"
Andreas Huber0d0f9a22016-08-17 10:26:11 -070067
Andreas Huberc9410c72016-07-28 12:18:40 -070068%}
69
Andreas Huber0d0f9a22016-08-17 10:26:11 -070070%option yylineno
71%option noyywrap
Steven Moreland60818632017-02-04 00:33:42 -080072%option nounput
73%option noinput
Andreas Huberc9410c72016-07-28 12:18:40 -070074%option reentrant
75%option bison-bridge
Andreas Huber0d0f9a22016-08-17 10:26:11 -070076%option bison-locations
Andreas Huberc9410c72016-07-28 12:18:40 -070077
Steven Moreland605c4212016-09-06 10:19:53 -070078%x COMMENT_STATE
79
Andreas Huberc9410c72016-07-28 12:18:40 -070080%%
81
Steven Moreland605c4212016-09-06 10:19:53 -070082"/*" { BEGIN(COMMENT_STATE); }
83<COMMENT_STATE>"*/" { BEGIN(INITIAL); }
84<COMMENT_STATE>[\n] { yylloc->lines(); }
85<COMMENT_STATE>. { }
86
87"//"[^\r\n]* { /* skip C++ style comment */ }
Andreas Huberc9410c72016-07-28 12:18:40 -070088
Andreas Huber0d0f9a22016-08-17 10:26:11 -070089"enum" { return token::ENUM; }
90"extends" { return token::EXTENDS; }
91"generates" { return token::GENERATES; }
92"import" { return token::IMPORT; }
93"interface" { return token::INTERFACE; }
94"package" { return token::PACKAGE; }
95"struct" { return token::STRUCT; }
96"typedef" { return token::TYPEDEF; }
97"union" { return token::UNION; }
Yifan Hongc57c8bb2016-12-01 11:37:18 -080098"bitfield" { yylval->templatedType = new BitFieldType; return token::TEMPLATED; }
Yifan Hongbf459bc2016-08-23 16:50:37 -070099"vec" { yylval->templatedType = new VectorType; return token::TEMPLATED; }
100"ref" { yylval->templatedType = new RefType; return token::TEMPLATED; }
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700101"oneway" { return token::ONEWAY; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700102
Andreas Huberc9410c72016-07-28 12:18:40 -0700103"bool" { SCALAR_TYPE(KIND_BOOL); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700104"int8_t" { SCALAR_TYPE(KIND_INT8); }
105"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
106"int16_t" { SCALAR_TYPE(KIND_INT16); }
107"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
108"int32_t" { SCALAR_TYPE(KIND_INT32); }
109"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
110"int64_t" { SCALAR_TYPE(KIND_INT64); }
111"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
112"float" { SCALAR_TYPE(KIND_FLOAT); }
113"double" { SCALAR_TYPE(KIND_DOUBLE); }
114
Martijn Coenen115d4282016-12-19 05:14:04 +0100115"death_recipient" { yylval->type = new DeathRecipientType; return token::TYPE; }
Hridya Valsarajucd91bf62016-10-25 12:41:04 -0700116"handle" { yylval->type = new HandleType; return token::TYPE; }
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100117"memory" { yylval->type = new MemoryType; return token::TYPE; }
118"pointer" { yylval->type = new PointerType; return token::TYPE; }
Hridya Valsarajucd91bf62016-10-25 12:41:04 -0700119"string" { yylval->type = new StringType; return token::TYPE; }
120
Hridya Valsarajua32bde82016-12-27 11:47:46 -0800121"fmq_sync" { yylval->type = new FmqType("::android::hardware", "MQDescriptorSync"); return token::TEMPLATED; }
122"fmq_unsync" { yylval->type = new FmqType("::android::hardware", "MQDescriptorUnsync"); return token::TEMPLATED; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700123
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700124"(" { 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('&'); }
143"|" { return('|'); }
144"^" { return('^'); }
145"<<" { return(token::LSHIFT); }
146">>" { return(token::RSHIFT); }
147"&&" { return(token::LOGICAL_AND); }
148"||" { return(token::LOGICAL_OR); }
149"!" { return('!'); }
150"~" { return('~'); }
151"<=" { return(token::LEQ); }
152">=" { return(token::GEQ); }
153"==" { return(token::EQUALITY); }
154"!=" { return(token::NEQ); }
155"?" { return('?'); }
156"@" { return('@'); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700157
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700158{COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; }
Steven Moreland3effa832017-06-16 16:17:41 -0700159{FQNAME} { yylval->str = strdup(yytext); return token::FQNAME; }
Yifan Hongb44a6c82016-09-22 15:50:18 -0700160
Andreas Huber0d0f9a22016-08-17 10:26:11 -07001610[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
1620{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
163{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
164L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; }
Yifan Hong52165692016-08-12 18:06:40 -0700165
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700166{D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
167{D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
168{D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
Yifan Hong52165692016-08-12 18:06:40 -0700169
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700170[\n] { yylloc->lines(); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700171. { /* ignore bad characters */ }
172
173%%
174
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700175#pragma clang diagnostic pop
Andreas Huberc9410c72016-07-28 12:18:40 -0700176
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700177status_t parseFile(AST *ast) {
178 FILE *file = fopen(ast->getFilename().c_str(), "rb");
Andreas Huberc9410c72016-07-28 12:18:40 -0700179
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700180 if (file == nullptr) {
Andreas Huber68f24592016-07-29 14:53:48 -0700181 return -errno;
182 }
183
Andreas Huberc9410c72016-07-28 12:18:40 -0700184 yyscan_t scanner;
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700185 yylex_init(&scanner);
Andreas Huberc9410c72016-07-28 12:18:40 -0700186
187 yyset_in(file, scanner);
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700188
189 Scope* scopeStack = ast->getRootScope();
190 int res = yy::parser(scanner, ast, &scopeStack).parse();
Andreas Huberc9410c72016-07-28 12:18:40 -0700191
192 yylex_destroy(scanner);
Andreas Huberc9410c72016-07-28 12:18:40 -0700193
194 fclose(file);
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700195 file = nullptr;
Andreas Huber68f24592016-07-29 14:53:48 -0700196
Yifan Hongbe627b32016-10-28 18:38:56 -0700197 if (res != 0 || ast->syntaxErrors() != 0) {
Andreas Huber68f24592016-07-29 14:53:48 -0700198 return UNKNOWN_ERROR;
199 }
200
201 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700202}