diff options
author | Martin Fischer <martin@push-f.com> | 2021-11-30 10:56:59 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-11-30 11:22:35 +0100 |
commit | 915530c02029f8bd4444930ed949e14f09afab03 (patch) | |
tree | 6f58b9728386dc5c1709137bc0a250640a7ce572 /src | |
parent | 414e5838618123cb00216a7426b898aab88ee45a (diff) |
report spans for errors
Diffstat (limited to 'src')
-rw-r--r-- | src/tokenizer/error.rs | 9 | ||||
-rw-r--r-- | src/tokenizer/interface.rs | 7 | ||||
-rw-r--r-- | src/tokenizer/mod.rs | 15 |
3 files changed, 24 insertions, 7 deletions
diff --git a/src/tokenizer/error.rs b/src/tokenizer/error.rs index 0acc88f..dad3fd2 100644 --- a/src/tokenizer/error.rs +++ b/src/tokenizer/error.rs @@ -6,7 +6,12 @@ use std::fmt::Display; pub enum Error { AttributesOnEndTag, SelfClosingEndTag, - DuplicateAttribute, + DuplicateAttribute { + #[cfg(feature = "spans")] + #[cfg_attr(docsrs, doc(cfg(feature = "spans")))] + /// Span of the duplicate attribute name. + span: std::ops::Range<usize>, + }, BadCharacter(char), UnexpectedCharacter(char, InternalState), UnexpectedEOF(InternalState), @@ -22,7 +27,7 @@ impl Display for Error { match self { Error::AttributesOnEndTag => write!(f, "attributes on an end tag"), Error::SelfClosingEndTag => write!(f, "self-closing end tag"), - Error::DuplicateAttribute => write!(f, "duplicate attribute"), + Error::DuplicateAttribute { .. } => write!(f, "duplicate attribute"), Error::BadCharacter(char) => write!(f, "bad character {:?}", char), Error::UnexpectedCharacter(char, state) => { write!( diff --git a/src/tokenizer/interface.rs b/src/tokenizer/interface.rs index 715f9bc..128807e 100644 --- a/src/tokenizer/interface.rs +++ b/src/tokenizer/interface.rs @@ -112,7 +112,12 @@ pub enum Token { CharacterTokens(String), NullCharacterToken, EOFToken, - ParseError(Error), + ParseError { + error: Error, + #[cfg(feature = "spans")] + #[cfg_attr(docsrs, doc(cfg(feature = "spans")))] + span: std::ops::Range<usize>, + }, } #[derive(Debug, PartialEq)] diff --git a/src/tokenizer/mod.rs b/src/tokenizer/mod.rs index 6793eb2..1809275 100644 --- a/src/tokenizer/mod.rs +++ b/src/tokenizer/mod.rs @@ -538,7 +538,10 @@ impl<Sink: TokenSink> Tokenizer<Sink> { }; if dup { - self.emit_error(Error::DuplicateAttribute); + self.emit_error(Error::DuplicateAttribute { + #[cfg(feature = "spans")] + span: self.spans.current_attr_name.clone(), + }); self.current_attr_name.clear(); self.current_attr_value.clear(); } else { @@ -598,7 +601,11 @@ impl<Sink: TokenSink> Tokenizer<Sink> { } fn emit_error(&mut self, error: Error) { - self.process_token_and_continue(ParseError(error)); + self.process_token_and_continue(ParseError { + error, + #[cfg(feature = "spans")] + span: self.spans.current_pos - 1..self.spans.current_pos - 1, + }); } } //ยง END @@ -2293,7 +2300,7 @@ mod test { self.current_str.push('\0'); } - token @ ParseError(_) => { + token @ ParseError { .. } => { self.push(token, line_number); } @@ -2453,7 +2460,7 @@ mod test { (3, CharacterTokens(c1)), ( 3, - ParseError(Error::CharRef(CharRefError::InvalidNamedCharRef)), + ParseError{error: Error::CharRef(CharRefError::InvalidNamedCharRef), ..}, ), (4, CharacterTokens(c2)), ] if c1 == "&\n" && c2 == "&aamp;\n" |