blob: 68112da9fe6521053229a8154d9253ce4a962c5d [file] [log] [blame]
Andreas Huberc9410c72016-07-28 12:18:40 -07001D [0-9]
2L [a-zA-Z_]
3H [a-fA-F0-9]
4E [Ee][+-]?{D}+
5FS (f|F|l|L)
6IS (u|U|l|L)*
7
Andreas Huber84f89de2016-07-28 15:39:51 -07008COMPONENT {L}({L}|{D})*
9DOT [.]
10PATH {COMPONENT}({DOT}{COMPONENT})*
11AT [@]
12VERSION {AT}{D}+{DOT}{D}+
13
Andreas Huberc9410c72016-07-28 12:18:40 -070014%{
15
Andreas Huber3599d922016-08-09 10:42:57 -070016#include "Annotation.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070017#include "AST.h"
18#include "CompoundType.h"
Yifan Hong52165692016-08-12 18:06:40 -070019#include "ConstantExpression.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070020#include "EnumType.h"
21#include "HandleType.h"
22#include "Method.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070023#include "ScalarType.h"
24#include "StringType.h"
25
26#include "hidl-gen_y.h"
27
28#include <assert.h>
29
30using namespace android;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070031using token = yy::parser::token;
Andreas Huberc9410c72016-07-28 12:18:40 -070032
Andreas Huberc9410c72016-07-28 12:18:40 -070033void comment(yyscan_t yyscanner, struct yyguts_t *yyg);
34int check_type(yyscan_t yyscanner, struct yyguts_t *yyg);
35
36#define SCALAR_TYPE(kind) \
37 do { \
Andreas Huberc9410c72016-07-28 12:18:40 -070038 yylval->type = new ScalarType(ScalarType::kind); \
Andreas Huber0d0f9a22016-08-17 10:26:11 -070039 return token::SCALAR; \
Andreas Huberc9410c72016-07-28 12:18:40 -070040 } while (0)
41
Andreas Huber0d0f9a22016-08-17 10:26:11 -070042#define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng);
43
44#pragma clang diagnostic push
45#pragma clang diagnostic ignored "-Wunused-parameter"
46
Andreas Huberc9410c72016-07-28 12:18:40 -070047%}
48
Andreas Huber0d0f9a22016-08-17 10:26:11 -070049%option yylineno
50%option noyywrap
Andreas Huberc9410c72016-07-28 12:18:40 -070051%option reentrant
52%option bison-bridge
Andreas Huber0d0f9a22016-08-17 10:26:11 -070053%option bison-locations
Andreas Huberc9410c72016-07-28 12:18:40 -070054
55%%
56
57"/*" { comment(yyscanner, yyg); }
58"//"[^\r\n]* { /* skip C++ style comment */ }
59
Andreas Huber0d0f9a22016-08-17 10:26:11 -070060"enum" { return token::ENUM; }
61"extends" { return token::EXTENDS; }
62"generates" { return token::GENERATES; }
63"import" { return token::IMPORT; }
64"interface" { return token::INTERFACE; }
65"package" { return token::PACKAGE; }
66"struct" { return token::STRUCT; }
67"typedef" { return token::TYPEDEF; }
68"union" { return token::UNION; }
69"vec" { return token::VEC; }
70"oneway" { return token::ONEWAY; }
Andreas Huberc9410c72016-07-28 12:18:40 -070071
Andreas Huberc9410c72016-07-28 12:18:40 -070072"bool" { SCALAR_TYPE(KIND_BOOL); }
73"opaque" { SCALAR_TYPE(KIND_OPAQUE); }
74"int8_t" { SCALAR_TYPE(KIND_INT8); }
75"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
76"int16_t" { SCALAR_TYPE(KIND_INT16); }
77"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
78"int32_t" { SCALAR_TYPE(KIND_INT32); }
79"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
80"int64_t" { SCALAR_TYPE(KIND_INT64); }
81"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
82"float" { SCALAR_TYPE(KIND_FLOAT); }
83"double" { SCALAR_TYPE(KIND_DOUBLE); }
84
Andreas Huber0d0f9a22016-08-17 10:26:11 -070085"handle" { yylval->type = new HandleType; return token::SCALAR; }
86"string" { yylval->type = new StringType; return token::SCALAR; }
Andreas Huberc9410c72016-07-28 12:18:40 -070087
Andreas Huber0d0f9a22016-08-17 10:26:11 -070088"(" { return('('); }
89")" { return(')'); }
90"<" { return('<'); }
91">" { return('>'); }
92"{" { return('{'); }
93"}" { return('}'); }
94"[" { return('['); }
95"]" { return(']'); }
96":" { return(':'); }
97";" { return(';'); }
98"," { return(','); }
99"." { return('.'); }
100"=" { return('='); }
101"+" { return('+'); }
102"-" { return('-'); }
103"*" { return('*'); }
104"/" { return('/'); }
105"%" { return('%'); }
106"&" { return('&'); }
107"|" { return('|'); }
108"^" { return('^'); }
109"<<" { return(token::LSHIFT); }
110">>" { return(token::RSHIFT); }
111"&&" { return(token::LOGICAL_AND); }
112"||" { return(token::LOGICAL_OR); }
113"!" { return('!'); }
114"~" { return('~'); }
115"<=" { return(token::LEQ); }
116">=" { return(token::GEQ); }
117"==" { return(token::EQUALITY); }
118"!=" { return(token::NEQ); }
119"?" { return('?'); }
120"@" { return('@'); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700121
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700122{PATH}{VERSION}?"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
123{VERSION}"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; }
124{PATH}{VERSION} { yylval->str = strdup(yytext); return token::FQNAME; }
125{COMPONENT}({DOT}{COMPONENT})+ { yylval->str = strdup(yytext); return token::FQNAME; }
126{COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700127
Andreas Huber0d0f9a22016-08-17 10:26:11 -07001280[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
1290{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
130{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; }
131L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; }
Yifan Hong52165692016-08-12 18:06:40 -0700132
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700133{D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
134{D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
135{D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
Yifan Hong52165692016-08-12 18:06:40 -0700136
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700137[\n] { yylloc->lines(); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700138. { /* ignore bad characters */ }
139
140%%
141
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700142#pragma clang diagnostic pop
Andreas Huberc9410c72016-07-28 12:18:40 -0700143
144void comment(yyscan_t yyscanner, yyguts_t *yyg) {
145 char c, c1;
146
147loop:
Andreas Huber39fa7182016-08-19 14:27:33 -0700148 while ((c = yyinput(yyscanner)) != '*' && c != 0) {
149 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700150
151 if ((c1 = yyinput(yyscanner)) != '/' && c != 0)
152 {
153 unput(c1);
154 goto loop;
155 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700156}
157
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700158status_t parseFile(AST *ast) {
159 FILE *file = fopen(ast->getFilename().c_str(), "rb");
Andreas Huberc9410c72016-07-28 12:18:40 -0700160
Andreas Huber68f24592016-07-29 14:53:48 -0700161 if (file == NULL) {
162 return -errno;
163 }
164
Andreas Huberc9410c72016-07-28 12:18:40 -0700165 yyscan_t scanner;
166 yylex_init_extra(ast, &scanner);
167 ast->setScanner(scanner);
168
169 yyset_in(file, scanner);
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700170 int res = yy::parser(ast).parse();
Andreas Huberc9410c72016-07-28 12:18:40 -0700171
172 yylex_destroy(scanner);
173 ast->setScanner(NULL);
174
175 fclose(file);
176 file = NULL;
Andreas Huber68f24592016-07-29 14:53:48 -0700177
178 if (res != 0) {
179 return UNKNOWN_ERROR;
180 }
181
182 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700183}