use std::borrow::Cow;
use styx_tokenizer::Span;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Event<'src> {
pub span: Span,
pub kind: EventKind<'src>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EventKind<'src> {
DocumentStart,
DocumentEnd,
ObjectStart,
ObjectEnd,
SequenceStart,
SequenceEnd,
EntryStart,
Key {
tag: Option<&'src str>,
payload: Option<Cow<'src, str>>,
kind: ScalarKind,
},
EntryEnd,
Scalar {
value: Cow<'src, str>,
kind: ScalarKind,
},
Unit,
TagStart {
name: &'src str,
},
TagEnd,
Comment {
text: &'src str,
},
DocComment {
lines: Vec<&'src str>,
},
Error {
kind: ParseErrorKind,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "facet", derive(facet::Facet))]
#[repr(u8)]
pub enum ScalarKind {
Bare,
Quoted,
Raw,
Heredoc,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParseErrorKind {
UnexpectedToken,
UnclosedObject,
UnclosedSequence,
InvalidEscape(String),
ExpectedKey,
ExpectedValue,
UnexpectedEof,
DuplicateKey { original: Span },
InvalidTagName,
InvalidKey,
DanglingDocComment,
TooManyAtoms,
ReopenedPath {
closed_path: Vec<String>,
},
NestIntoTerminal {
terminal_path: Vec<String>,
},
CommaInSequence,
MissingWhitespaceBeforeBlock,
TrailingContent,
}
impl std::fmt::Display for ParseErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ParseErrorKind::UnexpectedToken => write!(f, "unexpected token"),
ParseErrorKind::UnclosedObject => write!(f, "unclosed object (missing `}}`)"),
ParseErrorKind::UnclosedSequence => write!(f, "unclosed sequence (missing `)`)"),
ParseErrorKind::InvalidEscape(seq) => write!(f, "invalid escape sequence: {}", seq),
ParseErrorKind::ExpectedKey => write!(f, "expected a key"),
ParseErrorKind::ExpectedValue => write!(f, "expected a value"),
ParseErrorKind::UnexpectedEof => write!(f, "unexpected end of input"),
ParseErrorKind::DuplicateKey { .. } => write!(f, "duplicate key"),
ParseErrorKind::InvalidTagName => write!(f, "invalid tag name"),
ParseErrorKind::InvalidKey => write!(f, "invalid key"),
ParseErrorKind::DanglingDocComment => {
write!(f, "doc comment not followed by an entry")
}
ParseErrorKind::TooManyAtoms => {
write!(f, "unexpected atom after value (entry has too many atoms)")
}
ParseErrorKind::ReopenedPath { closed_path } => {
write!(
f,
"cannot reopen path `{}` after sibling appeared",
closed_path.join(".")
)
}
ParseErrorKind::NestIntoTerminal { terminal_path } => {
write!(
f,
"cannot nest into `{}` which has a terminal value",
terminal_path.join(".")
)
}
ParseErrorKind::CommaInSequence => {
write!(
f,
"unexpected `,` in sequence (sequences are whitespace-separated, not comma-separated)"
)
}
ParseErrorKind::MissingWhitespaceBeforeBlock => {
write!(
f,
"missing whitespace before `{{` or `(` (required after bare scalar to distinguish from tag syntax like `@tag{{}}`)"
)
}
ParseErrorKind::TrailingContent => {
write!(f, "trailing content after explicit root object")
}
}
}
}