blob: e6b4bef788ad44850590a21cd9eb3d72466bde2a [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
Andreas Huberc9410c72016-07-28 12:18:40 -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)*
23
Andreas Huber84f89de2016-07-28 15:39:51 -070024COMPONENT {L}({L}|{D})*
25DOT [.]
26PATH {COMPONENT}({DOT}{COMPONENT})*
27AT [@]
28VERSION {AT}{D}+{DOT}{D}+
29
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"
44#include "StringType.h"
Yifan Hongbf459bc2016-08-23 16:50:37 -070045#include "VectorType.h"
46#include "RefType.h"
Hridya Valsarajucd91bf62016-10-25 12:41:04 -070047#include "PredefinedType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070048
49#include "hidl-gen_y.h"
50
51#include <assert.h>
52
53using namespace android;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070054using token = yy::parser::token;
Andreas Huberc9410c72016-07-28 12:18:40 -070055
Andreas Huberc9410c72016-07-28 12:18:40 -070056int check_type(yyscan_t yyscanner, struct yyguts_t *yyg);
57
58#define SCALAR_TYPE(kind) \
59 do { \
Andreas Huberc9410c72016-07-28 12:18:40 -070060 yylval->type = new ScalarType(ScalarType::kind); \
Hridya Valsarajucd91bf62016-10-25 12:41:04 -070061 return token::TYPE; \
Andreas Huberc9410c72016-07-28 12:18:40 -070062 } while (0)
63
Andreas Huber0d0f9a22016-08-17 10:26:11 -070064#define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng);
65
66#pragma clang diagnostic push
67#pragma clang diagnostic ignored "-Wunused-parameter"
Steven Moreland191c1fe2016-09-13 13:05:46 -070068#pragma clang diagnostic ignored "-Wdeprecated-register"
Andreas Huber0d0f9a22016-08-17 10:26:11 -070069
Andreas Huberc9410c72016-07-28 12:18:40 -070070%}
71
Andreas Huber0d0f9a22016-08-17 10:26:11 -070072%option yylineno
73%option noyywrap
Andreas Huberc9410c72016-07-28 12:18:40 -070074%option reentrant
75%option bison-bridge
Andreas Huber0d0f9a22016-08-17 10:26:11 -070076%option bison-locations
Andreas Huberc9410c72016-07-28 12:18:40 -070077
Steven Moreland605c4212016-09-06 10:19:53 -070078%x COMMENT_STATE
79
Andreas Huberc9410c72016-07-28 12:18:40 -070080%%
81
Steven Moreland605c4212016-09-06 10:19:53 -070082"/*" { BEGIN(COMMENT_STATE); }
83<COMMENT_STATE>"*/" { BEGIN(INITIAL); }
84<COMMENT_STATE>[\n] { yylloc->lines(); }
85<COMMENT_STATE>. { }
86
87"//"[^\r\n]* { /* skip C++ style comment */ }
Andreas Huberc9410c72016-07-28 12:18:40 -070088
Andreas Huber0d0f9a22016-08-17 10:26:11 -070089"enum" { return token::ENUM; }
90"extends" { return token::EXTENDS; }
91"generates" { return token::GENERATES; }
92"import" { return token::IMPORT; }
93"interface" { return token::INTERFACE; }
94"package" { return token::PACKAGE; }
95"struct" { return token::STRUCT; }
96"typedef" { return token::TYPEDEF; }
97"union" { return token::UNION; }
Yifan Hongc57c8bb2016-12-01 11:37:18 -080098"bitfield" { yylval->templatedType = new BitFieldType; return token::TEMPLATED; }
Yifan Hongbf459bc2016-08-23 16:50:37 -070099"vec" { yylval->templatedType = new VectorType; return token::TEMPLATED; }
100"ref" { yylval->templatedType = new RefType; return token::TEMPLATED; }
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700101"oneway" { return token::ONEWAY; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700102
Andreas Huberc9410c72016-07-28 12:18:40 -0700103"bool" { SCALAR_TYPE(KIND_BOOL); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700104"int8_t" { SCALAR_TYPE(KIND_INT8); }
105"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
106"int16_t" { SCALAR_TYPE(KIND_INT16); }
107"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
108"int32_t" { SCALAR_TYPE(KIND_INT32); }
109"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
110"int64_t" { SCALAR_TYPE(KIND_INT64); }
111"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
112"float" { SCALAR_TYPE(KIND_FLOAT); }
113"double" { SCALAR_TYPE(KIND_DOUBLE); }
114
Martijn Coenen115d4282016-12-19 05:14:04 +0100115"death_recipient" { yylval->type = new DeathRecipientType; return token::TYPE; }
Hridya Valsarajucd91bf62016-10-25 12:41:04 -0700116"handle" { yylval->type = new HandleType; return token::TYPE; }
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100117"memory" { yylval->type = new MemoryType; return token::TYPE; }
118"pointer" { yylval->type = new PointerType; return token::TYPE; }
Hridya Valsarajucd91bf62016-10-25 12:41:04 -0700119"string" { yylval->type = new StringType; return token::TYPE; }
120
Yifan Hong244e82d2016-11-11 11:13:57 -0800121"MQDescriptorSync" { yylval->type = new PredefinedType("::android::hardware", "MQDescriptorSync"); return token::TYPE; }
122"MQDescriptorUnsync" { yylval->type = new PredefinedType("::android::hardware", "MQDescriptorUnsync"); return token::TYPE; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700123
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700124"(" { return('('); }
125")" { return(')'); }
126"<" { return('<'); }
127">" { return('>'); }
128"{" { 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(token::LSHIFT); }
146">>" { return(token::RSHIFT); }
147"&&" { return(token::LOGICAL_AND); }
148"||" { return(token::LOGICAL_OR); }
149"!" { return('!'); }
150"~" { return('~'); }
151"<=" { return(token::LEQ); }
152">=" { return(token::GEQ); }
153"==" { return(token::EQUALITY); }
154"!=" { return(token::NEQ); }
155"?" { return('?'); }
156"@" { return('@'); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700157
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700158{PATH}{VERSION}?"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
159{VERSION}"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
160{PATH}{VERSION} { yylval->str = strdup(yytext); return token::FQNAME; }
161{COMPONENT}({DOT}{COMPONENT})+ { yylval->str = strdup(yytext); return token::FQNAME; }
162{COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700163
Yifan Hongb44a6c82016-09-22 15:50:18 -0700164{PATH}{VERSION}?"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
165{VERSION}"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
166{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
167
Andreas Huber0d0f9a22016-08-17 10:26:11 -07001680[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
1690{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
170{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
171L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; }
Yifan Hong52165692016-08-12 18:06:40 -0700172
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700173{D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
174{D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
175{D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
Yifan Hong52165692016-08-12 18:06:40 -0700176
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700177[\n] { yylloc->lines(); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700178. { /* ignore bad characters */ }
179
180%%
181
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700182#pragma clang diagnostic pop
Andreas Huberc9410c72016-07-28 12:18:40 -0700183
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700184status_t parseFile(AST *ast) {
185 FILE *file = fopen(ast->getFilename().c_str(), "rb");
Andreas Huberc9410c72016-07-28 12:18:40 -0700186
Andreas Huber68f24592016-07-29 14:53:48 -0700187 if (file == NULL) {
188 return -errno;
189 }
190
Andreas Huberc9410c72016-07-28 12:18:40 -0700191 yyscan_t scanner;
192 yylex_init_extra(ast, &scanner);
193 ast->setScanner(scanner);
194
195 yyset_in(file, scanner);
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700196 int res = yy::parser(ast).parse();
Andreas Huberc9410c72016-07-28 12:18:40 -0700197
198 yylex_destroy(scanner);
199 ast->setScanner(NULL);
200
201 fclose(file);
202 file = NULL;
Andreas Huber68f24592016-07-29 14:53:48 -0700203
Yifan Hongbe627b32016-10-28 18:38:56 -0700204 if (res != 0 || ast->syntaxErrors() != 0) {
Andreas Huber68f24592016-07-29 14:53:48 -0700205 return UNKNOWN_ERROR;
206 }
207
208 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700209}