2012-05-31 08:45:02 +02:00
|
|
|
%{
|
2012-05-31 16:04:51 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2012-05-31 16:04:51 +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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "config/configcompiler.h"
|
|
|
|
#include "config/typerule.h"
|
|
|
|
#include "config/configcompilercontext.h"
|
2013-11-03 12:40:39 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-11-03 13:45:26 +01:00
|
|
|
#include "config/config_parser.hh"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <sstream>
|
2012-05-31 09:14:44 +02:00
|
|
|
|
2013-11-03 12:40:39 +01:00
|
|
|
#define YYLTYPE icinga::DebugInfo
|
2012-05-31 08:45:02 +02:00
|
|
|
|
2012-06-06 14:38:28 +02:00
|
|
|
#define YY_EXTRA_TYPE ConfigCompiler *
|
|
|
|
#define YY_USER_ACTION \
|
|
|
|
do { \
|
2012-07-08 21:33:51 +02:00
|
|
|
yylloc->Path = yyextra->GetPath(); \
|
2012-06-06 14:38:28 +02:00
|
|
|
yylloc->FirstLine = yylineno; \
|
|
|
|
yylloc->FirstColumn = yycolumn; \
|
|
|
|
yylloc->LastLine = yylineno; \
|
|
|
|
yylloc->LastColumn = yycolumn + yyleng - 1; \
|
|
|
|
yycolumn += yyleng; \
|
|
|
|
} while (0);
|
2012-05-31 08:45:02 +02:00
|
|
|
|
2012-05-31 09:43:46 +02:00
|
|
|
#define YY_INPUT(buf, result, max_size) \
|
|
|
|
do { \
|
|
|
|
result = yyextra->ReadInput(buf, max_size); \
|
2012-05-31 08:45:02 +02:00
|
|
|
} while (0)
|
2013-02-16 19:12:56 +01:00
|
|
|
|
|
|
|
struct lex_buf {
|
|
|
|
char *buf;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void lb_init(lex_buf *lb)
|
|
|
|
{
|
|
|
|
lb->buf = NULL;
|
|
|
|
lb->size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lb_cleanup(lex_buf *lb)
|
|
|
|
{
|
|
|
|
free(lb->buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lb_append_char(lex_buf *lb, char new_char)
|
|
|
|
{
|
2013-03-27 13:51:29 +01:00
|
|
|
const size_t block_size = 64;
|
2013-02-16 19:12:56 +01:00
|
|
|
|
2013-03-27 13:51:29 +01:00
|
|
|
size_t old_blocks = (lb->size + (block_size - 1)) / block_size;
|
|
|
|
size_t new_blocks = ((lb->size + 1) + (block_size - 1)) / block_size;
|
2013-02-16 19:12:56 +01:00
|
|
|
|
2013-03-27 13:51:29 +01:00
|
|
|
if (old_blocks != new_blocks) {
|
|
|
|
char *new_buf = (char *)realloc(lb->buf, new_blocks * block_size);
|
2013-02-16 19:12:56 +01:00
|
|
|
|
2013-03-27 13:51:29 +01:00
|
|
|
if (new_buf == NULL && new_blocks > 0)
|
|
|
|
throw std::bad_alloc();
|
|
|
|
|
|
|
|
lb->buf = new_buf;
|
|
|
|
}
|
2013-02-16 19:12:56 +01:00
|
|
|
|
2013-03-27 13:51:29 +01:00
|
|
|
lb->size++;
|
2013-02-16 19:12:56 +01:00
|
|
|
lb->buf[lb->size - 1] = new_char;
|
|
|
|
}
|
2013-03-27 16:01:16 +01:00
|
|
|
|
|
|
|
static char *lb_steal(lex_buf *lb)
|
|
|
|
{
|
|
|
|
lb_append_char(lb, '\0');
|
|
|
|
|
|
|
|
char *buf = lb->buf;
|
|
|
|
lb->buf = NULL;
|
|
|
|
lb->size = 0;
|
|
|
|
return buf;
|
|
|
|
}
|
2012-05-31 08:45:02 +02:00
|
|
|
%}
|
|
|
|
|
|
|
|
%option reentrant noyywrap yylineno
|
|
|
|
%option bison-bridge bison-locations
|
2012-05-31 09:14:44 +02:00
|
|
|
%option never-interactive nounistd
|
2012-05-31 08:45:02 +02:00
|
|
|
|
2013-02-16 19:12:56 +01:00
|
|
|
%x C_COMMENT
|
|
|
|
%x STRING
|
2013-02-16 22:49:13 +01:00
|
|
|
%x HEREDOC
|
2012-05-31 08:45:02 +02:00
|
|
|
|
|
|
|
%%
|
2013-02-16 19:12:56 +01:00
|
|
|
lex_buf string_buf;
|
|
|
|
|
|
|
|
\" { lb_init(&string_buf); BEGIN(STRING); }
|
|
|
|
|
|
|
|
<STRING>\" {
|
|
|
|
BEGIN(INITIAL);
|
|
|
|
|
|
|
|
lb_append_char(&string_buf, '\0');
|
|
|
|
|
|
|
|
yylval->text = lb_steal(&string_buf);
|
|
|
|
|
|
|
|
return T_STRING;
|
|
|
|
}
|
|
|
|
|
|
|
|
<STRING>\n {
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream msgbuf;
|
2013-02-16 19:12:56 +01:00
|
|
|
msgbuf << "Unterminated string found: " << *yylloc;
|
2013-09-24 13:13:14 +02:00
|
|
|
ConfigCompilerContext::GetInstance()->AddMessage(true, msgbuf.str());
|
2013-02-16 19:12:56 +01:00
|
|
|
BEGIN(INITIAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
<STRING>\\[0-7]{1,3} {
|
|
|
|
/* octal escape sequence */
|
|
|
|
int result;
|
|
|
|
|
|
|
|
(void) sscanf(yytext + 1, "%o", &result);
|
|
|
|
|
|
|
|
if (result > 0xff) {
|
|
|
|
/* error, constant is out-of-bounds */
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream msgbuf;
|
2013-02-16 19:12:56 +01:00
|
|
|
msgbuf << "Constant is out-of-bounds: " << yytext << " " << *yylloc;
|
2013-09-24 13:13:14 +02:00
|
|
|
ConfigCompilerContext::GetInstance()->AddMessage(true, msgbuf.str());
|
2013-02-16 19:12:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lb_append_char(&string_buf, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
<STRING>\\[0-9]+ {
|
|
|
|
/* generate error - bad escape sequence; something
|
|
|
|
* like '\48' or '\0777777'
|
|
|
|
*/
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream msgbuf;
|
2013-02-16 19:12:56 +01:00
|
|
|
msgbuf << "Bad escape sequence found: " << yytext << " " << *yylloc;
|
2013-09-24 13:13:14 +02:00
|
|
|
ConfigCompilerContext::GetInstance()->AddMessage(true, msgbuf.str());
|
2013-02-16 19:12:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
<STRING>\\n { lb_append_char(&string_buf, '\n'); }
|
|
|
|
<STRING>\\t { lb_append_char(&string_buf, '\t'); }
|
|
|
|
<STRING>\\r { lb_append_char(&string_buf, '\r'); }
|
|
|
|
<STRING>\\b { lb_append_char(&string_buf, '\b'); }
|
|
|
|
<STRING>\\f { lb_append_char(&string_buf, '\f'); }
|
|
|
|
<STRING>\\(.|\n) { lb_append_char(&string_buf, yytext[1]); }
|
|
|
|
|
|
|
|
<STRING>[^\\\n\"]+ {
|
|
|
|
char *yptr = yytext;
|
|
|
|
|
|
|
|
while (*yptr)
|
|
|
|
lb_append_char(&string_buf, *yptr++);
|
|
|
|
}
|
|
|
|
|
2013-02-16 22:49:13 +01:00
|
|
|
\{\{\{ { 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]); }
|
|
|
|
|
2013-02-16 19:12:56 +01:00
|
|
|
<INITIAL>{
|
|
|
|
"/*" BEGIN(C_COMMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
<C_COMMENT>{
|
|
|
|
"*/" BEGIN(INITIAL);
|
2013-03-27 15:51:33 +01:00
|
|
|
[^*] /* ignore comment */
|
2013-02-16 19:12:56 +01:00
|
|
|
"*" /* ignore star */
|
|
|
|
}
|
|
|
|
|
2013-10-09 08:56:28 +02:00
|
|
|
<C_COMMENT><<EOF>> {
|
|
|
|
std::ostringstream msgbuf;
|
|
|
|
msgbuf << "End-of-file while in comment: " << yytext << " " << *yylloc;
|
|
|
|
ConfigCompilerContext::GetInstance()->AddMessage(true, msgbuf.str());
|
|
|
|
yyterminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-24 01:10:34 +01:00
|
|
|
\/\/[^\n]* /* ignore C++-style comments */
|
2013-03-27 15:51:33 +01:00
|
|
|
[ \t\r\n] /* ignore whitespace */
|
2013-02-16 19:12:56 +01:00
|
|
|
|
|
|
|
<INITIAL>{
|
2013-02-02 14:28:11 +01:00
|
|
|
type return T_TYPE;
|
|
|
|
dictionary { yylval->type = TypeDictionary; return T_TYPE_DICTIONARY; }
|
2013-03-14 12:17:46 +01:00
|
|
|
array { yylval->type = TypeArray; return T_TYPE_ARRAY; }
|
2013-02-02 14:28:11 +01:00
|
|
|
number { yylval->type = TypeNumber; return T_TYPE_NUMBER; }
|
|
|
|
string { yylval->type = TypeString; return T_TYPE_STRING; }
|
|
|
|
scalar { yylval->type = TypeScalar; return T_TYPE_SCALAR; }
|
|
|
|
any { yylval->type = TypeAny; return T_TYPE_ANY; }
|
2013-05-03 10:48:28 +02:00
|
|
|
name { yylval->type = TypeName; return T_TYPE_NAME; }
|
2013-02-06 00:32:05 +01:00
|
|
|
%validator { return T_VALIDATOR; }
|
|
|
|
%require { return T_REQUIRE; }
|
|
|
|
%attribute { return T_ATTRIBUTE; }
|
2012-05-31 16:04:51 +02:00
|
|
|
object return T_OBJECT;
|
2013-02-16 09:18:34 +01:00
|
|
|
template return T_TEMPLATE;
|
2013-03-21 14:30:28 +01:00
|
|
|
include return T_INCLUDE;
|
2013-11-29 10:39:48 +01:00
|
|
|
include_recursive return T_INCLUDE_RECURSIVE;
|
2013-03-21 14:30:28 +01:00
|
|
|
library return T_LIBRARY;
|
2012-05-31 16:04:51 +02:00
|
|
|
inherits return T_INHERITS;
|
2012-06-01 16:49:33 +02:00
|
|
|
null return T_NULL;
|
2013-02-02 14:28:11 +01:00
|
|
|
partial return T_PARTIAL;
|
2013-01-24 10:08:03 +01:00
|
|
|
true { yylval->num = 1; return T_NUMBER; }
|
|
|
|
false { yylval->num = 0; return T_NUMBER; }
|
2013-12-04 11:04:36 +01:00
|
|
|
set return T_VAR;
|
|
|
|
var return T_VAR;
|
|
|
|
const return T_CONST;
|
2014-03-18 11:44:09 +01:00
|
|
|
apply return T_APPLY;
|
|
|
|
to return T_TO;
|
|
|
|
where return T_WHERE;
|
2014-03-27 12:30:24 +01:00
|
|
|
import return T_IMPORT;
|
2014-03-23 11:27:40 +01:00
|
|
|
\<\< { yylval->op = &AExpression::OpShiftLeft; return T_SHIFT_LEFT; }
|
|
|
|
\>\> { yylval->op = &AExpression::OpShiftRight; return T_SHIFT_RIGHT; }
|
|
|
|
\<= { yylval->op = &AExpression::OpLessThanOrEqual; return T_LESS_THAN_OR_EQUAL; }
|
|
|
|
\>= { yylval->op = &AExpression::OpGreaterThanOrEqual; return T_GREATER_THAN_OR_EQUAL; }
|
|
|
|
== { yylval->op = &AExpression::OpEqual; return T_EQUAL; }
|
|
|
|
!= { yylval->op = &AExpression::OpNotEqual; return T_NOT_EQUAL; }
|
|
|
|
!in { yylval->op = &AExpression::OpNotIn; return T_NOT_IN; }
|
|
|
|
in { yylval->op = &AExpression::OpIn; return T_IN; }
|
|
|
|
&& { yylval->op = &AExpression::OpLogicalAnd; return T_LOGICAL_AND; }
|
|
|
|
\|\| { yylval->op = &AExpression::OpLogicalOr; return T_LOGICAL_OR; }
|
2013-06-25 10:06:58 +02:00
|
|
|
[a-zA-Z_][:a-zA-Z0-9\-_]* { yylval->text = strdup(yytext); return T_IDENTIFIER; }
|
2013-02-01 22:44:58 +01:00
|
|
|
\<[^\>]*\> { yytext[yyleng-1] = '\0'; yylval->text = strdup(yytext + 1); return T_STRING_ANGLE; }
|
2013-02-08 10:14:24 +01:00
|
|
|
-?[0-9]+(\.[0-9]+)?ms { yylval->num = strtod(yytext, NULL) / 1000; return T_NUMBER; }
|
2013-09-26 19:03:27 +02:00
|
|
|
-?[0-9]+(\.[0-9]+)?d { yylval->num = strtod(yytext, NULL) * 60 * 60 * 24; return T_NUMBER; }
|
2012-09-25 11:17:04 +02:00
|
|
|
-?[0-9]+(\.[0-9]+)?h { yylval->num = strtod(yytext, NULL) * 60 * 60; return T_NUMBER; }
|
|
|
|
-?[0-9]+(\.[0-9]+)?m { yylval->num = strtod(yytext, NULL) * 60; return T_NUMBER; }
|
|
|
|
-?[0-9]+(\.[0-9]+)?s { yylval->num = strtod(yytext, NULL); return T_NUMBER; }
|
2012-07-11 21:03:22 +02:00
|
|
|
-?[0-9]+(\.[0-9]+)? { yylval->num = strtod(yytext, NULL); return T_NUMBER; }
|
2014-03-23 11:27:40 +01:00
|
|
|
= { yylval->op = &AExpression::OpSet; return T_SET; }
|
|
|
|
\+= { yylval->op = &AExpression::OpSetPlus; return T_SET_PLUS; }
|
|
|
|
-= { yylval->op = &AExpression::OpSetMinus; return T_SET_MINUS; }
|
|
|
|
\*= { yylval->op = &AExpression::OpSetMultiply; return T_SET_MULTIPLY; }
|
|
|
|
\/= { yylval->op = &AExpression::OpSetDivide; return T_SET_DIVIDE; }
|
|
|
|
\+ { yylval->op = &AExpression::OpAdd; return T_PLUS; }
|
|
|
|
\- { yylval->op = &AExpression::OpSubtract; return T_MINUS; }
|
|
|
|
\* { yylval->op = &AExpression::OpMultiply; return T_MULTIPLY; }
|
2014-03-24 12:54:23 +01:00
|
|
|
\/ { yylval->op = &AExpression::OpMultiply; return T_DIVIDE_OP; }
|
2014-03-23 11:27:40 +01:00
|
|
|
\& { yylval->op = &AExpression::OpBinaryAnd; return T_BINARY_AND; }
|
|
|
|
\| { yylval->op = &AExpression::OpBinaryOr; return T_BINARY_OR; }
|
|
|
|
\< { yylval->op = &AExpression::OpLessThan; return T_LESS_THAN; }
|
|
|
|
\> { yylval->op = &AExpression::OpLessThan; return T_GREATER_THAN; }
|
2012-05-31 08:45:02 +02:00
|
|
|
}
|
|
|
|
|
2012-06-05 15:05:15 +02:00
|
|
|
. return yytext[0];
|
2012-05-31 08:45:02 +02:00
|
|
|
|
2013-02-16 19:12:56 +01:00
|
|
|
%%
|
2012-05-31 08:45:02 +02:00
|
|
|
|
2012-06-06 14:38:28 +02:00
|
|
|
void ConfigCompiler::InitializeScanner(void)
|
2012-05-31 08:45:02 +02:00
|
|
|
{
|
2012-05-31 09:43:46 +02:00
|
|
|
yylex_init(&m_Scanner);
|
|
|
|
yyset_extra(this, m_Scanner);
|
2012-05-31 08:45:02 +02:00
|
|
|
}
|
|
|
|
|
2012-06-06 14:38:28 +02:00
|
|
|
void ConfigCompiler::DestroyScanner(void)
|
2012-05-31 08:45:02 +02:00
|
|
|
{
|
2012-05-31 09:43:46 +02:00
|
|
|
yylex_destroy(m_Scanner);
|
2012-05-31 08:45:02 +02:00
|
|
|
}
|