SimpleParser.hpp
Parser to convert function descriptions to lambdas using maps for variable lookup.
A fully functional parser that will convert a string-description of a function to a C++ lambda. A map-typed object should be passed in to provide values associated with variables. Allowed map types include std::map<std::string,T>, std::unordered_map<std::string,T>, DataMap, and (soon) derivations from AnnotatedType. For standard maps, T must be convertable to Datum.
Developer TODO:
Setup operator RegEx to be built dynamically
Setup LVALUES as a type, and allow assignment
Allow types other than Datum (string and double)?
Note
Status: ALPHA
-
class SimpleParser
- #include <SimpleParser.hpp>
Public Functions
-
inline SimpleParser(bool use_defaults = true)
-
inline SimpleParser(bool use_defaults, Random &random)
Construct with a random number generator to automatically include random functions.
-
inline const std::set<std::string> &GetNamesUsed() const
Get the set of variable names that the most recently generated function used.
-
inline const std::set<std::string> &GetNamesUsed(const std::string &expression)
Get the set of names used in the provided equation.
-
inline void AddOp(const std::string &op, size_t prec, std::function<Datum(Datum, Datum)> fun)
Add a binary operator.
-
inline void AddDefaultOperators()
-
inline void AddDefaultFunctions()
-
template<typename SYMBOLS_T>
inline SYMBOLS_T::value_t ParseValue(const SYMBOLS_T &symbols, pos_t &pos) Helpers for parsing.
-
template<typename SYMBOLS_T>
inline SYMBOLS_T::value_t ParseMath(const SYMBOLS_T &symbols, pos_t &pos, size_t prec_limit = 0)
-
template<typename T1, typename ...Ts>
inline void SetupStaticValues(T1 arg1, Ts... args) Take a set of variables and use them to replace $0, $1, etc. in any function.
-
inline void SetupStaticValues()
If there are no input args, just clear external values.
-
template<typename MAP_T, typename ...EXTRA_Ts>
inline auto BuildMathFunction(const MAP_T &symbol_map, const std::string &expression, EXTRA_Ts... extra_args) Parse a function description that will take a map and return the results. For example, if the string “foo * 2 + bar” is passed in, a function will be returned that takes a map (of the proper type) loads in the values of “foo” and “bar”, and returns the result of the above equation.
- Parameters:
symbol_map – The map or layout to use, specifying variables.
expression – The primary expression to convert.
extra_args – Extra value arguments (accessed as $1, $2, etc.)
-
template<typename MAP_T, typename ...ARG_Ts>
inline Datum RunMathFunction(const MAP_T &symbol_map, ARG_Ts... args) Generate a temporary math function and immediately run it on the provided arguments.
- Parameters:
symbol_map – The map containing the required variables.
expression – The mathematical expression to be run on the data map.
extras – Any extra values to fill in a $0, $1, etc.
Public Static Functions
-
static inline int ApproxCompare(double x, double y)
Private Types
-
using pos_t = TokenStream::Iterator
Private Members
-
std::unordered_map<std::string, BinaryOperator> binary_ops
Private Static Attributes
-
static constexpr const bool verbose = false
-
struct BinaryOperator
-
struct Function
Public Types
Public Functions
-
class MapLexer : public Lexer
Public Functions
-
inline MapLexer()
Private Members
-
int token_identifier
Token id for identifiers.
-
int token_number
Token id for literal numbers.
-
int token_string
Token id for literal strings.
-
int token_char
Token id for literal characters.
-
int token_external
Token id for an external value that was passed in.
-
int token_symbol
Token id for other symbols.
-
inline MapLexer()
-
template<typename MAP_T, typename DUMMY_T = int>
struct SymbolTable Public Types
Public Functions
-
inline SymbolTable()
-
inline SymbolTable()
-
template<typename DUMMY_T>
struct SymbolTable<DataLayout, DUMMY_T> Specialty implementation for DataLayouts.
Public Types
Public Functions
-
inline SymbolTable(const DataLayout &in_layout)
Public Members
-
const DataLayout &layout
-
inline SymbolTable(const DataLayout &in_layout)
-
template<typename DUMMY_T>
struct SymbolTable<DataMap, DUMMY_T> : public SimpleParser::SymbolTable<DataLayout> Special DataMap implementation that just converts to underlying layout.
- template<typename VALUE_T, typename DUMMY_T> string, VALUE_T >, DUMMY_T >
Public Types
Public Functions
-
template<typename ARG_T>
struct ValueType Public Types
-
inline SimpleParser(bool use_defaults = true)