blob: e2276e755e384755939267da09bab7bbb36a7f2d [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); }
Andreas Huberc9410c72016-07-28 12:18:40 -070098"int8_t" { SCALAR_TYPE(KIND_INT8); }
99"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
100"int16_t" { SCALAR_TYPE(KIND_INT16); }
101"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
102"int32_t" { SCALAR_TYPE(KIND_INT32); }
103"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
104"int64_t" { SCALAR_TYPE(KIND_INT64); }
105"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
106"float" { SCALAR_TYPE(KIND_FLOAT); }
107"double" { SCALAR_TYPE(KIND_DOUBLE); }
108
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700109"handle" { yylval->type = new HandleType; return token::SCALAR; }
110"string" { yylval->type = new StringType; return token::SCALAR; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700111
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700112"(" { return('('); }
113")" { 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(token::LSHIFT); }
134">>" { return(token::RSHIFT); }
135"&&" { return(token::LOGICAL_AND); }
136"||" { return(token::LOGICAL_OR); }
137"!" { return('!'); }
138"~" { return('~'); }
139"<=" { return(token::LEQ); }
140">=" { return(token::GEQ); }
141"==" { return(token::EQUALITY); }
142"!=" { return(token::NEQ); }
143"?" { return('?'); }
144"@" { return('@'); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700145
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700146{PATH}{VERSION}?"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
147{VERSION}"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
148{PATH}{VERSION} { yylval->str = strdup(yytext); return token::FQNAME; }
149{COMPONENT}({DOT}{COMPONENT})+ { yylval->str = strdup(yytext); return token::FQNAME; }
150{COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700151
Yifan Hongb44a6c82016-09-22 15:50:18 -0700152{PATH}{VERSION}?"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
153{VERSION}"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
154{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
155
Andreas Huber0d0f9a22016-08-17 10:26:11 -07001560[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
1570{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
158{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
159L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; }
Yifan Hong52165692016-08-12 18:06:40 -0700160
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700161{D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
162{D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
163{D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
Yifan Hong52165692016-08-12 18:06:40 -0700164
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700165[\n] { yylloc->lines(); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700166. { /* ignore bad characters */ }
167
168%%
169
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700170#pragma clang diagnostic pop
Andreas Huberc9410c72016-07-28 12:18:40 -0700171
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700172status_t parseFile(AST *ast) {
173 FILE *file = fopen(ast->getFilename().c_str(), "rb");
Andreas Huberc9410c72016-07-28 12:18:40 -0700174
Andreas Huber68f24592016-07-29 14:53:48 -0700175 if (file == NULL) {
176 return -errno;
177 }
178
Andreas Huberc9410c72016-07-28 12:18:40 -0700179 yyscan_t scanner;
180 yylex_init_extra(ast, &scanner);
181 ast->setScanner(scanner);
182
183 yyset_in(file, scanner);
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700184 int res = yy::parser(ast).parse();
Andreas Huberc9410c72016-07-28 12:18:40 -0700185
186 yylex_destroy(scanner);
187 ast->setScanner(NULL);
188
189 fclose(file);
190 file = NULL;
Andreas Huber68f24592016-07-29 14:53:48 -0700191
192 if (res != 0) {
193 return UNKNOWN_ERROR;
194 }
195
196 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700197}