blob: 835d8f6777ee53b3d2a98fe06412d98320e0ccc8 [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"
48#include "RefType.h"
Hridya Valsarajua32bde82016-12-27 11:47:46 -080049#include "FmqType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070050
51#include "hidl-gen_y.h"
52
53#include <assert.h>
54
55using namespace android;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070056using token = yy::parser::token;
Andreas Huberc9410c72016-07-28 12:18:40 -070057
Steven Moreland073269e2018-05-17 15:45:26 -070058static std::string gCurrentComment;
59
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, \
67 yyscan_t yyscanner, 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
Steven Moreland605c4212016-09-06 10:19:53 -070081%x COMMENT_STATE
Steven Moreland073269e2018-05-17 15:45:26 -070082%x DOC_COMMENT_STATE
Steven Moreland605c4212016-09-06 10:19:53 -070083
Andreas Huberc9410c72016-07-28 12:18:40 -070084%%
85
Steven Moreland073269e2018-05-17 15:45:26 -070086"/**" { gCurrentComment.clear(); BEGIN(DOC_COMMENT_STATE); }
87<DOC_COMMENT_STATE>"*/" {
88 BEGIN(INITIAL);
89 yylval->docComment = new DocComment(gCurrentComment);
90 return token::DOC_COMMENT;
91 }
92<DOC_COMMENT_STATE>[^*\n]* { gCurrentComment += yytext; }
93<DOC_COMMENT_STATE>[\n] { gCurrentComment += yytext; yylloc->lines(); }
94<DOC_COMMENT_STATE>[*] { gCurrentComment += yytext; }
95
96"/*" { BEGIN(COMMENT_STATE); }
97<COMMENT_STATE>"*/" { BEGIN(INITIAL); }
98<COMMENT_STATE>[\n] { yylloc->lines(); }
99<COMMENT_STATE>. { }
Steven Moreland605c4212016-09-06 10:19:53 -0700100
Steven Morelandd13d08f2017-09-07 10:56:43 -0700101"//"[^\r\n]* { /* skip C++ style comment */ }
Andreas Huberc9410c72016-07-28 12:18:40 -0700102
Steven Morelandd13d08f2017-09-07 10:56:43 -0700103"enum" { return token::ENUM; }
104"extends" { return token::EXTENDS; }
105"generates" { return token::GENERATES; }
106"import" { return token::IMPORT; }
107"interface" { return token::INTERFACE; }
108"package" { return token::PACKAGE; }
Nirav Atrefd184e82018-06-01 13:35:13 -0700109"safe_union" { return token::SAFE_UNION; }
Steven Morelandd13d08f2017-09-07 10:56:43 -0700110"struct" { return token::STRUCT; }
111"typedef" { return token::TYPEDEF; }
112"union" { return token::UNION; }
113"bitfield" { yylval->templatedType = new BitFieldType(*scope); return token::TEMPLATED; }
114"vec" { yylval->templatedType = new VectorType(*scope); return token::TEMPLATED; }
115"ref" { yylval->templatedType = new RefType(*scope); return token::TEMPLATED; }
116"oneway" { return token::ONEWAY; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700117
Steven Morelandd13d08f2017-09-07 10:56:43 -0700118"bool" { SCALAR_TYPE(KIND_BOOL); }
119"int8_t" { SCALAR_TYPE(KIND_INT8); }
120"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
121"int16_t" { SCALAR_TYPE(KIND_INT16); }
122"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
123"int32_t" { SCALAR_TYPE(KIND_INT32); }
124"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
125"int64_t" { SCALAR_TYPE(KIND_INT64); }
126"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
127"float" { SCALAR_TYPE(KIND_FLOAT); }
128"double" { SCALAR_TYPE(KIND_DOUBLE); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700129
Steven Morelandd13d08f2017-09-07 10:56:43 -0700130"death_recipient" { yylval->type = new DeathRecipientType(*scope); return token::TYPE; }
131"handle" { yylval->type = new HandleType(*scope); return token::TYPE; }
132"memory" { yylval->type = new MemoryType(*scope); return token::TYPE; }
133"pointer" { yylval->type = new PointerType(*scope); return token::TYPE; }
134"string" { yylval->type = new StringType(*scope); return token::TYPE; }
Hridya Valsarajucd91bf62016-10-25 12:41:04 -0700135
Steven Morelandd13d08f2017-09-07 10:56:43 -0700136"fmq_sync" { yylval->type = new FmqType("::android::hardware", "MQDescriptorSync", *scope); return token::TEMPLATED; }
137"fmq_unsync" { yylval->type = new FmqType("::android::hardware", "MQDescriptorUnsync", *scope); return token::TEMPLATED; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700138
Steven Morelandd13d08f2017-09-07 10:56:43 -0700139"(" { 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('%'); }
157"&" { return('&'); }
158"|" { return('|'); }
159"^" { return('^'); }
160"<<" { return(token::LSHIFT); }
161">>" { return(token::RSHIFT); }
162"&&" { return(token::LOGICAL_AND); }
163"||" { return(token::LOGICAL_OR); }
164"!" { return('!'); }
165"~" { return('~'); }
166"<=" { return(token::LEQ); }
167">=" { return(token::GEQ); }
168"==" { return(token::EQUALITY); }
169"!=" { return(token::NEQ); }
170"?" { return('?'); }
171"@" { return('@'); }
Steven Moreland12f0ab12018-11-02 17:27:37 -0700172"#" { return('#'); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700173
Steven Morelandd13d08f2017-09-07 10:56:43 -0700174{COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; }
175{FQNAME} { yylval->str = strdup(yytext); return token::FQNAME; }
Yifan Hongb44a6c82016-09-22 15:50:18 -0700176
Steven Morelandd13d08f2017-09-07 10:56:43 -07001770[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
1780{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
179{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
180L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; }
Yifan Hong52165692016-08-12 18:06:40 -0700181
Steven Morelandd13d08f2017-09-07 10:56:43 -0700182{D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
183{D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
184{D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
Yifan Hong52165692016-08-12 18:06:40 -0700185
Steven Morelandd13d08f2017-09-07 10:56:43 -0700186\n|\r\n { yylloc->lines(); }
187[ \t\f\v] { /* ignore all other whitespace */ }
Steven Moreland7a4c6622017-09-07 10:32:20 -0700188
Steven Morelandd13d08f2017-09-07 10:56:43 -0700189. { yylval->str = strdup(yytext); return token::UNKNOWN; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700190
191%%
192
Steven Moreland06a088b2018-02-20 12:34:47 -0800193namespace android {
194
Steven Moreland161afb22018-02-20 14:22:44 -0800195status_t parseFile(AST* ast, std::unique_ptr<FILE, std::function<void(FILE *)>> file) {
Andreas Huberc9410c72016-07-28 12:18:40 -0700196 yyscan_t scanner;
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700197 yylex_init(&scanner);
Andreas Huberc9410c72016-07-28 12:18:40 -0700198
Steven Moreland161afb22018-02-20 14:22:44 -0800199 yyset_in(file.get(), scanner);
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700200
201 Scope* scopeStack = ast->getRootScope();
202 int res = yy::parser(scanner, ast, &scopeStack).parse();
Andreas Huberc9410c72016-07-28 12:18:40 -0700203
204 yylex_destroy(scanner);
Andreas Huberc9410c72016-07-28 12:18:40 -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}
Steven Moreland06a088b2018-02-20 12:34:47 -0800212
213} // namespace android