Skip to content

Add a one-token cache #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jul 24, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a one-token cache.
This will avoid duplicating tokenization work,
hopefully in most uses of `Parser::try`.
  • Loading branch information
SimonSapin committed Jul 18, 2017
commit b6072ca53148cf4eba1cdb02f4cc8378cafda104
56 changes: 51 additions & 5 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,23 @@ impl<'a, T> ParseError<'a, T> {
}

/// The owned input for a parser.
pub struct ParserInput<'t> {
tokenizer: Tokenizer<'t>,
pub struct ParserInput<'i> {
tokenizer: Tokenizer<'i>,
cached_token: Option<CachedToken<'i>>,
}

impl<'t> ParserInput<'t> {
struct CachedToken<'i> {
token: Token<'i>,
start_position: tokenizer::SourcePosition,
end_position: tokenizer::SourcePosition,
}

impl<'i> ParserInput<'i> {
/// Create a new input for a parser.
pub fn new(input: &'t str) -> ParserInput<'t> {
pub fn new(input: &'i str) -> ParserInput<'i> {
ParserInput {
tokenizer: Tokenizer::new(input),
cached_token: None,
}
}
}
Expand Down Expand Up @@ -348,11 +356,49 @@ impl<'i: 't, 't> Parser<'i, 't> {
if let Some(block_type) = self.at_start_of.take() {
consume_until_end_of_block(block_type, &mut self.input.tokenizer);
}

let byte = self.input.tokenizer.next_byte();
if self.stop_before.contains(Delimiters::from_byte(byte)) {
return Err(BasicParseError::EndOfInput)
}
let token = self.input.tokenizer.next().map_err(|()| BasicParseError::EndOfInput)?;

let token_start_position = self.input.tokenizer.position();
let token;
match self.input.cached_token {
Some(ref cached_token) if cached_token.start_position == token_start_position => {
self.input.tokenizer.reset(cached_token.end_position);
token = cached_token.token.clone();
}
_ => {
token = self.input.tokenizer.next().map_err(|()| BasicParseError::EndOfInput)?;
match token {
// Don’t cache whitespace or comment tokens.
// A typical pattern is:
//
// ```
// parser.try(|parser| {
// match parser.next() { … }
// }).or_else(|| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this change behaviour if I do:

parser.try(|p| match parser.next_including_whitespace_and_comments() { .. })
    .or_else(|| match parser.next_including_whitespace_and_comments() { .. })

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIU, the position won't be the same after the whitespace token and thus the cache won't be used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right

// match parser.next() { … }
// })
// ```
//
// If the curren position at the start of this code is at a whitespace token,
// the "interesting" token (returned by `next`) comes later.
// So in the second call to `next`, we don’t want "uninteresting" tokens
// to overwrite the cache.
Token::WhiteSpace(_) | Token::Comment(_) => {}
_ => {
self.input.cached_token = Some(CachedToken {
token: token.clone(),
start_position: token_start_position,
end_position: self.input.tokenizer.position(),
})
}
}
}
}

if let Some(block_type) = BlockType::opening(&token) {
self.at_start_of = Some(block_type);
}
Expand Down