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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
use super::{fresh_token, Lang, SyntaxNode};
use crate::as_yaml::{AsYaml, YamlKind};
use crate::lex::SyntaxKind;
use crate::scalar::ScalarValue;
use crate::yaml::ValueNode;
use rowan::ast::AstNode;
use std::borrow::Cow;
use std::fmt;
ast_node!(Scalar, SCALAR, "A YAML scalar value");
/// Chomping indicator for block scalars
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Chomping {
/// Strip final line breaks (indicator: -)
Strip,
/// Keep final line breaks (indicator: +)
Keep,
/// Clip to single final line break (default, no indicator)
Clip,
}
/// Error type for scalar type conversions
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScalarConversionError {
/// The scalar value is quoted, indicating it's a string type in YAML
QuotedValue,
/// The scalar value cannot be parsed as the target type
ParseError(String),
}
impl fmt::Display for ScalarConversionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ScalarConversionError::QuotedValue => {
write!(f, "Cannot convert quoted scalar to numeric/boolean type")
}
ScalarConversionError::ParseError(msg) => {
write!(f, "Failed to parse scalar: {msg}")
}
}
}
}
impl std::error::Error for ScalarConversionError {}
/// The chomping and explicit indentation indicators in `header`, the
/// first line of a block scalar. Only the run straight after `|` or `>`
/// holds them; a comment may follow (`> # a-b`) and its text says nothing
/// about either.
fn header_indicators(header: &str) -> (Chomping, Option<usize>) {
let indicators: String = header
.chars()
.skip(1)
.take_while(|c| c.is_ascii_digit() || *c == '+' || *c == '-')
.collect();
let chomping = if indicators.contains('-') {
Chomping::Strip
} else if indicators.contains('+') {
Chomping::Keep
} else {
Chomping::Clip
};
let indent = indicators
.chars()
.find(|c| c.is_ascii_digit() && *c != '0')
.and_then(|c| c.to_digit(10))
.map(|d| d as usize);
(chomping, indent)
}
/// The chomping and explicit indentation indicators of `scalar`, a block
/// scalar.
pub(crate) fn block_scalar_indicators(scalar: &SyntaxNode) -> (Chomping, Option<usize>) {
let header: String = scalar
.children_with_tokens()
.filter_map(|c| c.into_token())
.take_while(|t| t.kind() != SyntaxKind::NEWLINE)
.map(|t| t.text().to_string())
.collect();
header_indicators(&header)
}
/// The column a block scalar's indentation indicator counts from: that of
/// the innermost collection entry holding `scalar`, or 0 at the document
/// root.
pub(crate) fn block_parent_column(scalar: &SyntaxNode) -> usize {
scalar
.ancestors()
.find(|n| {
matches!(
n.kind(),
SyntaxKind::MAPPING_ENTRY | SyntaxKind::SEQUENCE_ENTRY
)
})
.map_or(0, |entry| crate::column::node_column(&entry))
}
/// Replace `\r\n` and lone `\r` line breaks with `\n`, which
/// `str::lines` does not split on.
fn normalize_line_breaks(text: &str) -> Cow<'_, str> {
if text.contains('\r') {
Cow::Owned(text.replace("\r\n", "\n").replace('\r', "\n"))
} else {
Cow::Borrowed(text)
}
}
impl Scalar {
/// Get the string value of this scalar
pub fn value(&self) -> String {
self.0.text().to_string()
}
/// Get the string representation of this scalar, properly unquoted and unescaped
pub fn as_string(&self) -> String {
let text = self.value();
// Handle quoted strings
if text.len() >= 2 && text.starts_with('"') && text.ends_with('"') {
// Double-quoted string - handle escape sequences
ScalarValue::parse_escape_sequences(&text[1..text.len() - 1])
} else if text.len() >= 2 && text.starts_with('\'') && text.ends_with('\'') {
// Single-quoted string - handle '' -> ' escape and fold multi-line strings
let content = &text[1..text.len() - 1];
let unescaped = content.replace("''", "'");
// Only fold lines if actually multi-line
if unescaped.contains(['\n', '\r']) {
// A line break folds to a space, or to one newline per empty
// line after it, and takes the white space around it along.
let unescaped = normalize_line_breaks(&unescaped);
let lines: Vec<&str> = unescaped.split('\n').collect();
let last = lines.len() - 1;
let mut result = lines[0].trim_end_matches([' ', '\t']).to_string();
let mut empty_lines = 0;
for (i, line) in lines.iter().enumerate().skip(1) {
let line = line.trim_start_matches([' ', '\t']);
let line = if i == last {
line
} else {
line.trim_end_matches([' ', '\t'])
};
if line.is_empty() && i != last {
empty_lines += 1;
continue;
}
match empty_lines {
0 => result.push(' '),
n => result.push_str(&"\n".repeat(n)),
}
empty_lines = 0;
result.push_str(line);
}
result
} else {
unescaped
}
} else if self.is_block() {
Self::parse_block_scalar(&text, block_parent_column(&self.0))
} else {
// Plain scalar - fold lines if multi-line
if text.contains(['\n', '\r']) {
// Multi-line plain scalar: a single line break folds to a
// space, and each additional one is kept, so a blank line
// between two lines becomes a newline (YAML 1.2 line folding).
let mut result = String::new();
let mut pending_breaks = 0usize;
for line in normalize_line_breaks(&text).lines() {
let trimmed = crate::scalar::trim_white(line);
if trimmed.is_empty() {
// Only counts once content has started; leading blank
// lines are stripped along with the indentation.
if !result.is_empty() {
pending_breaks += 1;
}
continue;
}
if !result.is_empty() {
if pending_breaks == 0 {
result.push(' ');
} else {
for _ in 0..pending_breaks {
result.push('\n');
}
}
}
pending_breaks = 0;
result.push_str(trimmed);
}
result
} else {
text
}
}
}
/// Parse a block scalar (literal `|` or folded `>`) into its string content
///
/// `parent_column` is the column of the collection the scalar is a node
/// of, which an explicit indentation indicator counts from.
fn parse_block_scalar(text: &str, parent_column: usize) -> String {
let text = normalize_line_breaks(text);
let mut lines = text.lines();
let first_line = match lines.next() {
Some(line) => line,
None => return String::new(),
};
let is_literal = first_line.starts_with('|');
let (chomping, explicit_indent) = header_indicators(first_line);
// Collect content lines
let content_lines: Vec<&str> = lines.collect();
if content_lines.is_empty() {
return String::new();
}
// An explicit indentation indicator (`|2`, `>2-`) says where the
// content starts, counted from the parent collection's column.
// Anything further right is part of the value: `|2` over a 4-space
// body at the root is `" x"`, not `"x"`. Without it, take the first
// non-empty line's indentation as the base.
let base_indent = match explicit_indent {
Some(explicit) => parent_column + explicit,
// With no content line at all, every line is empty however
// many spaces it holds. A tab is not indentation, so a line
// with one is not empty: `|\n \t\n` is "\t\n".
None => content_lines
.iter()
.find(|line| !line.trim_start_matches(' ').is_empty())
.map_or(usize::MAX, |line| crate::column::space_indent(line)),
};
// A line with no more than `base_indent` spaces and nothing else is
// empty; other lines lose up to `base_indent` spaces of indentation.
let mut result = String::new();
let mut empty_lines = 0;
// Whether the previous content line, if any, started with white space
let mut prev_spaced: Option<bool> = None;
for line in &content_lines {
// base_indent is a character count, so we step by chars to
// stay on UTF-8 boundaries even if the line starts with
// multi-byte content at less than base_indent spaces of
// indentation.
let leading_spaces = crate::column::space_indent(line);
let strip = leading_spaces.min(base_indent);
let strip_bytes = line
.char_indices()
.nth(strip)
.map_or(line.len(), |(i, _)| i);
let stripped = &line[strip_bytes..];
// A line indented less than the body holds no content, so tabs
// after its spaces are white space too.
let less_indented = leading_spaces < base_indent;
if stripped.is_empty()
|| (less_indented && stripped.trim_start_matches([' ', '\t']).is_empty())
{
empty_lines += 1;
continue;
}
// Folding joins two lines with a space, unless empty lines
// separate them or either starts with white space, in which
// case every line break is kept.
let spaced = stripped.starts_with([' ', '\t']);
let breaks = match prev_spaced {
Some(prev) if is_literal || prev || spaced => empty_lines + 1,
Some(_) if empty_lines == 0 => {
result.push(' ');
0
}
_ => empty_lines,
};
result.push_str(&"\n".repeat(breaks));
result.push_str(stripped);
prev_spaced = Some(spaced);
empty_lines = 0;
}
// `empty_lines` is now the number of trailing empty lines
let has_content = prev_spaced.is_some();
match chomping {
Chomping::Strip => {}
Chomping::Clip => {
if has_content {
result.push('\n');
}
}
Chomping::Keep => {
// One newline ends the last content line, then one per
// trailing blank. A body that is nothing but blank lines has
// no content line to end, so it gets only the blanks:
// `k: |+\n\n\n` is `"\n\n"`, as saphyr and PyYAML both
// read it, not `"\n\n\n"`.
let newlines = empty_lines + usize::from(has_content);
result.push_str(&"\n".repeat(newlines));
}
}
result
}
/// Check if this scalar is quoted
pub fn is_quoted(&self) -> bool {
let text = self.value();
text.len() >= 2
&& ((text.starts_with('"') && text.ends_with('"'))
|| (text.starts_with('\'') && text.ends_with('\'')))
}
/// Get the raw content of this scalar with outer quotes stripped, but
/// without processing any escape sequences.
///
/// For most purposes [`as_string`](Self::as_string) is more appropriate as
/// it fully unescapes double-quoted strings (`\"`, `\\`, `\n`, etc.) and
/// handles the `''` → `'` escape in single-quoted strings. Use this method
/// only when you need the verbatim content without escape processing.
pub fn unquoted_value(&self) -> String {
let text = self.value();
if self.is_quoted() {
text[1..text.len() - 1].to_string()
} else {
text
}
}
}
impl Scalar {
/// Replace the text content of this scalar with `value`.
///
/// The token is stored with `SyntaxKind::STRING` regardless of the semantic
/// type of `value` (e.g., setting `"42"` does not produce an `INT` token).
/// If token-kind accuracy matters, build a replacement scalar node via the
/// higher-level API instead.
pub fn set_value(&self, value: &str) {
let children: Vec<_> = self.0.children_with_tokens().collect();
// A block scalar holds the line break ending its last line and the
// blank lines after that, which stay but must move out of it.
let mut tail = Vec::new();
if crate::nodes::is_block_scalar(&self.0) {
let is_layout = |k| {
matches!(
k,
SyntaxKind::NEWLINE | SyntaxKind::INDENT | SyntaxKind::WHITESPACE
)
};
let content_end = children
.iter()
.rposition(|c| !is_layout(c.kind()))
.map_or(0, |i| i + 1);
tail = children[content_end..]
.iter()
.skip_while(|c| c.kind() != SyntaxKind::NEWLINE)
.filter_map(|c| c.as_token().cloned())
.collect();
}
crate::nodes::replace_children(
&self.0,
0..children.len(),
vec![fresh_token(SyntaxKind::STRING, value).into()],
);
if let Some((line_break, blank_lines)) = tail.split_first() {
crate::nodes::trivia::end_former_block_scalar(&self.0, line_break, blank_lines);
}
}
crate::nodes::ast_node_spans!("scalar");
/// Try to interpret this scalar as an i64.
///
/// Returns `None` if the scalar is not a plain YAML 1.2 integer (decimal,
/// `0o` octal or `0x` hexadecimal) or does not fit in an `i64`.
pub fn as_i64(&self) -> Option<i64> {
TryInto::<i64>::try_into(self).ok()
}
/// Try to interpret this scalar as an f64.
///
/// Returns `None` if the scalar is not a plain YAML 1.2 float or
/// integer.
pub fn as_f64(&self) -> Option<f64> {
TryInto::<f64>::try_into(self).ok()
}
/// Try to interpret this scalar as a bool.
///
/// Returns `None` if the scalar is not a plain YAML 1.2 boolean: true,
/// True, TRUE, false, False or FALSE.
pub fn as_bool(&self) -> Option<bool> {
TryInto::<bool>::try_into(self).ok()
}
/// Check if this scalar is a literal (`|`) or folded (`>`) block scalar
///
/// A plain scalar starting with `|` or `>`, which the parser accepts
/// in a flow collection, is not one.
pub fn is_block(&self) -> bool {
crate::nodes::is_block_scalar(&self.0)
}
/// Check if this scalar represents a null value.
///
/// Returns `true` if the plain value is null, Null, NULL, ~, or empty.
/// Quoted and block scalars are always strings, never null.
pub fn is_null(&self) -> bool {
if self.is_quoted() || self.is_block() {
return false;
}
let val = self.as_string();
matches!(val.as_str(), "null" | "Null" | "NULL" | "~" | "")
}
/// Whether this is a plain scalar holding a flow indicator, at which
/// it would end inside a flow collection.
pub(crate) fn needs_flow_quotes(&self) -> bool {
!self.is_quoted() && !self.is_block() && self.value().contains([',', '[', ']', '{', '}'])
}
}
impl AsYaml for Scalar {
fn as_node(&self) -> Option<&SyntaxNode> {
Some(&self.0)
}
fn kind(&self) -> YamlKind {
YamlKind::Scalar
}
/// The continuation lines of a multi-line plain or quoted scalar are
/// shifted as for a flow collection, keeping their offset from the
/// indentation of the line the scalar starts on, which becomes
/// `indent`. Leading whitespace on those lines is not content, so the
/// value is unchanged. A block scalar is copied as is: its body is laid
/// out by the caller, and its indentation can be content.
fn build_content(
&self,
builder: &mut rowan::GreenNodeBuilder,
indent: usize,
flow_context: bool,
) -> bool {
if flow_context && self.needs_flow_quotes() {
return ScalarValue::double_quoted(self.as_string())
.build_content(builder, indent, true);
}
if !ValueNode::is_inline(self) {
crate::yaml::copy_node_to_builder(builder, &self.0);
return false;
}
let delta = indent as isize - crate::column::line_indent(&self.0) as isize;
let delta = crate::as_yaml::clear_parent_column(&self.0, indent, delta);
builder.start_node(SyntaxKind::SCALAR.into());
copy_shifting_continuation_lines(builder, &self.0, delta, false);
builder.finish_node();
// Scalars don't end with newlines
false
}
fn is_inline(&self) -> bool {
ValueNode::is_inline(self)
}
}
/// Copy the children of `node`, moving the indentation of every line after
/// the first by `delta` columns, both between tokens (a plain scalar) and
/// within them (a quoted scalar). Empty lines stay empty. Returns whether
/// the copy ends with a line break.
fn copy_shifting_continuation_lines(
builder: &mut rowan::GreenNodeBuilder,
node: &SyntaxNode,
delta: isize,
mut after_newline: bool,
) -> bool {
for child in node.children_with_tokens() {
let token = match child {
rowan::NodeOrToken::Node(n) => {
builder.start_node(n.kind().into());
after_newline = copy_shifting_continuation_lines(builder, &n, delta, after_newline);
builder.finish_node();
continue;
}
rowan::NodeOrToken::Token(t) => t,
};
match token.kind() {
SyntaxKind::NEWLINE => {
builder.token(token.kind().into(), token.text());
after_newline = true;
continue;
}
SyntaxKind::INDENT | SyntaxKind::WHITESPACE if after_newline => {
let shifted = crate::as_yaml::shift_line_start(token.text(), delta);
if !shifted.is_empty() {
builder.token(SyntaxKind::INDENT.into(), &shifted);
}
}
kind => {
if after_newline && delta > 0 {
builder.token(SyntaxKind::INDENT.into(), &" ".repeat(delta as usize));
}
builder.token(
kind.into(),
&crate::as_yaml::shift_inner_lines(token.text(), delta),
);
}
}
after_newline = false;
}
after_newline
}
// TryFrom implementations for typed access. These read the scalar the way
// the YAML 1.2 core schema resolves it, so only a plain scalar of the
// matching type converts.
fn typed_value(scalar: &Scalar) -> Result<ScalarValue, ScalarConversionError> {
if scalar.is_quoted() {
return Err(ScalarConversionError::QuotedValue);
}
Ok(ScalarValue::from_scalar(scalar))
}
impl TryFrom<&Scalar> for i64 {
type Error = ScalarConversionError;
fn try_from(scalar: &Scalar) -> Result<Self, Self::Error> {
let value = typed_value(scalar)?;
value.to_i64().ok_or_else(|| {
ScalarConversionError::ParseError(format!(
"'{}' is not an integer that fits in an i64",
value.value()
))
})
}
}
impl TryFrom<&Scalar> for f64 {
type Error = ScalarConversionError;
fn try_from(scalar: &Scalar) -> Result<Self, Self::Error> {
let value = typed_value(scalar)?;
value
.to_f64()
.or_else(|| value.to_i64().map(|i| i as f64))
.ok_or_else(|| {
ScalarConversionError::ParseError(format!("'{}' is not a float", value.value()))
})
}
}
impl TryFrom<&Scalar> for bool {
type Error = ScalarConversionError;
fn try_from(scalar: &Scalar) -> Result<Self, Self::Error> {
let value = typed_value(scalar)?;
value.to_bool().ok_or_else(|| {
ScalarConversionError::ParseError(format!(
"'{}' is not a recognized boolean value",
value.value()
))
})
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_parse_block_scalar_multibyte_after_dedent() {
// Regression: `base_indent` is a char count but we sliced bytes,
// which panicked when a continuation line started with a multi-byte
// character at less than `base_indent` spaces of indentation.
let yaml = ">\n a\n\u{4f1}b\n";
// We only care that it does not panic.
let _ = super::Scalar::parse_block_scalar(yaml, 0);
}
}