blob: ab743d57380885c556726577bffddff1af689028 [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"
Yifan Hongbd33e382016-11-02 13:30:17 -070034#include "ArrayType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070035#include "CompoundType.h"
Yifan Hong52165692016-08-12 18:06:40 -070036#include "ConstantExpression.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070037#include "EnumType.h"
38#include "HandleType.h"
39#include "Method.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070040#include "ScalarType.h"
41#include "StringType.h"
Yifan Hongbf459bc2016-08-23 16:50:37 -070042#include "VectorType.h"
43#include "RefType.h"
Hridya Valsarajucd91bf62016-10-25 12:41:04 -070044#include "PredefinedType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070045
46#include "hidl-gen_y.h"
47
48#include <assert.h>
49
50using namespace android;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070051using token = yy::parser::token;
Andreas Huberc9410c72016-07-28 12:18:40 -070052
Andreas Huberc9410c72016-07-28 12:18:40 -070053int check_type(yyscan_t yyscanner, struct yyguts_t *yyg);
54
55#define SCALAR_TYPE(kind) \
56 do { \
Andreas Huberc9410c72016-07-28 12:18:40 -070057 yylval->type = new ScalarType(ScalarType::kind); \
Hridya Valsarajucd91bf62016-10-25 12:41:04 -070058 return token::TYPE; \
Andreas Huberc9410c72016-07-28 12:18:40 -070059 } while (0)
60
Andreas Huber0d0f9a22016-08-17 10:26:11 -070061#define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng);
62
63#pragma clang diagnostic push
64#pragma clang diagnostic ignored "-Wunused-parameter"
Steven Moreland191c1fe2016-09-13 13:05:46 -070065#pragma clang diagnostic ignored "-Wdeprecated-register"
Andreas Huber0d0f9a22016-08-17 10:26:11 -070066
Andreas Huberc9410c72016-07-28 12:18:40 -070067%}
68
Andreas Huber0d0f9a22016-08-17 10:26:11 -070069%option yylineno
70%option noyywrap
Andreas Huberc9410c72016-07-28 12:18:40 -070071%option reentrant
72%option bison-bridge
Andreas Huber0d0f9a22016-08-17 10:26:11 -070073%option bison-locations
Andreas Huberc9410c72016-07-28 12:18:40 -070074
Steven Moreland605c4212016-09-06 10:19:53 -070075%x COMMENT_STATE
76
Andreas Huberc9410c72016-07-28 12:18:40 -070077%%
78
Steven Moreland605c4212016-09-06 10:19:53 -070079"/*" { BEGIN(COMMENT_STATE); }
80<COMMENT_STATE>"*/" { BEGIN(INITIAL); }
81<COMMENT_STATE>[\n] { yylloc->lines(); }
82<COMMENT_STATE>. { }
83
84"//"[^\r\n]* { /* skip C++ style comment */ }
Andreas Huberc9410c72016-07-28 12:18:40 -070085
Andreas Huber0d0f9a22016-08-17 10:26:11 -070086"enum" { return token::ENUM; }
87"extends" { return token::EXTENDS; }
88"generates" { return token::GENERATES; }
89"import" { return token::IMPORT; }
90"interface" { return token::INTERFACE; }
91"package" { return token::PACKAGE; }
92"struct" { return token::STRUCT; }
93"typedef" { return token::TYPEDEF; }
94"union" { return token::UNION; }
Yifan Hongbf459bc2016-08-23 16:50:37 -070095"vec" { yylval->templatedType = new VectorType; return token::TEMPLATED; }
96"ref" { yylval->templatedType = new RefType; return token::TEMPLATED; }
Andreas Huber0d0f9a22016-08-17 10:26:11 -070097"oneway" { return token::ONEWAY; }
Andreas Huberc9410c72016-07-28 12:18:40 -070098
Andreas Huberc9410c72016-07-28 12:18:40 -070099"bool" { SCALAR_TYPE(KIND_BOOL); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700100"int8_t" { SCALAR_TYPE(KIND_INT8); }
101"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
102"int16_t" { SCALAR_TYPE(KIND_INT16); }
103"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
104"int32_t" { SCALAR_TYPE(KIND_INT32); }
105"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
106"int64_t" { SCALAR_TYPE(KIND_INT64); }
107"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
108"float" { SCALAR_TYPE(KIND_FLOAT); }
109"double" { SCALAR_TYPE(KIND_DOUBLE); }
110
Hridya Valsarajucd91bf62016-10-25 12:41:04 -0700111"handle" { yylval->type = new HandleType; return token::TYPE; }
112"string" { yylval->type = new StringType; return token::TYPE; }
113
Yifan Hong244e82d2016-11-11 11:13:57 -0800114"MQDescriptorSync" { yylval->type = new PredefinedType("::android::hardware", "MQDescriptorSync"); return token::TYPE; }
115"MQDescriptorUnsync" { yylval->type = new PredefinedType("::android::hardware", "MQDescriptorUnsync"); return token::TYPE; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700116
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700117"(" { 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('%'); }
135"&" { return('&'); }
136"|" { return('|'); }
137"^" { return('^'); }
138"<<" { return(token::LSHIFT); }
139">>" { return(token::RSHIFT); }
140"&&" { return(token::LOGICAL_AND); }
141"||" { return(token::LOGICAL_OR); }
142"!" { return('!'); }
143"~" { return('~'); }
144"<=" { return(token::LEQ); }
145">=" { return(token::GEQ); }
146"==" { return(token::EQUALITY); }
147"!=" { return(token::NEQ); }
148"?" { return('?'); }
149"@" { return('@'); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700150
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700151{PATH}{VERSION}?"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
152{VERSION}"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
153{PATH}{VERSION} { yylval->str = strdup(yytext); return token::FQNAME; }
154{COMPONENT}({DOT}{COMPONENT})+ { yylval->str = strdup(yytext); return token::FQNAME; }
155{COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700156
Yifan Hongb44a6c82016-09-22 15:50:18 -0700157{PATH}{VERSION}?"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
158{VERSION}"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
159{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; }
160
Andreas Huber0d0f9a22016-08-17 10:26:11 -07001610[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
1620{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
163{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
164L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; }
Yifan Hong52165692016-08-12 18:06:40 -0700165
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700166{D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
167{D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
168{D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
Yifan Hong52165692016-08-12 18:06:40 -0700169
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700170[\n] { yylloc->lines(); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700171. { /* ignore bad characters */ }
172
173%%
174
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700175#pragma clang diagnostic pop
Andreas Huberc9410c72016-07-28 12:18:40 -0700176
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700177status_t parseFile(AST *ast) {
178 FILE *file = fopen(ast->getFilename().c_str(), "rb");
Andreas Huberc9410c72016-07-28 12:18:40 -0700179
Andreas Huber68f24592016-07-29 14:53:48 -0700180 if (file == NULL) {
181 return -errno;
182 }
183
Andreas Huberc9410c72016-07-28 12:18:40 -0700184 yyscan_t scanner;
185 yylex_init_extra(ast, &scanner);
186 ast->setScanner(scanner);
187
188 yyset_in(file, scanner);
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700189 int res = yy::parser(ast).parse();
Andreas Huberc9410c72016-07-28 12:18:40 -0700190
191 yylex_destroy(scanner);
192 ast->setScanner(NULL);
193
194 fclose(file);
195 file = NULL;
Andreas Huber68f24592016-07-29 14:53:48 -0700196
Yifan Hongbe627b32016-10-28 18:38:56 -0700197 if (res != 0 || ast->syntaxErrors() != 0) {
Andreas Huber68f24592016-07-29 14:53:48 -0700198 return UNKNOWN_ERROR;
199 }
200
201 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700202}