diff options
Diffstat (limited to 'src/tokenizer/error.rs')
-rw-r--r-- | src/tokenizer/error.rs | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/tokenizer/error.rs b/src/tokenizer/error.rs new file mode 100644 index 0000000..89eed2a --- /dev/null +++ b/src/tokenizer/error.rs @@ -0,0 +1,78 @@ +//! Types to represent the parser errors that can occur. +use std::fmt::Display; + +#[derive(PartialEq, Eq, Debug)] +#[non_exhaustive] +pub enum Error { + AttributesOnEndTag, + SelfClosingEndTag, + DuplicateAttribute, + BadCharacter(char), + UnexpectedCharacter(char, InternalState), + UnexpectedEOF(InternalState), + CharRef(CharRefError), +} + +/// Allows Error variants to include the internal tokenizer state without making it public. +#[derive(PartialEq, Eq, Debug)] +pub struct InternalState(pub(crate) crate::tokenizer::states::State); + +impl Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + 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::BadCharacter(char) => write!(f, "bad character {:?}", char), + Error::UnexpectedCharacter(char, state) => { + write!( + f, + "unexpected character: saw {:?} in state {:?}", + char, state.0 + ) + } + Error::UnexpectedEOF(state) => write!(f, "unexpected EOF in state {:?}", state.0), + Error::CharRef(error) => error.fmt(f), + } + } +} + +#[derive(PartialEq, Eq, Debug)] +#[non_exhaustive] +pub enum CharRefError { + MissingSemicolon, + NumericCharRefWithoutDigits, + NumericCharRefInvalid(u32), + EofInNumericCharRef, + EofAfterNumberSign, + EqualsSignAfterCharRefInAttr, + InvalidNamedCharRef, +} + +impl Display for CharRefError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + CharRefError::NumericCharRefWithoutDigits => { + write!(f, "numeric character reference without digits") + } + CharRefError::MissingSemicolon => { + write!(f, "semicolon missing after character reference") + } + CharRefError::NumericCharRefInvalid(num) => { + write!(f, "invalid numeric character reference value 0x{:06X}", num) + } + CharRefError::EofInNumericCharRef => { + write!(f, "EOF in numeric character reference") + } + CharRefError::EofAfterNumberSign => { + write!(f, "EOF after '#' in character reference") + } + CharRefError::EqualsSignAfterCharRefInAttr => { + write!(f, "equals sign after character reference in attribute") + } + CharRefError::InvalidNamedCharRef => { + write!(f, "invalid named character reference") + } + } + } +} |