blob: 7d768329e7f8d61938362b788f70096324061bbf [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
Steven Morelandd13d08f2017-09-07 10:56:43 -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)*
Andreas Huberc9410c72016-07-28 12:18:40 -070023
Steven Morelandd13d08f2017-09-07 10:56:43 -070024COMPONENT {L}({L}|{D})*
25DOT [.]
26AT [@]
27VERSION {AT}{D}+{DOT}{D}+
28FQNAME ({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"
Steven Moreland073269e2018-05-17 15:45:26 -070038#include "DocComment.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070039#include "EnumType.h"
40#include "HandleType.h"
Martijn Coenen99e6beb2016-12-01 15:48:42 +010041#include "MemoryType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070042#include "Method.h"
Martijn Coenen99e6beb2016-12-01 15:48:42 +010043#include "PointerType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070044#include "ScalarType.h"
Timur Iskhakov63f39902017-08-29 15:47:29 -070045#include "Scope.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070046#include "StringType.h"
Yifan Hongbf459bc2016-08-23 16:50:37 -070047#include "VectorType.h"
Hridya Valsarajua32bde82016-12-27 11:47:46 -080048#include "FmqType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070049
Neel Mehtae36e9ed2019-06-25 12:40:34 -070050#include "hidl-gen_y-helpers.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070051#include "hidl-gen_y.h"
52
53#include <assert.h>
Neel Mehta291d02e2019-06-06 17:51:07 -070054#include <algorithm>
Steven Moreland4d89ee22019-03-08 13:25:32 -080055#include <hidl-util/StringHelper.h>
Andreas Huberc9410c72016-07-28 12:18:40 -070056
57using namespace android;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070058using token = yy::parser::token;
Andreas Huberc9410c72016-07-28 12:18:40 -070059
Timur Iskhakov63f39902017-08-29 15:47:29 -070060#define SCALAR_TYPE(kind) \
61 { \
62 yylval->type = new ScalarType(ScalarType::kind, *scope); \
63 return token::TYPE; \
64 }
65
66#define YY_DECL int yylex(YYSTYPE* yylval_param, YYLTYPE* yylloc_param, \
Neel Mehtae36e9ed2019-06-25 12:40:34 -070067 yyscan_t yyscanner, android::AST* const ast, android::Scope** const scope)
Andreas Huberc9410c72016-07-28 12:18:40 -070068
Andreas Huber0d0f9a22016-08-17 10:26:11 -070069#define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng);
70
Andreas Huberc9410c72016-07-28 12:18:40 -070071%}
72
Andreas Huber0d0f9a22016-08-17 10:26:11 -070073%option yylineno
74%option noyywrap
Steven Moreland60818632017-02-04 00:33:42 -080075%option nounput
76%option noinput
Andreas Huberc9410c72016-07-28 12:18:40 -070077%option reentrant
78%option bison-bridge
Andreas Huber0d0f9a22016-08-17 10:26:11 -070079%option bison-locations
Andreas Huberc9410c72016-07-28 12:18:40 -070080
81%%
82
Steven Moreland4d89ee22019-03-08 13:25:32 -080083\/\*([^*]|\*+[^*\/])*\*+\/ {
84 std::string str(yytext);
Steven Moreland073269e2018-05-17 15:45:26 -070085
Steven Moreland4d89ee22019-03-08 13:25:32 -080086 // Add the lines to location (to keep it updated)
87 yylloc->lines(std::count(str.begin(), str.end(), '\n'));
88
89 str = StringHelper::LTrim(str, "/");
90 str = StringHelper::LTrimAll(str, "*");
91 str = StringHelper::RTrim(str, "/");
92 str = StringHelper::RTrimAll(str, "*");
93
94 yylval->str = strdup(str.c_str());
95 return token::DOC_COMMENT;
96 }
Steven Moreland605c4212016-09-06 10:19:53 -070097
Neel Mehtae36e9ed2019-06-25 12:40:34 -070098"//"[^\r\n]* { ast->addUnhandledComment(new DocComment(yytext, convertYYLoc(*yylloc, ast))); }
Andreas Huberc9410c72016-07-28 12:18:40 -070099
Steven Morelandd13d08f2017-09-07 10:56:43 -0700100"enum" { return token::ENUM; }
101"extends" { return token::EXTENDS; }
102"generates" { return token::GENERATES; }
103"import" { return token::IMPORT; }
104"interface" { return token::INTERFACE; }
105"package" { return token::PACKAGE; }
Nirav Atrefd184e82018-06-01 13:35:13 -0700106"safe_union" { return token::SAFE_UNION; }
Steven Morelandd13d08f2017-09-07 10:56:43 -0700107"struct" { return token::STRUCT; }
108"typedef" { return token::TYPEDEF; }
109"union" { return token::UNION; }
110"bitfield" { yylval->templatedType = new BitFieldType(*scope); return token::TEMPLATED; }
111"vec" { yylval->templatedType = new VectorType(*scope); return token::TEMPLATED; }
Steven Morelandd13d08f2017-09-07 10:56:43 -0700112"oneway" { return token::ONEWAY; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700113
Steven Morelandd13d08f2017-09-07 10:56:43 -0700114"bool" { SCALAR_TYPE(KIND_BOOL); }
115"int8_t" { SCALAR_TYPE(KIND_INT8); }
116"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
117"int16_t" { SCALAR_TYPE(KIND_INT16); }
118"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
119"int32_t" { SCALAR_TYPE(KIND_INT32); }
120"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
121"int64_t" { SCALAR_TYPE(KIND_INT64); }
122"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
123"float" { SCALAR_TYPE(KIND_FLOAT); }
124"double" { SCALAR_TYPE(KIND_DOUBLE); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700125
Steven Morelandd13d08f2017-09-07 10:56:43 -0700126"death_recipient" { yylval->type = new DeathRecipientType(*scope); return token::TYPE; }
127"handle" { yylval->type = new HandleType(*scope); return token::TYPE; }
128"memory" { yylval->type = new MemoryType(*scope); return token::TYPE; }
129"pointer" { yylval->type = new PointerType(*scope); return token::TYPE; }
130"string" { yylval->type = new StringType(*scope); return token::TYPE; }
Hridya Valsarajucd91bf62016-10-25 12:41:04 -0700131
Neel Mehta3b414a82019-07-02 15:47:48 -0700132"fmq_sync" { yylval->type = new FmqType("::android::hardware", "MQDescriptorSync", *scope, "fmq_sync"); return token::TEMPLATED; }
133"fmq_unsync" { yylval->type = new FmqType("::android::hardware", "MQDescriptorUnsync", *scope, "fmq_unsync"); return token::TEMPLATED; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700134
Steven Morelandd13d08f2017-09-07 10:56:43 -0700135"(" { 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('='); }
148"+" { return('+'); }
149"-" { return('-'); }
150"*" { return('*'); }
151"/" { return('/'); }
152"%" { return('%'); }
153"&" { return('&'); }
154"|" { return('|'); }
155"^" { return('^'); }
156"<<" { return(token::LSHIFT); }
157">>" { return(token::RSHIFT); }
158"&&" { return(token::LOGICAL_AND); }
159"||" { return(token::LOGICAL_OR); }
160"!" { return('!'); }
161"~" { return('~'); }
162"<=" { return(token::LEQ); }
163">=" { return(token::GEQ); }
164"==" { return(token::EQUALITY); }
165"!=" { return(token::NEQ); }
166"?" { return('?'); }
167"@" { return('@'); }
Steven Moreland12f0ab12018-11-02 17:27:37 -0700168"#" { return('#'); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700169
Steven Morelandd13d08f2017-09-07 10:56:43 -0700170{COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; }
171{FQNAME} { yylval->str = strdup(yytext); return token::FQNAME; }
Yifan Hongb44a6c82016-09-22 15:50:18 -0700172
Steven Morelandd13d08f2017-09-07 10:56:43 -07001730[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
1740{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
175{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
176L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; }
Yifan Hong52165692016-08-12 18:06:40 -0700177
Steven Morelandd13d08f2017-09-07 10:56:43 -0700178{D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
179{D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
180{D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
Yifan Hong52165692016-08-12 18:06:40 -0700181
Steven Morelandd13d08f2017-09-07 10:56:43 -0700182\n|\r\n { yylloc->lines(); }
183[ \t\f\v] { /* ignore all other whitespace */ }
Steven Moreland7a4c6622017-09-07 10:32:20 -0700184
Steven Morelandd13d08f2017-09-07 10:56:43 -0700185. { yylval->str = strdup(yytext); return token::UNKNOWN; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700186
187%%
188
Steven Moreland06a088b2018-02-20 12:34:47 -0800189namespace android {
190
Steven Moreland161afb22018-02-20 14:22:44 -0800191status_t parseFile(AST* ast, std::unique_ptr<FILE, std::function<void(FILE *)>> file) {
Andreas Huberc9410c72016-07-28 12:18:40 -0700192 yyscan_t scanner;
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700193 yylex_init(&scanner);
Andreas Huberc9410c72016-07-28 12:18:40 -0700194
Steven Moreland161afb22018-02-20 14:22:44 -0800195 yyset_in(file.get(), scanner);
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700196
Neel Mehta693169b2019-05-29 18:45:25 -0700197 Scope* scopeStack = ast->getMutableRootScope();
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700198 int res = yy::parser(scanner, ast, &scopeStack).parse();
Andreas Huberc9410c72016-07-28 12:18:40 -0700199
200 yylex_destroy(scanner);
Andreas Huberc9410c72016-07-28 12:18:40 -0700201
Yifan Hongbe627b32016-10-28 18:38:56 -0700202 if (res != 0 || ast->syntaxErrors() != 0) {
Andreas Huber68f24592016-07-29 14:53:48 -0700203 return UNKNOWN_ERROR;
204 }
205
206 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700207}
Steven Moreland06a088b2018-02-20 12:34:47 -0800208
209} // namespace android