From 6862a5a1dceaf719ac144a2c636635d3147dd08e Mon Sep 17 00:00:00 2001 From: Arash Partow Date: Tue, 21 Apr 2015 22:49:03 +1000 Subject: [PATCH] C++ Mathematical Expression Library (ExprTk) http://www.partow.net/programming/exprtk/index.html --- exprtk.hpp | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/exprtk.hpp b/exprtk.hpp index 0a26029..0854400 100644 --- a/exprtk.hpp +++ b/exprtk.hpp @@ -3601,6 +3601,84 @@ namespace exprtk lexer::token_inserter* error_token_inserter; }; } + + struct parser_helper + { + typedef token token_t; + typedef generator generator_t; + + inline bool init(const std::string& str) + { + if (!lexer_.process(str)) + { + return false; + } + + lexer_.begin(); + + next_token(); + + return true; + } + + inline void next_token() + { + current_token_ = lexer_.next_token(); + } + + inline const token_t& current_token() const + { + return current_token_; + } + + inline bool token_is(const token_t::token_type& ttype, const bool advance_token = true) + { + if (current_token_.type != ttype) + { + return false; + } + + if (advance_token) + { + next_token(); + } + + return true; + } + + inline bool token_is(const token_t::token_type& ttype, + const std::string& value, + const bool advance_token = true) + { + if ( + (current_token_.type != ttype) || + !exprtk::details::imatch(value,current_token().value) + ) + { + return false; + } + + if (advance_token) + { + next_token(); + } + + return true; + } + + inline bool peek_token_is(const token_t::token_type& ttype) + { + return (lexer_.peek_next_token().type == ttype); + } + + inline bool peek_token_is(const std::string& s) + { + return (exprtk::details::imatch(lexer_.peek_next_token().value,s)); + } + + generator_t lexer_; + token_t current_token_; + }; } template class results_context;