hidl-gen_l.ll revision c57c8bb9f368faca636bdb6b39773e72255e8b08
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 "MemoryType.h"
40#include "Method.h"
41#include "PointerType.h"
42#include "ScalarType.h"
43#include "StringType.h"
44#include "VectorType.h"
45#include "RefType.h"
46#include "PredefinedType.h"
47
48#include "hidl-gen_y.h"
49
50#include <assert.h>
51
52using namespace android;
53using token = yy::parser::token;
54
55int check_type(yyscan_t yyscanner, struct yyguts_t *yyg);
56
57#define SCALAR_TYPE(kind)                                       \
58    do {                                                        \
59        yylval->type = new ScalarType(ScalarType::kind);        \
60        return token::TYPE;                                   \
61    } while (0)
62
63#define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng);
64
65#pragma clang diagnostic push
66#pragma clang diagnostic ignored "-Wunused-parameter"
67#pragma clang diagnostic ignored "-Wdeprecated-register"
68
69%}
70
71%option yylineno
72%option noyywrap
73%option reentrant
74%option bison-bridge
75%option bison-locations
76
77%x COMMENT_STATE
78
79%%
80
81"/*"                 { BEGIN(COMMENT_STATE); }
82<COMMENT_STATE>"*/"  { BEGIN(INITIAL); }
83<COMMENT_STATE>[\n]  { yylloc->lines(); }
84<COMMENT_STATE>.     { }
85
86"//"[^\r\n]*         { /* skip C++ style comment */ }
87
88"enum"			{ return token::ENUM; }
89"extends"		{ return token::EXTENDS; }
90"generates"		{ return token::GENERATES; }
91"import"		{ return token::IMPORT; }
92"interface"		{ return token::INTERFACE; }
93"package"		{ return token::PACKAGE; }
94"struct"		{ return token::STRUCT; }
95"typedef"		{ return token::TYPEDEF; }
96"union"			{ return token::UNION; }
97"bitfield"		{ yylval->templatedType = new BitFieldType; return token::TEMPLATED; }
98"vec"			{ yylval->templatedType = new VectorType; return token::TEMPLATED; }
99"ref"			{ yylval->templatedType = new RefType; return token::TEMPLATED; }
100"oneway"		{ return token::ONEWAY; }
101
102"bool"			{ SCALAR_TYPE(KIND_BOOL); }
103"int8_t"		{ SCALAR_TYPE(KIND_INT8); }
104"uint8_t"		{ SCALAR_TYPE(KIND_UINT8); }
105"int16_t"		{ SCALAR_TYPE(KIND_INT16); }
106"uint16_t"		{ SCALAR_TYPE(KIND_UINT16); }
107"int32_t"		{ SCALAR_TYPE(KIND_INT32); }
108"uint32_t"		{ SCALAR_TYPE(KIND_UINT32); }
109"int64_t"		{ SCALAR_TYPE(KIND_INT64); }
110"uint64_t"		{ SCALAR_TYPE(KIND_UINT64); }
111"float"			{ SCALAR_TYPE(KIND_FLOAT); }
112"double"		{ SCALAR_TYPE(KIND_DOUBLE); }
113
114"handle"		{ yylval->type = new HandleType; return token::TYPE; }
115"memory"		{ yylval->type = new MemoryType; return token::TYPE; }
116"pointer"		{ yylval->type = new PointerType; return token::TYPE; }
117"string"		{ yylval->type = new StringType; return token::TYPE; }
118
119"MQDescriptorSync" { yylval->type = new PredefinedType("::android::hardware", "MQDescriptorSync"); return token::TYPE; }
120"MQDescriptorUnsync" { yylval->type = new PredefinedType("::android::hardware", "MQDescriptorUnsync"); return token::TYPE; }
121
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('/'); }
139"%"			{ return('%'); }
140"&"			{ return('&'); }
141"|"			{ return('|'); }
142"^"			{ return('^'); }
143"<<"			{ return(token::LSHIFT); }
144">>"			{ return(token::RSHIFT); }
145"&&"			{ return(token::LOGICAL_AND); }
146"||"			{ return(token::LOGICAL_OR);  }
147"!"			{ return('!'); }
148"~"			{ return('~'); }
149"<="			{ return(token::LEQ); }
150">="			{ return(token::GEQ); }
151"=="			{ return(token::EQUALITY); }
152"!="			{ return(token::NEQ); }
153"?"			{ return('?'); }
154"@"			{ return('@'); }
155
156{PATH}{VERSION}?"::"{PATH}      { yylval->str = strdup(yytext); return token::FQNAME; }
157{VERSION}"::"{PATH}             { yylval->str = strdup(yytext); return token::FQNAME; }
158{PATH}{VERSION}                 { yylval->str = strdup(yytext); return token::FQNAME; }
159{COMPONENT}({DOT}{COMPONENT})+  { yylval->str = strdup(yytext); return token::FQNAME; }
160{COMPONENT}                     { yylval->str = strdup(yytext); return token::IDENTIFIER; }
161
162{PATH}{VERSION}?"::"{PATH}":"{COMPONENT}      { yylval->str = strdup(yytext); return token::FQNAME; }
163{VERSION}"::"{PATH}":"{COMPONENT}             { yylval->str = strdup(yytext); return token::FQNAME; }
164{PATH}":"{COMPONENT}                          { yylval->str = strdup(yytext); return token::FQNAME; }
165
1660[xX]{H}+{IS}?		{ yylval->str = strdup(yytext); return token::INTEGER; }
1670{D}+{IS}?		{ yylval->str = strdup(yytext); return token::INTEGER; }
168{D}+{IS}?		{ yylval->str = strdup(yytext); return token::INTEGER; }
169L?\"(\\.|[^\\"])*\"	{ yylval->str = strdup(yytext); return token::STRING_LITERAL; }
170
171{D}+{E}{FS}?		{ yylval->str = strdup(yytext); return token::FLOAT; }
172{D}+\.{E}?{FS}?		{ yylval->str = strdup(yytext); return token::FLOAT; }
173{D}*\.{D}+{E}?{FS}?	{ yylval->str = strdup(yytext); return token::FLOAT; }
174
175[\n]		{ yylloc->lines(); }
176.			{ /* ignore bad characters */ }
177
178%%
179
180#pragma clang diagnostic pop
181
182status_t parseFile(AST *ast) {
183    FILE *file = fopen(ast->getFilename().c_str(), "rb");
184
185    if (file == NULL) {
186        return -errno;
187    }
188
189    yyscan_t scanner;
190    yylex_init_extra(ast, &scanner);
191    ast->setScanner(scanner);
192
193    yyset_in(file, scanner);
194    int res = yy::parser(ast).parse();
195
196    yylex_destroy(scanner);
197    ast->setScanner(NULL);
198
199    fclose(file);
200    file = NULL;
201
202    if (res != 0 || ast->syntaxErrors() != 0) {
203        return UNKNOWN_ERROR;
204    }
205
206    return OK;
207}
208