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