ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
PhysicsAnalysis
Algorithms
SelectionHelpers
Root
SelectionExprParser.cxx
Go to the documentation of this file.
1
/*
2
Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3
*/
4
5
#include "
SelectionHelpers/SelectionExprParser.h
"
6
#include "
SelectionHelpers/SelectionAccessorExprNot.h
"
7
#include "
SelectionHelpers/SelectionAccessorExprOr.h
"
8
#include "
SelectionHelpers/SelectionAccessorList.h
"
9
#include "
SelectionHelpers/SelectionReadAccessorNull.h
"
10
11
#include <iostream>
12
#include <regex>
13
#include <string>
14
15
using
CP::DetailSelectionExprParser::Lexer
;
16
17
namespace
CP
{
18
using namespace
msgSelectionHelpers;
19
20
namespace
DetailSelectionExprParser
{
21
bool
Separator::operator()
(std::string::const_iterator& next,
22
const
std::string::const_iterator& end,
23
std::string& tok)
const
{
24
if
(next == end) {
25
return
false
;
26
}
27
28
static
const
std::regex base_regex(
29
"^( ?((?:[^|&()! ]+)|(?:&&)|(?:!)|(?:\\()|(?:\\))|(?:\\|\\|))).*"
);
30
std::smatch m;
31
32
if
(std::regex_match(next, end, m, base_regex)) {
33
tok = m[2].str();
34
next += m[1].length();
35
}
else
{
36
std::smatch m2;
37
static
const
std::regex space_re{
"^ +$"
};
38
if
(std::regex_match(next, end, m, space_re)) {
39
// only spaces left, we're done
40
return
false
;
41
}
42
throw
std::runtime_error(
"Cannot tokenize: '"
+ std::string(next, end) +
43
"'"
);
44
}
45
return
true
;
46
}
47
48
Lexer::Lexer
(
const
std::string& s) :
m_string
(s),
m_tokenizer
(
m_string
, {}) {
49
m_iterator = m_tokenizer.begin();
50
}
51
52
auto
Lexer::nextSymbol
() ->
Symbol
{
53
if
(
m_iterator
==
m_tokenizer
.end()) {
54
return
Symbol
{
END
,
""
};
55
}
56
std::string t = *
m_iterator
;
57
Type
type
;
58
if
(t ==
"&&"
)
59
type
=
AND
;
60
else
if
(t ==
"||"
)
61
type
=
OR
;
62
else
if
(t ==
"("
)
63
type
=
LEFT
;
64
else
if
(t ==
")"
)
65
type
=
RIGHT
;
66
else
if
(t ==
"!"
)
67
type
=
NOT
;
68
else
if
(t ==
"true"
)
69
type
=
TRUE_LITERAL
;
70
else
if
(t ==
"false"
)
71
type
=
FALSE_LITERAL
;
72
else
{
73
// check if variable is valid
74
const
std::regex base_regex(
"^([^|()&! ]*)$"
);
75
std::smatch base_match;
76
77
if
(!std::regex_match(t, base_match, base_regex)) {
78
throw
std::runtime_error(
"illegal variable encountered"
);
79
}
else
{
80
type
=
VAR
;
81
}
82
}
83
84
++
m_iterator
;
85
return
Symbol
{
type
, std::move(t)};
86
}
87
88
}
// namespace DetailSelectionExprParser
89
90
SelectionExprParser::SelectionExprParser
(
Lexer
lexer,
bool
defaultToChar)
91
:
m_lexer
(
std
::move(lexer)),
m_defaultToChar
(defaultToChar) {}
92
93
StatusCode
SelectionExprParser::build
(
94
std::unique_ptr<ISelectionReadAccessor>& accessor) {
95
std::unique_ptr<ISelectionReadAccessor> root{
nullptr
};
96
ANA_CHECK
(
expression
(root));
97
98
if
(
m_symbol
.type !=
Lexer::END
) {
99
throw
std::runtime_error(
100
"Not all symbols in expression were consumed. Check your expression."
);
101
}
102
103
accessor = std::move(root);
104
return
StatusCode::SUCCESS;
105
}
106
107
StatusCode
SelectionExprParser::expression
(
108
std::unique_ptr<ISelectionReadAccessor>& root) {
109
ANA_CHECK
(
term
(root));
110
while
(
m_symbol
.type ==
Lexer::OR
) {
111
std::unique_ptr<ISelectionReadAccessor> left = std::move(root);
112
ANA_CHECK
(
term
(root));
113
std::unique_ptr<ISelectionReadAccessor> right = std::move(root);
114
root = std::make_unique<SelectionAccessorExprOr>(std::move(left),
115
std::move(right));
116
}
117
return
StatusCode::SUCCESS;
118
}
119
120
StatusCode
SelectionExprParser::term
(
121
std::unique_ptr<ISelectionReadAccessor>& root) {
122
ANA_CHECK
(
factor
(root));
123
std::vector<std::unique_ptr<ISelectionReadAccessor>> factors;
124
factors.push_back(std::move(root));
125
126
while
(
m_symbol
.type ==
Lexer::AND
) {
127
ANA_CHECK
(
factor
(root));
128
factors.push_back(std::move(root));
129
}
130
131
if
(factors.size() == 1) {
132
root = std::move(factors[0]);
133
}
else
{
134
root = std::make_unique<SelectionAccessorList>(std::move(factors));
135
}
136
return
StatusCode::SUCCESS;
137
}
138
139
StatusCode
SelectionExprParser::factor
(
140
std::unique_ptr<ISelectionReadAccessor>& root) {
141
m_symbol
=
m_lexer
.nextSymbol();
142
if
(
m_symbol
.type ==
Lexer::TRUE_LITERAL
) {
143
root = std::make_unique<SelectionReadAccessorNull>(
true
);
144
m_symbol
=
m_lexer
.nextSymbol();
145
}
else
if
(
m_symbol
.type ==
Lexer::FALSE_LITERAL
) {
146
root = std::make_unique<SelectionReadAccessorNull>(
false
);
147
m_symbol
=
m_lexer
.nextSymbol();
148
}
else
if
(
m_symbol
.type ==
Lexer::NOT
) {
149
ANA_CHECK
(
factor
(root));
150
std::unique_ptr<ISelectionReadAccessor> notEx =
151
std::make_unique<SelectionAccessorExprNot>(std::move(root));
152
root = std::move(notEx);
153
}
else
if
(
m_symbol
.type ==
Lexer::LEFT
) {
154
ANA_CHECK
(
expression
(root));
155
if
(
m_symbol
.type !=
Lexer::RIGHT
) {
156
throw
std::runtime_error(
157
"Missing closing bracket, check your expression."
);
158
}
159
m_symbol
=
m_lexer
.nextSymbol();
160
161
}
else
if
(
m_symbol
.type ==
Lexer::VAR
) {
162
ANA_CHECK
(
makeSelectionReadAccessorVar
(
m_symbol
.value, root,
m_defaultToChar
));
163
m_symbol
=
m_lexer
.nextSymbol();
164
}
else
{
165
throw
std::runtime_error(
"Malformed expression."
);
166
}
167
168
return
StatusCode::SUCCESS;
169
}
170
171
}
// namespace CP
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition
Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
SelectionAccessorExprNot.h
SelectionAccessorExprOr.h
SelectionAccessorList.h
SelectionExprParser.h
SelectionReadAccessorNull.h
CP::DetailSelectionExprParser::Lexer
Lexer which turns a token stream into a stream of unambigous symbols to be used by a parser.
Definition
SelectionExprParser.h:38
CP::DetailSelectionExprParser::Lexer::Type
Type
Enum over the possible symbols that can be extracted from the token stream.
Definition
SelectionExprParser.h:50
CP::DetailSelectionExprParser::Lexer::RIGHT
@ RIGHT
Definition
SelectionExprParser.h:54
CP::DetailSelectionExprParser::Lexer::FALSE_LITERAL
@ FALSE_LITERAL
Definition
SelectionExprParser.h:57
CP::DetailSelectionExprParser::Lexer::AND
@ AND
Definition
SelectionExprParser.h:51
CP::DetailSelectionExprParser::Lexer::VAR
@ VAR
Definition
SelectionExprParser.h:58
CP::DetailSelectionExprParser::Lexer::LEFT
@ LEFT
Definition
SelectionExprParser.h:53
CP::DetailSelectionExprParser::Lexer::NOT
@ NOT
Definition
SelectionExprParser.h:55
CP::DetailSelectionExprParser::Lexer::TRUE_LITERAL
@ TRUE_LITERAL
Definition
SelectionExprParser.h:56
CP::DetailSelectionExprParser::Lexer::OR
@ OR
Definition
SelectionExprParser.h:52
CP::DetailSelectionExprParser::Lexer::END
@ END
Definition
SelectionExprParser.h:59
CP::DetailSelectionExprParser::Lexer::m_tokenizer
Tokenizer m_tokenizer
Definition
SelectionExprParser.h:74
CP::DetailSelectionExprParser::Lexer::Lexer
Lexer(const std::string &s)
Constructor from a strig.
Definition
SelectionExprParser.cxx:48
CP::DetailSelectionExprParser::Lexer::nextSymbol
Symbol nextSymbol()
Generate a new symbol from the token sequence.
Definition
SelectionExprParser.cxx:52
CP::DetailSelectionExprParser::Lexer::m_string
std::string m_string
Definition
SelectionExprParser.h:73
CP::DetailSelectionExprParser::Lexer::m_iterator
Tokenizer::iterator m_iterator
Definition
SelectionExprParser.h:75
CP::DetailSelectionExprParser::Separator::operator()
bool operator()(std::string::const_iterator &next, const std::string::const_iterator &end, std::string &tok) const
Extracts the subsequent token from the input string iterator.
Definition
SelectionExprParser.cxx:21
CP::SelectionExprParser::term
StatusCode term(std::unique_ptr< ISelectionReadAccessor > &root)
Definition
SelectionExprParser.cxx:120
CP::SelectionExprParser::SelectionExprParser
SelectionExprParser(DetailSelectionExprParser::Lexer lexer, bool defaultToChar=false)
Constructor for the parser which accepts a Lecer.
Definition
SelectionExprParser.cxx:90
CP::SelectionExprParser::m_lexer
DetailSelectionExprParser::Lexer m_lexer
Definition
SelectionExprParser.h:103
CP::SelectionExprParser::m_symbol
DetailSelectionExprParser::Lexer::Symbol m_symbol
Definition
SelectionExprParser.h:105
CP::SelectionExprParser::factor
StatusCode factor(std::unique_ptr< ISelectionReadAccessor > &root)
Definition
SelectionExprParser.cxx:139
CP::SelectionExprParser::expression
StatusCode expression(std::unique_ptr< ISelectionReadAccessor > &root)
Definition
SelectionExprParser.cxx:107
CP::SelectionExprParser::m_defaultToChar
bool m_defaultToChar
Definition
SelectionExprParser.h:107
CP::SelectionExprParser::build
StatusCode build(std::unique_ptr< ISelectionReadAccessor > &accessor)
Triggers the actual parsing of the expression.
Definition
SelectionExprParser.cxx:93
CP::DetailSelectionExprParser
Definition
SelectionExprParser.cxx:20
CP
Select isolated Photons, Electrons and Muons.
Definition
Control/xAODRootAccess/xAODRootAccess/TEvent.h:27
CP::makeSelectionReadAccessorVar
StatusCode makeSelectionReadAccessorVar(const std::string &name, std::unique_ptr< ISelectionReadAccessor > &accessor, bool defaultToChar)
Produces a simple ISelectionReadAccessor accessing the given decoration.
Definition
ISelectionAccessor.cxx:138
std
STL namespace.
CP::DetailSelectionExprParser::Lexer::Symbol
Struct grouping together the type and original string representation of a symbol.
Definition
SelectionExprParser.h:64
type
Generated on
for ATLAS Offline Software by
1.17.0