blob: 71d3067a619870df7ec3a494cd8a4c06e1ee168c [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"
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"
Timur Iskhakov63f39902017-08-29 15:47:29 -070044#include "Scope.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070045#include "StringType.h"
Yifan Hongbf459bc2016-08-23 16:50:37 -070046#include "VectorType.h"
47#include "RefType.h"
Hridya Valsarajua32bde82016-12-27 11:47:46 -080048#include "FmqType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070049
50#include "hidl-gen_y.h"
51
52#include <assert.h>
53
54using namespace android;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070055using token = yy::parser::token;
Andreas Huberc9410c72016-07-28 12:18:40 -070056
Timur Iskhakov63f39902017-08-29 15:47:29 -070057#define SCALAR_TYPE(kind) \
58 { \
59 yylval->type = new ScalarType(ScalarType::kind, *scope); \
60 return token::TYPE; \
61 }
62
63#define YY_DECL int yylex(YYSTYPE* yylval_param, YYLTYPE* yylloc_param, \
64 yyscan_t yyscanner, android::Scope** const scope)
Andreas Huberc9410c72016-07-28 12:18:40 -070065
Andreas Huber0d0f9a22016-08-17 10:26:11 -070066#define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng);
67
68#pragma clang diagnostic push
69#pragma clang diagnostic ignored "-Wunused-parameter"
Steven Moreland191c1fe2016-09-13 13:05:46 -070070#pragma clang diagnostic ignored "-Wdeprecated-register"
Andreas Huber0d0f9a22016-08-17 10:26:11 -070071
Andreas Huberc9410c72016-07-28 12:18:40 -070072%}
73
Andreas Huber0d0f9a22016-08-17 10:26:11 -070074%option yylineno
75%option noyywrap
Steven Moreland60818632017-02-04 00:33:42 -080076%option nounput
77%option noinput
Andreas Huberc9410c72016-07-28 12:18:40 -070078%option reentrant
79%option bison-bridge
Andreas Huber0d0f9a22016-08-17 10:26:11 -070080%option bison-locations
Andreas Huberc9410c72016-07-28 12:18:40 -070081
Steven Moreland605c4212016-09-06 10:19:53 -070082%x COMMENT_STATE
83
Andreas Huberc9410c72016-07-28 12:18:40 -070084%%
85
Steven Morelandd13d08f2017-09-07 10:56:43 -070086"/*" { BEGIN(COMMENT_STATE); }
87<COMMENT_STATE>"*/" { BEGIN(INITIAL); }
88<COMMENT_STATE>[\n] { yylloc->lines(); }
89<COMMENT_STATE>. { }
Steven Moreland605c4212016-09-06 10:19:53 -070090
Steven Morelandd13d08f2017-09-07 10:56:43 -070091"//"[^\r\n]* { /* skip C++ style comment */ }
Andreas Huberc9410c72016-07-28 12:18:40 -070092
Steven Morelandd13d08f2017-09-07 10:56:43 -070093"enum" { return token::ENUM; }
94"extends" { return token::EXTENDS; }
95"generates" { return token::GENERATES; }
96"import" { return token::IMPORT; }
97"interface" { return token::INTERFACE; }
98"package" { return token::PACKAGE; }
99"struct" { return token::STRUCT; }
100"typedef" { return token::TYPEDEF; }
101"union" { return token::UNION; }
102"bitfield" { yylval->templatedType = new BitFieldType(*scope); return token::TEMPLATED; }
103"vec" { yylval->templatedType = new VectorType(*scope); return token::TEMPLATED; }
104"ref" { yylval->templatedType = new RefType(*scope); return token::TEMPLATED; }
105"oneway" { return token::ONEWAY; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700106
Steven Morelandd13d08f2017-09-07 10:56:43 -0700107"bool" { SCALAR_TYPE(KIND_BOOL); }
108"int8_t" { SCALAR_TYPE(KIND_INT8); }
109"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
110"int16_t" { SCALAR_TYPE(KIND_INT16); }
111"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
112"int32_t" { SCALAR_TYPE(KIND_INT32); }
113"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
114"int64_t" { SCALAR_TYPE(KIND_INT64); }
115"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
116"float" { SCALAR_TYPE(KIND_FLOAT); }
117"double" { SCALAR_TYPE(KIND_DOUBLE); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700118
Steven Morelandd13d08f2017-09-07 10:56:43 -0700119"death_recipient" { yylval->type = new DeathRecipientType(*scope); return token::TYPE; }
120"handle" { yylval->type = new HandleType(*scope); return token::TYPE; }
121"memory" { yylval->type = new MemoryType(*scope); return token::TYPE; }
122"pointer" { yylval->type = new PointerType(*scope); return token::TYPE; }
123"string" { yylval->type = new StringType(*scope); return token::TYPE; }
Hridya Valsarajucd91bf62016-10-25 12:41:04 -0700124
Steven Morelandd13d08f2017-09-07 10:56:43 -0700125"fmq_sync" { yylval->type = new FmqType("::android::hardware", "MQDescriptorSync", *scope); return token::TEMPLATED; }
126"fmq_unsync" { yylval->type = new FmqType("::android::hardware", "MQDescriptorUnsync", *scope); return token::TEMPLATED; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700127
Steven Morelandd13d08f2017-09-07 10:56:43 -0700128"(" { 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('%'); }
146"&" { return('&'); }
147"|" { return('|'); }
148"^" { return('^'); }
149"<<" { return(token::LSHIFT); }
150">>" { return(token::RSHIFT); }
151"&&" { return(token::LOGICAL_AND); }
152"||" { return(token::LOGICAL_OR); }
153"!" { return('!'); }
154"~" { return('~'); }
155"<=" { return(token::LEQ); }
156">=" { return(token::GEQ); }
157"==" { return(token::EQUALITY); }
158"!=" { return(token::NEQ); }
159"?" { return('?'); }
160"@" { return('@'); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700161
Steven Morelandd13d08f2017-09-07 10:56:43 -0700162{COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; }
163{FQNAME} { yylval->str = strdup(yytext); return token::FQNAME; }
Yifan Hongb44a6c82016-09-22 15:50:18 -0700164
Steven Morelandd13d08f2017-09-07 10:56:43 -07001650[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
1660{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
167{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
168L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; }
Yifan Hong52165692016-08-12 18:06:40 -0700169
Steven Morelandd13d08f2017-09-07 10:56:43 -0700170{D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
171{D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
172{D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
Yifan Hong52165692016-08-12 18:06:40 -0700173
Steven Morelandd13d08f2017-09-07 10:56:43 -0700174\n|\r\n { yylloc->lines(); }
175[ \t\f\v] { /* ignore all other whitespace */ }
Steven Moreland7a4c6622017-09-07 10:32:20 -0700176
Steven Morelandd13d08f2017-09-07 10:56:43 -0700177. { yylval->str = strdup(yytext); return token::UNKNOWN; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700178
179%%
180
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700181#pragma clang diagnostic pop
Andreas Huberc9410c72016-07-28 12:18:40 -0700182
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700183status_t parseFile(AST *ast) {
184 FILE *file = fopen(ast->getFilename().c_str(), "rb");
Andreas Huberc9410c72016-07-28 12:18:40 -0700185
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700186 if (file == nullptr) {
Andreas Huber68f24592016-07-29 14:53:48 -0700187 return -errno;
188 }
189
Andreas Huberc9410c72016-07-28 12:18:40 -0700190 yyscan_t scanner;
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700191 yylex_init(&scanner);
Andreas Huberc9410c72016-07-28 12:18:40 -0700192
193 yyset_in(file, scanner);
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700194
195 Scope* scopeStack = ast->getRootScope();
196 int res = yy::parser(scanner, ast, &scopeStack).parse();
Andreas Huberc9410c72016-07-28 12:18:40 -0700197
198 yylex_destroy(scanner);
Andreas Huberc9410c72016-07-28 12:18:40 -0700199
200 fclose(file);
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700201 file = nullptr;
Andreas Huber68f24592016-07-29 14:53:48 -0700202
Yifan Hongbe627b32016-10-28 18:38:56 -0700203 if (res != 0 || ast->syntaxErrors() != 0) {
Andreas Huber68f24592016-07-29 14:53:48 -0700204 return UNKNOWN_ERROR;
205 }
206
207 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700208}