Skip to content
Merged
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
Provide ParserInput::new_with_line_number_offset
This patch adds ParserInput::new_with_line_number_offset, a new
constructor that calls Tokenizer::with_first_line_number.  Servo has its
own code for tracking the initial line number, but that code can be
replaced by this.
  • Loading branch information
tromey committed Aug 24, 2017
commit 4d42b6a68e5a4c8806fcbf7e67ec74187a9b8001
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cssparser"
version = "0.19.4"
version = "0.19.5"
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]

description = "Rust implementation of CSS Syntax Level 3"
Expand Down
9 changes: 9 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ impl<'i> ParserInput<'i> {
}
}

/// Create a new input for a parser. Line numbers in locations
/// are offset by the given value.
pub fn new_with_line_number_offset(input: &'i str, first_line_number: u32) -> ParserInput<'i> {
ParserInput {
tokenizer: Tokenizer::with_first_line_number(input, first_line_number),
cached_token: None,
}
}

#[inline]
fn cached_token_ref(&self) -> &Token<'i> {
&self.cached_token.as_ref().unwrap().token
Expand Down
14 changes: 14 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,20 @@ fn parser_maintains_current_line() {
assert_eq!(parser.current_line(), "ident");
}

#[test]
fn parser_with_line_number_offset() {
let mut input = ParserInput::new_with_line_number_offset("ident\nident", 72);
let mut parser = Parser::new(&mut input);
assert_eq!(parser.current_source_location(), SourceLocation { line: 72, column: 0 });
assert_eq!(parser.next_including_whitespace_and_comments(), Ok(&Token::Ident("ident".into())));
assert_eq!(parser.current_source_location(), SourceLocation { line: 72, column: 5 });
assert_eq!(parser.next_including_whitespace_and_comments(),
Ok(&Token::WhiteSpace("\n".into())));
assert_eq!(parser.current_source_location(), SourceLocation { line: 73, column: 0 });
assert_eq!(parser.next_including_whitespace_and_comments(), Ok(&Token::Ident("ident".into())));
assert_eq!(parser.current_source_location(), SourceLocation { line: 73, column: 5 });
}

#[test]
fn cdc_regression_test() {
let mut input = ParserInput::new("-->x");
Expand Down