C++ Mathematical Expression Library (ExprTk) http://www.partow.net/programming/exprtk/index.html

This commit is contained in:
Arash Partow 2014-04-09 07:42:19 +10:00
parent e5f917033f
commit 03c90e1893
1 changed files with 53 additions and 5 deletions

View File

@ -9978,6 +9978,9 @@ namespace exprtk
lexer::token token; lexer::token token;
error_mode mode; error_mode mode;
std::string diagnostic; std::string diagnostic;
std::string error_line;
std::size_t line_no;
std::size_t column_no;
}; };
inline type make_error(error_mode mode, const std::string& diagnostic = "") inline type make_error(error_mode mode, const std::string& diagnostic = "")
@ -9986,6 +9989,8 @@ namespace exprtk
t.mode = mode; t.mode = mode;
t.token.type = lexer::token::e_error; t.token.type = lexer::token::e_error;
t.diagnostic = diagnostic; t.diagnostic = diagnostic;
t.line_no = 0;
t.column_no = 0;
return t; return t;
} }
@ -10013,6 +10018,45 @@ namespace exprtk
} }
} }
bool update_error(type& error, const std::string& expression)
{
if (
expression.empty() ||
(error.token.position >= expression.size()) ||
(std::numeric_limits<std::size_t>::max() == error.token.position)
)
{
return false;
}
std::size_t error_line_start = 0;
for (std::size_t i = error.token.position; i > 0; --i)
{
if ('\n' == expression[i])
{
error_line_start = i;
break;
}
}
std::size_t next_nl_position = std::min(expression.size(),
expression.find_first_of('\n',error.token.position + 1));
error.column_no = error.token.position - error_line_start;
error.error_line = expression.substr(error_line_start,
next_nl_position - error_line_start);
error.line_no = 0;
for (std::size_t i = 0; i < next_nl_position; ++i)
{
if ('\n' == expression[i])
++error.line_no;
}
return true;
}
inline void dump_error(const type& error) inline void dump_error(const type& error)
{ {
printf("Position: %02d Type: [%s] Msg: %s\n", printf("Position: %02d Type: [%s] Msg: %s\n",
@ -10289,12 +10333,16 @@ namespace exprtk
{ {
set_error( set_error(
make_error(parser_error::e_syntax, make_error(parser_error::e_syntax,
"ERR02 - Incomplete expression!")); current_token_,
"ERR02 - Incomplete expression due to unexpected token: '" + current_token_.value + "'"));
symbol_name_cache_.clear(); symbol_name_cache_.clear();
if (0 != e) if (0 != e)
{ {
delete e; delete e;
} }
return false; return false;
} }
} }