icinga2/dyn/config_parser.yy

286 lines
5.9 KiB
Plaintext
Raw Normal View History

%{
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* 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. *
******************************************************************************/
#include "i2-dyn.h"
using namespace icinga;
%}
2012-05-31 08:45:02 +02:00
%pure-parser
%locations
%defines
%error-verbose
%parse-param { ConfigContext *context }
%lex-param { void *scanner }
%union {
char *text;
int num;
icinga::Variant *variant;
icinga::ExpressionOperator op;
2012-05-31 08:45:02 +02:00
}
%token <text> T_STRING
%token <num> T_NUMBER
%token T_NULL
%token <text> T_IDENTIFIER
%token T_OPEN_PARENTHESIS
%token T_CLOSE_PARENTHESIS
2012-05-31 08:45:02 +02:00
%token T_OPEN_BRACE
%token T_CLOSE_BRACE
%token T_OPEN_BRACKET
%token T_CLOSE_BRACKET
%token <op> T_EQUAL
%token <op> T_PLUS_EQUAL
%token <op> T_MINUS_EQUAL
%token <op> T_MULTIPLY_EQUAL
%token <op> T_DIVIDE_EQUAL
2012-05-31 08:45:02 +02:00
%token T_COMMA
%token T_ABSTRACT
%token T_LOCAL
%token T_OBJECT
%token T_INCLUDE
%token T_INHERITS
%type <variant> simplevalue
%type <variant> value
%type <variant> tuple
%type <variant> expressionlist
%type <op> operator
%left '+' '-'
%left '*' '/'
2012-05-31 08:45:02 +02:00
%{
2012-05-31 08:59:40 +02:00
int yylex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner);
2012-05-31 08:45:02 +02:00
2012-05-31 08:59:40 +02:00
void yyerror(YYLTYPE *locp, ConfigContext *context, const char *err)
2012-05-31 08:45:02 +02:00
{
2012-05-31 09:43:46 +02:00
stringstream message;
message << locp->first_line << ":" << locp->first_column
2012-05-31 08:45:02 +02:00
<< "-"
<< locp->last_line << ":" << locp->last_column
2012-05-31 09:43:46 +02:00
<< ": " << err << endl;
throw runtime_error(message.str());
2012-05-31 08:45:02 +02:00
}
2012-05-31 10:16:32 +02:00
int yyparse(ConfigContext *context);
void ConfigContext::Compile(void)
{
yyparse(this);
}
2012-05-31 09:43:46 +02:00
#define scanner (context->GetScanner())
2012-05-31 08:45:02 +02:00
static stack<ExpressionList::Ptr> m_ExpressionLists;
static set<DConfigObject::Ptr> m_Objects;
static DConfigObject::Ptr m_Object;
static bool m_Abstract;
static bool m_Local;
static Dictionary::Ptr m_Array;
2012-05-31 08:45:02 +02:00
%}
%%
statements: /* empty */
| statements statement
{
context->SetResult(m_Objects);
}
2012-05-31 08:45:02 +02:00
;
statement: object | include
;
include: T_INCLUDE T_STRING
;
object:
{
m_Abstract = false;
m_Local = false;
}
attributes T_OBJECT T_IDENTIFIER T_STRING
{
m_Object = make_shared<DConfigObject>($4, $5, yylloc.first_line);
free($4);
free($5);
}
inherits_specifier expressionlist
{
Object::Ptr exprl_object = *$8;
delete $8;
ExpressionList::Ptr exprl = dynamic_pointer_cast<ExpressionList>(exprl_object);
2012-05-31 08:45:02 +02:00
Expression typeexpr("__type", OperatorSet, m_Object->GetType(), yylloc.first_line);
exprl->AddExpression(typeexpr);
Expression nameexpr("__name", OperatorSet, m_Object->GetName(), yylloc.first_line);
exprl->AddExpression(nameexpr);
Expression abstractexpr("__abstract", OperatorSet, m_Abstract ? 1 : 0, yylloc.first_line);
exprl->AddExpression(abstractexpr);
Expression localexpr("__local", OperatorSet, m_Local ? 1 : 0, yylloc.first_line);
exprl->AddExpression(localexpr);
m_Object->SetExpressionList(exprl);
DConfigObject::AddObject(m_Object);
m_Objects.insert(m_Object);
m_Object.reset();
}
2012-05-31 08:45:02 +02:00
;
attributes: /* empty */
| attributes attribute
2012-05-31 08:45:02 +02:00
;
attribute: T_ABSTRACT
{
m_Abstract = true;
}
2012-05-31 08:45:02 +02:00
| T_LOCAL
{
m_Local = true;
}
;
inherits_list: inherits_item
| inherits_list T_COMMA inherits_item
2012-05-31 08:45:02 +02:00
;
inherits_item: T_STRING
{
m_Object->AddParent($1);
}
2012-05-31 08:45:02 +02:00
;
inherits_specifier: /* empty */
| T_INHERITS inherits_list
;
expressionlist: T_OPEN_BRACE
{
m_ExpressionLists.push(make_shared<ExpressionList>());
}
expressions
T_CLOSE_BRACE
{
$$ = new Variant(m_ExpressionLists.top());
m_ExpressionLists.pop();
}
2012-05-31 08:45:02 +02:00
;
expressions: /* empty */
| expression
| expression T_COMMA expressions
2012-05-31 08:45:02 +02:00
;
expression: T_IDENTIFIER operator value
{
Expression expr($1, $2, *$3, yylloc.first_line);
free($1);
delete $3;
m_ExpressionLists.top()->AddExpression(expr);
}
| T_IDENTIFIER T_OPEN_BRACKET T_STRING T_CLOSE_BRACKET operator value
{
Expression subexpr($3, $5, *$6, yylloc.first_line);
free($3);
delete $6;
ExpressionList::Ptr subexprl = make_shared<ExpressionList>();
subexprl->AddExpression(subexpr);
Expression expr($1, OperatorPlus, subexprl, yylloc.first_line);
m_ExpressionLists.top()->AddExpression(expr);
}
| T_STRING
{
Expression expr($1, OperatorSet, $1, yylloc.first_line);
free($1);
m_ExpressionLists.top()->AddExpression(expr);
}
;
operator: T_EQUAL
| T_PLUS_EQUAL
| T_MINUS_EQUAL
| T_MULTIPLY_EQUAL
| T_DIVIDE_EQUAL
{
$$ = $1;
}
2012-05-31 08:45:02 +02:00
;
simplevalue: T_STRING
{
$$ = new Variant($1);
}
| T_NUMBER
{
$$ = new Variant($1);
}
| T_NULL
{
$$ = new Variant();
}
;
value: simplevalue
| tuple
| expressionlist
{
$$ = $1;
}
2012-05-31 08:45:02 +02:00
;
tuple: T_OPEN_PARENTHESIS
{
m_Array = make_shared<Dictionary>();
}
tupleitems
T_CLOSE_PARENTHESIS
{
$$ = new Variant(m_Array);
m_Array.reset();
}
2012-05-31 08:45:02 +02:00
;
tupleitem: simplevalue
{
m_Array->AddUnnamedProperty(*$1);
delete $1;
}
tupleitems:
/* empty */
| tupleitem
| tupleitem T_COMMA tupleitems
2012-05-31 08:45:02 +02:00
;
%%