Skip to content

Parser changes for Gecko integration #168

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 9 commits into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
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
Make column and line numbers match Gecko's CSS parser.
  • Loading branch information
jdm committed Jul 5, 2017
commit a8e2252c1f7d68df17d90516d3798e4bba32e89c
18 changes: 9 additions & 9 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,26 +451,26 @@ fn serialize_rgba_two_digit_float_if_roundtrips() {
fn line_numbers() {
let mut input = ParserInput::new("foo bar\nbaz\r\n\n\"a\\\r\nb\"");
let mut input = Parser::new(&mut input);
assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 1 });
assert_eq!(input.current_source_location(), SourceLocation { line: 0, column: 0 });
assert_eq!(input.next_including_whitespace(), Ok(Token::Ident("foo".into())));
assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 4 });
assert_eq!(input.current_source_location(), SourceLocation { line: 0, column: 3 });
assert_eq!(input.next_including_whitespace(), Ok(Token::WhiteSpace(" ")));
assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 5 });
assert_eq!(input.current_source_location(), SourceLocation { line: 0, column: 4 });
assert_eq!(input.next_including_whitespace(), Ok(Token::Ident("bar".into())));
assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 8 });
assert_eq!(input.current_source_location(), SourceLocation { line: 0, column: 7 });
assert_eq!(input.next_including_whitespace(), Ok(Token::WhiteSpace("\n")));
assert_eq!(input.current_source_location(), SourceLocation { line: 2, column: 1 });
assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 0 });
assert_eq!(input.next_including_whitespace(), Ok(Token::Ident("baz".into())));
assert_eq!(input.current_source_location(), SourceLocation { line: 2, column: 4 });
assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 3 });
let position = input.position();

assert_eq!(input.next_including_whitespace(), Ok(Token::WhiteSpace("\r\n\n")));
assert_eq!(input.current_source_location(), SourceLocation { line: 4, column: 1 });
assert_eq!(input.current_source_location(), SourceLocation { line: 3, column: 0 });

assert_eq!(input.source_location(position), SourceLocation { line: 2, column: 4 });
assert_eq!(input.source_location(position), SourceLocation { line: 1, column: 3 });

assert_eq!(input.next_including_whitespace(), Ok(Token::QuotedString("ab".into())));
assert_eq!(input.current_source_location(), SourceLocation { line: 5, column: 3 });
assert_eq!(input.current_source_location(), SourceLocation { line: 4, column: 2 });
assert!(input.next_including_whitespace().is_err());
}

Expand Down
10 changes: 5 additions & 5 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl<'a> Tokenizer<'a> {
input: input,
position: 0,
last_known_source_location: Cell::new((SourcePosition(0),
SourceLocation { line: 1, column: 1 })),
SourceLocation { line: 0, column: 0 })),
Copy link
Member

Choose a reason for hiding this comment

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

SourceLocation’s doc-comments needs to up updated accordingly.

var_functions: SeenStatus::DontCare,
viewport_percentages: SeenStatus::DontCare,
}
Expand Down Expand Up @@ -312,7 +312,7 @@ impl<'a> Tokenizer<'a> {
// So if the requested position is before the last known one,
// start over from the beginning.
position = 0;
location = SourceLocation { line: 1, column: 1 };
location = SourceLocation { line: 0, column: 0 };
}
let mut source = &self.input[position..target];
while let Some(newline_position) = source.find(|c| matches!(c, '\n' | '\r' | '\x0C')) {
Expand All @@ -321,7 +321,7 @@ impl<'a> Tokenizer<'a> {
source = &source[offset..];
position += offset;
location.line += 1;
location.column = 1;
location.column = 0;
}
debug_assert!(position <= target);
location.column += (target - position) as u32;
Expand Down Expand Up @@ -397,10 +397,10 @@ pub struct SourcePosition(usize);
/// The line and column number for a given position within the input.
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub struct SourceLocation {
/// The line number, starting at 1 for the first line.
/// The line number, starting at 0 for the first line.
pub line: u32,

/// The column number within a line, starting at 1 for first the character of the line.
/// The column number within a line, starting at 0 for first the character of the line.
pub column: u32,
}

Expand Down