![]() |
Belle II Software light-2505-deimos
|
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>
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 variable object of the correct type. | |
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< OutputToken > | m_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< EOperator > | m_operatorStack |
Stack of operators for the Shunting-yard algorithm. | |
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
type
specifying the desired type of the output objectDefinition at line 134 of file FormulaParser.h.
|
inherited |
Input token type: an input tokein is either a string or a float variable.
Definition at line 59 of file FormulaParser.h.
typedef std::variant<VariableType, double> OutputToken |
typedef for output tokens on the stack: either a variable or a double
Definition at line 139 of file FormulaParser.h.
typedef VariableConstructor::type VariableType |
Type of the return variable object.
Definition at line 137 of file FormulaParser.h.
|
stronginherited |
States of a string literal when checking if it is a valid number.
Definition at line 45 of file FormulaParser.h.
|
stronginherited |
List of known operators.
The second word encodes the operator precedence
Definition at line 31 of file FormulaParser.h.
|
inlineoverrideprotectedvirtual |
Add the variable to the output token stack, create it from a string or keep it as it is.
Implements FormulaParserBase.
Definition at line 186 of file FormulaParser.h.
|
inlineoverrideprotectedvirtual |
Execute the given operator by taking the operands from the stack and applying the operator to them.
Implements FormulaParserBase.
Definition at line 168 of file FormulaParser.h.
|
inline |
Parse the formula and return a variable object of the correct type.
Throws std::runtime_error if there is a problem with parsing.
Definition at line 144 of file FormulaParser.h.
|
privateinherited |
Buffer for the formula.
Definition at line 103 of file FormulaParser.h.
|
privateinherited |
collect characters into a variable name
Definition at line 105 of file FormulaParser.h.
|
privateinherited |
State of the current variable name being a valid float literal.
Definition at line 107 of file FormulaParser.h.
|
privateinherited |
Bool to check whether there were consecutive operators or variables.
Definition at line 101 of file FormulaParser.h.
|
privateinherited |
Stack of operators for the Shunting-yard algorithm.
Definition at line 109 of file FormulaParser.h.
|
protected |
Stack of output tokens in the reversh polish notation.
Definition at line 197 of file FormulaParser.h.