blob: a3c2201bf57867ad3724151a093ee7a72cac5e95 [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, "/");
Steven Moreland04fff2b2019-10-24 16:14:48 -070090 bool isDoc = StringHelper::StartsWith(str, "**");
Steven Moreland4d89ee22019-03-08 13:25:32 -080091 str = StringHelper::LTrimAll(str, "*");
92 str = StringHelper::RTrim(str, "/");
93 str = StringHelper::RTrimAll(str, "*");
94
95 yylval->str = strdup(str.c_str());
Steven Moreland04fff2b2019-10-24 16:14:48 -070096 return isDoc ? token::DOC_COMMENT : token::MULTILINE_COMMENT;
Steven Moreland4d89ee22019-03-08 13:25:32 -080097 }
Steven Moreland605c4212016-09-06 10:19:53 -070098
Steven Moreland04fff2b2019-10-24 16:14:48 -070099"//"[^\r\n]* {
100 ast->addUnhandledComment(
101 new DocComment(yytext,
102 convertYYLoc(*yylloc, ast),
103 CommentType::SINGLELINE));
104 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700105
Steven Morelandd13d08f2017-09-07 10:56:43 -0700106"enum" { return token::ENUM; }
107"extends" { return token::EXTENDS; }
108"generates" { return token::GENERATES; }
109"import" { return token::IMPORT; }
110"interface" { return token::INTERFACE; }
111"package" { return token::PACKAGE; }
Nirav Atrefd184e82018-06-01 13:35:13 -0700112"safe_union" { return token::SAFE_UNION; }
Steven Morelandd13d08f2017-09-07 10:56:43 -0700113"struct" { return token::STRUCT; }
114"typedef" { return token::TYPEDEF; }
115"union" { return token::UNION; }
116"bitfield" { yylval->templatedType = new BitFieldType(*scope); return token::TEMPLATED; }
117"vec" { yylval->templatedType = new VectorType(*scope); return token::TEMPLATED; }
Steven Morelandd13d08f2017-09-07 10:56:43 -0700118"oneway" { return token::ONEWAY; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700119
Steven Morelandd13d08f2017-09-07 10:56:43 -0700120"bool" { SCALAR_TYPE(KIND_BOOL); }
121"int8_t" { SCALAR_TYPE(KIND_INT8); }
122"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
123"int16_t" { SCALAR_TYPE(KIND_INT16); }
124"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
125"int32_t" { SCALAR_TYPE(KIND_INT32); }
126"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
127"int64_t" { SCALAR_TYPE(KIND_INT64); }
128"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
129"float" { SCALAR_TYPE(KIND_FLOAT); }
130"double" { SCALAR_TYPE(KIND_DOUBLE); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700131
Steven Morelandd13d08f2017-09-07 10:56:43 -0700132"death_recipient" { yylval->type = new DeathRecipientType(*scope); return token::TYPE; }
133"handle" { yylval->type = new HandleType(*scope); return token::TYPE; }
134"memory" { yylval->type = new MemoryType(*scope); return token::TYPE; }
135"pointer" { yylval->type = new PointerType(*scope); return token::TYPE; }
136"string" { yylval->type = new StringType(*scope); return token::TYPE; }
Hridya Valsarajucd91bf62016-10-25 12:41:04 -0700137
Neel Mehta3b414a82019-07-02 15:47:48 -0700138"fmq_sync" { yylval->type = new FmqType("::android::hardware", "MQDescriptorSync", *scope, "fmq_sync"); return token::TEMPLATED; }
139"fmq_unsync" { yylval->type = new FmqType("::android::hardware", "MQDescriptorUnsync", *scope, "fmq_unsync"); return token::TEMPLATED; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700140
Steven Morelandd13d08f2017-09-07 10:56:43 -0700141"(" { 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('*'); }
157"/" { return('/'); }
158"%" { return('%'); }
159"&" { return('&'); }
160"|" { return('|'); }
161"^" { return('^'); }
162"<<" { return(token::LSHIFT); }
163">>" { return(token::RSHIFT); }
164"&&" { return(token::LOGICAL_AND); }
165"||" { return(token::LOGICAL_OR); }
166"!" { return('!'); }
167"~" { return('~'); }
168"<=" { return(token::LEQ); }
169">=" { return(token::GEQ); }
170"==" { return(token::EQUALITY); }
171"!=" { return(token::NEQ); }
172"?" { return('?'); }
173"@" { return('@'); }
Steven Moreland12f0ab12018-11-02 17:27:37 -0700174"#" { return('#'); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700175
Steven Morelandd13d08f2017-09-07 10:56:43 -0700176{COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; }
177{FQNAME} { yylval->str = strdup(yytext); return token::FQNAME; }
Yifan Hongb44a6c82016-09-22 15:50:18 -0700178
Steven Morelandd13d08f2017-09-07 10:56:43 -07001790[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
1800{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
181{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
182L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; }
Yifan Hong52165692016-08-12 18:06:40 -0700183
Steven Morelandd13d08f2017-09-07 10:56:43 -0700184{D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
185{D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
186{D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
Yifan Hong52165692016-08-12 18:06:40 -0700187
Steven Morelandd13d08f2017-09-07 10:56:43 -0700188\n|\r\n { yylloc->lines(); }
189[ \t\f\v] { /* ignore all other whitespace */ }
Steven Moreland7a4c6622017-09-07 10:32:20 -0700190
Steven Morelandd13d08f2017-09-07 10:56:43 -0700191. { yylval->str = strdup(yytext); return token::UNKNOWN; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700192
193%%
194
Steven Moreland06a088b2018-02-20 12:34:47 -0800195namespace android {
196
Steven Moreland161afb22018-02-20 14:22:44 -0800197status_t parseFile(AST* ast, std::unique_ptr<FILE, std::function<void(FILE *)>> file) {
Andreas Huberc9410c72016-07-28 12:18:40 -0700198 yyscan_t scanner;
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700199 yylex_init(&scanner);
Andreas Huberc9410c72016-07-28 12:18:40 -0700200
Steven Moreland161afb22018-02-20 14:22:44 -0800201 yyset_in(file.get(), scanner);
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700202
Neel Mehta693169b2019-05-29 18:45:25 -0700203 Scope* scopeStack = ast->getMutableRootScope();
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700204 int res = yy::parser(scanner, ast, &scopeStack).parse();
Andreas Huberc9410c72016-07-28 12:18:40 -0700205
206 yylex_destroy(scanner);
Andreas Huberc9410c72016-07-28 12:18:40 -0700207
Yifan Hongbe627b32016-10-28 18:38:56 -0700208 if (res != 0 || ast->syntaxErrors() != 0) {
Andreas Huber68f24592016-07-29 14:53:48 -0700209 return UNKNOWN_ERROR;
210 }
211
212 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700213}
Steven Moreland06a088b2018-02-20 12:34:47 -0800214
215} // namespace android