11#include <boost/algorithm/string/trim.hpp>
26std::vector<std::string>
35 boost::algorithm::trim(
str);
40 std::replace(
str.begin(),
str.end(),
'\t',
' ');
47 for(
size_t i=0; i<s.size(); i++) s[i] += (s[i]>=
'A'&&s[i]<=
'Z')?
'a'-
'A':0;
55 parseToken(
const std::string& givenlogic,
56 std::string::size_type& pos,
57 const std::vector<std::string>& conditions,
58 const std::vector<TrigConf::TriggerThreshold*>& thrs);
62 parseExpr(
const std::string& expr,
63 const std::vector<std::string>& conditions,
64 const std::vector<TrigConf::TriggerThreshold*>& thrs)
68 std::string::size_type pos = 0;
69 std::string::size_type last = expr.size();
88 if(expr[pos]==
'&' || expr[pos]==
'|') {
91 throw std::logic_error(
string(
"Unexpected character '") + expr[pos] +
"' in expression '" + expr
92 +
"' at position" + std::to_string(pos) +
" [b]");
101 if(newNode->
type() != typeFromChar) {
102 throw std::logic_error(
string(
"Unexpected character '") + expr[pos] +
"' in expression '" + expr
103 +
"' at position" + std::to_string(pos) +
" [c] Expected "
115 std::string getSubExpr(
const std::string &exp,
const std::string::size_type begin) {
117 std::string::size_type last =
exp.size();
119 int openBrackets = 0;
122 if(cc==
'(') openBrackets++;
123 if(cc==
')') openBrackets--;
124 if(openBrackets==0 && (cc==
'&' or cc==
'|') )
break;
127 if (openBrackets>0) {
128 std::cout <<
"ERROR: No matching closing bracket in '" <<
exp <<
"'" << std::endl;
131 std::string
se(exp, begin, pos-begin);
138 parseToken(
const std::string& logic,
139 std::string::size_type& pos,
140 const std::vector<std::string>& conditions,
141 const std::vector<TrigConf::TriggerThreshold*>& thrs) {
164 std::string
se = getSubExpr(logic,pos);
165 thisNode->addChild(
parse(se,conditions,thrs) );
171 std::string
se = getSubExpr(logic,pos);
174 std::string senop(se, 1,
se.size()-2);
175 thisNode =
parse(senop,conditions,thrs);
178 case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
180 std::string
se = getSubExpr(logic,pos);
188 std::string errMsg =
"Unexpected character '";
189 errMsg += logic[
pos];
190 errMsg +=
"' in expression '" + logic +
"' at position" + std::to_string(pos) +
" [a]";
191 throw std::logic_error(errMsg);
205 const vector<string>& conditions,
206 const vector<TrigConf::TriggerThreshold*>& thrs) {
211 vector<string> condDef =
split(conditions[condIdx-1],
",");
213 if(condDef.size()==1) {
221 std::string&
name = condDef[2];
223 std::vector<TrigConf::TriggerThreshold*>::const_iterator thrIt = thrs.begin();
224 for(;thrIt!=thrs.end(); ++thrIt ) {
225 if((*thrIt)->name()==
name) {
235 catch(
const std::exception& e) {
236 std::cout <<
"Exeption caught in buildObjNode for " << conditions[condIdx-1] <<
": " << e.what() << std::endl;
245 const std::vector<std::string>& conditions,
246 const std::vector<TrigConf::TriggerThreshold*>& thrs) {
270std::string::size_type
272 const std::string& logic) {
275 uint32_t openBrackets = 1;
276 std::string::size_type last = logic.size();
277 while(openBrackets>0 && pos!=last) {
278 if(logic[pos]==
')') openBrackets--;
279 if(logic[pos]==
'(') openBrackets++;
282 if (openBrackets>0) {
283 std::string errMsg =
"No matching closing bracket in '";
286 throw std::logic_error(errMsg);
293 std::string logicWithPars(givenlogic);
294 size_t last = givenlogic.size()-1;
295 bool leadingAnd(
false);
296 for(
size_t pos = 0;pos<last;pos++) {
297 char c = logicWithPars[pos];
299 if(c==
'&') leadingAnd=
true;
302 logicWithPars.insert(pos,
")");
303 logicWithPars.insert(0,
"(");
308 return logicWithPars;
315 auto [ptr, ec] = std::from_chars(binary.data(),
316 binary.data() + binary.size(), value, 2);
317 if (ec != std::errc{} || ptr != binary.data() + binary.size()) {
318 throw std::invalid_argument(
"Invalid binary string");
326 std::string s(
width,
'0');
327 for (uint16_t i = 0; i <
width; ++i) {
328 s[
width - 1 - i] =
'0' + ((value >> i) & 1);
std::pair< std::vector< unsigned int >, bool > res
static NodeType typeFromChar(const char &c)
void setMultiplicity(int mult)
static std::string typeAsString(NodeType)
void setInternalTrigger(L1DataDef::TriggerType x, unsigned int thresholdNumber)
void addChild(TriggerItemNode *node)
void setTriggerThreshold(TriggerThreshold *thr)
void setThresholdName(const std::string &thrname)
std::vector< std::string > tokenize(std::string_view the_str, std::string_view delimiters)
Splits the string into smaller substrings.
Forward iterator to traverse the main components of the trigger configuration.
std::string uint2bin(uint32_t uinteger, uint16_t width)
std::string insertParenthesis(const std::string &givenlogic)
void replaceTabs(std::string &str)
std::string::size_type findClosingBracket(std::string::size_type pos, const std::string &logic)
uint32_t bin2uint(const std::string &binary)
void toLower(std::string &)
std::vector< std::string > split(const std::string &line, const std::string &del=" ")
TrigConf::TriggerItemNode * buildObjNode(uint32_t condIdx, const std::vector< std::string > &conditions, const std::vector< TrigConf::TriggerThreshold * > &thrs)
std::vector< std::string > parse(std::string names)
void strip(std::string &str)