blob: fe95c3f864cf3310781e49daf26e0c3bcdef8390 [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
16#include "AST.h"
17#include "CompoundType.h"
18#include "EnumType.h"
19#include "HandleType.h"
20#include "Method.h"
21#include "RefType.h"
22#include "ScalarType.h"
23#include "StringType.h"
24
25#include "hidl-gen_y.h"
26
27#include <assert.h>
28
29using namespace android;
30
31void count(struct yyguts_t *yyg);
32void comment(yyscan_t yyscanner, struct yyguts_t *yyg);
33int check_type(yyscan_t yyscanner, struct yyguts_t *yyg);
34
35#define SCALAR_TYPE(kind) \
36 do { \
37 count(yyg); \
38 yylval->type = new ScalarType(ScalarType::kind); \
Andreas Huber84f89de2016-07-28 15:39:51 -070039 return SCALAR; \
Andreas Huberc9410c72016-07-28 12:18:40 -070040 } while (0)
41
42%}
43
44%option reentrant
45%option bison-bridge
46%option extra-type="android::AST *"
47
48%%
49
50"/*" { comment(yyscanner, yyg); }
51"//"[^\r\n]* { /* skip C++ style comment */ }
52
53"const" { count(yyg); return(CONST); }
54"enum" { count(yyg); return(ENUM); }
55"extends" { count(yyg); return(EXTENDS); }
56"generates" { count(yyg); return(GENERATES); }
57"import" { count(yyg); return(IMPORT); }
58"interface" { count(yyg); return(INTERFACE); }
59"package" { count(yyg); return(PACKAGE); }
60"struct" { count(yyg); return(STRUCT); }
61"typedef" { count(yyg); return(TYPEDEF); }
62"union" { count(yyg); return(UNION); }
63"vec" { count(yyg); return(VEC); }
Andreas Huberc9410c72016-07-28 12:18:40 -070064
65"char" { SCALAR_TYPE(KIND_CHAR); }
66"bool" { SCALAR_TYPE(KIND_BOOL); }
67"opaque" { SCALAR_TYPE(KIND_OPAQUE); }
68"int8_t" { SCALAR_TYPE(KIND_INT8); }
69"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
70"int16_t" { SCALAR_TYPE(KIND_INT16); }
71"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
72"int32_t" { SCALAR_TYPE(KIND_INT32); }
73"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
74"int64_t" { SCALAR_TYPE(KIND_INT64); }
75"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
76"float" { SCALAR_TYPE(KIND_FLOAT); }
77"double" { SCALAR_TYPE(KIND_DOUBLE); }
78
Andreas Huber84f89de2016-07-28 15:39:51 -070079"handle" { count(yyg); yylval->type = new HandleType; return SCALAR; }
80"string" { count(yyg); yylval->type = new StringType; return SCALAR; }
Andreas Huberc9410c72016-07-28 12:18:40 -070081
82"(" { count(yyg); return('('); }
83")" { count(yyg); return(')'); }
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
Andreas Huber84f89de2016-07-28 15:39:51 -070096{PATH}{VERSION}?"::"{PATH} { count(yyg); yylval->str = strdup(yytext); return FQNAME; }
97{VERSION}"::"{PATH} { count(yyg); yylval->str = strdup(yytext); return FQNAME; }
Andreas Huberda51b8e2016-07-28 16:00:57 -070098{PATH}{VERSION} { count(yyg); yylval->str = strdup(yytext); return FQNAME; }
Andreas Huber84f89de2016-07-28 15:39:51 -070099{COMPONENT}({DOT}{COMPONENT})+ { count(yyg); yylval->str = strdup(yytext); return FQNAME; }
100{COMPONENT} { count(yyg); yylval->str = strdup(yytext); return IDENTIFIER; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700101
1020[xX]{H}+{IS}? { count(yyg); yylval->str = strdup(yytext); return(INTEGER); }
1030{D}+{IS}? { count(yyg); yylval->str = strdup(yytext); return(INTEGER); }
104{D}+{IS}? { count(yyg); yylval->str = strdup(yytext); return(INTEGER); }
105L?\"(\\.|[^\\"])*\" { count(yyg); yylval->str = strdup(yytext); return(STRING_LITERAL); }
106
107[ \t\v\n\f] { count(yyg); }
108. { /* ignore bad characters */ }
109
110%%
111
112int yywrap(yyscan_t scanner) {
113 return 1;
114}
115
116void comment(yyscan_t yyscanner, yyguts_t *yyg) {
117 char c, c1;
118
119loop:
120 while ((c = yyinput(yyscanner)) != '*' && c != 0)
121 putchar(c);
122
123 if ((c1 = yyinput(yyscanner)) != '/' && c != 0)
124 {
125 unput(c1);
126 goto loop;
127 }
128
129 if (c != 0) {
130 putchar(c1);
131 }
132}
133
134
135int column = 0;
136
137void count(yyguts_t *yyg) {
138 int i;
139
140 for (i = 0; yytext[i] != '\0'; i++)
141 if (yytext[i] == '\n')
142 column = 0;
143 else if (yytext[i] == '\t')
144 column += 8 - (column % 8);
145 else
146 column++;
147
148 ECHO;
149}
150
Andreas Huber68f24592016-07-29 14:53:48 -0700151status_t parseFile(AST *ast, const char *path) {
Andreas Huberc9410c72016-07-28 12:18:40 -0700152 FILE *file = fopen(path, "rb");
153
Andreas Huber68f24592016-07-29 14:53:48 -0700154 if (file == NULL) {
155 return -errno;
156 }
157
Andreas Huberc9410c72016-07-28 12:18:40 -0700158 yyscan_t scanner;
159 yylex_init_extra(ast, &scanner);
160 ast->setScanner(scanner);
161
162 yyset_in(file, scanner);
163 int res = yyparse(ast);
Andreas Huberc9410c72016-07-28 12:18:40 -0700164
165 yylex_destroy(scanner);
166 ast->setScanner(NULL);
167
168 fclose(file);
169 file = NULL;
Andreas Huber68f24592016-07-29 14:53:48 -0700170
171 if (res != 0) {
172 return UNKNOWN_ERROR;
173 }
174
175 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700176}