Skip to content

Commit

Permalink
fix: inconsistent YAML multiline parsing (#475)
Browse files Browse the repository at this point in the history
When the YAML block scalar (multiline string) syntax is used with only one line of text, the single line is not matched by the current regexp. It currently only works with at least two lines.

This fixes the regexp by adding a special case for a single line after the first indentation in the block.
  • Loading branch information
guilgaly authored and alecthomas committed Jan 12, 2023
1 parent d330b76 commit 5ca7345
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lexers/embedded/yaml.xml
Expand Up @@ -49,7 +49,7 @@
</rule>
</state>
<state name="value">
<rule pattern="([&gt;|](?:[+-])?)(\n(^ {1,})(?:.*\n*(?:^\3 *).*)*)">
<rule pattern="([&gt;|](?:[+-])?)(\n(^ {1,})(?:(?:.*\n*(?:^\3 *).*)+|.*))">
<bygroups>
<token type="Punctuation"/>
<token type="LiteralStringDoc"/>
Expand Down
8 changes: 6 additions & 2 deletions lexers/testdata/yaml.actual
Expand Up @@ -105,7 +105,9 @@ e: |+
f: >+
multiline folded keep one space
line 2
g: test
g: |
multiline literal with only one line
h: test

block_scalars_with_indent:
a: |
Expand All @@ -129,7 +131,9 @@ block_scalars_with_indent:
f: >+
multiline folded keep
line 2
g: test
g: |
multiline literal with only one line
h: test

####################
# COLLECTION TYPES #
Expand Down
12 changes: 12 additions & 0 deletions lexers/testdata/yaml.expected
Expand Up @@ -251,6 +251,12 @@
{"type":"NameTag","value":"g"},
{"type":"Punctuation","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"|"},
{"type":"LiteralStringDoc","value":"\n multiline literal with only one line"},
{"type":"TextWhitespace","value":" \n"},
{"type":"NameTag","value":"h"},
{"type":"Punctuation","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"Literal","value":"test"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"NameTag","value":"block_scalars_with_indent"},
Expand Down Expand Up @@ -297,6 +303,12 @@
{"type":"NameTag","value":"g"},
{"type":"Punctuation","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"|"},
{"type":"LiteralStringDoc","value":"\n multiline literal with only one line"},
{"type":"TextWhitespace","value":" \n "},
{"type":"NameTag","value":"h"},
{"type":"Punctuation","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"Literal","value":"test"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"Comment","value":"####################"},
Expand Down

0 comments on commit 5ca7345

Please sign in to comment.