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, std::function<Datum(Datum)> fun)

Add a unary operator.

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()
inline void AddRandomFunctions(Random &random)
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 Functions

template<typename ...Ts>
inline size_t ParseError(Ts&&... args)

Private Members

MapLexer lexer
std::unordered_map<std::string, std::function<Datum(Datum)>> unary_ops
std::unordered_map<std::string, BinaryOperator> binary_ops
std::unordered_map<std::string, Function> functions
vector<Datum> external_vals
std::set<std::string> var_names

Private Static Attributes

static constexpr const bool verbose = false
struct BinaryOperator

Public Types

using fun_t = std::function<Datum(Datum, Datum)>

Public Functions

inline void Set(size_t in_prec, fun_t in_fun)

Public Members

size_t prec
fun_t fun
struct Function

Public Types

using fun0_t = std::function<Datum()>
using fun1_t = std::function<Datum(Datum)>
using fun2_t = std::function<Datum(Datum, Datum)>
using fun3_t = std::function<Datum(Datum, Datum, Datum)>

Public Functions

inline void Set0(fun0_t in_fun)
inline void Set1(fun1_t in_fun)
inline void Set2(fun2_t in_fun)
inline void Set3(fun3_t in_fun)

Public Members

size_t num_args = 0
fun0_t fun0
fun1_t fun1
fun2_t fun2
fun3_t fun3
class MapLexer : public Lexer

Public Functions

inline MapLexer()
inline bool IsID(const Token &token) const noexcept
inline bool IsNumber(const Token &token) const noexcept
inline bool IsString(const Token &token) const noexcept
inline bool IsChar(const Token &token) const noexcept
inline bool IsExternal(const Token &token) const noexcept
inline bool IsSymbol(const Token &token) const noexcept

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.

template<typename MAP_T, typename DUMMY_T = int>
struct SymbolTable

Public Types

using arg_t = const MAP_T&
using fun_t = std::function<Datum(arg_t)>
using value_t = ValueType<arg_t>

Public Functions

inline SymbolTable()
inline SymbolTable(arg_t)
inline auto AsFunction(ValueType<arg_t> &val) const

By default, let the value handle its own converstion to a function.

Public Static Functions

static inline fun_t MakeDatumAccessor(const std::string &name)
template<typename DUMMY_T>
struct SymbolTable<DataLayout, DUMMY_T>

Specialty implementation for DataLayouts.

Public Types

using arg_t = const DataMap&
using fun_t = std::function<Datum(arg_t)>
using value_t = ValueType<arg_t>

Public Functions

inline SymbolTable(const DataLayout &in_layout)
inline auto MakeDatumAccessor(const std::string &name) const
inline auto AsFunction(ValueType<arg_t> &val) const

Public Members

const DataLayout &layout
template<typename DUMMY_T>
struct SymbolTable<DataMap, DUMMY_T> : public SimpleParser::SymbolTable<DataLayout>

Special DataMap implementation that just converts to underlying layout.

Public Functions

inline SymbolTable(const DataMap &dm)
template<typename VALUE_T, typename DUMMY_T> string, VALUE_T >, DUMMY_T >

Public Types

using map_t = ra_map<std::string, VALUE_T>
using arg_t = const map_t&
using fun_t = std::function<Datum(arg_t)>
using value_t = ValueType<arg_t>

Public Functions

inline SymbolTable(const ra_map<std::string, VALUE_T> &in_map)
inline fun_t MakeDatumAccessor(const std::string &name) const
inline auto AsFunction(ValueType<arg_t> &val) const

By default, let the value handle its own converstion to a function.

Public Members

const map_t::layout_t &layout
template<typename ARG_T>
struct ValueType

Public Types

enum type_t

Values:

enumerator ERROR
enumerator VALUE
enumerator FUNCTION
using fun_t = std::function<Datum(ARG_T)>

Public Functions

inline ValueType()
ValueType(const ValueType&) = default
inline ValueType(double in_val)
inline ValueType(std::string in_val)
inline ValueType(Datum in_val)
inline ValueType(fun_t in_fun)
ValueType &operator=(const ValueType&) = default
inline ValueType &operator=(Datum in_val)
inline ValueType &operator=(double in_val)
inline ValueType &operator=(const std::string &in_val)
inline ValueType &operator=(fun_t in_fun)
inline fun_t AsFunction()

Public Members

type_t type
Datum value
fun_t fun
template<typename VALUE_T, typename DUMMY_T>
struct SymbolTable<ra_map<std::string, VALUE_T>, DUMMY_T>

Public Types

using map_t = ra_map<std::string, VALUE_T>
using arg_t = const map_t&
using fun_t = std::function<Datum(arg_t)>
using value_t = ValueType<arg_t>

Public Functions

inline SymbolTable(const ra_map<std::string, VALUE_T> &in_map)
inline fun_t MakeDatumAccessor(const std::string &name) const
inline auto AsFunction(ValueType<arg_t> &val) const

By default, let the value handle its own converstion to a function.

Public Members

const map_t::layout_t &layout