Skip to content

Commit f0f9cdb

Browse files
authored
Merge pull request #4099 from HassanElDesouky/pick-yamlparser
[🍒][YAMLParser] Add multi-line literal folding support
2 parents 96968bd + 42e989c commit f0f9cdb

File tree

3 files changed

+161
-15
lines changed

3 files changed

+161
-15
lines changed

llvm/include/llvm/Support/YAMLParser.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// See http://www.yaml.org/spec/1.2/spec.html for the full standard.
1212
//
1313
// This currently does not implement the following:
14-
// * Multi-line literal folding.
1514
// * Tag resolution.
1615
// * UTF-16.
1716
// * BOMs anywhere other than the first Unicode scalar value in the file.

llvm/lib/Support/YAMLParser.cpp

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ class Scanner {
393393
/// Pos is whitespace or a new line
394394
bool isBlankOrBreak(StringRef::iterator Position);
395395

396+
/// Return true if the line is a line break, false otherwise.
397+
bool isLineEmpty(StringRef Line);
398+
396399
/// Consume a single b-break[28] if it's present at the current position.
397400
///
398401
/// Return false if the code unit at the current position isn't a line break.
@@ -471,6 +474,18 @@ class Scanner {
471474
/// Scan a block scalar starting with | or >.
472475
bool scanBlockScalar(bool IsLiteral);
473476

477+
/// Scan a block scalar style indicator and header.
478+
///
479+
/// Note: This is distinct from scanBlockScalarHeader to mirror the fact that
480+
/// YAML does not consider the style indicator to be a part of the header.
481+
///
482+
/// Return false if an error occurred.
483+
bool scanBlockScalarIndicators(char &StyleIndicator, char &ChompingIndicator,
484+
unsigned &IndentIndicator, bool &IsDone);
485+
486+
/// Scan a style indicator in a block scalar header.
487+
char scanBlockStyleIndicator();
488+
474489
/// Scan a chomping indicator in a block scalar header.
475490
char scanBlockChompingIndicator();
476491

@@ -1035,6 +1050,13 @@ bool Scanner::isBlankOrBreak(StringRef::iterator Position) {
10351050
*Position == '\n';
10361051
}
10371052

1053+
bool Scanner::isLineEmpty(StringRef Line) {
1054+
for (const auto *Position = Line.begin(); Position != Line.end(); ++Position)
1055+
if (!isBlankOrBreak(Position))
1056+
return false;
1057+
return true;
1058+
}
1059+
10381060
bool Scanner::consumeLineBreakIfPresent() {
10391061
auto Next = skip_b_break(Current);
10401062
if (Next == Current)
@@ -1517,6 +1539,25 @@ bool Scanner::scanAliasOrAnchor(bool IsAlias) {
15171539
return true;
15181540
}
15191541

1542+
bool Scanner::scanBlockScalarIndicators(char &StyleIndicator,
1543+
char &ChompingIndicator,
1544+
unsigned &IndentIndicator,
1545+
bool &IsDone) {
1546+
StyleIndicator = scanBlockStyleIndicator();
1547+
if (!scanBlockScalarHeader(ChompingIndicator, IndentIndicator, IsDone))
1548+
return false;
1549+
return true;
1550+
}
1551+
1552+
char Scanner::scanBlockStyleIndicator() {
1553+
char Indicator = ' ';
1554+
if (Current != End && (*Current == '>' || *Current == '|')) {
1555+
Indicator = *Current;
1556+
skip(1);
1557+
}
1558+
return Indicator;
1559+
}
1560+
15201561
char Scanner::scanBlockChompingIndicator() {
15211562
char Indicator = ' ';
15221563
if (Current != End && (*Current == '+' || *Current == '-')) {
@@ -1655,19 +1696,19 @@ bool Scanner::scanBlockScalarIndent(unsigned BlockIndent,
16551696
}
16561697

16571698
bool Scanner::scanBlockScalar(bool IsLiteral) {
1658-
// Eat '|' or '>'
16591699
assert(*Current == '|' || *Current == '>');
1660-
skip(1);
1661-
1700+
char StyleIndicator;
16621701
char ChompingIndicator;
16631702
unsigned BlockIndent;
16641703
bool IsDone = false;
1665-
if (!scanBlockScalarHeader(ChompingIndicator, BlockIndent, IsDone))
1704+
if (!scanBlockScalarIndicators(StyleIndicator, ChompingIndicator, BlockIndent,
1705+
IsDone))
16661706
return false;
16671707
if (IsDone)
16681708
return true;
1709+
bool IsFolded = StyleIndicator == '>';
16691710

1670-
auto Start = Current;
1711+
const auto *Start = Current;
16711712
unsigned BlockExitIndent = Indent < 0 ? 0 : (unsigned)Indent;
16721713
unsigned LineBreaks = 0;
16731714
if (BlockIndent == 0) {
@@ -1688,6 +1729,22 @@ bool Scanner::scanBlockScalar(bool IsLiteral) {
16881729
auto LineStart = Current;
16891730
advanceWhile(&Scanner::skip_nb_char);
16901731
if (LineStart != Current) {
1732+
if (LineBreaks && IsFolded && !Scanner::isLineEmpty(Str)) {
1733+
// The folded style "folds" any single line break between content into a
1734+
// single space, except when that content is "empty" (only contains
1735+
// whitespace) in which case the line break is left as-is.
1736+
if (LineBreaks == 1) {
1737+
Str.append(LineBreaks,
1738+
isLineEmpty(StringRef(LineStart, Current - LineStart))
1739+
? '\n'
1740+
: ' ');
1741+
}
1742+
// If we saw a single line break, we are completely replacing it and so
1743+
// want `LineBreaks == 0`. Otherwise this decrement accounts for the
1744+
// fact that the first line break is "trimmed", only being used to
1745+
// signal a sequence of line breaks which should not be folded.
1746+
LineBreaks--;
1747+
}
16911748
Str.append(LineBreaks, '\n');
16921749
Str.append(StringRef(LineStart, Current - LineStart));
16931750
LineBreaks = 0;

llvm/test/YAMLParser/spec-09-24.test

Lines changed: 99 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,103 @@
11
# RUN: yaml-bench -canonical %s | FileCheck %s
2-
# CHECK: ? !!str "strip"
3-
# CHECK: : !!str ""
4-
# CHECK: ? !!str "clip"
5-
# CHECK: : !!str ""
6-
# CHECK: ? !!str "keep"
7-
# CHECK: : !!str "\n"
2+
# CHECK: ? !!str "literal_strip"
3+
# CHECK: : !!str "Hello\n\n\nworld\non\nmultiple \n\n\nlines\n\nfoo bar"
4+
# CHECK: ? !!str "literal_clip"
5+
# CHECK: : !!str "Hello\n\n\nworld\non\nmultiple \n\n\nlines\n\nfoo bar\n"
6+
# CHECK: ? !!str "literal_keep"
7+
# CHECK: : !!str "Hello\n\n\nworld\non\nmultiple \n\n\nlines\n\nfoo bar\n\n\n\n"
8+
# CHECK: ? !!str "folded_strip"
9+
# CHECK: : !!str "Hello\n\nworld on multiple \n\nlines\nfoo bar"
10+
# CHECK: ? !!str "folded_clip"
11+
# CHECK: : !!str "Hello\n\nworld on multiple \n\nlines\nfoo bar\n"
12+
# CHECK: ? !!str "folded_keep"
13+
# CHECK: : !!str "Hello\n\nworld on multiple \n\nlines\nfoo bar\n\n\n"
814

9-
strip: >-
15+
literal_strip: |-
16+
Hello
1017

11-
clip: >
1218

13-
keep: |+
19+
world
20+
on
21+
multiple
22+
23+
24+
lines
25+
26+
foo bar
27+
28+
29+
30+
literal_clip: |
31+
Hello
32+
33+
34+
world
35+
on
36+
multiple
37+
38+
39+
lines
40+
41+
foo bar
42+
43+
44+
45+
literal_keep: |+
46+
Hello
47+
48+
49+
world
50+
on
51+
multiple
52+
53+
54+
lines
55+
56+
foo bar
57+
58+
59+
60+
folded_strip: >-
61+
Hello
62+
63+
64+
world
65+
on
66+
multiple
67+
68+
69+
lines
70+
71+
foo bar
72+
73+
74+
75+
folded_clip: >
76+
Hello
77+
78+
79+
world
80+
on
81+
multiple
82+
83+
84+
lines
85+
86+
foo bar
87+
88+
89+
90+
folded_keep: >+
91+
Hello
92+
93+
94+
world
95+
on
96+
multiple
97+
98+
99+
lines
100+
101+
foo bar
102+
103+

0 commit comments

Comments
 (0)