hidl-gen_l.ll revision bd33e3854555589f312c4d6d89491e2f5cc2f782
1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17D [0-9] 18L [a-zA-Z_] 19H [a-fA-F0-9] 20E [Ee][+-]?{D}+ 21FS (f|F|l|L) 22IS (u|U|l|L)* 23 24COMPONENT {L}({L}|{D})* 25DOT [.] 26PATH {COMPONENT}({DOT}{COMPONENT})* 27AT [@] 28VERSION {AT}{D}+{DOT}{D}+ 29 30%{ 31 32#include "Annotation.h" 33#include "AST.h" 34#include "ArrayType.h" 35#include "CompoundType.h" 36#include "ConstantExpression.h" 37#include "EnumType.h" 38#include "HandleType.h" 39#include "Method.h" 40#include "ScalarType.h" 41#include "StringType.h" 42#include "VectorType.h" 43#include "RefType.h" 44#include "PredefinedType.h" 45 46#include "hidl-gen_y.h" 47 48#include <assert.h> 49 50using namespace android; 51using token = yy::parser::token; 52 53int check_type(yyscan_t yyscanner, struct yyguts_t *yyg); 54 55#define SCALAR_TYPE(kind) \ 56 do { \ 57 yylval->type = new ScalarType(ScalarType::kind); \ 58 return token::TYPE; \ 59 } while (0) 60 61#define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng); 62 63#pragma clang diagnostic push 64#pragma clang diagnostic ignored "-Wunused-parameter" 65#pragma clang diagnostic ignored "-Wdeprecated-register" 66 67%} 68 69%option yylineno 70%option noyywrap 71%option reentrant 72%option bison-bridge 73%option bison-locations 74 75%x COMMENT_STATE 76 77%% 78 79"/*" { BEGIN(COMMENT_STATE); } 80<COMMENT_STATE>"*/" { BEGIN(INITIAL); } 81<COMMENT_STATE>[\n] { yylloc->lines(); } 82<COMMENT_STATE>. { } 83 84"//"[^\r\n]* { /* skip C++ style comment */ } 85 86"enum" { return token::ENUM; } 87"extends" { return token::EXTENDS; } 88"generates" { return token::GENERATES; } 89"import" { return token::IMPORT; } 90"interface" { return token::INTERFACE; } 91"package" { return token::PACKAGE; } 92"struct" { return token::STRUCT; } 93"typedef" { return token::TYPEDEF; } 94"union" { return token::UNION; } 95"vec" { yylval->templatedType = new VectorType; return token::TEMPLATED; } 96"ref" { yylval->templatedType = new RefType; return token::TEMPLATED; } 97"oneway" { return token::ONEWAY; } 98 99"bool" { SCALAR_TYPE(KIND_BOOL); } 100"int8_t" { SCALAR_TYPE(KIND_INT8); } 101"uint8_t" { SCALAR_TYPE(KIND_UINT8); } 102"int16_t" { SCALAR_TYPE(KIND_INT16); } 103"uint16_t" { SCALAR_TYPE(KIND_UINT16); } 104"int32_t" { SCALAR_TYPE(KIND_INT32); } 105"uint32_t" { SCALAR_TYPE(KIND_UINT32); } 106"int64_t" { SCALAR_TYPE(KIND_INT64); } 107"uint64_t" { SCALAR_TYPE(KIND_UINT64); } 108"float" { SCALAR_TYPE(KIND_FLOAT); } 109"double" { SCALAR_TYPE(KIND_DOUBLE); } 110 111"handle" { yylval->type = new HandleType; return token::TYPE; } 112"string" { yylval->type = new StringType; return token::TYPE; } 113 114"MQDescriptorSync" { yylval->type = new PredefinedType("::android::hardware::MQDescriptorSync"); return token::TYPE; } 115"MQDescriptorUnsync" { yylval->type = new PredefinedType("::android::hardware::MQDescriptorUnsync"); return token::TYPE; } 116 117"(" { return('('); } 118")" { return(')'); } 119"<" { return('<'); } 120">" { return('>'); } 121"{" { return('{'); } 122"}" { return('}'); } 123"[" { return('['); } 124"]" { return(']'); } 125":" { return(':'); } 126";" { return(';'); } 127"," { return(','); } 128"." { return('.'); } 129"=" { return('='); } 130"+" { return('+'); } 131"-" { return('-'); } 132"*" { return('*'); } 133"/" { return('/'); } 134"%" { return('%'); } 135"&" { return('&'); } 136"|" { return('|'); } 137"^" { return('^'); } 138"<<" { return(token::LSHIFT); } 139">>" { return(token::RSHIFT); } 140"&&" { return(token::LOGICAL_AND); } 141"||" { return(token::LOGICAL_OR); } 142"!" { return('!'); } 143"~" { return('~'); } 144"<=" { return(token::LEQ); } 145">=" { return(token::GEQ); } 146"==" { return(token::EQUALITY); } 147"!=" { return(token::NEQ); } 148"?" { return('?'); } 149"@" { return('@'); } 150 151{PATH}{VERSION}?"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; } 152{VERSION}"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; } 153{PATH}{VERSION} { yylval->str = strdup(yytext); return token::FQNAME; } 154{COMPONENT}({DOT}{COMPONENT})+ { yylval->str = strdup(yytext); return token::FQNAME; } 155{COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; } 156 157{PATH}{VERSION}?"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; } 158{VERSION}"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; } 159{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; } 160 1610[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; } 1620{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; } 163{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; } 164L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; } 165 166{D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; } 167{D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; } 168{D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; } 169 170[\n] { yylloc->lines(); } 171. { /* ignore bad characters */ } 172 173%% 174 175#pragma clang diagnostic pop 176 177status_t parseFile(AST *ast) { 178 FILE *file = fopen(ast->getFilename().c_str(), "rb"); 179 180 if (file == NULL) { 181 return -errno; 182 } 183 184 yyscan_t scanner; 185 yylex_init_extra(ast, &scanner); 186 ast->setScanner(scanner); 187 188 yyset_in(file, scanner); 189 int res = yy::parser(ast).parse(); 190 191 yylex_destroy(scanner); 192 ast->setScanner(NULL); 193 194 fclose(file); 195 file = NULL; 196 197 if (res != 0) { 198 return UNKNOWN_ERROR; 199 } 200 201 return OK; 202} 203