blob: 007cd4ce775cdd16d3e47c95d736a36633916a61 [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
52#include <assert.h>
Neel Mehta291d02e2019-06-06 17:51:07 -070053#include <algorithm>
Steven Moreland4d89ee22019-03-08 13:25:32 -080054#include <hidl-util/StringHelper.h>
Andreas Huberc9410c72016-07-28 12:18:40 -070055
56using namespace android;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070057using token = yy::parser::token;
Andreas Huberc9410c72016-07-28 12:18:40 -070058
Timur Iskhakov63f39902017-08-29 15:47:29 -070059#define SCALAR_TYPE(kind) \
60 { \
61 yylval->type = new ScalarType(ScalarType::kind, *scope); \
62 return token::TYPE; \
63 }
64
65#define YY_DECL int yylex(YYSTYPE* yylval_param, YYLTYPE* yylloc_param, \
Neel Mehtae36e9ed2019-06-25 12:40:34 -070066 yyscan_t yyscanner, android::AST* const ast, android::Scope** const scope)
Andreas Huberc9410c72016-07-28 12:18:40 -070067
Andreas Huber0d0f9a22016-08-17 10:26:11 -070068#define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng);
69
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
80%%
81
Steven Moreland4d89ee22019-03-08 13:25:32 -080082\/\*([^*]|\*+[^*\/])*\*+\/ {
83 std::string str(yytext);
Steven Moreland073269e2018-05-17 15:45:26 -070084
Steven Moreland4d89ee22019-03-08 13:25:32 -080085 // Add the lines to location (to keep it updated)
86 yylloc->lines(std::count(str.begin(), str.end(), '\n'));
87
88 str = StringHelper::LTrim(str, "/");
Steven Moreland04fff2b2019-10-24 16:14:48 -070089 bool isDoc = StringHelper::StartsWith(str, "**");
Steven Moreland4d89ee22019-03-08 13:25:32 -080090 str = StringHelper::LTrimAll(str, "*");
91 str = StringHelper::RTrim(str, "/");
92 str = StringHelper::RTrimAll(str, "*");
93
94 yylval->str = strdup(str.c_str());
Steven Moreland04fff2b2019-10-24 16:14:48 -070095 return isDoc ? token::DOC_COMMENT : token::MULTILINE_COMMENT;
Steven Moreland4d89ee22019-03-08 13:25:32 -080096 }
Steven Moreland605c4212016-09-06 10:19:53 -070097
Steven Moreland04fff2b2019-10-24 16:14:48 -070098"//"[^\r\n]* {
99 ast->addUnhandledComment(
100 new DocComment(yytext,
101 convertYYLoc(*yylloc, ast),
102 CommentType::SINGLELINE));
103 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700104
Steven Morelandd13d08f2017-09-07 10:56:43 -0700105"enum" { return token::ENUM; }
106"extends" { return token::EXTENDS; }
107"generates" { return token::GENERATES; }
108"import" { return token::IMPORT; }
109"interface" { return token::INTERFACE; }
110"package" { return token::PACKAGE; }
Nirav Atrefd184e82018-06-01 13:35:13 -0700111"safe_union" { return token::SAFE_UNION; }
Steven Morelandd13d08f2017-09-07 10:56:43 -0700112"struct" { return token::STRUCT; }
113"typedef" { return token::TYPEDEF; }
114"union" { return token::UNION; }
115"bitfield" { yylval->templatedType = new BitFieldType(*scope); return token::TEMPLATED; }
116"vec" { yylval->templatedType = new VectorType(*scope); return token::TEMPLATED; }
Steven Morelandd13d08f2017-09-07 10:56:43 -0700117"oneway" { return token::ONEWAY; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700118
Steven Morelandd13d08f2017-09-07 10:56:43 -0700119"bool" { SCALAR_TYPE(KIND_BOOL); }
120"int8_t" { SCALAR_TYPE(KIND_INT8); }
121"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
122"int16_t" { SCALAR_TYPE(KIND_INT16); }
123"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
124"int32_t" { SCALAR_TYPE(KIND_INT32); }
125"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
126"int64_t" { SCALAR_TYPE(KIND_INT64); }
127"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
128"float" { SCALAR_TYPE(KIND_FLOAT); }
129"double" { SCALAR_TYPE(KIND_DOUBLE); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700130
Steven Morelandd13d08f2017-09-07 10:56:43 -0700131"death_recipient" { yylval->type = new DeathRecipientType(*scope); return token::TYPE; }
132"handle" { yylval->type = new HandleType(*scope); return token::TYPE; }
133"memory" { yylval->type = new MemoryType(*scope); return token::TYPE; }
134"pointer" { yylval->type = new PointerType(*scope); return token::TYPE; }
135"string" { yylval->type = new StringType(*scope); return token::TYPE; }
Hridya Valsarajucd91bf62016-10-25 12:41:04 -0700136
Neel Mehta3b414a82019-07-02 15:47:48 -0700137"fmq_sync" { yylval->type = new FmqType("::android::hardware", "MQDescriptorSync", *scope, "fmq_sync"); return token::TEMPLATED; }
138"fmq_unsync" { yylval->type = new FmqType("::android::hardware", "MQDescriptorUnsync", *scope, "fmq_unsync"); return token::TEMPLATED; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700139
Steven Morelandd13d08f2017-09-07 10:56:43 -0700140"(" { 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('/'); }
157"%" { return('%'); }
158"&" { return('&'); }
159"|" { return('|'); }
160"^" { return('^'); }
161"<<" { return(token::LSHIFT); }
162">>" { return(token::RSHIFT); }
163"&&" { return(token::LOGICAL_AND); }
164"||" { return(token::LOGICAL_OR); }
165"!" { return('!'); }
166"~" { return('~'); }
167"<=" { return(token::LEQ); }
168">=" { return(token::GEQ); }
169"==" { return(token::EQUALITY); }
170"!=" { return(token::NEQ); }
171"?" { return('?'); }
172"@" { return('@'); }
Steven Moreland12f0ab12018-11-02 17:27:37 -0700173"#" { return('#'); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700174
Steven Morelandd13d08f2017-09-07 10:56:43 -0700175{COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; }
176{FQNAME} { yylval->str = strdup(yytext); return token::FQNAME; }
Yifan Hongb44a6c82016-09-22 15:50:18 -0700177
Steven Morelandd13d08f2017-09-07 10:56:43 -07001780[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
1790{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
180{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
181L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; }
Yifan Hong52165692016-08-12 18:06:40 -0700182
Steven Morelandd13d08f2017-09-07 10:56:43 -0700183{D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
184{D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
185{D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
Yifan Hong52165692016-08-12 18:06:40 -0700186
Steven Morelandd13d08f2017-09-07 10:56:43 -0700187\n|\r\n { yylloc->lines(); }
188[ \t\f\v] { /* ignore all other whitespace */ }
Steven Moreland7a4c6622017-09-07 10:32:20 -0700189
Steven Morelandd13d08f2017-09-07 10:56:43 -0700190. { yylval->str = strdup(yytext); return token::UNKNOWN; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700191
192%%
193
Steven Moreland06a088b2018-02-20 12:34:47 -0800194namespace android {
195
Steven Moreland161afb22018-02-20 14:22:44 -0800196status_t parseFile(AST* ast, std::unique_ptr<FILE, std::function<void(FILE *)>> file) {
Andreas Huberc9410c72016-07-28 12:18:40 -0700197 yyscan_t scanner;
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700198 yylex_init(&scanner);
Andreas Huberc9410c72016-07-28 12:18:40 -0700199
Steven Moreland161afb22018-02-20 14:22:44 -0800200 yyset_in(file.get(), scanner);
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700201
Neel Mehta693169b2019-05-29 18:45:25 -0700202 Scope* scopeStack = ast->getMutableRootScope();
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700203 int res = yy::parser(scanner, ast, &scopeStack).parse();
Andreas Huberc9410c72016-07-28 12:18:40 -0700204
205 yylex_destroy(scanner);
Andreas Huberc9410c72016-07-28 12:18:40 -0700206
Yifan Hongbe627b32016-10-28 18:38:56 -0700207 if (res != 0 || ast->syntaxErrors() != 0) {
Andreas Huber68f24592016-07-29 14:53:48 -0700208 return UNKNOWN_ERROR;
209 }
210
211 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700212}
Steven Moreland06a088b2018-02-20 12:34:47 -0800213
214} // namespace android