blob: 97e37377a93360203b0f4c662e2b41e0d9d02224 [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"
34#include "CompoundType.h"
Yifan Hong52165692016-08-12 18:06:40 -070035#include "ConstantExpression.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070036#include "EnumType.h"
37#include "HandleType.h"
38#include "Method.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070039#include "ScalarType.h"
40#include "StringType.h"
Yifan Hongbf459bc2016-08-23 16:50:37 -070041#include "VectorType.h"
42#include "RefType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070043
44#include "hidl-gen_y.h"
45
46#include <assert.h>
47
48using namespace android;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070049using token = yy::parser::token;
Andreas Huberc9410c72016-07-28 12:18:40 -070050
Andreas Huberc9410c72016-07-28 12:18:40 -070051int check_type(yyscan_t yyscanner, struct yyguts_t *yyg);
52
53#define SCALAR_TYPE(kind) \
54 do { \
Andreas Huberc9410c72016-07-28 12:18:40 -070055 yylval->type = new ScalarType(ScalarType::kind); \
Andreas Huber0d0f9a22016-08-17 10:26:11 -070056 return token::SCALAR; \
Andreas Huberc9410c72016-07-28 12:18:40 -070057 } while (0)
58
Andreas Huber0d0f9a22016-08-17 10:26:11 -070059#define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng);
60
61#pragma clang diagnostic push
62#pragma clang diagnostic ignored "-Wunused-parameter"
Steven Moreland191c1fe2016-09-13 13:05:46 -070063#pragma clang diagnostic ignored "-Wdeprecated-register"
Andreas Huber0d0f9a22016-08-17 10:26:11 -070064
Andreas Huberc9410c72016-07-28 12:18:40 -070065%}
66
Andreas Huber0d0f9a22016-08-17 10:26:11 -070067%option yylineno
68%option noyywrap
Andreas Huberc9410c72016-07-28 12:18:40 -070069%option reentrant
70%option bison-bridge
Andreas Huber0d0f9a22016-08-17 10:26:11 -070071%option bison-locations
Andreas Huberc9410c72016-07-28 12:18:40 -070072
Steven Moreland605c4212016-09-06 10:19:53 -070073%x COMMENT_STATE
74
Andreas Huberc9410c72016-07-28 12:18:40 -070075%%
76
Steven Moreland605c4212016-09-06 10:19:53 -070077"/*" { BEGIN(COMMENT_STATE); }
78<COMMENT_STATE>"*/" { BEGIN(INITIAL); }
79<COMMENT_STATE>[\n] { yylloc->lines(); }
80<COMMENT_STATE>. { }
81
82"//"[^\r\n]* { /* skip C++ style comment */ }
Andreas Huberc9410c72016-07-28 12:18:40 -070083
Andreas Huber0d0f9a22016-08-17 10:26:11 -070084"enum" { return token::ENUM; }
85"extends" { return token::EXTENDS; }
86"generates" { return token::GENERATES; }
87"import" { return token::IMPORT; }
88"interface" { return token::INTERFACE; }
89"package" { return token::PACKAGE; }
90"struct" { return token::STRUCT; }
91"typedef" { return token::TYPEDEF; }
92"union" { return token::UNION; }
Yifan Hongbf459bc2016-08-23 16:50:37 -070093"vec" { yylval->templatedType = new VectorType; return token::TEMPLATED; }
94"ref" { yylval->templatedType = new RefType; return token::TEMPLATED; }
Andreas Huber0d0f9a22016-08-17 10:26:11 -070095"oneway" { return token::ONEWAY; }
Andreas Huberc9410c72016-07-28 12:18:40 -070096
Andreas Huberc9410c72016-07-28 12:18:40 -070097"bool" { SCALAR_TYPE(KIND_BOOL); }
98"opaque" { SCALAR_TYPE(KIND_OPAQUE); }
99"int8_t" { SCALAR_TYPE(KIND_INT8); }
100"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
101"int16_t" { SCALAR_TYPE(KIND_INT16); }
102"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
103"int32_t" { SCALAR_TYPE(KIND_INT32); }
104"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
105"int64_t" { SCALAR_TYPE(KIND_INT64); }
106"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
107"float" { SCALAR_TYPE(KIND_FLOAT); }
108"double" { SCALAR_TYPE(KIND_DOUBLE); }
109
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700110"handle" { yylval->type = new HandleType; return token::SCALAR; }
111"string" { yylval->type = new StringType; return token::SCALAR; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700112
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700113"(" { return('('); }
114")" { return(')'); }
115"<" { return('<'); }
116">" { return('>'); }
117"{" { return('{'); }
118"}" { return('}'); }
119"[" { return('['); }
120"]" { return(']'); }
121":" { return(':'); }
122";" { return(';'); }
123"," { return(','); }
124"." { return('.'); }
125"=" { return('='); }
126"+" { return('+'); }
127"-" { return('-'); }
128"*" { return('*'); }
129"/" { return('/'); }
130"%" { return('%'); }
131"&" { return('&'); }
132"|" { return('|'); }
133"^" { return('^'); }
134"<<" { return(token::LSHIFT); }
135">>" { return(token::RSHIFT); }
136"&&" { return(token::LOGICAL_AND); }
137"||" { return(token::LOGICAL_OR); }
138"!" { return('!'); }
139"~" { return('~'); }
140"<=" { return(token::LEQ); }
141">=" { return(token::GEQ); }
142"==" { return(token::EQUALITY); }
143"!=" { return(token::NEQ); }
144"?" { return('?'); }
145"@" { return('@'); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700146
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700147{PATH}{VERSION}?"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
148{VERSION}"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
149{PATH}{VERSION} { yylval->str = strdup(yytext); return token::FQNAME; }
150{COMPONENT}({DOT}{COMPONENT})+ { yylval->str = strdup(yytext); return token::FQNAME; }
151{COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700152
Yifan Hongb44a6c82016-09-22 15:50:18 -0700153{PATH}{VERSION}?"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
154{VERSION}"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
155{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
156
Andreas Huber0d0f9a22016-08-17 10:26:11 -07001570[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
1580{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
159{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
160L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; }
Yifan Hong52165692016-08-12 18:06:40 -0700161
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700162{D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
163{D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
164{D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
Yifan Hong52165692016-08-12 18:06:40 -0700165
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700166[\n] { yylloc->lines(); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700167. { /* ignore bad characters */ }
168
169%%
170
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700171#pragma clang diagnostic pop
Andreas Huberc9410c72016-07-28 12:18:40 -0700172
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700173status_t parseFile(AST *ast) {
174 FILE *file = fopen(ast->getFilename().c_str(), "rb");
Andreas Huberc9410c72016-07-28 12:18:40 -0700175
Andreas Huber68f24592016-07-29 14:53:48 -0700176 if (file == NULL) {
177 return -errno;
178 }
179
Andreas Huberc9410c72016-07-28 12:18:40 -0700180 yyscan_t scanner;
181 yylex_init_extra(ast, &scanner);
182 ast->setScanner(scanner);
183
184 yyset_in(file, scanner);
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700185 int res = yy::parser(ast).parse();
Andreas Huberc9410c72016-07-28 12:18:40 -0700186
187 yylex_destroy(scanner);
188 ast->setScanner(NULL);
189
190 fclose(file);
191 file = NULL;
Andreas Huber68f24592016-07-29 14:53:48 -0700192
193 if (res != 0) {
194 return UNKNOWN_ERROR;
195 }
196
197 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700198}