blob: 750f4263e393b8d1eda1c496433abac97771b4b4 [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"
41
42#include "hidl-gen_y.h"
43
44#include <assert.h>
45
46using namespace android;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070047using token = yy::parser::token;
Andreas Huberc9410c72016-07-28 12:18:40 -070048
Andreas Huberc9410c72016-07-28 12:18:40 -070049void comment(yyscan_t yyscanner, struct yyguts_t *yyg);
50int check_type(yyscan_t yyscanner, struct yyguts_t *yyg);
51
52#define SCALAR_TYPE(kind) \
53 do { \
Andreas Huberc9410c72016-07-28 12:18:40 -070054 yylval->type = new ScalarType(ScalarType::kind); \
Andreas Huber0d0f9a22016-08-17 10:26:11 -070055 return token::SCALAR; \
Andreas Huberc9410c72016-07-28 12:18:40 -070056 } while (0)
57
Andreas Huber0d0f9a22016-08-17 10:26:11 -070058#define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng);
59
60#pragma clang diagnostic push
61#pragma clang diagnostic ignored "-Wunused-parameter"
62
Andreas Huberc9410c72016-07-28 12:18:40 -070063%}
64
Andreas Huber0d0f9a22016-08-17 10:26:11 -070065%option yylineno
66%option noyywrap
Andreas Huberc9410c72016-07-28 12:18:40 -070067%option reentrant
68%option bison-bridge
Andreas Huber0d0f9a22016-08-17 10:26:11 -070069%option bison-locations
Andreas Huberc9410c72016-07-28 12:18:40 -070070
71%%
72
73"/*" { comment(yyscanner, yyg); }
74"//"[^\r\n]* { /* skip C++ style comment */ }
75
Andreas Huber0d0f9a22016-08-17 10:26:11 -070076"enum" { return token::ENUM; }
77"extends" { return token::EXTENDS; }
78"generates" { return token::GENERATES; }
79"import" { return token::IMPORT; }
80"interface" { return token::INTERFACE; }
81"package" { return token::PACKAGE; }
82"struct" { return token::STRUCT; }
83"typedef" { return token::TYPEDEF; }
84"union" { return token::UNION; }
85"vec" { return token::VEC; }
86"oneway" { return token::ONEWAY; }
Andreas Huberc9410c72016-07-28 12:18:40 -070087
Andreas Huberc9410c72016-07-28 12:18:40 -070088"bool" { SCALAR_TYPE(KIND_BOOL); }
89"opaque" { SCALAR_TYPE(KIND_OPAQUE); }
90"int8_t" { SCALAR_TYPE(KIND_INT8); }
91"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
92"int16_t" { SCALAR_TYPE(KIND_INT16); }
93"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
94"int32_t" { SCALAR_TYPE(KIND_INT32); }
95"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
96"int64_t" { SCALAR_TYPE(KIND_INT64); }
97"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
98"float" { SCALAR_TYPE(KIND_FLOAT); }
99"double" { SCALAR_TYPE(KIND_DOUBLE); }
100
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700101"handle" { yylval->type = new HandleType; return token::SCALAR; }
102"string" { yylval->type = new StringType; return token::SCALAR; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700103
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700104"(" { return('('); }
105")" { return(')'); }
106"<" { return('<'); }
107">" { return('>'); }
108"{" { return('{'); }
109"}" { return('}'); }
110"[" { return('['); }
111"]" { return(']'); }
112":" { 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(token::LSHIFT); }
126">>" { return(token::RSHIFT); }
127"&&" { return(token::LOGICAL_AND); }
128"||" { return(token::LOGICAL_OR); }
129"!" { return('!'); }
130"~" { return('~'); }
131"<=" { return(token::LEQ); }
132">=" { return(token::GEQ); }
133"==" { return(token::EQUALITY); }
134"!=" { return(token::NEQ); }
135"?" { return('?'); }
136"@" { return('@'); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700137
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700138{PATH}{VERSION}?"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
139{VERSION}"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
140{PATH}{VERSION} { yylval->str = strdup(yytext); return token::FQNAME; }
141{COMPONENT}({DOT}{COMPONENT})+ { yylval->str = strdup(yytext); return token::FQNAME; }
142{COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700143
Andreas Huber0d0f9a22016-08-17 10:26:11 -07001440[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
1450{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
146{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
147L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; }
Yifan Hong52165692016-08-12 18:06:40 -0700148
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700149{D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
150{D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
151{D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
Yifan Hong52165692016-08-12 18:06:40 -0700152
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700153[\n] { yylloc->lines(); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700154. { /* ignore bad characters */ }
155
156%%
157
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700158#pragma clang diagnostic pop
Andreas Huberc9410c72016-07-28 12:18:40 -0700159
160void comment(yyscan_t yyscanner, yyguts_t *yyg) {
161 char c, c1;
162
163loop:
Andreas Huber39fa7182016-08-19 14:27:33 -0700164 while ((c = yyinput(yyscanner)) != '*' && c != 0) {
165 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700166
167 if ((c1 = yyinput(yyscanner)) != '/' && c != 0)
168 {
169 unput(c1);
170 goto loop;
171 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700172}
173
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700174status_t parseFile(AST *ast) {
175 FILE *file = fopen(ast->getFilename().c_str(), "rb");
Andreas Huberc9410c72016-07-28 12:18:40 -0700176
Andreas Huber68f24592016-07-29 14:53:48 -0700177 if (file == NULL) {
178 return -errno;
179 }
180
Andreas Huberc9410c72016-07-28 12:18:40 -0700181 yyscan_t scanner;
182 yylex_init_extra(ast, &scanner);
183 ast->setScanner(scanner);
184
185 yyset_in(file, scanner);
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700186 int res = yy::parser(ast).parse();
Andreas Huberc9410c72016-07-28 12:18:40 -0700187
188 yylex_destroy(scanner);
189 ast->setScanner(NULL);
190
191 fclose(file);
192 file = NULL;
Andreas Huber68f24592016-07-29 14:53:48 -0700193
194 if (res != 0) {
195 return UNKNOWN_ERROR;
196 }
197
198 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700199}