Skip to content

Commit a161e1f

Browse files
author
bors-servo
authored
Auto merge of #184 - tromey:new-with-line-number, r=SimonSapin
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. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-cssparser/184) <!-- Reviewable:end -->
2 parents 9542ab4 + 4d42b6a commit a161e1f

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cssparser"
3-
version = "0.19.4"
3+
version = "0.19.5"
44
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]
55

66
description = "Rust implementation of CSS Syntax Level 3"

src/parser.rs

+9
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ impl<'i> ParserInput<'i> {
101101
}
102102
}
103103

104+
/// Create a new input for a parser. Line numbers in locations
105+
/// are offset by the given value.
106+
pub fn new_with_line_number_offset(input: &'i str, first_line_number: u32) -> ParserInput<'i> {
107+
ParserInput {
108+
tokenizer: Tokenizer::with_first_line_number(input, first_line_number),
109+
cached_token: None,
110+
}
111+
}
112+
104113
#[inline]
105114
fn cached_token_ref(&self) -> &Token<'i> {
106115
&self.cached_token.as_ref().unwrap().token

src/tests.rs

+14
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,20 @@ fn parser_maintains_current_line() {
970970
assert_eq!(parser.current_line(), "ident");
971971
}
972972

973+
#[test]
974+
fn parser_with_line_number_offset() {
975+
let mut input = ParserInput::new_with_line_number_offset("ident\nident", 72);
976+
let mut parser = Parser::new(&mut input);
977+
assert_eq!(parser.current_source_location(), SourceLocation { line: 72, column: 0 });
978+
assert_eq!(parser.next_including_whitespace_and_comments(), Ok(&Token::Ident("ident".into())));
979+
assert_eq!(parser.current_source_location(), SourceLocation { line: 72, column: 5 });
980+
assert_eq!(parser.next_including_whitespace_and_comments(),
981+
Ok(&Token::WhiteSpace("\n".into())));
982+
assert_eq!(parser.current_source_location(), SourceLocation { line: 73, column: 0 });
983+
assert_eq!(parser.next_including_whitespace_and_comments(), Ok(&Token::Ident("ident".into())));
984+
assert_eq!(parser.current_source_location(), SourceLocation { line: 73, column: 5 });
985+
}
986+
973987
#[test]
974988
fn cdc_regression_test() {
975989
let mut input = ParserInput::new("-->x");

0 commit comments

Comments
 (0)