diff options
author | Martin Fischer <martin@push-f.com> | 2021-11-30 07:28:21 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-11-30 11:22:35 +0100 |
commit | 14f1a85d994ad97dae3d9de735fc51adb25d390a (patch) | |
tree | 0fa0d7c173a19dcb7117132325a801808302bcf8 /src/tokenizer/error.rs | |
parent | baf1477c587fe22d27e94408cf2505d588ba007e (diff) |
introduce Error enum
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") + } + } + } +} |