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