blob: 359c351cb0dbc264e0a11be0f75f4cb223874cf7 [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"
19#include "EnumType.h"
20#include "HandleType.h"
21#include "Method.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070022#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('='); }
Andreas Huber006189f2016-07-29 15:51:17 -070095"+" { count(yyg); return('+'); }
Andreas Huber3599d922016-08-09 10:42:57 -070096"@" { count(yyg); return('@'); }
Andreas Huberc9410c72016-07-28 12:18:40 -070097
Andreas Huber84f89de2016-07-28 15:39:51 -070098{PATH}{VERSION}?"::"{PATH} { count(yyg); yylval->str = strdup(yytext); return FQNAME; }
99{VERSION}"::"{PATH} { count(yyg); yylval->str = strdup(yytext); return FQNAME; }
Andreas Huberda51b8e2016-07-28 16:00:57 -0700100{PATH}{VERSION} { count(yyg); yylval->str = strdup(yytext); return FQNAME; }
Andreas Huber84f89de2016-07-28 15:39:51 -0700101{COMPONENT}({DOT}{COMPONENT})+ { count(yyg); yylval->str = strdup(yytext); return FQNAME; }
102{COMPONENT} { count(yyg); yylval->str = strdup(yytext); return IDENTIFIER; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700103
1040[xX]{H}+{IS}? { count(yyg); yylval->str = strdup(yytext); return(INTEGER); }
1050{D}+{IS}? { count(yyg); yylval->str = strdup(yytext); return(INTEGER); }
106{D}+{IS}? { count(yyg); yylval->str = strdup(yytext); return(INTEGER); }
107L?\"(\\.|[^\\"])*\" { count(yyg); yylval->str = strdup(yytext); return(STRING_LITERAL); }
108
109[ \t\v\n\f] { count(yyg); }
110. { /* ignore bad characters */ }
111
112%%
113
Andreas Huber881227d2016-08-02 14:20:21 -0700114int yywrap(yyscan_t) {
Andreas Huberc9410c72016-07-28 12:18:40 -0700115 return 1;
116}
117
118void comment(yyscan_t yyscanner, yyguts_t *yyg) {
119 char c, c1;
120
121loop:
122 while ((c = yyinput(yyscanner)) != '*' && c != 0)
123 putchar(c);
124
125 if ((c1 = yyinput(yyscanner)) != '/' && c != 0)
126 {
127 unput(c1);
128 goto loop;
129 }
130
131 if (c != 0) {
132 putchar(c1);
133 }
134}
135
136
137int column = 0;
138
139void count(yyguts_t *yyg) {
140 int i;
141
142 for (i = 0; yytext[i] != '\0'; i++)
143 if (yytext[i] == '\n')
144 column = 0;
145 else if (yytext[i] == '\t')
146 column += 8 - (column % 8);
147 else
148 column++;
149
150 ECHO;
151}
152
Andreas Huber68f24592016-07-29 14:53:48 -0700153status_t parseFile(AST *ast, const char *path) {
Andreas Huberc9410c72016-07-28 12:18:40 -0700154 FILE *file = fopen(path, "rb");
155
Andreas Huber68f24592016-07-29 14:53:48 -0700156 if (file == NULL) {
157 return -errno;
158 }
159
Andreas Huberc9410c72016-07-28 12:18:40 -0700160 yyscan_t scanner;
161 yylex_init_extra(ast, &scanner);
162 ast->setScanner(scanner);
163
164 yyset_in(file, scanner);
165 int res = yyparse(ast);
Andreas Huberc9410c72016-07-28 12:18:40 -0700166
167 yylex_destroy(scanner);
168 ast->setScanner(NULL);
169
170 fclose(file);
171 file = NULL;
Andreas Huber68f24592016-07-29 14:53:48 -0700172
173 if (res != 0) {
174 return UNKNOWN_ERROR;
175 }
176
177 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700178}