icinga2/tools/mkclass/class_lexer.ll

165 lines
4.9 KiB
LLVM
Raw Normal View History

2013-10-26 09:41:45 +02:00
%{
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
2013-10-26 09:41:45 +02:00
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
2014-05-25 16:23:35 +02:00
#include "classcompiler.hpp"
2013-10-26 09:41:45 +02:00
using namespace icinga;
#define YYLTYPE icinga::ClassDebugInfo
#include "class_parser.hh"
2013-10-26 09:41:45 +02:00
#define YY_EXTRA_TYPE ClassCompiler *
#define YY_USER_ACTION \
do { \
yylloc->path = yyextra->GetPath(); \
yylloc->first_line = yylineno; \
yylloc->first_column = yycolumn; \
yylloc->last_line = yylineno; \
yylloc->last_column = yycolumn + yyleng - 1; \
yycolumn += yyleng; \
2013-10-26 09:41:45 +02:00
} while (0);
#define YY_INPUT(buf, result, max_size) \
do { \
result = yyextra->ReadInput(buf, max_size); \
} while (0)
struct lex_buf {
char *buf;
size_t size;
};
static void lb_init(lex_buf *lb)
{
lb->buf = NULL;
lb->size = 0;
}
2014-05-22 09:02:44 +02:00
/*static void lb_cleanup(lex_buf *lb)
2013-10-26 09:41:45 +02:00
{
free(lb->buf);
2014-05-22 09:02:44 +02:00
}*/
2013-10-26 09:41:45 +02:00
static void lb_append_char(lex_buf *lb, char new_char)
{
const size_t block_size = 64;
size_t old_blocks = (lb->size + (block_size - 1)) / block_size;
size_t new_blocks = ((lb->size + 1) + (block_size - 1)) / block_size;
if (old_blocks != new_blocks) {
char *new_buf = (char *)realloc(lb->buf, new_blocks * block_size);
if (new_buf == NULL && new_blocks > 0)
throw std::bad_alloc();
lb->buf = new_buf;
}
lb->size++;
lb->buf[lb->size - 1] = new_char;
}
static char *lb_steal(lex_buf *lb)
{
lb_append_char(lb, '\0');
char *buf = lb->buf;
lb->buf = NULL;
lb->size = 0;
return buf;
}
%}
%option reentrant noyywrap yylineno
%option bison-bridge bison-locations
%option never-interactive nounistd
2014-11-06 06:23:25 +01:00
%option noinput nounput
2013-10-26 09:41:45 +02:00
%x HEREDOC
%x C_COMMENT
2013-10-26 09:41:45 +02:00
%%
lex_buf string_buf;
\{\{\{ { lb_init(&string_buf); BEGIN(HEREDOC); }
<HEREDOC>\}\}\} {
BEGIN(INITIAL);
lb_append_char(&string_buf, '\0');
yylval->text = lb_steal(&string_buf);
return T_STRING;
}
<HEREDOC>(.|\n) { lb_append_char(&string_buf, yytext[0]); }
"/*" { BEGIN(C_COMMENT); }
<C_COMMENT>{
"*/" { BEGIN(INITIAL); }
[^*] /* ignore comment */
"*" /* ignore star */
}
<C_COMMENT><<EOF>> {
fprintf(stderr, "End-of-file while in comment.\n");
yyterminate();
}
\/\/[^\n]* /* ignore C++-style comments */
2013-10-26 09:41:45 +02:00
[ \t\r\n] /* ignore whitespace */
#include { return T_INCLUDE; }
class { return T_CLASS; }
namespace { return T_NAMESPACE; }
code { return T_CODE; }
abstract { yylval->num = TAAbstract; return T_CLASS_ATTRIBUTE; }
2013-10-26 09:41:45 +02:00
config { yylval->num = FAConfig; return T_FIELD_ATTRIBUTE; }
state { yylval->num = FAState; return T_FIELD_ATTRIBUTE; }
enum { yylval->num = FAEnum; return T_FIELD_ATTRIBUTE; }
get_protected { yylval->num = FAGetProtected; return T_FIELD_ATTRIBUTE; }
set_protected { yylval->num = FASetProtected; return T_FIELD_ATTRIBUTE; }
2013-10-26 09:41:45 +02:00
protected { yylval->num = FAGetProtected | FASetProtected; return T_FIELD_ATTRIBUTE; }
internal { yylval->num = FAInternal; return T_FIELD_ATTRIBUTE; }
2013-10-26 09:41:45 +02:00
default { yylval->num = FTDefault; return T_FIELD_ACCESSOR_TYPE; }
get { yylval->num = FTGet; return T_FIELD_ACCESSOR_TYPE; }
set { yylval->num = FTSet; return T_FIELD_ACCESSOR_TYPE; }
2013-10-26 09:41:45 +02:00
\"[^\"]+\" { yylval->text = strdup(yytext + 1); yylval->text[strlen(yylval->text) - 1] = '\0'; return T_STRING; }
\<[^>]+\> { yylval->text = strdup(yytext + 1); yylval->text[strlen(yylval->text) - 1] = '\0'; return T_ANGLE_STRING; }
2013-10-26 09:41:45 +02:00
[a-zA-Z_][:a-zA-Z0-9\-_]* { yylval->text = strdup(yytext); return T_IDENTIFIER; }
. return yytext[0];
%%
void ClassCompiler::InitializeScanner(void)
{
yylex_init(&m_Scanner);
yyset_extra(this, m_Scanner);
}
void ClassCompiler::DestroyScanner(void)
{
yylex_destroy(m_Scanner);
}