Skip to content

Commit 290ff86

Browse files
yaythomasalecthomas
authored andcommitted
yaml: block scalar multiline improvements.
1 parent d65f279 commit 290ff86

3 files changed

Lines changed: 157 additions & 11 deletions

File tree

lexers/testdata/yaml.actual

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,55 @@ literal_block_with_keep_chomping: |+
8282

8383
The literal continues until de-dented, and the leading indentation is
8484
stripped.
85+
86+
a: |
87+
multiline literal
88+
line 2
89+
b: >
90+
multiline: folded
91+
line 2
92+
c: |-
93+
multiline # literal strip
94+
line 2
95+
d: >-
96+
multiline folded strip
97+
line 2: test
98+
99+
# not a comment
100+
indented by 1
101+
e: |+
102+
multiline literal keep
103+
line: 2
104+
# this is a comment
105+
f: >+
106+
multiline folded keep one space
107+
line 2
108+
g: test
109+
110+
block_scalars_with_indent:
111+
a: |
112+
multiline literal
113+
line 2
114+
b: >
115+
multiline: folded
116+
line 2
117+
c: |-
118+
multiline # literal strip
119+
line 2 6 leading spaces
120+
121+
d: >-
122+
multiline folded strip
123+
line 2: test
124+
# not a comment
125+
e: |+
126+
multiline literal keep
127+
line: 2
128+
# this is a comment
129+
f: >+
130+
multiline folded keep
131+
line 2
132+
g: test
133+
85134
####################
86135
# COLLECTION TYPES #
87136
####################

lexers/testdata/yaml.expected

Lines changed: 107 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,23 +189,116 @@
189189
{"type":"NameTag","value":"literal_block"},
190190
{"type":"Punctuation","value":":"},
191191
{"type":"TextWhitespace","value":" "},
192-
{"type":"LiteralStringDoc","value":"|\n This entire block of text will be the value of the 'literal_block' key,\n with line breaks being preserved.\n\n The literal continues until de-dented, and the leading indentation is\n stripped.\n\n Any lines that are 'more-indented' keep the rest of their indentation -\n these lines will be indented by 4 spaces."},
193-
{"type":"TextWhitespace","value":"\n"},
192+
{"type":"Punctuation","value":"|"},
193+
{"type":"LiteralStringDoc","value":"\n This entire block of text will be the value of the 'literal_block' key,\n with line breaks being preserved.\n\n The literal continues until de-dented, and the leading indentation is\n stripped.\n\n Any lines that are 'more-indented' keep the rest of their indentation -\n these lines will be indented by 4 spaces."},
194+
{"type":"TextWhitespace","value":" \n"},
194195
{"type":"NameTag","value":"folded_style"},
195196
{"type":"Punctuation","value":":"},
196197
{"type":"TextWhitespace","value":" "},
197-
{"type":"LiteralStringDoc","value":"\u003e\n This entire block of text will be the value of 'folded_style', but this\n time, all newlines will be replaced with a single space.\n\n Blank lines, like above, are converted to a newline character.\n\n 'More-indented' lines keep their newlines, too -\n this text will appear over two lines."},
198-
{"type":"TextWhitespace","value":"\n"},
198+
{"type":"Punctuation","value":"\u003e"},
199+
{"type":"LiteralStringDoc","value":"\n This entire block of text will be the value of 'folded_style', but this\n time, all newlines will be replaced with a single space.\n\n Blank lines, like above, are converted to a newline character.\n\n 'More-indented' lines keep their newlines, too -\n this text will appear over two lines."},
200+
{"type":"TextWhitespace","value":" \n"},
199201
{"type":"NameTag","value":"literal_block_with_strip_chomping"},
200202
{"type":"Punctuation","value":":"},
201203
{"type":"TextWhitespace","value":" "},
202-
{"type":"LiteralStringDoc","value":"|-\n This entire block of text will be the value of the 'literal_block' key,\n with line breaks being preserved and the strip chomping indicator.\n\n The literal continues until de-dented, and the leading indentation is\n stripped.\n\n Any lines that are 'more-indented' keep the rest of their indentation -\n these lines will be indented by 4 spaces."},
203-
{"type":"TextWhitespace","value":"\n"},
204+
{"type":"Punctuation","value":"|-"},
205+
{"type":"LiteralStringDoc","value":"\n This entire block of text will be the value of the 'literal_block' key,\n with line breaks being preserved and the strip chomping indicator.\n\n The literal continues until de-dented, and the leading indentation is\n stripped.\n\n Any lines that are 'more-indented' keep the rest of their indentation -\n these lines will be indented by 4 spaces."},
206+
{"type":"TextWhitespace","value":" \n"},
204207
{"type":"NameTag","value":"literal_block_with_keep_chomping"},
205208
{"type":"Punctuation","value":":"},
206209
{"type":"TextWhitespace","value":" "},
207-
{"type":"LiteralStringDoc","value":"|+\n This entire block of text will be the value of the 'literal_block' key,\n with line breaks being preserved and the keep chomping indicator.\n\n The literal continues until de-dented, and the leading indentation is\n stripped."},
210+
{"type":"Punctuation","value":"|+"},
211+
{"type":"LiteralStringDoc","value":"\n This entire block of text will be the value of the 'literal_block' key,\n with line breaks being preserved and the keep chomping indicator.\n\n The literal continues until de-dented, and the leading indentation is\n stripped."},
212+
{"type":"TextWhitespace","value":" \n\n"},
213+
{"type":"NameTag","value":"a"},
214+
{"type":"Punctuation","value":":"},
215+
{"type":"TextWhitespace","value":" "},
216+
{"type":"Punctuation","value":"|"},
217+
{"type":"LiteralStringDoc","value":"\n multiline literal\n line 2"},
218+
{"type":"TextWhitespace","value":" \n"},
219+
{"type":"NameTag","value":"b"},
220+
{"type":"Punctuation","value":":"},
221+
{"type":"TextWhitespace","value":" "},
222+
{"type":"Punctuation","value":"\u003e"},
223+
{"type":"LiteralStringDoc","value":"\n multiline: folded\n line 2"},
224+
{"type":"TextWhitespace","value":" \n"},
225+
{"type":"NameTag","value":"c"},
226+
{"type":"Punctuation","value":":"},
227+
{"type":"TextWhitespace","value":" "},
228+
{"type":"Punctuation","value":"|-"},
229+
{"type":"LiteralStringDoc","value":"\n multiline # literal strip\n line 2"},
230+
{"type":"TextWhitespace","value":" \n"},
231+
{"type":"NameTag","value":"d"},
232+
{"type":"Punctuation","value":":"},
233+
{"type":"TextWhitespace","value":" "},
234+
{"type":"Punctuation","value":"\u003e-"},
235+
{"type":"LiteralStringDoc","value":"\n multiline folded strip\n line 2: test\n\n # not a comment\n indented by 1"},
236+
{"type":"TextWhitespace","value":" \n"},
237+
{"type":"NameTag","value":"e"},
238+
{"type":"Punctuation","value":":"},
239+
{"type":"TextWhitespace","value":" "},
240+
{"type":"Punctuation","value":"|+"},
241+
{"type":"LiteralStringDoc","value":"\n multiline literal keep\n line: 2"},
242+
{"type":"TextWhitespace","value":" \n"},
243+
{"type":"Comment","value":"# this is a comment"},
208244
{"type":"TextWhitespace","value":"\n"},
245+
{"type":"NameTag","value":"f"},
246+
{"type":"Punctuation","value":":"},
247+
{"type":"TextWhitespace","value":" "},
248+
{"type":"Punctuation","value":"\u003e+"},
249+
{"type":"LiteralStringDoc","value":"\n multiline folded keep one space\n line 2"},
250+
{"type":"TextWhitespace","value":" \n"},
251+
{"type":"NameTag","value":"g"},
252+
{"type":"Punctuation","value":":"},
253+
{"type":"TextWhitespace","value":" "},
254+
{"type":"Literal","value":"test"},
255+
{"type":"TextWhitespace","value":"\n\n"},
256+
{"type":"NameTag","value":"block_scalars_with_indent"},
257+
{"type":"Punctuation","value":":"},
258+
{"type":"TextWhitespace","value":"\n "},
259+
{"type":"NameTag","value":"a"},
260+
{"type":"Punctuation","value":":"},
261+
{"type":"TextWhitespace","value":" "},
262+
{"type":"Punctuation","value":"|"},
263+
{"type":"LiteralStringDoc","value":"\n multiline literal\n line 2"},
264+
{"type":"TextWhitespace","value":" \n "},
265+
{"type":"NameTag","value":"b"},
266+
{"type":"Punctuation","value":":"},
267+
{"type":"TextWhitespace","value":" "},
268+
{"type":"Punctuation","value":"\u003e"},
269+
{"type":"LiteralStringDoc","value":"\n multiline: folded\n line 2"},
270+
{"type":"TextWhitespace","value":" \n "},
271+
{"type":"NameTag","value":"c"},
272+
{"type":"Punctuation","value":":"},
273+
{"type":"TextWhitespace","value":" "},
274+
{"type":"Punctuation","value":"|-"},
275+
{"type":"LiteralStringDoc","value":"\n multiline # literal strip\n line 2 6 leading spaces"},
276+
{"type":"TextWhitespace","value":" \n\n "},
277+
{"type":"NameTag","value":"d"},
278+
{"type":"Punctuation","value":":"},
279+
{"type":"TextWhitespace","value":" "},
280+
{"type":"Punctuation","value":"\u003e-"},
281+
{"type":"LiteralStringDoc","value":"\n multiline folded strip\n line 2: test\n # not a comment"},
282+
{"type":"TextWhitespace","value":" \n "},
283+
{"type":"NameTag","value":"e"},
284+
{"type":"Punctuation","value":":"},
285+
{"type":"TextWhitespace","value":" "},
286+
{"type":"Punctuation","value":"|+"},
287+
{"type":"LiteralStringDoc","value":"\n multiline literal keep\n line: 2"},
288+
{"type":"TextWhitespace","value":" \n "},
289+
{"type":"Comment","value":"# this is a comment"},
290+
{"type":"TextWhitespace","value":"\n "},
291+
{"type":"NameTag","value":"f"},
292+
{"type":"Punctuation","value":":"},
293+
{"type":"TextWhitespace","value":" "},
294+
{"type":"Punctuation","value":"\u003e+"},
295+
{"type":"LiteralStringDoc","value":"\n multiline folded keep\n line 2"},
296+
{"type":"TextWhitespace","value":" \n "},
297+
{"type":"NameTag","value":"g"},
298+
{"type":"Punctuation","value":":"},
299+
{"type":"TextWhitespace","value":" "},
300+
{"type":"Literal","value":"test"},
301+
{"type":"TextWhitespace","value":"\n\n"},
209302
{"type":"Comment","value":"####################"},
210303
{"type":"TextWhitespace","value":"\n"},
211304
{"type":"Comment","value":"# COLLECTION TYPES #"},
@@ -248,8 +341,9 @@
248341
{"type":"TextWhitespace","value":"\n"},
249342
{"type":"Punctuation","value":"?"},
250343
{"type":"TextWhitespace","value":" "},
251-
{"type":"LiteralStringDoc","value":"|\n This is a key\n that has multiple lines"},
252-
{"type":"TextWhitespace","value":"\n"},
344+
{"type":"Punctuation","value":"|"},
345+
{"type":"LiteralStringDoc","value":"\n This is a key\n that has multiple lines"},
346+
{"type":"TextWhitespace","value":" \n"},
253347
{"type":"Punctuation","value":":"},
254348
{"type":"TextWhitespace","value":" "},
255349
{"type":"Literal","value":"and this is its value"},
@@ -514,7 +608,10 @@
514608
{"type":"TextWhitespace","value":" "},
515609
{"type":"CommentPreproc","value":"!!binary"},
516610
{"type":"TextWhitespace","value":" "},
517-
{"type":"LiteralStringDoc","value":"|\n R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5\n OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+\n +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC\n AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=\n\n# YAML also has a set type, which looks like this:"},
611+
{"type":"Punctuation","value":"|"},
612+
{"type":"LiteralStringDoc","value":"\n R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5\n OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+\n +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC\n AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs="},
613+
{"type":"TextWhitespace","value":" \n\n"},
614+
{"type":"Comment","value":"# YAML also has a set type, which looks like this:"},
518615
{"type":"TextWhitespace","value":"\n"},
519616
{"type":"NameTag","value":"set"},
520617
{"type":"Punctuation","value":":"},

lexers/y/yaml.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ var YAML = internal.Register(MustNewLexer(
2323
{`&[^\s]+`, CommentPreproc, nil},
2424
{`\*[^\s]+`, CommentPreproc, nil},
2525
{`^%include\s+[^\n\r]+`, CommentPreproc, nil},
26-
{`[>|](?:[+-])?\s(?:^(?:[ \n]{1})+.*\n?)*$`, StringDoc, nil},
2726
Include("key"),
2827
Include("value"),
2928
{`[?:,\[\]]`, Punctuation, nil},
3029
{`.`, Text, nil},
3130
},
3231
"value": {
32+
{`([>|](?:[+-])?)(\n(^ {1,})(?:.*\n*(?:^\3 *).*)*)`, ByGroups(Punctuation, StringDoc, Whitespace), nil},
3333
{Words(``, `\b`, "true", "True", "TRUE", "false", "False", "FALSE", "null",
3434
"y", "Y", "yes", "Yes", "YES", "n", "N", "no", "No", "NO",
3535
"on", "On", "ON", "off", "Off", "OFF"), KeywordConstant, nil},

0 commit comments

Comments
 (0)