Newer
Older

Graziano Scalamera
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* alarm_grammar.h
*
* $Author: $
*
* $Revision: $
*
* $Log: alarm_grammar.h,v $
*
*
* copyleft: Sincrotrone Trieste S.C.p.A. di interesse nazionale
* Strada Statale 14 - km 163,5 in AREA Science Park
* 34012 Basovizza, Trieste ITALY
*/
#ifndef ALARM_GRAMMAR_H_
#define ALARM_GRAMMAR_H_
//--------------------------NOTE!!---------------------------------
//----if a grammar is intended to be used in multithreaded code----
//----it is required to "#define BOOST_SPIRIT_THREADSAFE" and------
//----to link against Boost.Threads--------------------------------
//-----------------------------------------------------------------
#if BOOST_VERSION < 103600
#ifndef BOOST_SPIRIT_THREADSAFE
#define BOOST_SPIRIT_THREADSAFE
#endif
#ifndef PHOENIX_THREADSAFE
#define PHOENIX_THREADSAFE
#endif
#endif
#if BOOST_VERSION < 103600
#include <boost/spirit/core.hpp>
#include <boost/spirit/actor/assign_actor.hpp> //for assign_a
#include <boost/spirit/actor/push_back_actor.hpp> //for push_back_a
#include <boost/spirit/actor/insert_at_actor.hpp> //for insert_at_a
#include <boost/spirit/actor/clear_actor.hpp> //for clear_a
#include <boost/spirit/symbols/symbols.hpp> //for symbol table
#include <boost/spirit/utility/confix.hpp> //for confix
#include <boost/spirit/phoenix/primitives.hpp> //needed for "var" in group rule
#include <boost/spirit/phoenix/operators.hpp> //needed for "var" in group rule
#include <boost/spirit/phoenix/functions.hpp>
#else
#include <boost/spirit/include/classic_core.hpp>
#include <boost/spirit/include/classic_assign_actor.hpp> //for assign_a
#include <boost/spirit/include/classic_push_back_actor.hpp> //for push_back_a
#include <boost/spirit/include/classic_insert_at_actor.hpp> //for insert_at_a
#include <boost/spirit/include/classic_clear_actor.hpp> //for clear_a
#include <boost/spirit/include/classic_symbols.hpp> //for symbol table
#include <boost/spirit/include/classic_confix.hpp> //for confix
#include <boost/spirit/include/phoenix1_primitives.hpp> //needed for "var" in group rule
#include <boost/spirit/include/phoenix1_operators.hpp> //needed for "var" in group rule
#include <boost/spirit/include/phoenix1_functions.hpp>
#endif
//#include <boost/spirit/attribute.hpp> //for closure
//#include <boost/spirit/tree/ast.hpp> //for ast parse trees (in tree_formula)
#include <iostream>
#include <string>
#include <map>
#include "event_table.h"
#include "formula_grammar.h"
////////////////////////////////////////////////////////////////////////////
using namespace std;
#if BOOST_VERSION < 103600
using namespace boost::spirit;
#else
using namespace boost::spirit::classic;
#endif
using namespace phoenix; //needed for "var" in group rule
////////////////////////////////////////////////////////////////////////////
//
// Semantic actions for alarm gramamr
//
////////////////////////////////////////////////////////////////////////////
#if 0
struct push_back_impl
{
template <typename Container, typename Item>
struct result
{
typedef void type;
};
template <typename Container, typename Item>
void operator()(Container& c, Item const& item) const
{
c.push_back(item);
}
};
function<push_back_impl> const push_back = push_back_impl();
struct insert_map_impl
{
template <typename Container, typename Item1, typename Item2>
struct result
{
typedef void type;
};
template <typename Container, typename Item1, typename Item2>
void operator()(Container& c, Item1 const& item1, Item2 const& item2) const
{
c.insert(make_pair(item1, item2));
}
};
function<insert_map_impl> const insert_map = insert_map_impl();
#endif
////////////////////////////////////////////////////////////////////////////
//
// alarm grammar
//
////////////////////////////////////////////////////////////////////////////
struct alarm_parse : public grammar<alarm_parse>
{
alarm_t &m_alarm;
symbols<unsigned int> sym_grp;
alarm_parse(alarm_t &a) \
: m_alarm(a)
{
//init symbol table with group defined in alarm_t::grp_str
map<string,unsigned int>::iterator i = m_alarm.grp_str.begin();
while(i != m_alarm.grp_str.end())
{
sym_grp.add(i->first.c_str(), i->second);
i++;
}
}
template <typename ScannerT>
struct definition
{
definition(alarm_parse const& self)
{
//-------BOOST.SPIRIT GLOSSARY-------
// >> : sequence
// | : union (i.e. alternative)
// - : difference
// * : kleene star (matches 0 or more times)
// + : positive (matches 1 or more times)
// ! : optional (matches 0 or 1 time)
// str_p : matches string
// ch_p : matches char
// hex_p :
// alnum_p : matches alpha-numeric characters
// anychar_p : matches any single character (including the null terminator: '\0')
// lexemd_d : turns off white space skipping
// confix : recognize a sequence of: an opening, an expression and a closing
// assign_a :
// push_back_a :
//std::pair<string, vector<string> > temp;
expression
= discard_node_d
[
name
[
assign_a(self.m_alarm.name) //save name in alarm_t
]
] //discard_node_d
>>
root_node_d
[
formula
[
assign_a(self.m_alarm.formula) //save formula in alarm_t
]
] //root_node_d
>> discard_node_d[!time_threshold] //save time_threshold in alarm_t (leave it optional)
>> discard_node_d
[
level
[
assign_a(self.m_alarm.lev) //save level in alarm_t
]
] //discard_node_d
>> discard_node_d[!silent_time] //save silent_time in alarm_t (leave it optional)
>> discard_node_d[group]
>> discard_node_d[msg] //save msg in alarm_t
>> discard_node_d
[
!name //leave it optional
[
assign_a(self.m_alarm.cmd_name_a) //save cmd_name_a in alarm_t
]
] //discard_node_d
>> discard_node_d[!ch_p(';')] //action_a and action_n separed by ';'
>> discard_node_d
[
!name //leave it optional
[
assign_a(self.m_alarm.cmd_name_n) //save cmd_name_n in alarm_t
]
] //discard_node_d
;
//------------------------------ALARM NAME--------------------------------------
symbol
= alnum_p | '.' | '_' | '-' | '+' //any alpha numeric char plus '.', '_', '-', '+'
;
name
= (+symbol) >> '/' >> (+symbol)
>> '/' >> (+symbol) >> '/' >> (+symbol)
;
//------------------------------LEVEL--------------------------------------
level
= lexeme_d[(+alnum_p)] //match only possible levels?? (fault, log, ...)
;
//------------------------------GROUP--------------------------------------
group
= self.sym_grp //match only group defined in sym_grp symbol table
[
var(self.m_alarm.grp) |= arg1 //using phoenix::var
]
>> *(
ch_p('|')
>> self.sym_grp //match only group defined in sym_grp symbol table
[
var(self.m_alarm.grp) |= arg1 //using phoenix::var
]
)
;
//------------------------------MESSAGE--------------------------------------
msg
= ch_p('"')
>> (+(anychar_p - '\"')) //one ore more char except '"'
[
assign_a(self.m_alarm.msg)
]
>> '"'
;
//---------------------------TIME THRESHOLD----------------------------------
time_threshold
= uint_p
[
assign_a(self.m_alarm.time_threshold)
]
;
//-----------------------------SILENT TIME------------------------------------
silent_time
= int_p
[
assign_a(self.m_alarm.silent_time)
]
;
}
typedef rule<ScannerT> rule_t;
rule_t expression, event;
rule_t symbol, name, val, token, oper, msg, group, level, time_threshold, silent_time;
formula_grammar formula;
rule_t const&
start() const { return expression; }
};
};
#endif /*ALARM_GRAMMAR_H_*/