Belle II Software development
FormulaParserBase Class Referenceabstract

Base class with the non-templated part of the formula parser. More...

#include <FormulaParser.h>

Inheritance diagram for FormulaParserBase:
FormulaParser< VariableConstructor >

Public Types

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

 FormulaParserBase ()=default
 Default constructor.
 
virtual ~FormulaParserBase ()=default
 virtual, but empty destructor
 

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

virtual void executeOperator (EOperator op)=0
 Execute an operator on the current state.
 
virtual void addVariable (const InputToken &token)=0
 Add a variable token to the current state.
 
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.
 

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

Base class with the non-templated part of the formula parser.

Not very useful on its own.

See also
FormulaParser

Definition at line 27 of file FormulaParser.h.

Member Typedef Documentation

◆ InputToken

typedef std::variant<std::string, double> InputToken

Input token type: an input tokein is either a string or a float variable.

Definition at line 59 of file FormulaParser.h.

Member Enumeration Documentation

◆ ENumberStatus

enum class ENumberStatus
strong

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.

45 {
46 c_Invalid,
47 c_Empty,
48 c_Sign,
49 c_Int,
50 c_Dot,
52 c_Float,
56 };
@ c_LeadingDot
leading dot without preceding digits
@ c_Float
[leading sign] + [digits] + dot + digits
@ c_Scientific
exponent followed by sign and digits
@ c_Dot
[leading sign] + digits + dot
@ c_ExponentSign
exponent followed by plus or minus

◆ EOperator

enum class EOperator : unsigned char
strong

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.

31 : unsigned char {
32 c_noop = 0x00,
33 c_roundBracketOpen = 0x01,
34 c_roundBracketClose = 0x02,
35 c_squareBracketOpen = 0x03,
37 c_plus = 0x11,
38 c_minus = 0x12,
39 c_multiply = 0x21,
40 c_divide = 0x22,
41 c_power = 0x31
42 };
@ c_roundBracketClose
Close round bracket.
@ c_roundBracketOpen
Open round bracket.
@ c_squareBracketOpen
Open square bracket.
@ c_squareBracketClose
Close square bracket.

Member Function Documentation

◆ addVariable()

virtual void addVariable ( const InputToken token)
protectedpure virtual

Add a variable token to the current state.

Implemented in FormulaParser< VariableConstructor >.

◆ executeOperator()

virtual void executeOperator ( EOperator  op)
protectedpure virtual

Execute an operator on the current state.

Implemented in FormulaParser< VariableConstructor >.

Member Data Documentation

◆ m_buffer

std::istringstream m_buffer
private

Buffer for the formula.

Definition at line 103 of file FormulaParser.h.

◆ m_currentVariableName

std::string m_currentVariableName
private

collect characters into a variable name

Definition at line 105 of file FormulaParser.h.

◆ m_currentVariableNameNumberStatus

ENumberStatus m_currentVariableNameNumberStatus
private

State of the current variable name being a valid float literal.

Definition at line 107 of file FormulaParser.h.

◆ m_lastTokenWasOperator

bool m_lastTokenWasOperator
private

Bool to check whether there were consecutive operators or variables.

Definition at line 101 of file FormulaParser.h.

◆ m_operatorStack

std::stack<EOperator> m_operatorStack
private

Stack of operators for the Shunting-yard algorithm.

Definition at line 109 of file FormulaParser.h.


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