1use crate::semantic::SemanticStore;
2use crate::{NodeId, Parser, Source, Span, YamlError};
3
4#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct Node {
7 pub(crate) kind: NodeKind,
9 pub(crate) syntax_flags: u8,
10 pub(crate) span: Span,
12 pub(crate) parent: u32,
13 pub(crate) first_child: u32,
14 pub(crate) last_child: u32,
15 pub(crate) next_sibling: u32,
16 pub(crate) semantic: u32,
17}
18
19pub(crate) const NO_NODE: u32 = u32::MAX;
20pub(crate) const NO_SEMANTIC_NODE: u32 = u32::MAX;
21pub(crate) const COMMON_SEMANTIC_NODE: u32 = u32::MAX - 1;
22pub(crate) const NODE_SEMANTIC_ALIAS: u8 = 1 << 3;
23pub(crate) const NODE_EXPLICIT_START: u8 = 1 << 4;
24pub(crate) const NODE_EXPLICIT_END: u8 = 1 << 5;
25pub(crate) const NODE_SCALAR_STYLE_MASK: u8 = 0b11;
26pub(crate) const NODE_SCALAR_PLAIN: u8 = 1;
27pub(crate) const NODE_SCALAR_SINGLE_QUOTED: u8 = 2;
28pub(crate) const NODE_SCALAR_DOUBLE_QUOTED: u8 = 3;
29pub(crate) const NODE_SCALAR_SYNTAX_VALIDATED: u8 = 1 << 2;
30
31impl Node {
32 #[must_use]
34 pub const fn kind(&self) -> NodeKind {
35 self.kind
36 }
37
38 #[must_use]
40 pub const fn span(&self) -> Span {
41 self.span
42 }
43
44 #[must_use]
46 pub const fn parent(&self) -> Option<NodeId> {
47 node_link(self.parent)
48 }
49}
50
51#[derive(Debug, Clone)]
53pub struct Children<'doc> {
54 nodes: &'doc [Node],
55 next: u32,
56}
57
58impl<'doc> Children<'doc> {
59 pub(crate) fn new(nodes: &'doc [Node], parent: NodeId) -> Self {
60 let next = nodes
61 .get(parent.as_usize())
62 .map_or(NO_NODE, |node| node.first_child);
63 Self { nodes, next }
64 }
65}
66
67impl Iterator for Children<'_> {
68 type Item = NodeId;
69
70 fn next(&mut self) -> Option<Self::Item> {
71 let id = node_link(self.next)?;
72 self.next = self.nodes[id.as_usize()].next_sibling;
73 Some(id)
74 }
75}
76
77pub(crate) const fn node_link(link: u32) -> Option<NodeId> {
78 if link == NO_NODE {
79 None
80 } else {
81 Some(NodeId(link))
82 }
83}
84
85#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
87pub enum NodeKind {
88 Stream,
90 Document,
92 DocumentMarker,
94 Directive,
96 BlockMapping,
98 MappingEntry,
100 BlockSequence,
102 SequenceEntry,
104 FlowSequence,
106 FlowMapping,
108 LiteralScalar,
110 FoldedScalar,
112 Scalar,
114}
115
116#[derive(Debug, Clone, PartialEq, Eq)]
118pub struct YamlEvent {
119 pub kind: YamlEventKind,
121 pub span: Span,
123 pub(crate) cst: Option<NodeId>,
125 pub(crate) content_indent: Option<u32>,
126}
127
128#[derive(Debug, Clone, Copy, PartialEq, Eq)]
130pub enum CollectionStyle {
131 Block,
133 Flow,
135}
136
137#[derive(Debug, Clone, Copy, PartialEq, Eq)]
139pub enum YamlScalarStyle {
140 Plain,
142 SingleQuoted,
144 DoubleQuoted,
146 Literal,
148 Folded,
150}
151
152#[derive(Debug, Clone, PartialEq, Eq)]
154pub enum YamlEventKind {
155 StreamStart,
157 StreamEnd,
159 DocumentStart {
161 explicit: bool,
163 },
164 DocumentEnd {
166 explicit: bool,
168 },
169 SequenceStart {
171 style: CollectionStyle,
173 tag: Option<String>,
175 anchor: Option<String>,
177 },
178 SequenceEnd,
180 MappingStart {
182 style: CollectionStyle,
184 tag: Option<String>,
186 anchor: Option<String>,
188 },
189 MappingEnd,
191 Scalar {
193 style: YamlScalarStyle,
195 value: String,
197 tag: Option<String>,
199 anchor: Option<String>,
201 },
202 Alias {
204 name: String,
206 },
207}
208
209pub fn parse_cst(source: &Source) -> Result<Vec<Node>, YamlError> {
216 Parser::new(source).parse().map(|parsed| parsed.nodes)
217}
218
219#[derive(Debug, Clone, PartialEq, Eq)]
220pub(crate) struct ParsedYaml {
221 pub(crate) nodes: Vec<Node>,
222 pub(crate) semantics: SemanticStore,
223}