blob: 8a5213725f71e874edf3638397fc6b2b550a5dcf [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 -070049int check_type(yyscan_t yyscanner, struct yyguts_t *yyg);
50
51#define SCALAR_TYPE(kind) \
52 do { \
Andreas Huberc9410c72016-07-28 12:18:40 -070053 yylval->type = new ScalarType(ScalarType::kind); \
Andreas Huber0d0f9a22016-08-17 10:26:11 -070054 return token::SCALAR; \
Andreas Huberc9410c72016-07-28 12:18:40 -070055 } while (0)
56
Andreas Huber0d0f9a22016-08-17 10:26:11 -070057#define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng);
58
59#pragma clang diagnostic push
60#pragma clang diagnostic ignored "-Wunused-parameter"
Steven Moreland191c1fe2016-09-13 13:05:46 -070061#pragma clang diagnostic ignored "-Wdeprecated-register"
Andreas Huber0d0f9a22016-08-17 10:26:11 -070062
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
Steven Moreland605c4212016-09-06 10:19:53 -070071%x COMMENT_STATE
72
Andreas Huberc9410c72016-07-28 12:18:40 -070073%%
74
Steven Moreland605c4212016-09-06 10:19:53 -070075"/*" { BEGIN(COMMENT_STATE); }
76<COMMENT_STATE>"*/" { BEGIN(INITIAL); }
77<COMMENT_STATE>[\n] { yylloc->lines(); }
78<COMMENT_STATE>. { }
79
80"//"[^\r\n]* { /* skip C++ style comment */ }
Andreas Huberc9410c72016-07-28 12:18:40 -070081
Andreas Huber0d0f9a22016-08-17 10:26:11 -070082"enum" { return token::ENUM; }
83"extends" { return token::EXTENDS; }
84"generates" { return token::GENERATES; }
85"import" { return token::IMPORT; }
86"interface" { return token::INTERFACE; }
87"package" { return token::PACKAGE; }
88"struct" { return token::STRUCT; }
89"typedef" { return token::TYPEDEF; }
90"union" { return token::UNION; }
91"vec" { return token::VEC; }
92"oneway" { return token::ONEWAY; }
Andreas Huberc9410c72016-07-28 12:18:40 -070093
Andreas Huberc9410c72016-07-28 12:18:40 -070094"bool" { SCALAR_TYPE(KIND_BOOL); }
95"opaque" { SCALAR_TYPE(KIND_OPAQUE); }
96"int8_t" { SCALAR_TYPE(KIND_INT8); }
97"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
98"int16_t" { SCALAR_TYPE(KIND_INT16); }
99"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
100"int32_t" { SCALAR_TYPE(KIND_INT32); }
101"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
102"int64_t" { SCALAR_TYPE(KIND_INT64); }
103"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
104"float" { SCALAR_TYPE(KIND_FLOAT); }
105"double" { SCALAR_TYPE(KIND_DOUBLE); }
106
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700107"handle" { yylval->type = new HandleType; return token::SCALAR; }
108"string" { yylval->type = new StringType; return token::SCALAR; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700109
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700110"(" { 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('*'); }
126"/" { return('/'); }
127"%" { return('%'); }
128"&" { return('&'); }
129"|" { return('|'); }
130"^" { return('^'); }
131"<<" { return(token::LSHIFT); }
132">>" { return(token::RSHIFT); }
133"&&" { return(token::LOGICAL_AND); }
134"||" { return(token::LOGICAL_OR); }
135"!" { return('!'); }
136"~" { return('~'); }
137"<=" { return(token::LEQ); }
138">=" { return(token::GEQ); }
139"==" { return(token::EQUALITY); }
140"!=" { return(token::NEQ); }
141"?" { return('?'); }
142"@" { return('@'); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700143
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700144{PATH}{VERSION}?"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
145{VERSION}"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
146{PATH}{VERSION} { yylval->str = strdup(yytext); return token::FQNAME; }
147{COMPONENT}({DOT}{COMPONENT})+ { yylval->str = strdup(yytext); return token::FQNAME; }
148{COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700149
Yifan Hongb44a6c82016-09-22 15:50:18 -0700150{PATH}{VERSION}?"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
151{VERSION}"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
152{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
153
Andreas Huber0d0f9a22016-08-17 10:26:11 -07001540[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
1550{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
156{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
157L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; }
Yifan Hong52165692016-08-12 18:06:40 -0700158
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700159{D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
160{D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
161{D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
Yifan Hong52165692016-08-12 18:06:40 -0700162
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700163[\n] { yylloc->lines(); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700164. { /* ignore bad characters */ }
165
166%%
167
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700168#pragma clang diagnostic pop
Andreas Huberc9410c72016-07-28 12:18:40 -0700169
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700170status_t parseFile(AST *ast) {
171 FILE *file = fopen(ast->getFilename().c_str(), "rb");
Andreas Huberc9410c72016-07-28 12:18:40 -0700172
Andreas Huber68f24592016-07-29 14:53:48 -0700173 if (file == NULL) {
174 return -errno;
175 }
176
Andreas Huberc9410c72016-07-28 12:18:40 -0700177 yyscan_t scanner;
178 yylex_init_extra(ast, &scanner);
179 ast->setScanner(scanner);
180
181 yyset_in(file, scanner);
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700182 int res = yy::parser(ast).parse();
Andreas Huberc9410c72016-07-28 12:18:40 -0700183
184 yylex_destroy(scanner);
185 ast->setScanner(NULL);
186
187 fclose(file);
188 file = NULL;
Andreas Huber68f24592016-07-29 14:53:48 -0700189
190 if (res != 0) {
191 return UNKNOWN_ERROR;
192 }
193
194 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700195}