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                 [.]
26AT                  [@]
27VERSION             {AT}{D}+{DOT}{D}+
28FQNAME              ({COMPONENT}|{VERSION})(({DOT}|":"+){COMPONENT}|{VERSION})*
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 "DeathRecipientType.h"
38#include "DocComment.h"
39#include "EnumType.h"
40#include "HandleType.h"
41#include "MemoryType.h"
42#include "Method.h"
43#include "PointerType.h"
44#include "ScalarType.h"
45#include "Scope.h"
46#include "StringType.h"
47#include "VectorType.h"
48#include "RefType.h"
49#include "FmqType.h"
50
51#include "hidl-gen_y.h"
52
53#include <assert.h>
54
55using namespace android;
56using token = yy::parser::token;
57
58static std::string gCurrentComment;
59
60#define SCALAR_TYPE(kind)                                        \
61    {                                                            \
62        yylval->type = new ScalarType(ScalarType::kind, *scope); \
63        return token::TYPE;                                      \
64    }
65
66#define YY_DECL int yylex(YYSTYPE* yylval_param, YYLTYPE* yylloc_param,  \
67    yyscan_t yyscanner, android::Scope** const scope)
68
69#define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng);
70
71#pragma clang diagnostic push
72#pragma clang diagnostic ignored "-Wunused-parameter"
73#pragma clang diagnostic ignored "-Wdeprecated-register"
74#pragma clang diagnostic ignored "-Wregister"
75
76%}
77
78%option yylineno
79%option noyywrap
80%option nounput
81%option noinput
82%option reentrant
83%option bison-bridge
84%option bison-locations
85
86%x COMMENT_STATE
87%x DOC_COMMENT_STATE
88
89%%
90
91"/**"                       { gCurrentComment.clear(); BEGIN(DOC_COMMENT_STATE); }
92<DOC_COMMENT_STATE>"*/"     {
93                                BEGIN(INITIAL);
94                                yylval->docComment = new DocComment(gCurrentComment);
95                                return token::DOC_COMMENT;
96                            }
97<DOC_COMMENT_STATE>[^*\n]*                          { gCurrentComment += yytext; }
98<DOC_COMMENT_STATE>[\n]                             { gCurrentComment += yytext; yylloc->lines(); }
99<DOC_COMMENT_STATE>[*]                              { gCurrentComment += yytext; }
100
101"/*"                        { BEGIN(COMMENT_STATE); }
102<COMMENT_STATE>"*/"         { BEGIN(INITIAL); }
103<COMMENT_STATE>[\n]         { yylloc->lines(); }
104<COMMENT_STATE>.            { }
105
106"//"[^\r\n]*        { /* skip C++ style comment */ }
107
108"enum"              { return token::ENUM; }
109"extends"           { return token::EXTENDS; }
110"generates"         { return token::GENERATES; }
111"import"            { return token::IMPORT; }
112"interface"         { return token::INTERFACE; }
113"package"           { return token::PACKAGE; }
114"struct"            { return token::STRUCT; }
115"typedef"           { return token::TYPEDEF; }
116"union"             { return token::UNION; }
117"bitfield"          { yylval->templatedType = new BitFieldType(*scope); return token::TEMPLATED; }
118"vec"               { yylval->templatedType = new VectorType(*scope); return token::TEMPLATED; }
119"ref"               { yylval->templatedType = new RefType(*scope); return token::TEMPLATED; }
120"oneway"            { return token::ONEWAY; }
121
122"bool"              { SCALAR_TYPE(KIND_BOOL); }
123"int8_t"            { SCALAR_TYPE(KIND_INT8); }
124"uint8_t"           { SCALAR_TYPE(KIND_UINT8); }
125"int16_t"           { SCALAR_TYPE(KIND_INT16); }
126"uint16_t"          { SCALAR_TYPE(KIND_UINT16); }
127"int32_t"           { SCALAR_TYPE(KIND_INT32); }
128"uint32_t"          { SCALAR_TYPE(KIND_UINT32); }
129"int64_t"           { SCALAR_TYPE(KIND_INT64); }
130"uint64_t"          { SCALAR_TYPE(KIND_UINT64); }
131"float"             { SCALAR_TYPE(KIND_FLOAT); }
132"double"            { SCALAR_TYPE(KIND_DOUBLE); }
133
134"death_recipient"   { yylval->type = new DeathRecipientType(*scope); return token::TYPE; }
135"handle"            { yylval->type = new HandleType(*scope); return token::TYPE; }
136"memory"            { yylval->type = new MemoryType(*scope); return token::TYPE; }
137"pointer"           { yylval->type = new PointerType(*scope); return token::TYPE; }
138"string"            { yylval->type = new StringType(*scope); return token::TYPE; }
139
140"fmq_sync"          { yylval->type = new FmqType("::android::hardware", "MQDescriptorSync", *scope); return token::TEMPLATED; }
141"fmq_unsync"        { yylval->type = new FmqType("::android::hardware", "MQDescriptorUnsync", *scope); return token::TEMPLATED; }
142
143"("                 { return('('); }
144")"                 { return(')'); }
145"<"                 { return('<'); }
146">"                 { return('>'); }
147"{"                 { return('{'); }
148"}"                 { return('}'); }
149"["                 { return('['); }
150"]"                 { return(']'); }
151":"                 { return(':'); }
152";"                 { return(';'); }
153","                 { return(','); }
154"."                 { return('.'); }
155"="                 { return('='); }
156"+"                 { return('+'); }
157"-"                 { return('-'); }
158"*"                 { return('*'); }
159"/"                 { return('/'); }
160"%"                 { return('%'); }
161"&"                 { return('&'); }
162"|"                 { return('|'); }
163"^"                 { return('^'); }
164"<<"                { return(token::LSHIFT); }
165">>"                { return(token::RSHIFT); }
166"&&"                { return(token::LOGICAL_AND); }
167"||"                { return(token::LOGICAL_OR);  }
168"!"                 { return('!'); }
169"~"                 { return('~'); }
170"<="                { return(token::LEQ); }
171">="                { return(token::GEQ); }
172"=="                { return(token::EQUALITY); }
173"!="                { return(token::NEQ); }
174"?"                 { return('?'); }
175"@"                 { return('@'); }
176
177{COMPONENT}         { yylval->str = strdup(yytext); return token::IDENTIFIER; }
178{FQNAME}            { yylval->str = strdup(yytext); return token::FQNAME; }
179
1800[xX]{H}+{IS}?      { yylval->str = strdup(yytext); return token::INTEGER; }
1810{D}+{IS}?          { yylval->str = strdup(yytext); return token::INTEGER; }
182{D}+{IS}?           { yylval->str = strdup(yytext); return token::INTEGER; }
183L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; }
184
185{D}+{E}{FS}?        { yylval->str = strdup(yytext); return token::FLOAT; }
186{D}+\.{E}?{FS}?     { yylval->str = strdup(yytext); return token::FLOAT; }
187{D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; }
188
189\n|\r\n             { yylloc->lines(); }
190[ \t\f\v]           { /* ignore all other whitespace */ }
191
192.                   { yylval->str = strdup(yytext); return token::UNKNOWN; }
193
194%%
195
196#pragma clang diagnostic pop
197
198namespace android {
199
200status_t parseFile(AST* ast, std::unique_ptr<FILE, std::function<void(FILE *)>> file) {
201    yyscan_t scanner;
202    yylex_init(&scanner);
203
204    yyset_in(file.get(), scanner);
205
206    Scope* scopeStack = ast->getRootScope();
207    int res = yy::parser(scanner, ast, &scopeStack).parse();
208
209    yylex_destroy(scanner);
210
211    if (res != 0 || ast->syntaxErrors() != 0) {
212        return UNKNOWN_ERROR;
213    }
214
215    return OK;
216}
217
218}  // namespace android
219