blob: d2066a99f896a198b44b4b73fddab1418767c28f [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); }
Iliyan Malchev639bff82016-08-13 14:24:11 -070064"oneway" { count(yyg); return(ONEWAY); }
Andreas Huberc9410c72016-07-28 12:18:40 -070065
66"char" { SCALAR_TYPE(KIND_CHAR); }
67"bool" { SCALAR_TYPE(KIND_BOOL); }
68"opaque" { SCALAR_TYPE(KIND_OPAQUE); }
69"int8_t" { SCALAR_TYPE(KIND_INT8); }
70"uint8_t" { SCALAR_TYPE(KIND_UINT8); }
71"int16_t" { SCALAR_TYPE(KIND_INT16); }
72"uint16_t" { SCALAR_TYPE(KIND_UINT16); }
73"int32_t" { SCALAR_TYPE(KIND_INT32); }
74"uint32_t" { SCALAR_TYPE(KIND_UINT32); }
75"int64_t" { SCALAR_TYPE(KIND_INT64); }
76"uint64_t" { SCALAR_TYPE(KIND_UINT64); }
77"float" { SCALAR_TYPE(KIND_FLOAT); }
78"double" { SCALAR_TYPE(KIND_DOUBLE); }
79
Andreas Huber84f89de2016-07-28 15:39:51 -070080"handle" { count(yyg); yylval->type = new HandleType; return SCALAR; }
81"string" { count(yyg); yylval->type = new StringType; return SCALAR; }
Andreas Huberc9410c72016-07-28 12:18:40 -070082
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"=" { count(yyg); return('='); }
Andreas Huber006189f2016-07-29 15:51:17 -070096"+" { count(yyg); return('+'); }
Andreas Huber3599d922016-08-09 10:42:57 -070097"@" { count(yyg); return('@'); }
Andreas Huberc9410c72016-07-28 12:18:40 -070098
Andreas Huber84f89de2016-07-28 15:39:51 -070099{PATH}{VERSION}?"::"{PATH} { count(yyg); yylval->str = strdup(yytext); return FQNAME; }
100{VERSION}"::"{PATH} { count(yyg); yylval->str = strdup(yytext); return FQNAME; }
Andreas Huberda51b8e2016-07-28 16:00:57 -0700101{PATH}{VERSION} { count(yyg); yylval->str = strdup(yytext); return FQNAME; }
Andreas Huber84f89de2016-07-28 15:39:51 -0700102{COMPONENT}({DOT}{COMPONENT})+ { count(yyg); yylval->str = strdup(yytext); return FQNAME; }
103{COMPONENT} { count(yyg); yylval->str = strdup(yytext); return IDENTIFIER; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700104
1050[xX]{H}+{IS}? { count(yyg); yylval->str = strdup(yytext); return(INTEGER); }
1060{D}+{IS}? { count(yyg); yylval->str = strdup(yytext); return(INTEGER); }
107{D}+{IS}? { count(yyg); yylval->str = strdup(yytext); return(INTEGER); }
108L?\"(\\.|[^\\"])*\" { count(yyg); yylval->str = strdup(yytext); return(STRING_LITERAL); }
109
110[ \t\v\n\f] { count(yyg); }
111. { /* ignore bad characters */ }
112
113%%
114
Andreas Huber881227d2016-08-02 14:20:21 -0700115int yywrap(yyscan_t) {
Andreas Huberc9410c72016-07-28 12:18:40 -0700116 return 1;
117}
118
119void comment(yyscan_t yyscanner, yyguts_t *yyg) {
120 char c, c1;
121
122loop:
123 while ((c = yyinput(yyscanner)) != '*' && c != 0)
124 putchar(c);
125
126 if ((c1 = yyinput(yyscanner)) != '/' && c != 0)
127 {
128 unput(c1);
129 goto loop;
130 }
131
132 if (c != 0) {
133 putchar(c1);
134 }
135}
136
137
138int column = 0;
139
140void count(yyguts_t *yyg) {
141 int i;
142
143 for (i = 0; yytext[i] != '\0'; i++)
144 if (yytext[i] == '\n')
145 column = 0;
146 else if (yytext[i] == '\t')
147 column += 8 - (column % 8);
148 else
149 column++;
150
151 ECHO;
152}
153
Andreas Huber68f24592016-07-29 14:53:48 -0700154status_t parseFile(AST *ast, const char *path) {
Andreas Huberc9410c72016-07-28 12:18:40 -0700155 FILE *file = fopen(path, "rb");
156
Andreas Huber68f24592016-07-29 14:53:48 -0700157 if (file == NULL) {
158 return -errno;
159 }
160
Andreas Huberc9410c72016-07-28 12:18:40 -0700161 yyscan_t scanner;
162 yylex_init_extra(ast, &scanner);
163 ast->setScanner(scanner);
164
165 yyset_in(file, scanner);
166 int res = yyparse(ast);
Andreas Huberc9410c72016-07-28 12:18:40 -0700167
168 yylex_destroy(scanner);
169 ast->setScanner(NULL);
170
171 fclose(file);
172 file = NULL;
Andreas Huber68f24592016-07-29 14:53:48 -0700173
174 if (res != 0) {
175 return UNKNOWN_ERROR;
176 }
177
178 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700179}