Skip to content
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

Switch tokenizer to use a pointer rather than index for position tracking #383

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ license = "MPL-2.0"
edition = "2018"
rust-version = "1.63"

exclude = ["src/css-parsing-tests/**", "src/big-data-url.css"]
exclude = ["src/css-parsing-tests/**", "src/big-data-url.css", "src/bootstrap.min.css"]

[dev-dependencies]
serde_json = "1.0.25"
Expand Down
7 changes: 7 additions & 0 deletions src/bootstrap.min.css

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use std::ops::Range;
/// Should only be used with the `Parser` instance it came from.
#[derive(Debug, Clone)]
pub struct ParserState {
pub(crate) position: usize,
pub(crate) current_line_start_position: usize,
pub(crate) position: SourcePosition,
pub(crate) current_line_start_position: SourcePosition,
pub(crate) current_line_number: u32,
pub(crate) at_start_of: Option<BlockType>,
}
Expand All @@ -26,15 +26,15 @@ impl ParserState {
/// The position from the start of the input, counted in UTF-8 bytes.
#[inline]
pub fn position(&self) -> SourcePosition {
SourcePosition(self.position)
self.position
}

/// The line number and column number
#[inline]
pub fn source_location(&self) -> SourceLocation {
SourceLocation {
line: self.current_line_number,
column: (self.position - self.current_line_start_position + 1) as u32,
column: (self.position.0 - self.current_line_start_position.0 + 1) as u32,
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,41 @@ fn numeric(b: &mut Bencher) {
})
}

#[cfg(feature = "bench")]
const BOOTSTRAP: &'static str = include_str!("bootstrap.min.css");

#[cfg(feature = "bench")]
fn tokenize_recursive<'i>(input: &mut Parser<'i, '_>) -> Result<(), ParseError<'i, ()>> {
while !input.is_exhausted() {
match input.next()? {
Token::ParenthesisBlock |
Token::SquareBracketBlock |
Token::CurlyBracketBlock |
Token::Function(..) => {
input.parse_nested_block(|input| tokenize_recursive(input))?;
},
_ => {},
}
}
Ok(())
}

#[cfg(feature = "bench")]
#[bench]
fn bootstrap(b: &mut Bencher) {
b.iter(|| {
let mut input = ParserInput::new(BOOTSTRAP);
let mut input = Parser::new(&mut input);
input.look_for_var_or_env_functions();

let result = tokenize_recursive(&mut input);

assert!(result.is_ok());

(result.is_ok(), input.seen_var_or_env_functions())
})
}

struct JsonParser;

#[cfg_attr(all(miri, feature = "skip_long_tests"), ignore)]
Expand Down
Loading
Loading