ATLAS Offline Software
Loading...
Searching...
No Matches
ExpressionParser.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6// ExpressionParser.cxx, (c) ATLAS Detector software
8// Author: Thomas Gillam (thomas.gillam@cern.ch)
9// ExpressionParsing library
11
13#include "ParsingInternals.h"
17#include <boost/algorithm/string.hpp>
18namespace ExpressionParsing {
19
26
33
40
42 {
43 m_proxyLoader = proxyLoader;
44 m_unitInterpreter = unitInterpreter;
45 setup();
46 }
47
51
53 {
54
55 }
56
57 bool ExpressionParser::loadExpression(const std::string &expression)
58 {
59 std::string trimmedExpression = boost::algorithm::trim_copy_if(expression, boost::is_any_of("\""));
60
62 m_code.clear();
63
64 Grammar<std::string::const_iterator> grammar;
65 ast::expression expr;
66
67 std::string::const_iterator expressionIter = trimmedExpression.begin();
68 std::string::const_iterator expressionEnd = trimmedExpression.end();
69 using boost::spirit::ascii::space_type;
70 bool r = phrase_parse(expressionIter, expressionEnd, grammar, space_type(), expr);
71 std::string remainingExpression(expressionIter, expressionEnd);
72 boost::trim(remainingExpression);
73 if (remainingExpression.length() > 0) {
74 throw std::runtime_error("Did you forget an operator? This was not parsed: '" + remainingExpression +"'");
75 }
76
77 if (!r) return false;
78
79 Compiler compiler(m_code, m_proxyLoader, m_unitInterpreter);
80 compiler(expr);
81 m_code.shrink_to_fit();
82 return true;
83 }
84
85 std::vector<std::string> ExpressionParser::getVariables() const {
86 std::vector<std::string> vars;
87 for (const StackElement &element : m_code) {
88 if (element.isProxy() && std::find(vars.begin(),vars.end(),element.proxyVarName()) == vars.end() ) {
89 vars.push_back( element.proxyVarName() );
90 }
91 }
92 return vars;
93 }
94
95 StackElement ExpressionParser::evaluate(const EventContext& ctx) const
96 {
97 VirtualMachine vm{};
98 return vm.execute(ctx, m_code);
99 }
100
102 {
103 const EventContext& ctx = Gaudi::Hive::currentContext();
104 VirtualMachine vm{};
105 return vm.execute(ctx, m_code);
106 }
107
109 {
110 StackElement result = evaluate();
111 if (result.isScalar()) return result.asBool();
112 else throw std::runtime_error("Unable to evaluate vector quantity as a boolean");
113 }
114
116 {
117 StackElement result = evaluate();
118 if (result.isScalar()) return result.scalarValue<double>();
119 else throw std::runtime_error("Unable to evaluate vector quantity as a double");
120 }
121
122 std::vector<int> ExpressionParser::evaluateAsVector() const
123 {
124 StackElement result = evaluate();
125 if (result.isVector()) return result.vectorValue<int>();
126 else throw std::runtime_error("Unable to evaluate scalar quantity as a vector");
127 }
128}
std::vector< StackElement > m_code
std::vector< int > evaluateAsVector() const
bool loadExpression(const std::string &expression)
std::vector< std::string > getVariables() const
Class describing a single element in a text expression.
int r
Definition globals.cxx:22
Namespace holding all the expression evaluation code.