use crate::semantic::SemanticStore;
use crate::{NodeId, Parser, Source, Span, YamlError};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Node {
pub(crate) kind: NodeKind,
pub(crate) span: Span,
pub(crate) parent: u32,
pub(crate) first_child: u32,
pub(crate) last_child: u32,
pub(crate) next_sibling: u32,
}
pub(crate) const NO_NODE: u32 = u32::MAX;
impl Node {
#[must_use]
pub const fn kind(&self) -> NodeKind {
self.kind
}
#[must_use]
pub const fn span(&self) -> Span {
self.span
}
#[must_use]
pub const fn parent(&self) -> Option<NodeId> {
node_link(self.parent)
}
}
#[derive(Debug, Clone)]
pub struct Children<'doc> {
nodes: &'doc [Node],
next: u32,
}
impl<'doc> Children<'doc> {
pub(crate) fn new(nodes: &'doc [Node], parent: NodeId) -> Self {
let next = nodes
.get(parent.as_usize())
.map_or(NO_NODE, |node| node.first_child);
Self { nodes, next }
}
}
impl Iterator for Children<'_> {
type Item = NodeId;
fn next(&mut self) -> Option<Self::Item> {
let id = node_link(self.next)?;
self.next = self.nodes[id.as_usize()].next_sibling;
Some(id)
}
}
pub(crate) const fn node_link(link: u32) -> Option<NodeId> {
if link == NO_NODE {
None
} else {
Some(NodeId(link))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum NodeKind {
Stream,
Document,
DocumentMarker,
Directive,
BlockMapping,
MappingEntry,
BlockSequence,
SequenceEntry,
FlowSequence,
FlowMapping,
LiteralScalar,
FoldedScalar,
Scalar,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct YamlEvent {
pub kind: YamlEventKind,
pub span: Span,
pub(crate) cst: Option<NodeId>,
pub(crate) content_indent: Option<u32>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CollectionStyle {
Block,
Flow,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum YamlScalarStyle {
Plain,
SingleQuoted,
DoubleQuoted,
Literal,
Folded,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum YamlEventKind {
StreamStart,
StreamEnd,
DocumentStart {
explicit: bool,
},
DocumentEnd {
explicit: bool,
},
SequenceStart {
style: CollectionStyle,
tag: Option<String>,
anchor: Option<String>,
},
SequenceEnd,
MappingStart {
style: CollectionStyle,
tag: Option<String>,
anchor: Option<String>,
},
MappingEnd,
Scalar {
style: YamlScalarStyle,
value: String,
tag: Option<String>,
anchor: Option<String>,
},
Alias {
name: String,
},
}
pub fn parse_cst(source: &Source) -> Result<Vec<Node>, YamlError> {
Parser::new(source).parse().map(|parsed| parsed.nodes)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ParsedYaml {
pub(crate) nodes: Vec<Node>,
pub(crate) semantics: SemanticStore,
}