1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// SPDX-License-Identifier: MIT
//! YAML AST node types.
//!
//! [`Node<Loc>`] is the core type — a YAML value parameterized by its
//! location type. For most uses `Loc = Span`. The loader produces
//! `Vec<Document<Span>>`.
use crate::event::{CollectionStyle, ScalarStyle};
use crate::pos::Span;
// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------
/// A YAML document: a root node plus directive metadata.
#[derive(Debug, Clone, PartialEq)]
pub struct Document<Loc = Span> {
/// The root node of the document.
pub root: Node<Loc>,
/// YAML version declared by a `%YAML` directive, if present (e.g. `(1, 2)`).
pub version: Option<(u8, u8)>,
/// Tag handle/prefix pairs declared by `%TAG` directives (handle, prefix).
pub tags: Vec<(String, String)>,
/// Comments that appear at document level (before or between nodes).
pub comments: Vec<String>,
/// Whether the document was introduced with an explicit `---` marker.
pub explicit_start: bool,
/// Whether the document was closed with an explicit `...` marker.
pub explicit_end: bool,
}
/// A YAML node parameterized by its location type.
#[derive(Debug, Clone, PartialEq)]
pub enum Node<Loc = Span> {
/// A scalar value.
Scalar {
/// The scalar content as a UTF-8 string (after block/flow unfolding).
value: String,
/// The presentation style used in the source (plain, single-quoted, etc.).
style: ScalarStyle,
/// Anchor name defined on this node (e.g. `&anchor`), if any.
anchor: Option<String>,
/// Tag applied to this node (e.g. `!!str`), if any.
tag: Option<String>,
/// Source span covering this scalar in the input.
loc: Loc,
/// Comment lines that appear before this node (e.g. `# note`).
/// Populated only for non-first entries in a mapping or sequence.
/// Document-prefix leading comments are discarded by the tokenizer
/// per YAML §9.2 and cannot be recovered here.
leading_comments: Vec<String>,
/// Inline comment on the same line as this node (e.g. `# note`).
trailing_comment: Option<String>,
},
/// A mapping (sequence of key–value pairs preserving declaration order).
Mapping {
/// Key–value pairs in declaration order.
entries: Vec<(Self, Self)>,
/// The presentation style used in the source (block or flow).
style: CollectionStyle,
/// Anchor name defined on this mapping (e.g. `&anchor`), if any.
anchor: Option<String>,
/// Tag applied to this mapping (e.g. `!!map`), if any.
tag: Option<String>,
/// Source span from the opening indicator to the last entry.
loc: Loc,
/// Comment lines that appear before this node.
leading_comments: Vec<String>,
/// Inline comment on the same line as this node.
trailing_comment: Option<String>,
},
/// A sequence (ordered list of nodes).
Sequence {
/// Ordered list of child nodes.
items: Vec<Self>,
/// The presentation style used in the source (block or flow).
style: CollectionStyle,
/// Anchor name defined on this sequence (e.g. `&anchor`), if any.
anchor: Option<String>,
/// Tag applied to this sequence (e.g. `!!seq`), if any.
tag: Option<String>,
/// Source span from the opening indicator to the last item.
loc: Loc,
/// Comment lines that appear before this node.
leading_comments: Vec<String>,
/// Inline comment on the same line as this node.
trailing_comment: Option<String>,
},
/// An alias reference (lossless mode only — resolved mode expands these).
Alias {
/// The anchor name this alias refers to (without the `*` sigil).
name: String,
/// Source span covering the `*name` alias token.
loc: Loc,
/// Comment lines that appear before this node.
leading_comments: Vec<String>,
/// Inline comment on the same line as this node.
trailing_comment: Option<String>,
},
}
impl<Loc> Node<Loc> {
/// Returns the anchor name if this node defines one.
pub fn anchor(&self) -> Option<&str> {
match self {
Self::Scalar { anchor, .. }
| Self::Mapping { anchor, .. }
| Self::Sequence { anchor, .. } => anchor.as_deref(),
Self::Alias { .. } => None,
}
}
/// Returns the leading comments for this node.
pub fn leading_comments(&self) -> &[String] {
match self {
Self::Scalar {
leading_comments, ..
}
| Self::Mapping {
leading_comments, ..
}
| Self::Sequence {
leading_comments, ..
}
| Self::Alias {
leading_comments, ..
} => leading_comments,
}
}
/// Returns the trailing comment for this node, if any.
pub fn trailing_comment(&self) -> Option<&str> {
match self {
Self::Scalar {
trailing_comment, ..
}
| Self::Mapping {
trailing_comment, ..
}
| Self::Sequence {
trailing_comment, ..
}
| Self::Alias {
trailing_comment, ..
} => trailing_comment.as_deref(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::event::ScalarStyle;
use crate::pos::{Pos, Span};
fn zero_span() -> Span {
Span {
start: Pos::ORIGIN,
end: Pos::ORIGIN,
}
}
fn plain_scalar(value: &str) -> Node<Span> {
Node::Scalar {
value: value.to_owned(),
style: ScalarStyle::Plain,
anchor: None,
tag: None,
loc: zero_span(),
leading_comments: Vec::new(),
trailing_comment: None,
}
}
// NF-1: node_debug_includes_leading_comments
#[test]
fn node_debug_includes_leading_comments() {
let node = Node::Scalar {
value: "val".to_owned(),
style: ScalarStyle::Plain,
anchor: None,
tag: None,
loc: zero_span(),
leading_comments: vec!["# note".to_owned()],
trailing_comment: None,
};
let debug = format!("{node:?}");
assert!(debug.contains("# note"), "debug output: {debug}");
}
// NF-2: node_partial_eq_considers_leading_comments
#[test]
fn node_partial_eq_considers_leading_comments() {
let a = Node::Scalar {
value: "val".to_owned(),
style: ScalarStyle::Plain,
anchor: None,
tag: None,
loc: zero_span(),
leading_comments: vec!["# a".to_owned()],
trailing_comment: None,
};
let b = Node::Scalar {
value: "val".to_owned(),
style: ScalarStyle::Plain,
anchor: None,
tag: None,
loc: zero_span(),
leading_comments: vec!["# b".to_owned()],
trailing_comment: None,
};
assert_ne!(a, b);
}
// NF-3: node_clone_preserves_comments
#[test]
fn node_clone_preserves_comments() {
let node = Node::Scalar {
value: "val".to_owned(),
style: ScalarStyle::Plain,
anchor: None,
tag: None,
loc: zero_span(),
leading_comments: vec!["# x".to_owned()],
trailing_comment: Some("# y".to_owned()),
};
let cloned = node.clone();
assert_eq!(node, cloned);
assert_eq!(cloned.leading_comments(), &["# x"]);
assert_eq!(cloned.trailing_comment(), Some("# y"));
}
// Sanity: plain_scalar helper produces empty comment fields.
#[test]
fn plain_scalar_has_empty_comments() {
let n = plain_scalar("hello");
assert!(n.leading_comments().is_empty());
assert!(n.trailing_comment().is_none());
}
fn bare_document(explicit_start: bool, explicit_end: bool) -> Document<Span> {
Document {
root: plain_scalar("val"),
version: None,
tags: Vec::new(),
comments: Vec::new(),
explicit_start,
explicit_end,
}
}
// NF-DOC-1: explicit_start and explicit_end default to false
#[test]
fn document_explicit_flags_in_equality() {
let a = bare_document(false, false);
let b = bare_document(false, false);
assert_eq!(a, b);
}
// NF-DOC-2: PartialEq distinguishes differing explicit_start
#[test]
fn document_partial_eq_distinguishes_explicit_start() {
let a = bare_document(true, false);
let b = bare_document(false, false);
assert_ne!(a, b);
}
// NF-DOC-3: PartialEq distinguishes differing explicit_end
#[test]
fn document_partial_eq_distinguishes_explicit_end() {
let a = bare_document(false, true);
let b = bare_document(false, false);
assert_ne!(a, b);
}
// NF-DOC-4: Clone preserves both flags
#[test]
fn document_clone_preserves_explicit_flags() {
let doc = bare_document(true, true);
let cloned = doc.clone();
assert_eq!(doc, cloned);
assert!(cloned.explicit_start);
assert!(cloned.explicit_end);
}
}