Math formula parser. More...
#include <tinyexpr.h>
Classes | |
| struct | state |
Public Member Functions | |
| bool | compile (std::string_view expression) |
Parses the input expression. | |
| te_type | evaluate () |
| Evaluates expression passed to compile() previously and returns its result. | |
| te_type | evaluate (std::string_view expression) |
| Compiles and evaluates an expression and returns its result. | |
| te_type | get_result () const noexcept |
| bool | success () const noexcept |
| bool | success () const volatile noexcept |
| int64_t | get_last_error_position () const noexcept |
| const std::string & | get_last_error_message () const noexcept |
| void | set_variables_and_functions (std::set< te_variable > vars) |
| Sets the list of custom variables and functions. | |
| void | add_variable_or_function (te_variable var) |
| Adds a custom variable or function. | |
| void | remove_variable_or_function (te_variable::name_type var) |
| Removes a custom variable or function. | |
| void | remove_unused_variables_and_functions () |
| Removes any custom variables and functions that weren't used in the last compilation. | |
| void | set_unknown_symbol_resolver (te_usr_variant_type usr, const bool keepResolvedVariables=true) |
| Sets a custom function to resolve unknown symbols in an expression. | |
| std::set< te_variable > & | get_variables_and_functions () noexcept |
| char | get_decimal_separator () const noexcept |
| void | set_decimal_separator (const char sep) |
| Sets the decimal separator used for numbers. | |
| void | set_constant (const std::string_view name, const te_type value) |
| Sets a constant variable's value. | |
| te_type | get_constant (const std::string_view name) const |
| Retrieves a constant variable's value. | |
| char | get_list_separator () const noexcept |
| void | set_list_separator (const char sep) |
| Sets the separator used between function arguments. | |
| bool | is_function_used (const std::string_view name) const |
| bool | is_variable_used (const std::string_view name) const |
| std::string | list_available_functions_and_variables () |
| const std::string & | get_expression () const noexcept |
Static Public Member Functions | |
| static constexpr bool | supports_32bit () noexcept |
| static constexpr bool | supports_64bit () noexcept |
| static constexpr int | get_max_integer_bitness () noexcept |
| static te_type | get_max_integer () |
| static std::string | info () |
Static Public Attributes | |
| static constexpr auto | te_nan = std::numeric_limits<te_type>::quiet_NaN() |
| NaN (not-a-number) constant to indicate an invalid value. | |
| static constexpr int64_t | npos = -1 |
| No position, which is what get_last_error_position() returns when there was no parsing error. | |
Private Member Functions | |
| te_parser ()=default | |
| te_parser (const te_parser &that) | |
| te_parser & | operator= (const te_parser &that) |
| ~te_parser () | |
| te_type | get_result () const volatile noexcept |
| int64_t | get_last_error_position () const volatile noexcept |
| const std::set< te_variable > & | get_variables_and_functions () const noexcept |
| char | get_decimal_separator () const volatile noexcept |
| void | set_decimal_separator (const char sep) volatile |
| char | get_list_separator () const volatile noexcept |
| void | set_list_separator (const char sep) volatile |
| void | reset_state () |
| Reset everything from previous call. | |
| void | reset_usr_resolved_if_necessary () |
| Resets any resolved variables from USR if not being cached. | |
| const te_expr * | get_compiled_expression () const noexcept |
| Gets the compiled expression, which will be the optimized version of the original expression. | |
| const te_expr * | get_compiled_expression () const volatile noexcept |
| std::set< te_variable >::iterator | find_variable_or_function (const std::string_view name) |
| std::set< te_variable >::const_iterator | find_variable_or_function (const std::string_view name) const |
| TE_DEF_FUNCTION (0) | |
| TE_DEF_FUNCTION (1) | |
| TE_DEF_FUNCTION (2) | |
| TE_DEF_CLOSURE (0) | |
| TE_DEF_CLOSURE (1) | |
| TE_DEF_CLOSURE (2) | |
| te_expr * | te_compile (std::string_view expression, std::set< te_variable > &variables) |
| Parses the input expression and binds variables. | |
| void | next_token (state *theState) |
| te_expr * | base (state *theState) |
| te_expr * | base_impl (state *theState) |
| te_expr * | power (state *theState) |
| te_expr * | factor (state *theState) |
| te_expr * | term (state *theState) |
| te_expr * | expr_level1 (state *theState) |
| te_expr * | expr_level2 (state *theState) |
| te_expr * | expr_level3 (state *theState) |
| te_expr * | expr_level4 (state *theState) |
| te_expr * | expr_level5 (state *theState) |
| te_expr * | expr_level6 (state *theState) |
| te_expr * | expr_level7 (state *theState) |
| te_expr * | expr_level8 (state *theState) |
| te_expr * | expr_level9 (state *theState) |
| te_expr * | list (state *theState) |
Static Private Member Functions | |
| static constexpr bool | te_exceeds_uint32 (te_type val) noexcept |
| static bool | number_to_bool (te_type val) |
| Helper function to convert a number into boolean. | |
| static void | validate_name (const te_variable &var) |
| Validates that a variable only contains legal characters (and has a valid length). | |
| static constexpr bool | is_name_char_valid (const char chr) noexcept |
| static size_t | find_outside_of_string (const std::string &str, const char chr, const size_t startIndex) |
| static constexpr auto | is_pure (const te_variable_flags type) |
| static constexpr auto | is_variadic (const te_variable_flags type) |
| static auto | get_arity (const te_variant_type &var) |
| static constexpr bool | is_constant (const te_variant_type &var) noexcept |
| static constexpr te_type | get_constant_value (const te_variant_type &var) |
| static constexpr bool | is_variable (const te_variant_type &var) noexcept |
| static constexpr const te_type * | get_variable (const te_variant_type &var) |
| static constexpr bool | is_function (const te_variant_type &var) |
| static constexpr bool | is_string (const te_variant_type &var) noexcept |
| static constexpr std::string_view | get_string (const te_variant_type &var) |
| static constexpr bool | is_arg_function (const te_variant_type &var) noexcept |
| static constexpr bool | is_arg_closure (const te_variant_type &var) noexcept |
| static constexpr bool | is_closure (const te_variant_type &var) |
| static constexpr bool | is_any_function (const te_variant_type &var) |
| static constexpr bool | is_any_closure (const te_variant_type &var) |
| static size_t | get_parameter_count (const te_expr *texp) |
| static te_expr * | new_expr (const te_variable_flags type, te_variant_type value, const std::initializer_list< te_expr * > ¶meters) |
| static te_expr * | new_expr (const te_variable_flags type, te_variant_type value) |
| static constexpr bool | is_letter (const char chr) noexcept |
| static te_type | te_eval (const te_expr *texp) |
| static void | te_free (te_expr *texp) |
| static void | te_free_parameters (te_expr *texp) |
| static void | optimize (te_expr *texp) |
| static auto | find_builtin (const std::string_view name) |
| static auto | find_lookup (state *ste, const std::string_view name) |
Private Attributes | |
| std::set< te_variable > | m_customFuncsAndVars |
| te_usr_variant_type | m_unknownSymbolResolve {te_usr_noop{}} |
| bool | m_keepResolvedVariables {true} |
| char | m_decimalSeparator {'.'} |
| char | m_listSeparator {','} |
| std::string | m_expression |
| te_expr * | m_compiledExpression {nullptr} |
| bool | m_parseSuccess {false} |
| int64_t | m_errorPos {0} |
| std::string | m_lastErrorMessage |
| te_type | m_result {te_nan} |
| std::set< te_variable::name_type > | m_resolvedVariables |
| std::set< te_variable >::const_iterator | m_currentVar |
| bool | m_varFound {false} |
| std::set< te_variable::name_type, te_string_less > | m_usedFunctions |
| std::set< te_variable::name_type, te_string_less > | m_usedVars |
Static Private Attributes | |
| static constexpr double | MAX_BITOPS_VAL {281474976710655} |
| static const std::set< te_variable > | m_functions |
Math formula parser.
Definition at line 382 of file tinyexpr.h.
|
privatedefault |
|
inlineprivate |
Definition at line 389 of file tinyexpr.h.
|
inlineprivate |
Definition at line 435 of file tinyexpr.h.
|
inline |
Adds a custom variable or function.
| var | The variable/function to add. |
| std::runtime_error | Throws an exception if an illegal character is found in the variable name. |
Definition at line 594 of file tinyexpr.h.
| bool te_parser::compile | ( | std::string_view | expression | ) |
Parses the input expression.
| expression | The formula to compile. |
| std::runtime_error | Throws an exception in the case of arithmetic overflows (e.g., 1 << 64 would cause an overflow). |
|
nodiscard |
Evaluates expression passed to compile() previously and returns its result.
| std::runtime_error | Throws an exception in the case of arithmetic overflows (e.g., 1 << 64 would cause an overflow). |
|
nodiscard |
Compiles and evaluates an expression and returns its result.
| expression | The formula to compile and evaluate. |
| std::runtime_error | Throws an exception in the case of arithmetic overflows (e.g., 1 << 64 would cause an overflow). |
|
inlinestaticnodiscardprivate |
Definition at line 1177 of file tinyexpr.h.
|
inlinestaticnodiscardprivate |
Definition at line 1184 of file tinyexpr.h.
|
inlinestaticnodiscardprivate |
chr at or after startIndex, skipping quoted string literals, or std::string::npos if not found. | str | The string to search. |
| chr | The character to search for. |
| startIndex | Where to begin accepting matches. |
str, so this stays correct after the caller has erased earlier sections. Definition at line 892 of file tinyexpr.h.
|
inlinenodiscardprivate |
name, or end of get_variables_and_functions() if not found. | name | The name of the function or variable to search for. |
Definition at line 910 of file tinyexpr.h.
|
inlinenodiscardprivate |
name, or end of get_variables_and_functions() if not found. | name | The name of the function or variable to search for. |
Definition at line 926 of file tinyexpr.h.
|
inlinestaticnodiscardprivate |
Definition at line 949 of file tinyexpr.h.
|
inlinenodiscardprivatenoexcept |
Gets the compiled expression, which will be the optimized version of the original expression.
Definition at line 840 of file tinyexpr.h.
|
inlinenodiscardprivatenoexcept |
Definition at line 846 of file tinyexpr.h.
|
inlinenodiscard |
Retrieves a constant variable's value.
| name | The name of the (constant) variable. |
Definition at line 718 of file tinyexpr.h.
|
inlinestaticnodiscardconstexprprivate |
Definition at line 973 of file tinyexpr.h.
|
inlinenodiscardnoexcept |
Definition at line 659 of file tinyexpr.h.
|
inlinenodiscardprivatenoexcept |
Definition at line 665 of file tinyexpr.h.
|
inlinenodiscardnoexcept |
Definition at line 788 of file tinyexpr.h.
|
inlinenodiscardnoexcept |
Definition at line 566 of file tinyexpr.h.
|
inlinenodiscardnoexcept |
Definition at line 554 of file tinyexpr.h.
|
inlinenodiscardprivatenoexcept |
Definition at line 560 of file tinyexpr.h.
|
inlinenodiscardnoexcept |
Definition at line 734 of file tinyexpr.h.
|
inlinenodiscardprivatenoexcept |
Definition at line 740 of file tinyexpr.h.
|
inlinestaticnodiscard |
Definition at line 485 of file tinyexpr.h.
|
inlinestaticnodiscardconstexprnoexcept |
Definition at line 478 of file tinyexpr.h.
|
inlinestaticnodiscardprivate |
Definition at line 1077 of file tinyexpr.h.
|
inlinenodiscardnoexcept |
Definition at line 527 of file tinyexpr.h.
|
inlinenodiscardprivatenoexcept |
Definition at line 533 of file tinyexpr.h.
|
inlinestaticnodiscardconstexprprivate |
Definition at line 1005 of file tinyexpr.h.
|
inlinestaticnodiscardconstexprprivate |
Definition at line 984 of file tinyexpr.h.
|
inlinenodiscardprivatenoexcept |
Definition at line 647 of file tinyexpr.h.
|
inlinenodiscardnoexcept |
Definition at line 653 of file tinyexpr.h.
|
staticnodiscard |
|
inlinestaticnodiscardconstexprprivate |
true for any context function, including the ones taking te_arg. Definition at line 1069 of file tinyexpr.h.
|
inlinestaticnodiscardconstexprprivate |
true for any function, including the ones taking te_arg. Definition at line 1062 of file tinyexpr.h.
|
inlinestaticnodiscardconstexprprivatenoexcept |
Definition at line 1016 of file tinyexpr.h.
|
inlinestaticnodiscardconstexprprivatenoexcept |
Definition at line 1011 of file tinyexpr.h.
|
inlinestaticnodiscardconstexprprivate |
Definition at line 1036 of file tinyexpr.h.
|
inlinestaticnodiscardconstexprprivatenoexcept |
Definition at line 968 of file tinyexpr.h.
|
inlinestaticnodiscardconstexprprivate |
Definition at line 990 of file tinyexpr.h.
|
inlinenodiscard |
true if name is a function that had been used in the last parsed formula. | name | The name of the function. |
Definition at line 768 of file tinyexpr.h.
|
inlinestaticnodiscardconstexprprivatenoexcept |
Definition at line 1147 of file tinyexpr.h.
|
inlinestaticnodiscardconstexprprivatenoexcept |
true if character is valid for a function or variable name. | chr | The character to review. |
Definition at line 877 of file tinyexpr.h.
|
inlinestaticnodiscardconstexprprivate |
Definition at line 938 of file tinyexpr.h.
|
inlinestaticnodiscardconstexprprivatenoexcept |
Definition at line 1000 of file tinyexpr.h.
|
inlinestaticnodiscardconstexprprivatenoexcept |
Definition at line 979 of file tinyexpr.h.
|
inlinenodiscard |
true if name is a variable that had been used in the last parsed formula. | name | The name of the variable. |
Definition at line 777 of file tinyexpr.h.
|
inlinestaticnodiscardconstexprprivate |
Definition at line 943 of file tinyexpr.h.
|
nodiscard |
|
inlinestaticnodiscardprivate |
Definition at line 1138 of file tinyexpr.h.
|
inlinestaticnodiscardprivate |
Definition at line 1124 of file tinyexpr.h.
|
private |
|
inlinestaticnodiscardprivate |
Helper function to convert a number into boolean.
This takes NaN into account, returning for NaN.
| val | The value to examine. |
true if the value is non-zero and also valid (not NaN or infinite). Definition at line 799 of file tinyexpr.h.
Definition at line 409 of file tinyexpr.h.
|
staticprivate |
|
inline |
Removes any custom variables and functions that weren't used in the last compilation.
This can be useful if the parser is preloaded with a large number of variables and functions that needs to be pruned after the first expression is parsed.
Definition at line 619 of file tinyexpr.h.
|
inline |
Removes a custom variable or function.
| var | The variable/function to remove (by name). |
Definition at line 601 of file tinyexpr.h.
|
inlineprivate |
Reset everything from previous call.
Definition at line 810 of file tinyexpr.h.
|
inlineprivate |
Resets any resolved variables from USR if not being cached.
Definition at line 827 of file tinyexpr.h.
|
inline |
Sets a constant variable's value.
| name | The name of the (constant) variable. |
| value | The new value to set the constant to. |
Definition at line 698 of file tinyexpr.h.
|
inline |
Sets the decimal separator used for numbers.
| sep | The decimal separator. |
| std::runtime_error | Throws an exception if an illegal character is used. |
Definition at line 673 of file tinyexpr.h.
|
inlineprivate |
Definition at line 682 of file tinyexpr.h.
|
inline |
Sets the separator used between function arguments.
| sep | The list separator. |
| std::runtime_error | Throws an exception if an illegal character is used. |
Definition at line 748 of file tinyexpr.h.
|
inlineprivate |
Definition at line 756 of file tinyexpr.h.
|
inline |
Sets a custom function to resolve unknown symbols in an expression.
| usr | The function to use to resolve unknown symbols. |
| keepResolvedVariables | true to cache any resolved variables into the parser. This means that they will not need to be resolved again on subsequent calls to evaluate().Pass false to this if you wish to re-resolve any previously resolved variables on later evaluations. This can be useful for when a resolved variable's value is volatile and needs to be re-resolved on every use. |
Definition at line 639 of file tinyexpr.h.
|
inline |
Sets the list of custom variables and functions.
| vars | The list of variables and functions. |
| std::runtime_error | Throws an exception if an illegal character is found in any variable name. |
Definition at line 579 of file tinyexpr.h.
|
inlinenodiscardnoexcept |
Definition at line 540 of file tinyexpr.h.
|
inlinenodiscardnoexcept |
Definition at line 545 of file tinyexpr.h.
|
inlinestaticnodiscardconstexprnoexcept |
true if the parser's internal type can hold uint32_t without truncation. Definition at line 449 of file tinyexpr.h.
|
inlinestaticnodiscardconstexprnoexcept |
true if the parser's internal type can hold uint64_t without truncation. Definition at line 457 of file tinyexpr.h.
|
nodiscardprivate |
Parses the input expression and binds variables.
| expression | The formula to parse. |
| variables | The collection of custom functions and variables to add to the parser. |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
inlinestaticnodiscardconstexprprivatenoexcept |
true if val is too large to convert to a uint32_t. Definition at line 465 of file tinyexpr.h.
|
inlinestaticprivate |
Definition at line 1165 of file tinyexpr.h.
|
staticprivate |
|
inlinestaticprivate |
Validates that a variable only contains legal characters (and has a valid length).
| var | The variable to validate. |
| std::runtime_error | Throws an exception if an illegal character is found. |
Definition at line 855 of file tinyexpr.h.
|
private |
Definition at line 1238 of file tinyexpr.h.
|
private |
Definition at line 1247 of file tinyexpr.h.
|
private |
Definition at line 1227 of file tinyexpr.h.
|
private |
Definition at line 1233 of file tinyexpr.h.
|
private |
Definition at line 1241 of file tinyexpr.h.
|
private |
Definition at line 1237 of file tinyexpr.h.
|
staticprivate |
Definition at line 1224 of file tinyexpr.h.
|
private |
Definition at line 1231 of file tinyexpr.h.
|
private |
Definition at line 1242 of file tinyexpr.h.
|
private |
Definition at line 1234 of file tinyexpr.h.
|
private |
Definition at line 1240 of file tinyexpr.h.
|
private |
Definition at line 1245 of file tinyexpr.h.
Definition at line 1243 of file tinyexpr.h.
|
private |
Definition at line 1229 of file tinyexpr.h.
|
private |
Definition at line 1250 of file tinyexpr.h.
|
private |
Definition at line 1251 of file tinyexpr.h.
|
private |
Definition at line 1248 of file tinyexpr.h.
|
staticconstexprprivate |
Definition at line 444 of file tinyexpr.h.
|
staticconstexpr |
No position, which is what get_last_error_position() returns when there was no parsing error.
Definition at line 441 of file tinyexpr.h.
|
staticconstexpr |
NaN (not-a-number) constant to indicate an invalid value.
Definition at line 438 of file tinyexpr.h.