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
17%{
18#include <string.h>
19#include <stdlib.h>
20
21#include "aidl_language.h"
22#include "parser.h"
23#include "aidl_language_y.h"
24
25#ifndef YYSTYPE
26#define YYSTYPE yy::parser::semantic_type
27#endif
28
29#ifndef YYLTYPE
30#define YYLTYPE yy::parser::location_type
31#endif
32
33#define YY_USER_ACTION yylloc->columns(yyleng);
34%}
35
36%option noyywrap
37%option nounput
38%option noinput
39%option reentrant
40%option bison-bridge
41%option bison-locations
42
43%x LONG_COMMENT
44
45identifier  [_a-zA-Z][_a-zA-Z0-9]*
46whitespace  ([ \t\r]+)
47intvalue    [0-9_]+(u8|u32|u64|[lL])*
48hexvalue    0[x|X][0-9a-fA-F_]+(u8|u32|u64|[lL])*
49floatvalue  [0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?f?
50
51%%
52%{
53  /* This happens at every call to yylex (every time we receive one token) */
54  using android::aidl::Comments;
55  using android::aidl::Comment;
56  std::string extra_text;
57  Comments comments;
58  yylloc->step();
59%}
60
61\/\*                  { extra_text += yytext; BEGIN(LONG_COMMENT); }
62<LONG_COMMENT>\*+\/   { extra_text += yytext; yylloc->step(); BEGIN(INITIAL);
63                        comments.push_back({extra_text});
64                        extra_text.clear(); }
65<LONG_COMMENT>\*+     { extra_text += yytext; }
66<LONG_COMMENT>\n+     { extra_text += yytext; yylloc->lines(yyleng); }
67<LONG_COMMENT>[^*\n]+ { extra_text += yytext; }
68
69\"([^\"]|\\.)*\"      { yylval->token = new AidlToken(yytext, comments);
70                        return yy::parser::token::C_STR; }
71
72\/\/.*                { extra_text += yytext; extra_text += "\n";
73                        comments.push_back({extra_text});
74                        extra_text.clear(); }
75
76\n+                   { yylloc->lines(yyleng); yylloc->step(); }
77{whitespace}          {}
78<<EOF>>               { yyterminate(); }
79
80    /* symbols */
81"("                   { return('('); }
82")"                   { return(')'); }
83"<"                   { return('<'); }
84">"                   { return('>'); }
85"{"                   { return('{'); }
86"}"                   { return('}'); }
87"["                   { return('['); }
88"]"                   { return(']'); }
89":"                   { return(':'); }
90";"                   { return(';'); }
91","                   { return(','); }
92"."                   { return('.'); }
93"="                   { return('='); }
94"+"                   { return('+'); }
95"-"                   { return('-'); }
96"*"                   { return('*'); }
97"/"                   { return('/'); }
98"%"                   { return('%'); }
99"&"                   { return('&'); }
100"|"                   { return('|'); }
101"^"                   { return('^'); }
102"<<"                  { return(yy::parser::token::LSHIFT); }
103">>"                  { return(yy::parser::token::RSHIFT); }
104"&&"                  { return(yy::parser::token::LOGICAL_AND); }
105"||"                  { return(yy::parser::token::LOGICAL_OR);  }
106"!"                   { return('!'); }
107"~"                   { return('~'); }
108"<="                  { return(yy::parser::token::LEQ); }
109">="                  { return(yy::parser::token::GEQ); }
110"=="                  { return(yy::parser::token::EQUALITY); }
111"!="                  { return(yy::parser::token::NEQ); }
112
113    /* annotations */
114@{identifier}         { yylval->token = new AidlToken(yytext + 1, comments);
115                        return yy::parser::token::ANNOTATION;
116                      }
117
118    /* keywords */
119parcelable            { yylval->token = new AidlToken("parcelable", comments);
120                        return yy::parser::token::PARCELABLE;
121                      }
122import                { yylval->token = new AidlToken("import", comments);
123                        return yy::parser::token::IMPORT; }
124package               { yylval->token = new AidlToken("package", comments);
125                        return yy::parser::token::PACKAGE; }
126in                    { return yy::parser::token::IN; }
127out                   { return yy::parser::token::OUT; }
128inout                 { return yy::parser::token::INOUT; }
129cpp_header            { yylval->token = new AidlToken("cpp_header", comments);
130                        return yy::parser::token::CPP_HEADER; }
131ndk_header            { yylval->token = new AidlToken("ndk_header", comments);
132                        return yy::parser::token::NDK_HEADER; }
133rust_type            { yylval->token = new AidlToken("rust_type", comments);
134                        return yy::parser::token::RUST_TYPE; }
135const                 { yylval->token = new AidlToken("const", comments);
136                        return yy::parser::token::CONST; }
137true                  { return yy::parser::token::TRUE_LITERAL; }
138false                 { return yy::parser::token::FALSE_LITERAL; }
139
140interface             { yylval->token = new AidlToken("interface", comments);
141                        return yy::parser::token::INTERFACE;
142                      }
143oneway                { yylval->token = new AidlToken("oneway", comments);
144                        return yy::parser::token::ONEWAY;
145                      }
146enum                  { yylval->token = new AidlToken("enum", comments);
147                        return yy::parser::token::ENUM;
148                      }
149union                 { yylval->token = new AidlToken("union", comments);
150                        return yy::parser::token::UNION;
151                      }
152
153    /* scalars */
154{identifier}          { yylval->token = new AidlToken(yytext, comments);
155                        return yy::parser::token::IDENTIFIER;
156                      }
157'.'                   { yylval->token = new AidlToken(std::string(yytext, yyleng), comments);
158                        return yy::parser::token::CHARVALUE; }
159{intvalue}            { yylval->token = new AidlToken(yytext, comments);
160                        return yy::parser::token::INTVALUE; }
161{floatvalue}          { yylval->token = new AidlToken(yytext, comments);
162                        return yy::parser::token::FLOATVALUE; }
163{hexvalue}            { yylval->token = new AidlToken(yytext, comments);
164                        return yy::parser::token::HEXVALUE; }
165
166  /* lexical error! */
167.                     { return yy::parser::token::UNKNOWN; }
168
169%%
170
171// comment and whitespace handling
172// ================================================
173