blob: 2c69f9642594c7dc80429521ecf0f8e9b3a410bf [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;
31
32void count(struct yyguts_t *yyg);
33void 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 { \
38 count(yyg); \
39 yylval->type = new ScalarType(ScalarType::kind); \
Andreas Huber84f89de2016-07-28 15:39:51 -070040 return SCALAR; \
Andreas Huberc9410c72016-07-28 12:18:40 -070041 } while (0)
42
43%}
44
45%option reentrant
46%option bison-bridge
47%option extra-type="android::AST *"
48
49%%
50
51"/*" { comment(yyscanner, yyg); }
52"//"[^\r\n]* { /* skip C++ style comment */ }
53
54"const" { count(yyg); return(CONST); }
55"enum" { count(yyg); return(ENUM); }
56"extends" { count(yyg); return(EXTENDS); }
57"generates" { count(yyg); return(GENERATES); }
58"import" { count(yyg); return(IMPORT); }
59"interface" { count(yyg); return(INTERFACE); }
60"package" { count(yyg); return(PACKAGE); }
61"struct" { count(yyg); return(STRUCT); }
62"typedef" { count(yyg); return(TYPEDEF); }
63"union" { count(yyg); return(UNION); }
64"vec" { count(yyg); return(VEC); }
Iliyan Malchev639bff82016-08-13 14:24:11 -070065"oneway" { count(yyg); return(ONEWAY); }
Andreas Huberc9410c72016-07-28 12:18:40 -070066
67"char" { SCALAR_TYPE(KIND_CHAR); }
68"bool" { SCALAR_TYPE(KIND_BOOL); }
69"opaque" { SCALAR_TYPE(KIND_OPAQUE); }
70"int8_t" { SCALAR_TYPE(KIND_INT8); }
71"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
72"int16_t" { SCALAR_TYPE(KIND_INT16); }
73"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
74"int32_t" { SCALAR_TYPE(KIND_INT32); }
75"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
76"int64_t" { SCALAR_TYPE(KIND_INT64); }
77"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
78"float" { SCALAR_TYPE(KIND_FLOAT); }
79"double" { SCALAR_TYPE(KIND_DOUBLE); }
80
Andreas Huber84f89de2016-07-28 15:39:51 -070081"handle" { count(yyg); yylval->type = new HandleType; return SCALAR; }
82"string" { count(yyg); yylval->type = new StringType; return SCALAR; }
Andreas Huberc9410c72016-07-28 12:18:40 -070083
84"(" { count(yyg); return('('); }
85")" { count(yyg); return(')'); }
86"<" { count(yyg); return('<'); }
87">" { count(yyg); return('>'); }
88"{" { count(yyg); return('{'); }
89"}" { count(yyg); return('}'); }
90"[" { count(yyg); return('['); }
91"]" { count(yyg); return(']'); }
92":" { count(yyg); return(':'); }
93";" { count(yyg); return(';'); }
94"," { count(yyg); return(','); }
95"." { count(yyg); return('.'); }
96"=" { count(yyg); return('='); }
Andreas Huber006189f2016-07-29 15:51:17 -070097"+" { count(yyg); return('+'); }
Yifan Hong52165692016-08-12 18:06:40 -070098"-" { count(yyg); return('-'); }
99"*" { count(yyg); return('*'); }
100"/" { count(yyg); return('/'); }
101"%" { count(yyg); return('%'); }
102"&" { count(yyg); return('&'); }
103"|" { count(yyg); return('|'); }
104"^" { count(yyg); return('^'); }
105"<<" { count(yyg); return(LSHIFT); }
106">>" { count(yyg); return(RSHIFT); }
107"&&" { count(yyg); return(LOGICAL_AND); }
108"||" { count(yyg); return(LOGICAL_OR); }
109"!" { count(yyg); return('!'); }
110"~" { count(yyg); return('~'); }
111"<=" { count(yyg); return(LEQ); }
112">=" { count(yyg); return(GEQ); }
113"==" { count(yyg); return(EQUALITY); }
114"!=" { count(yyg); return(NEQ); }
115"?" { count(yyg); return('?'); }
Andreas Huber3599d922016-08-09 10:42:57 -0700116"@" { count(yyg); return('@'); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700117
Andreas Huber84f89de2016-07-28 15:39:51 -0700118{PATH}{VERSION}?"::"{PATH} { count(yyg); yylval->str = strdup(yytext); return FQNAME; }
119{VERSION}"::"{PATH} { count(yyg); yylval->str = strdup(yytext); return FQNAME; }
Andreas Huberda51b8e2016-07-28 16:00:57 -0700120{PATH}{VERSION} { count(yyg); yylval->str = strdup(yytext); return FQNAME; }
Andreas Huber84f89de2016-07-28 15:39:51 -0700121{COMPONENT}({DOT}{COMPONENT})+ { count(yyg); yylval->str = strdup(yytext); return FQNAME; }
122{COMPONENT} { count(yyg); yylval->str = strdup(yytext); return IDENTIFIER; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700123
1240[xX]{H}+{IS}? { count(yyg); yylval->str = strdup(yytext); return(INTEGER); }
1250{D}+{IS}? { count(yyg); yylval->str = strdup(yytext); return(INTEGER); }
126{D}+{IS}? { count(yyg); yylval->str = strdup(yytext); return(INTEGER); }
Yifan Hong52165692016-08-12 18:06:40 -0700127
128{D}+{E}{FS}? { count(yyg); yylval->str = strdup(yytext); return(FLOAT); }
129{D}+\.{E}?{FS}? { count(yyg); yylval->str = strdup(yytext); return(FLOAT); }
130{D}*\.{D}+{E}?{FS}? { count(yyg); yylval->str = strdup(yytext); return(FLOAT); }
131
Andreas Huberc9410c72016-07-28 12:18:40 -0700132L?\"(\\.|[^\\"])*\" { count(yyg); yylval->str = strdup(yytext); return(STRING_LITERAL); }
133
134[ \t\v\n\f] { count(yyg); }
135. { /* ignore bad characters */ }
136
137%%
138
Andreas Huber881227d2016-08-02 14:20:21 -0700139int yywrap(yyscan_t) {
Andreas Huberc9410c72016-07-28 12:18:40 -0700140 return 1;
141}
142
143void comment(yyscan_t yyscanner, yyguts_t *yyg) {
144 char c, c1;
145
146loop:
147 while ((c = yyinput(yyscanner)) != '*' && c != 0)
148 putchar(c);
149
150 if ((c1 = yyinput(yyscanner)) != '/' && c != 0)
151 {
152 unput(c1);
153 goto loop;
154 }
155
156 if (c != 0) {
157 putchar(c1);
158 }
159}
160
161
162int column = 0;
163
164void count(yyguts_t *yyg) {
165 int i;
166
167 for (i = 0; yytext[i] != '\0'; i++)
168 if (yytext[i] == '\n')
169 column = 0;
170 else if (yytext[i] == '\t')
171 column += 8 - (column % 8);
172 else
173 column++;
174
175 ECHO;
176}
177
Andreas Huber68f24592016-07-29 14:53:48 -0700178status_t parseFile(AST *ast, const char *path) {
Andreas Huberc9410c72016-07-28 12:18:40 -0700179 FILE *file = fopen(path, "rb");
180
Andreas Huber68f24592016-07-29 14:53:48 -0700181 if (file == NULL) {
182 return -errno;
183 }
184
Andreas Huberc9410c72016-07-28 12:18:40 -0700185 yyscan_t scanner;
186 yylex_init_extra(ast, &scanner);
187 ast->setScanner(scanner);
188
189 yyset_in(file, scanner);
190 int res = yyparse(ast);
Andreas Huberc9410c72016-07-28 12:18:40 -0700191
192 yylex_destroy(scanner);
193 ast->setScanner(NULL);
194
195 fclose(file);
196 file = NULL;
Andreas Huber68f24592016-07-29 14:53:48 -0700197
198 if (res != 0) {
199 return UNKNOWN_ERROR;
200 }
201
202 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700203}