mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 09:08:08 +08:00
perf: remove err-msg from combinator
Generate error message in `parse_something()`, not in `lex_something`. Since the error message generated by `lex_something` is too difficult to read for humans, I've disabled the error message generation for the sake of efficiency (it takes time to generate error message that will never be read). I think now the error message generation itself safely can be removed from combinators. At this stage, `lex_something` does not need to return `result<T, E>` because all the error type would be discarded. Now it is turned out that returing `optional<T>` from lex_* is enough. Maybe later I would change the return type itself, but currently I changed the error type from std::string to char because implementing optional takes time and effort. It makes the parsing process a bit faster.
This commit is contained in:
@@ -56,24 +56,19 @@ struct character
|
||||
static constexpr char target = C;
|
||||
|
||||
template<typename Cont>
|
||||
static result<region<Cont>, std::string>
|
||||
invoke(location<Cont>& loc, const bool msg = false)
|
||||
static result<region<Cont>, char>
|
||||
invoke(location<Cont>& loc)
|
||||
{
|
||||
static_assert(std::is_same<char, typename Cont::value_type>::value,
|
||||
"internal error: container::value_type should be `char`.");
|
||||
|
||||
if(loc.iter() == loc.end()) {return err("not sufficient characters");}
|
||||
if(loc.iter() == loc.end()) {return err('\0');}
|
||||
const auto first = loc.iter();
|
||||
|
||||
const char c = *(loc.iter());
|
||||
if(c != target)
|
||||
{
|
||||
if(msg)
|
||||
{
|
||||
return err(concat_to_string("expected '", show_char(target),
|
||||
"' but got '", show_char(c), "'."));
|
||||
}
|
||||
return err("");
|
||||
return err(c);
|
||||
}
|
||||
loc.advance(); // update location
|
||||
|
||||
@@ -94,25 +89,19 @@ struct in_range
|
||||
static constexpr char lower = Low;
|
||||
|
||||
template<typename Cont>
|
||||
static result<region<Cont>, std::string>
|
||||
invoke(location<Cont>& loc, const bool msg = false)
|
||||
static result<region<Cont>, char>
|
||||
invoke(location<Cont>& loc)
|
||||
{
|
||||
static_assert(std::is_same<char, typename Cont::value_type>::value,
|
||||
"internal error: container::value_type should be `char`.");
|
||||
|
||||
if(loc.iter() == loc.end()) {return err("not sufficient characters");}
|
||||
if(loc.iter() == loc.end()) {return err('\0');}
|
||||
const auto first = loc.iter();
|
||||
|
||||
const char c = *(loc.iter());
|
||||
if(c < lower || upper < c)
|
||||
{
|
||||
if(msg)
|
||||
{
|
||||
return err(concat_to_string("expected character in range "
|
||||
"[", show_char(lower), ", ", show_char(upper), "] but got ",
|
||||
"'", show_char(c), "'."));
|
||||
}
|
||||
return err("");
|
||||
return err(c);
|
||||
}
|
||||
|
||||
loc.advance();
|
||||
@@ -128,25 +117,20 @@ template<typename Combinator>
|
||||
struct exclude
|
||||
{
|
||||
template<typename Cont>
|
||||
static result<region<Cont>, std::string>
|
||||
invoke(location<Cont>& loc, const bool msg = false)
|
||||
static result<region<Cont>, char>
|
||||
invoke(location<Cont>& loc)
|
||||
{
|
||||
static_assert(std::is_same<char, typename Cont::value_type>::value,
|
||||
"internal error: container::value_type should be `char`.");
|
||||
|
||||
if(loc.iter() == loc.end()) {return err("not sufficient characters");}
|
||||
if(loc.iter() == loc.end()) {return err('\0');}
|
||||
auto first = loc.iter();
|
||||
|
||||
auto rslt = Combinator::invoke(loc, msg);
|
||||
auto rslt = Combinator::invoke(loc);
|
||||
if(rslt.is_ok())
|
||||
{
|
||||
loc.reset(first);
|
||||
if(msg)
|
||||
{
|
||||
return err(concat_to_string("invalid pattern appeared ",
|
||||
rslt.unwrap().str()));
|
||||
}
|
||||
return err("");
|
||||
return err(*first);
|
||||
}
|
||||
loc.reset(std::next(first)); // XXX maybe loc.advance() is okay but...
|
||||
return ok(region<Cont>(loc, first, loc.iter()));
|
||||
@@ -158,13 +142,13 @@ template<typename Combinator>
|
||||
struct maybe
|
||||
{
|
||||
template<typename Cont>
|
||||
static result<region<Cont>, std::string>
|
||||
invoke(location<Cont>& loc, const bool msg = false)
|
||||
static result<region<Cont>, char>
|
||||
invoke(location<Cont>& loc)
|
||||
{
|
||||
static_assert(std::is_same<char, typename Cont::value_type>::value,
|
||||
"internal error: container::value_type should be `char`.");
|
||||
|
||||
const auto rslt = Combinator::invoke(loc, msg);
|
||||
const auto rslt = Combinator::invoke(loc);
|
||||
if(rslt.is_ok())
|
||||
{
|
||||
return rslt;
|
||||
@@ -180,36 +164,35 @@ template<typename Head, typename ... Tail>
|
||||
struct sequence<Head, Tail...>
|
||||
{
|
||||
template<typename Cont>
|
||||
static result<region<Cont>, std::string>
|
||||
invoke(location<Cont>& loc, const bool msg = false)
|
||||
static result<region<Cont>, char>
|
||||
invoke(location<Cont>& loc)
|
||||
{
|
||||
static_assert(std::is_same<char, typename Cont::value_type>::value,
|
||||
"internal error: container::value_type should be `char`.");
|
||||
|
||||
const auto first = loc.iter();
|
||||
const auto rslt = Head::invoke(loc, msg);
|
||||
const auto rslt = Head::invoke(loc);
|
||||
if(rslt.is_err())
|
||||
{
|
||||
loc.reset(first);
|
||||
return err(rslt.unwrap_err());
|
||||
}
|
||||
return sequence<Tail...>::invoke(loc, std::move(rslt.unwrap()), first, msg);
|
||||
return sequence<Tail...>::invoke(loc, std::move(rslt.unwrap()), first);
|
||||
}
|
||||
|
||||
// called from the above function only, recursively.
|
||||
template<typename Cont, typename Iterator>
|
||||
static result<region<Cont>, std::string>
|
||||
invoke(location<Cont>& loc, region<Cont> reg, Iterator first,
|
||||
const bool msg = false)
|
||||
static result<region<Cont>, char>
|
||||
invoke(location<Cont>& loc, region<Cont> reg, Iterator first)
|
||||
{
|
||||
const auto rslt = Head::invoke(loc, msg);
|
||||
const auto rslt = Head::invoke(loc);
|
||||
if(rslt.is_err())
|
||||
{
|
||||
loc.reset(first);
|
||||
return err(rslt.unwrap_err());
|
||||
}
|
||||
reg += rslt.unwrap(); // concat regions
|
||||
return sequence<Tail...>::invoke(loc, std::move(reg), first, msg);
|
||||
return sequence<Tail...>::invoke(loc, std::move(reg), first);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -218,11 +201,10 @@ struct sequence<Head>
|
||||
{
|
||||
// would be called from sequence<T ...>::invoke only.
|
||||
template<typename Cont, typename Iterator>
|
||||
static result<region<Cont>, std::string>
|
||||
invoke(location<Cont>& loc, region<Cont> reg, Iterator first,
|
||||
const bool msg = false)
|
||||
static result<region<Cont>, char>
|
||||
invoke(location<Cont>& loc, region<Cont> reg, Iterator first)
|
||||
{
|
||||
const auto rslt = Head::invoke(loc, msg);
|
||||
const auto rslt = Head::invoke(loc);
|
||||
if(rslt.is_err())
|
||||
{
|
||||
loc.reset(first);
|
||||
@@ -240,27 +222,27 @@ template<typename Head, typename ... Tail>
|
||||
struct either<Head, Tail...>
|
||||
{
|
||||
template<typename Cont>
|
||||
static result<region<Cont>, std::string>
|
||||
invoke(location<Cont>& loc, const bool msg = false)
|
||||
static result<region<Cont>, char>
|
||||
invoke(location<Cont>& loc)
|
||||
{
|
||||
static_assert(std::is_same<char, typename Cont::value_type>::value,
|
||||
"internal error: container::value_type should be `char`.");
|
||||
|
||||
const auto rslt = Head::invoke(loc, msg);
|
||||
const auto rslt = Head::invoke(loc);
|
||||
if(rslt.is_ok()) {return rslt;}
|
||||
return either<Tail...>::invoke(loc, msg);
|
||||
return either<Tail...>::invoke(loc);
|
||||
}
|
||||
};
|
||||
template<typename Head>
|
||||
struct either<Head>
|
||||
{
|
||||
template<typename Cont>
|
||||
static result<region<Cont>, std::string>
|
||||
invoke(location<Cont>& loc, const bool msg = false)
|
||||
static result<region<Cont>, char>
|
||||
invoke(location<Cont>& loc)
|
||||
{
|
||||
static_assert(std::is_same<char, typename Cont::value_type>::value,
|
||||
"internal error: container::value_type should be `char`.");
|
||||
return Head::invoke(loc, msg);
|
||||
return Head::invoke(loc);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -275,14 +257,14 @@ template<typename T, std::size_t N>
|
||||
struct repeat<T, exactly<N>>
|
||||
{
|
||||
template<typename Cont>
|
||||
static result<region<Cont>, std::string>
|
||||
invoke(location<Cont>& loc, const bool msg = false)
|
||||
static result<region<Cont>, char>
|
||||
invoke(location<Cont>& loc)
|
||||
{
|
||||
region<Cont> retval(loc);
|
||||
const auto first = loc.iter();
|
||||
for(std::size_t i=0; i<N; ++i)
|
||||
{
|
||||
auto rslt = T::invoke(loc, msg);
|
||||
auto rslt = T::invoke(loc);
|
||||
if(rslt.is_err())
|
||||
{
|
||||
loc.reset(first);
|
||||
@@ -298,15 +280,15 @@ template<typename T, std::size_t N>
|
||||
struct repeat<T, at_least<N>>
|
||||
{
|
||||
template<typename Cont>
|
||||
static result<region<Cont>, std::string>
|
||||
invoke(location<Cont>& loc, const bool msg = false)
|
||||
static result<region<Cont>, char>
|
||||
invoke(location<Cont>& loc)
|
||||
{
|
||||
region<Cont> retval(loc);
|
||||
|
||||
const auto first = loc.iter();
|
||||
for(std::size_t i=0; i<N; ++i)
|
||||
{
|
||||
auto rslt = T::invoke(loc, msg);
|
||||
auto rslt = T::invoke(loc);
|
||||
if(rslt.is_err())
|
||||
{
|
||||
loc.reset(first);
|
||||
@@ -316,7 +298,7 @@ struct repeat<T, at_least<N>>
|
||||
}
|
||||
while(true)
|
||||
{
|
||||
auto rslt = T::invoke(loc, msg);
|
||||
auto rslt = T::invoke(loc);
|
||||
if(rslt.is_err())
|
||||
{
|
||||
return ok(std::move(retval));
|
||||
@@ -330,13 +312,13 @@ template<typename T>
|
||||
struct repeat<T, unlimited>
|
||||
{
|
||||
template<typename Cont>
|
||||
static result<region<Cont>, std::string>
|
||||
invoke(location<Cont>& loc, const bool msg = false)
|
||||
static result<region<Cont>, char>
|
||||
invoke(location<Cont>& loc)
|
||||
{
|
||||
region<Cont> retval(loc);
|
||||
while(true)
|
||||
{
|
||||
auto rslt = T::invoke(loc, msg);
|
||||
auto rslt = T::invoke(loc);
|
||||
if(rslt.is_err())
|
||||
{
|
||||
return ok(std::move(retval));
|
||||
|
Reference in New Issue
Block a user