blob: 578ec4794eca61885732623aff5070de99dd6f0a [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"
61
Andreas Huberc9410c72016-07-28 12:18:40 -070062%}
63
Andreas Huber0d0f9a22016-08-17 10:26:11 -070064%option yylineno
65%option noyywrap
Andreas Huberc9410c72016-07-28 12:18:40 -070066%option reentrant
67%option bison-bridge
Andreas Huber0d0f9a22016-08-17 10:26:11 -070068%option bison-locations
Andreas Huberc9410c72016-07-28 12:18:40 -070069
Steven Moreland605c4212016-09-06 10:19:53 -070070%x COMMENT_STATE
71
Andreas Huberc9410c72016-07-28 12:18:40 -070072%%
73
Steven Moreland605c4212016-09-06 10:19:53 -070074"/*" { BEGIN(COMMENT_STATE); }
75<COMMENT_STATE>"*/" { BEGIN(INITIAL); }
76<COMMENT_STATE>[\n] { yylloc->lines(); }
77<COMMENT_STATE>. { }
78
79"//"[^\r\n]* { /* skip C++ style comment */ }
Andreas Huberc9410c72016-07-28 12:18:40 -070080
Andreas Huber0d0f9a22016-08-17 10:26:11 -070081"enum" { return token::ENUM; }
82"extends" { return token::EXTENDS; }
83"generates" { return token::GENERATES; }
84"import" { return token::IMPORT; }
85"interface" { return token::INTERFACE; }
86"package" { return token::PACKAGE; }
87"struct" { return token::STRUCT; }
88"typedef" { return token::TYPEDEF; }
89"union" { return token::UNION; }
90"vec" { return token::VEC; }
91"oneway" { return token::ONEWAY; }
Andreas Huberc9410c72016-07-28 12:18:40 -070092
Andreas Huberc9410c72016-07-28 12:18:40 -070093"bool" { SCALAR_TYPE(KIND_BOOL); }
94"opaque" { SCALAR_TYPE(KIND_OPAQUE); }
95"int8_t" { SCALAR_TYPE(KIND_INT8); }
96"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
97"int16_t" { SCALAR_TYPE(KIND_INT16); }
98"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
99"int32_t" { SCALAR_TYPE(KIND_INT32); }
100"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
101"int64_t" { SCALAR_TYPE(KIND_INT64); }
102"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
103"float" { SCALAR_TYPE(KIND_FLOAT); }
104"double" { SCALAR_TYPE(KIND_DOUBLE); }
105
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700106"handle" { yylval->type = new HandleType; return token::SCALAR; }
107"string" { yylval->type = new StringType; return token::SCALAR; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700108
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700109"(" { 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('/'); }
126"%" { return('%'); }
127"&" { return('&'); }
128"|" { return('|'); }
129"^" { return('^'); }
130"<<" { return(token::LSHIFT); }
131">>" { return(token::RSHIFT); }
132"&&" { return(token::LOGICAL_AND); }
133"||" { return(token::LOGICAL_OR); }
134"!" { return('!'); }
135"~" { return('~'); }
136"<=" { return(token::LEQ); }
137">=" { return(token::GEQ); }
138"==" { return(token::EQUALITY); }
139"!=" { return(token::NEQ); }
140"?" { return('?'); }
141"@" { return('@'); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700142
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700143{PATH}{VERSION}?"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
144{VERSION}"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
145{PATH}{VERSION} { yylval->str = strdup(yytext); return token::FQNAME; }
146{COMPONENT}({DOT}{COMPONENT})+ { yylval->str = strdup(yytext); return token::FQNAME; }
147{COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700148
Andreas Huber0d0f9a22016-08-17 10:26:11 -07001490[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
1500{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
151{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
152L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; }
Yifan Hong52165692016-08-12 18:06:40 -0700153
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700154{D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
155{D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
156{D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
Yifan Hong52165692016-08-12 18:06:40 -0700157
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700158[\n] { yylloc->lines(); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700159. { /* ignore bad characters */ }
160
161%%
162
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700163#pragma clang diagnostic pop
Andreas Huberc9410c72016-07-28 12:18:40 -0700164
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700165status_t parseFile(AST *ast) {
166 FILE *file = fopen(ast->getFilename().c_str(), "rb");
Andreas Huberc9410c72016-07-28 12:18:40 -0700167
Andreas Huber68f24592016-07-29 14:53:48 -0700168 if (file == NULL) {
169 return -errno;
170 }
171
Andreas Huberc9410c72016-07-28 12:18:40 -0700172 yyscan_t scanner;
173 yylex_init_extra(ast, &scanner);
174 ast->setScanner(scanner);
175
176 yyset_in(file, scanner);
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700177 int res = yy::parser(ast).parse();
Andreas Huberc9410c72016-07-28 12:18:40 -0700178
179 yylex_destroy(scanner);
180 ast->setScanner(NULL);
181
182 fclose(file);
183 file = NULL;
Andreas Huber68f24592016-07-29 14:53:48 -0700184
185 if (res != 0) {
186 return UNKNOWN_ERROR;
187 }
188
189 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700190}