ATLAS Offline Software
Public Member Functions | Private Member Functions | Private Attributes | List of all members
ExpressionParsing::ExpressionParser Class Reference

#include <ExpressionParser.h>

Collaboration diagram for ExpressionParsing::ExpressionParser:

Public Member Functions

 ExpressionParser ()
 
 ExpressionParser (IProxyLoader *proxyLoader)
 
 ExpressionParser (IUnitInterpreter *unitInterpreter)
 
 ExpressionParser (IProxyLoader *proxyLoader, IUnitInterpreter *unitInterpreter)
 
 ~ExpressionParser ()
 
 ExpressionParser (const ExpressionParser &)=delete
 
ExpressionParseroperator= (const ExpressionParser &)=delete
 
bool loadExpression (const std::string &expression)
 
StackElement evaluate () const
 
bool evaluateAsBool () const
 
double evaluateAsDouble () const
 
std::vector< int > evaluateAsVector () const
 
std::vector< std::string > getVariables () const
 

Private Member Functions

void setup ()
 

Private Attributes

IProxyLoaderm_proxyLoader
 
IUnitInterpreterm_unitInterpreter
 
std::vector< StackElementm_code
 
VirtualMachine * m_vm
 

Detailed Description

Definition at line 29 of file ExpressionParser.h.

Constructor & Destructor Documentation

◆ ExpressionParser() [1/5]

ExpressionParsing::ExpressionParser::ExpressionParser ( )

◆ ExpressionParser() [2/5]

ExpressionParsing::ExpressionParser::ExpressionParser ( IProxyLoader proxyLoader)

Definition at line 28 of file ExpressionParser.cxx.

29  {
30  m_proxyLoader = proxyLoader;
32  setup();
33  }

◆ ExpressionParser() [3/5]

ExpressionParsing::ExpressionParser::ExpressionParser ( IUnitInterpreter unitInterpreter)

Definition at line 35 of file ExpressionParser.cxx.

36  {
38  m_unitInterpreter = unitInterpreter;
39  setup();
40  }

◆ ExpressionParser() [4/5]

ExpressionParsing::ExpressionParser::ExpressionParser ( IProxyLoader proxyLoader,
IUnitInterpreter unitInterpreter 
)

Definition at line 42 of file ExpressionParser.cxx.

43  {
44  m_proxyLoader = proxyLoader;
45  m_unitInterpreter = unitInterpreter;
46  setup();
47  }

◆ ~ExpressionParser()

ExpressionParsing::ExpressionParser::~ExpressionParser ( )

Definition at line 49 of file ExpressionParser.cxx.

50  {
51  if (m_vm) {
52  delete m_vm;
53  }
54  }

◆ ExpressionParser() [5/5]

ExpressionParsing::ExpressionParser::ExpressionParser ( const ExpressionParser )
delete

Member Function Documentation

◆ evaluate()

StackElement ExpressionParsing::ExpressionParser::evaluate ( ) const

Definition at line 99 of file ExpressionParser.cxx.

100  {
101  return m_vm->execute(m_code);
102  }

◆ evaluateAsBool()

bool ExpressionParsing::ExpressionParser::evaluateAsBool ( ) const

Definition at line 104 of file ExpressionParser.cxx.

105  {
107  if (result.isScalar()) return result.asBool();
108  else throw std::runtime_error("Unable to evaluate vector quantity as a boolean");
109  }

◆ evaluateAsDouble()

double ExpressionParsing::ExpressionParser::evaluateAsDouble ( ) const

Definition at line 111 of file ExpressionParser.cxx.

112  {
114  if (result.isScalar()) return result.scalarValue<double>();
115  else throw std::runtime_error("Unable to evaluate vector quantity as a double");
116  }

◆ evaluateAsVector()

std::vector< int > ExpressionParsing::ExpressionParser::evaluateAsVector ( ) const

Definition at line 118 of file ExpressionParser.cxx.

119  {
121  if (result.isVector()) return result.vectorValue<int>();
122  else throw std::runtime_error("Unable to evaluate scalar quantity as a vector");
123  }

◆ getVariables()

std::vector< std::string > ExpressionParsing::ExpressionParser::getVariables ( ) const

Definition at line 89 of file ExpressionParser.cxx.

89  {
90  std::vector<std::string> vars;
91  for (const StackElement &element : m_code) {
92  if (element.isProxy() && std::find(vars.begin(),vars.end(),element.proxyVarName()) == vars.end() ) {
93  vars.push_back( element.proxyVarName() );
94  }
95  }
96  return vars;
97  }

◆ loadExpression()

bool ExpressionParsing::ExpressionParser::loadExpression ( const std::string &  expression)

Definition at line 61 of file ExpressionParser.cxx.

62  {
63  std::string trimmedExpression = boost::algorithm::trim_copy_if(expression, boost::is_any_of("\""));
64 
66  m_code.clear();
67 
68  Grammar<std::string::const_iterator> grammar;
69  ast::expression expr;
70 
71  std::string::const_iterator expressionIter = trimmedExpression.begin();
72  std::string::const_iterator expressionEnd = trimmedExpression.end();
73  using boost::spirit::ascii::space_type;
74  bool r = phrase_parse(expressionIter, expressionEnd, grammar, space_type(), expr);
75  std::string remainingExpression(expressionIter, expressionEnd);
76  boost::trim(remainingExpression);
77  if (remainingExpression.length() > 0) {
78  throw std::runtime_error("Did you forget an operator? This was not parsed: '" + remainingExpression +"'");
79  }
80 
81  if (!r) return false;
82 
83  Compiler compiler(m_code, m_proxyLoader, m_unitInterpreter);
84  compiler(expr);
85 
86  return true;
87  }

◆ operator=()

ExpressionParser& ExpressionParsing::ExpressionParser::operator= ( const ExpressionParser )
delete

◆ setup()

void ExpressionParsing::ExpressionParser::setup ( )
private

Definition at line 56 of file ExpressionParser.cxx.

57  {
58  m_vm = new VirtualMachine();
59  }

Member Data Documentation

◆ m_code

std::vector<StackElement> ExpressionParsing::ExpressionParser::m_code
private

Definition at line 54 of file ExpressionParser.h.

◆ m_proxyLoader

IProxyLoader* ExpressionParsing::ExpressionParser::m_proxyLoader
private

Definition at line 52 of file ExpressionParser.h.

◆ m_unitInterpreter

IUnitInterpreter* ExpressionParsing::ExpressionParser::m_unitInterpreter
private

Definition at line 53 of file ExpressionParser.h.

◆ m_vm

VirtualMachine* ExpressionParsing::ExpressionParser::m_vm
private

Definition at line 55 of file ExpressionParser.h.


The documentation for this class was generated from the following files:
beamspotman.r
def r
Definition: beamspotman.py:676
han_lark_tester.grammar
grammar
Definition: han_lark_tester.py:39
get_generator_info.result
result
Definition: get_generator_info.py:21
ExpressionParsing::ExpressionParser::evaluate
StackElement evaluate() const
Definition: ExpressionParser.cxx:99
StackElement
void * StackElement
One element of a stack trace.
Definition: stackstash.h:74
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
python.HION12.expression
string expression
Definition: HION12.py:56
ExpressionParsing::ExpressionParser::m_proxyLoader
IProxyLoader * m_proxyLoader
Definition: ExpressionParser.h:52
ExpressionParsing::EncapsulatingSingleton< IProxyLoader >::getInstance
static IProxyLoader * getInstance()
Definition: EncapsulatingSingleton.h:24
ExpressionParsing::IProxyLoader::reset
virtual void reset()=0
GRLStrUtil::trim
void trim(std::string &input)
Definition: StrUtil.cxx:12
ExpressionParsing::ExpressionParser::m_code
std::vector< StackElement > m_code
Definition: ExpressionParser.h:54
ExpressionParsing::ExpressionParser::m_vm
VirtualMachine * m_vm
Definition: ExpressionParser.h:55
ExpressionParsing::ExpressionParser::setup
void setup()
Definition: ExpressionParser.cxx:56
ExpressionParsing::ExpressionParser::m_unitInterpreter
IUnitInterpreter * m_unitInterpreter
Definition: ExpressionParser.h:53