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
49 {
50 if (m_vm) {
51 delete m_vm;
52 }
53 }
54
56 {
57 m_vm = new VirtualMachine();
58 }
59
60 bool ExpressionParser::loadExpression(const std::string &expression)
61 {
62 std::string trimmedExpression = boost::algorithm::trim_copy_if(expression, boost::is_any_of("\""));
63
65 m_code.clear();
66
67 Grammar<std::string::const_iterator> grammar;
68 ast::expression expr;
69
70 std::string::const_iterator expressionIter = trimmedExpression.begin();
71 std::string::const_iterator expressionEnd = trimmedExpression.end();
72 using boost::spirit::ascii::space_type;
73 bool r = phrase_parse(expressionIter, expressionEnd, grammar, space_type(), expr);
74 std::string remainingExpression(expressionIter, expressionEnd);
75 boost::trim(remainingExpression);
76 if (remainingExpression.length() > 0) {
77 throw std::runtime_error("Did you forget an operator? This was not parsed: '" + remainingExpression +"'");
78 }
79
80 if (!r) return false;
81
82 Compiler compiler(m_code, m_proxyLoader, m_unitInterpreter);
83 compiler(expr);
84
85 return true;
86 }
87
88 std::vector<std::string> ExpressionParser::getVariables() const {
89 std::vector<std::string> vars;
90 for (const StackElement &element : m_code) {
91 if (element.isProxy() && std::find(vars.begin(),vars.end(),element.proxyVarName()) == vars.end() ) {
92 vars.push_back( element.proxyVarName() );
93 }
94 }
95 return vars;
96 }
97
98 StackElement ExpressionParser::evaluate(const EventContext& ctx) const
99 {
100 return m_vm->execute(ctx, m_code);
101 }
102
104 {
105 const EventContext& ctx = Gaudi::Hive::currentContext();
106 return m_vm->execute(ctx, m_code);
107 }
108
110 {
112 if (result.isScalar()) return result.asBool();
113 else throw std::runtime_error("Unable to evaluate vector quantity as a boolean");
114 }
115
117 {
119 if (result.isScalar()) return result.scalarValue<double>();
120 else throw std::runtime_error("Unable to evaluate vector quantity as a double");
121 }
122
123 std::vector<int> ExpressionParser::evaluateAsVector() const
124 {
126 if (result.isVector()) return result.vectorValue<int>();
127 else throw std::runtime_error("Unable to evaluate scalar quantity as a vector");
128 }
129}
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.