perf: cache current line number in location

`location::line_num()` function used to be implemented by using
`std::count`, so each time the parser encounters a type mismatch,
`std::count` was called with almost whole file. It decelerates the
parsing process too much, so I decided to add `line_number_` member
variable to `location` and add `advance/retrace/reset` to `location`
in order to modify the position that is pointed.
This commit is contained in:
ToruNiina
2019-04-12 18:32:46 +09:00
parent ea13e40889
commit 6f950c9ec8
4 changed files with 108 additions and 69 deletions

View File

@@ -65,7 +65,7 @@ struct character
return err(concat_to_string("expected '", show_char(target),
"' but got '", show_char(c), "'."));
}
++(loc.iter()); // update location
loc.advance(); // update location
return ok(region<Cont>(loc, first, loc.iter()));
}
@@ -102,7 +102,7 @@ struct in_range
"'", show_char(c), "'."));
}
++(loc.iter());
loc.advance();
return ok(region<Cont>(loc, first, loc.iter()));
}
@@ -131,12 +131,12 @@ struct exclude
auto rslt = Combinator::invoke(loc);
if(rslt.is_ok())
{
loc.iter() = first; // rollback
loc.reset(first);
return err(concat_to_string(
"invalid pattern (", Combinator::pattern(), ") appeared ",
rslt.unwrap().str()));
}
loc.iter() = std::next(first);
loc.reset(std::next(first)); // XXX maybe loc.advance() is okay but...
return ok(region<Cont>(loc, first, loc.iter()));
}
@@ -186,7 +186,7 @@ struct sequence<Head, Tail...>
const auto rslt = Head::invoke(loc);
if(rslt.is_err())
{
loc.iter() = first;
loc.reset(first);
return err(rslt.unwrap_err());
}
return sequence<Tail...>::invoke(loc, std::move(rslt.unwrap()), first);
@@ -200,7 +200,7 @@ struct sequence<Head, Tail...>
const auto rslt = Head::invoke(loc);
if(rslt.is_err())
{
loc.iter() = first;
loc.reset(first);
return err(rslt.unwrap_err());
}
reg += rslt.unwrap(); // concat regions
@@ -224,7 +224,7 @@ struct sequence<Head>
const auto rslt = Head::invoke(loc);
if(rslt.is_err())
{
loc.iter() = first;
loc.reset(first);
return err(rslt.unwrap_err());
}
reg += rslt.unwrap(); // concat regions
@@ -291,7 +291,7 @@ struct repeat<T, exactly<N>>
auto rslt = T::invoke(loc);
if(rslt.is_err())
{
loc.iter() = first;
loc.reset(first);
return err(rslt.unwrap_err());
}
retval += rslt.unwrap();
@@ -318,7 +318,7 @@ struct repeat<T, at_least<N>>
auto rslt = T::invoke(loc);
if(rslt.is_err())
{
loc.iter() = first;
loc.reset(first);
return err(rslt.unwrap_err());
}
retval += rslt.unwrap();