]> granicus.if.org Git - icinga2/blob - tools/mkclass/class_lexer.ll
Make default getters and setters non-virtual
[icinga2] / tools / mkclass / class_lexer.ll
1 %{
2 /******************************************************************************
3  * Icinga 2                                                                   *
4  * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
5  *                                                                            *
6  * This program is free software; you can redistribute it and/or              *
7  * modify it under the terms of the GNU General Public License                *
8  * as published by the Free Software Foundation; either version 2             *
9  * of the License, or (at your option) any later version.                     *
10  *                                                                            *
11  * This program is distributed in the hope that it will be useful,            *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
14  * GNU General Public License for more details.                               *
15  *                                                                            *
16  * You should have received a copy of the GNU General Public License          *
17  * along with this program; if not, write to the Free Software Foundation     *
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
19  ******************************************************************************/
20
21 #include "classcompiler.hpp"
22
23 using namespace icinga;
24
25 #define YYLTYPE icinga::ClassDebugInfo
26
27 #include "class_parser.hh"
28
29 #define YY_EXTRA_TYPE ClassCompiler *
30 #define YY_USER_ACTION                                  \
31 do {                                                    \
32         yylloc->path = yyextra->GetPath();              \
33         yylloc->first_line = yylineno;                  \
34         yylloc->first_column = yycolumn;                \
35         yylloc->last_line = yylineno;                   \
36         yylloc->last_column = yycolumn + yyleng - 1;    \
37         yycolumn += yyleng;                             \
38 } while (0);
39
40 #define YY_INPUT(buf, result, max_size)                 \
41 do {                                                    \
42         result = yyextra->ReadInput(buf, max_size);     \
43 } while (0)
44
45 struct lex_buf {
46         char *buf;
47         size_t size;
48 };
49
50 static void lb_init(lex_buf *lb)
51 {
52         lb->buf = NULL;
53         lb->size = 0;
54 }
55
56 /*static void lb_cleanup(lex_buf *lb)
57 {
58         free(lb->buf);
59 }*/
60
61 static void lb_append_char(lex_buf *lb, char new_char)
62 {
63         const size_t block_size = 64;
64
65         size_t old_blocks = (lb->size + (block_size - 1)) / block_size;
66         size_t new_blocks = ((lb->size + 1) + (block_size - 1)) / block_size;
67
68         if (old_blocks != new_blocks) {
69                 char *new_buf = (char *)realloc(lb->buf, new_blocks * block_size);
70
71                 if (new_buf == NULL && new_blocks > 0)
72                         throw std::bad_alloc();
73
74                 lb->buf = new_buf;
75         }
76
77         lb->size++;
78         lb->buf[lb->size - 1] = new_char;
79 }
80
81 static char *lb_steal(lex_buf *lb)
82 {
83         lb_append_char(lb, '\0');
84
85         char *buf = lb->buf;
86         lb->buf = NULL;
87         lb->size = 0;
88         return buf;
89 }
90 %}
91
92 %option reentrant noyywrap yylineno
93 %option bison-bridge bison-locations
94 %option never-interactive nounistd
95 %option noinput nounput
96
97 %x HEREDOC
98 %x C_COMMENT
99
100 %%
101         lex_buf string_buf;
102
103 \{\{\{                          { lb_init(&string_buf); BEGIN(HEREDOC); }
104
105 <HEREDOC>\}\}\}                 {
106         BEGIN(INITIAL);
107
108         lb_append_char(&string_buf, '\0');
109
110         yylval->text = lb_steal(&string_buf);
111
112         return T_STRING;
113                                 }
114
115 <HEREDOC>(.|\n)                 { lb_append_char(&string_buf, yytext[0]); }
116
117 "/*"                            { BEGIN(C_COMMENT); }
118
119 <C_COMMENT>{
120 "*/"                            { BEGIN(INITIAL); }
121 [^*]                            /* ignore comment */
122 "*"                             /* ignore star */
123 }
124
125 <C_COMMENT><<EOF>>              {
126         fprintf(stderr, "End-of-file while in comment.\n");
127         yyterminate();
128                                 }
129 \/\/[^\n]*                      /* ignore C++-style comments */
130 [ \t\r\n]                       /* ignore whitespace */
131
132 #include                        { return T_INCLUDE; }
133 #impl_include                   { return T_IMPL_INCLUDE; }
134 class                           { return T_CLASS; }
135 namespace                       { return T_NAMESPACE; }
136 code                            { return T_CODE; }
137 load_after                      { return T_LOAD_AFTER; }
138 library                         { return T_LIBRARY; }
139 abstract                        { yylval->num = TAAbstract; return T_CLASS_ATTRIBUTE; }
140 vararg_constructor              { yylval->num = TAVarArgConstructor; return T_CLASS_ATTRIBUTE; }
141 config                          { yylval->num = FAConfig; return T_FIELD_ATTRIBUTE; }
142 state                           { yylval->num = FAState; return T_FIELD_ATTRIBUTE; }
143 enum                            { yylval->num = FAEnum; return T_FIELD_ATTRIBUTE; }
144 get_protected                   { yylval->num = FAGetProtected; return T_FIELD_ATTRIBUTE; }
145 set_protected                   { yylval->num = FASetProtected; return T_FIELD_ATTRIBUTE; }
146 protected                       { yylval->num = FAGetProtected | FASetProtected; return T_FIELD_ATTRIBUTE; }
147 no_storage                      { yylval->num = FANoStorage; return T_FIELD_ATTRIBUTE; }
148 no_user_modify                  { yylval->num = FANoUserModify; return T_FIELD_ATTRIBUTE; }
149 no_user_view                    { yylval->num = FANoUserView; return T_FIELD_ATTRIBUTE; }
150 deprecated                      { yylval->num = FADeprecated; return T_FIELD_ATTRIBUTE; }
151 get_virtual                     { yylval->num = FAGetVirtual; return T_FIELD_ATTRIBUTE; }
152 set_virtual                     { yylval->num = FASetVirtual; return T_FIELD_ATTRIBUTE; }
153 virtual                         { yylval->num = FAGetVirtual | FASetVirtual; return T_FIELD_ATTRIBUTE; }
154 navigation                      { return T_NAVIGATION; }
155 validator                       { return T_VALIDATOR; }
156 required                        { return T_REQUIRED; }
157 name                            { return T_NAME; }
158 array                           { return T_ARRAY; }
159 default                         { yylval->num = FTDefault; return T_FIELD_ACCESSOR_TYPE; }
160 get                             { yylval->num = FTGet; return T_FIELD_ACCESSOR_TYPE; }
161 set                             { yylval->num = FTSet; return T_FIELD_ACCESSOR_TYPE; }
162 track                           { yylval->num = FTTrack; return T_FIELD_ACCESSOR_TYPE; }
163 navigate                        { yylval->num = FTNavigate; return T_FIELD_ACCESSOR_TYPE; }
164 \"[^\"]+\"                      { yylval->text = strdup(yytext + 1); yylval->text[strlen(yylval->text) - 1] = '\0'; return T_STRING; }
165 \<[^ \>]*\>                     { yylval->text = strdup(yytext + 1); yylval->text[strlen(yylval->text) - 1] = '\0'; return T_ANGLE_STRING; }
166 [a-zA-Z_][:a-zA-Z0-9\-_]*       { yylval->text = strdup(yytext); return T_IDENTIFIER; }
167
168 .                               return yytext[0];
169
170 %%
171
172 void ClassCompiler::InitializeScanner(void)
173 {
174         yylex_init(&m_Scanner);
175         yyset_extra(this, m_Scanner);
176 }
177
178 void ClassCompiler::DestroyScanner(void)
179 {
180         yylex_destroy(m_Scanner);
181 }