Belle II Software  release-08-01-10
FormulaParser< VariableConstructor > Class Template Reference

FormulaParser to parse a text formula like "a + b * c ^ d" where the separate parts can be either variables (with and without arguments) or numbers. More...

#include <FormulaParser.h>

Inheritance diagram for FormulaParser< VariableConstructor >:
Collaboration diagram for FormulaParser< VariableConstructor >:

Public Types

typedef VariableConstructor::type VariableType
 Type of the return variable object.
 
typedef std::variant< VariableType, double > OutputToken
 typedef for output tokens on the stack: either a variable or a double
 
enum class  EOperator : unsigned char {
  c_noop = 0x00 ,
  c_roundBracketOpen = 0x01 ,
  c_roundBracketClose = 0x02 ,
  c_squareBracketOpen = 0x03 ,
  c_squareBracketClose = 0x04 ,
  c_plus = 0x11 ,
  c_minus = 0x12 ,
  c_multiply = 0x21 ,
  c_divide = 0x22 ,
  c_power = 0x31
}
 List of known operators. More...
 
enum class  ENumberStatus {
  c_Invalid ,
  c_Empty ,
  c_Sign ,
  c_Int ,
  c_Dot ,
  c_LeadingDot ,
  c_Float ,
  c_Exponent ,
  c_ExponentSign ,
  c_Scientific
}
 States of a string literal when checking if it is a valid number. More...
 
typedef std::variant< std::string, double > InputToken
 Input token type: an input tokein is either a string or a float variable.
 

Public Member Functions

VariableType parse (const std::string &formula)
 Parse the formula and return a varible object of the correct type. More...
 

Static Public Member Functions

static char operatorToChar (EOperator op) noexcept
 Convert operator code to character.
 
static double applyOperator (EOperator op, double a, double b)
 Apply operator on two values.
 
static ENumberStatus checkNumber (ENumberStatus current, char next)
 Check if a string literal with a given number status continues to be a valid number if next is appended to it.
 

Protected Member Functions

void executeOperator (EOperator op) override
 Execute the given operator by taking the operands from the stack and applying the operator to them.
 
void addVariable (const InputToken &var) override
 Add the variable to the output token stack, create it from a string or keep it as it is.
 
void processString (const std::string &formula)
 Process the given formula and store the final state.
 
void raiseError (const std::runtime_error &e)
 Format the given runtime_error with context information and rethrow a new one.
 

Static Protected Member Functions

static void assertOperatorUsable (size_t stacksize)
 Make sure we have enough operands to use an operator.
 

Protected Attributes

std::stack< OutputTokenm_outputStack
 Stack of output tokens in the reversh polish notation.
 

Private Member Functions

void addOperator (EOperator op)
 Add an operator to the internal state, convert them to reverse polish notation using the shunting yard algorithm and execute them as they become available.
 
void flushCurrentVariable ()
 Flush the currently parsed variable name and add it to the state either as variable or number.
 
void flushPendingOperators ()
 Flush all pending operators at the end of processing.
 
EOperator checkForOperator (char next)
 Check if the next character is a operator.
 

Private Attributes

bool m_lastTokenWasOperator
 Bool to check whether there were consecutive operators or variables.
 
std::istringstream m_buffer
 Buffer for the formula.
 
std::string m_currentVariableName
 collect characters into a variable name
 
ENumberStatus m_currentVariableNameNumberStatus
 State of the current variable name being a valid float literal.
 
std::stack< EOperatorm_operatorStack
 Stack of operators for the Shunting-yard algorithm.
 

Detailed Description

template<class VariableConstructor>
class Belle2::FormulaParser< VariableConstructor >

FormulaParser to parse a text formula like "a + b * c ^ d" where the separate parts can be either variables (with and without arguments) or numbers.

The output can be configured by providing a custom VariableConstructor which creates the desired type of output variables. This VariableConstructor object needs to have

  1. a member type type specifying the desired type of the output object
  2. overloads for the call operator to create such objects from strings, doubles and to combine variables and doubles with a given EOperator. The necessary overloads are
    • type operator()(const std::string &name)
    • type operator()(double value)
    • type operator()(EOperator op, const type& a, const type& b)
    • type operator()(EOperator op, double a, const type &b)
    • type operator()(EOperator op, const type &a, double b)
See also
StringFormulaConstructor for an example

Definition at line 134 of file FormulaParser.h.

Member Enumeration Documentation

◆ ENumberStatus

enum ENumberStatus
stronginherited

States of a string literal when checking if it is a valid number.

Enumerator
c_Invalid 

Not a valid number.

c_Empty 

Empty string.

c_Sign 

Leading sign.

c_Int 

[leading sign] + digits

c_Dot 

[leading sign] + digits + dot

c_LeadingDot 

leading dot without preceding digits

c_Float 

[leading sign] + [digits] + dot + digits

c_Exponent 

[float] + E or e

c_ExponentSign 

exponent followed by plus or minus

c_Scientific 

exponent followed by sign and digits

Definition at line 45 of file FormulaParser.h.

◆ EOperator

enum EOperator : unsigned char
stronginherited

List of known operators.

The second word encodes the operator precedence

Enumerator
c_noop 

No operation.

c_roundBracketOpen 

Open round bracket.

c_roundBracketClose 

Close round bracket.

c_squareBracketOpen 

Open square bracket.

c_squareBracketClose 

Close square bracket.

c_plus 

Addition.

c_minus 

Subtraction.

c_multiply 

Multiply.

c_divide 

Division.

c_power 

Exponentation.

Definition at line 31 of file FormulaParser.h.

Member Function Documentation

◆ parse()

VariableType parse ( const std::string &  formula)
inline

Parse the formula and return a varible object of the correct type.

Throws std::runtime_error if there is a problem with parsing.

Definition at line 144 of file FormulaParser.h.


The documentation for this class was generated from the following file: