icinga2/tools/mkclass/class_lexer.ll

168 lines
4.6 KiB
LLVM
Raw Normal View History

2013-10-26 09:41:45 +02:00
%{
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2013-10-26 09:41:45 +02:00
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; }
#impl_include { return T_IMPL_INCLUDE; }
2013-10-26 09:41:45 +02:00
class { return T_CLASS; }
namespace { return T_NAMESPACE; }
code { return T_CODE; }
load_after { return T_LOAD_AFTER; }
activation_priority { return T_ACTIVATION_PRIORITY; }
library { return T_LIBRARY; }
abstract { yylval->num = TAAbstract; return T_CLASS_ATTRIBUTE; }
vararg_constructor { yylval->num = TAVarArgConstructor; 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; }
no_storage { yylval->num = FANoStorage; return T_FIELD_ATTRIBUTE; }
no_user_modify { yylval->num = FANoUserModify; return T_FIELD_ATTRIBUTE; }
no_user_view { yylval->num = FANoUserView; return T_FIELD_ATTRIBUTE; }
deprecated { yylval->num = FADeprecated; return T_FIELD_ATTRIBUTE; }
get_virtual { yylval->num = FAGetVirtual; return T_FIELD_ATTRIBUTE; }
set_virtual { yylval->num = FASetVirtual; return T_FIELD_ATTRIBUTE; }
signal_with_old_value { yylval->num = FASignalWithOldValue; return T_FIELD_ATTRIBUTE; }
virtual { yylval->num = FAGetVirtual | FASetVirtual; return T_FIELD_ATTRIBUTE; }
navigation { return T_NAVIGATION; }
validator { return T_VALIDATOR; }
required { return T_REQUIRED; }
name { return T_NAME; }
array { return T_ARRAY; }
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; }
track { yylval->num = FTTrack; return T_FIELD_ACCESSOR_TYPE; }
navigate { yylval->num = FTNavigate; 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; }
-?[0-9]+(\.[0-9]+)? { yylval->num = strtod(yytext, NULL); return T_NUMBER; }
2013-10-26 09:41:45 +02:00
. return yytext[0];
%%
void ClassCompiler::InitializeScanner(void)
{
yylex_init(&m_Scanner);
yyset_extra(this, m_Scanner);
}
void ClassCompiler::DestroyScanner(void)
{
yylex_destroy(m_Scanner);
}