Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 1 | D [0-9] |
| 2 | L [a-zA-Z_] |
| 3 | H [a-fA-F0-9] |
| 4 | E [Ee][+-]?{D}+ |
| 5 | FS (f|F|l|L) |
| 6 | IS (u|U|l|L)* |
| 7 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 8 | COMPONENT {L}({L}|{D})* |
| 9 | DOT [.] |
| 10 | PATH {COMPONENT}({DOT}{COMPONENT})* |
| 11 | AT [@] |
| 12 | VERSION {AT}{D}+{DOT}{D}+ |
| 13 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 14 | %{ |
| 15 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 16 | #include "Annotation.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 17 | #include "AST.h" |
| 18 | #include "CompoundType.h" |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame^] | 19 | #include "ConstantExpression.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 20 | #include "EnumType.h" |
| 21 | #include "HandleType.h" |
| 22 | #include "Method.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 23 | #include "ScalarType.h" |
| 24 | #include "StringType.h" |
| 25 | |
| 26 | #include "hidl-gen_y.h" |
| 27 | |
| 28 | #include <assert.h> |
| 29 | |
| 30 | using namespace android; |
| 31 | |
| 32 | void count(struct yyguts_t *yyg); |
| 33 | void comment(yyscan_t yyscanner, struct yyguts_t *yyg); |
| 34 | int 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 Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 40 | return SCALAR; \ |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 41 | } 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 Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 65 | "oneway" { count(yyg); return(ONEWAY); } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 66 | |
| 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 Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 81 | "handle" { count(yyg); yylval->type = new HandleType; return SCALAR; } |
| 82 | "string" { count(yyg); yylval->type = new StringType; return SCALAR; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 83 | |
| 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 Huber | 006189f | 2016-07-29 15:51:17 -0700 | [diff] [blame] | 97 | "+" { count(yyg); return('+'); } |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame^] | 98 | "-" { 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 Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 116 | "@" { count(yyg); return('@'); } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 117 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 118 | {PATH}{VERSION}?"::"{PATH} { count(yyg); yylval->str = strdup(yytext); return FQNAME; } |
| 119 | {VERSION}"::"{PATH} { count(yyg); yylval->str = strdup(yytext); return FQNAME; } |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 120 | {PATH}{VERSION} { count(yyg); yylval->str = strdup(yytext); return FQNAME; } |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 121 | {COMPONENT}({DOT}{COMPONENT})+ { count(yyg); yylval->str = strdup(yytext); return FQNAME; } |
| 122 | {COMPONENT} { count(yyg); yylval->str = strdup(yytext); return IDENTIFIER; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 123 | |
| 124 | 0[xX]{H}+{IS}? { count(yyg); yylval->str = strdup(yytext); return(INTEGER); } |
| 125 | 0{D}+{IS}? { count(yyg); yylval->str = strdup(yytext); return(INTEGER); } |
| 126 | {D}+{IS}? { count(yyg); yylval->str = strdup(yytext); return(INTEGER); } |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame^] | 127 | |
| 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 Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 132 | L?\"(\\.|[^\\"])*\" { 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 Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 139 | int yywrap(yyscan_t) { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 140 | return 1; |
| 141 | } |
| 142 | |
| 143 | void comment(yyscan_t yyscanner, yyguts_t *yyg) { |
| 144 | char c, c1; |
| 145 | |
| 146 | loop: |
| 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 | |
| 162 | int column = 0; |
| 163 | |
| 164 | void 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 Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 178 | status_t parseFile(AST *ast, const char *path) { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 179 | FILE *file = fopen(path, "rb"); |
| 180 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 181 | if (file == NULL) { |
| 182 | return -errno; |
| 183 | } |
| 184 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 185 | 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 Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 191 | |
| 192 | yylex_destroy(scanner); |
| 193 | ast->setScanner(NULL); |
| 194 | |
| 195 | fclose(file); |
| 196 | file = NULL; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 197 | |
| 198 | if (res != 0) { |
| 199 | return UNKNOWN_ERROR; |
| 200 | } |
| 201 | |
| 202 | return OK; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 203 | } |