Skip to content

Commit e10d532

Browse files
authored
Fix YAML slash comments with colons (#1278)
Fixes #1271. ## Summary - Treat `//` inline comment text as `Comment` in YAML highlighting. - Keep key matching from running through `//` comment text before a later colon. - Add YAML lexer fixture coverage for `//` comments with colon text. ## Tests - `PATH=/root/sdk/go1.26.2/bin:$PATH go test ./lexers -run 'TestLexers/YAML' -count=1` - `PATH=/root/sdk/go1.26.2/bin:$PATH go test ./... -count=1` - `git diff --check` Co-authored-by: cyphercodes <cyphercodes@users.noreply.github.com>
1 parent 56c7702 commit e10d532

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

lexers/embedded/yaml.xml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<rule pattern="#.*$">
2424
<token type="Comment"/>
2525
</rule>
26+
<rule pattern="//.*$">
27+
<token type="Comment"/>
28+
</rule>
2629
<rule pattern="!![^\s]+">
2730
<token type="CommentPreproc"/>
2831
</rule>
@@ -78,6 +81,13 @@
7881
<token type="Comment"/>
7982
</bygroups>
8083
</rule>
84+
<rule pattern="([^\{\}\[\]\?,\:\!\-\*&amp;\@].*)( )+(//.*)">
85+
<bygroups>
86+
<token type="Literal"/>
87+
<token type="TextWhitespace"/>
88+
<token type="Comment"/>
89+
</bygroups>
90+
</rule>
8191
<rule pattern="[^\{\}\[\]\?,\:\!\-\*&amp;\@].*">
8292
<token type="Literal"/>
8393
</rule>
@@ -86,7 +96,7 @@
8696
<rule pattern="&#34;[^&#34;\n].*&#34;: ">
8797
<token type="NameTag"/>
8898
</rule>
89-
<rule pattern="(-)( )([^&#34;\n{]*)(:)( )">
99+
<rule pattern="(-)( )((?:(?!//)[^&#34;\n{])*?)(:)( )">
90100
<bygroups>
91101
<token type="Punctuation"/>
92102
<token type="TextWhitespace"/>
@@ -95,14 +105,14 @@
95105
<token type="TextWhitespace"/>
96106
</bygroups>
97107
</rule>
98-
<rule pattern="([^&#34;\n{]*)(:)( )">
108+
<rule pattern="((?:(?!//)[^&#34;\n{])*?)(:)( )">
99109
<bygroups>
100110
<token type="NameTag"/>
101111
<token type="Punctuation"/>
102112
<token type="TextWhitespace"/>
103113
</bygroups>
104114
</rule>
105-
<rule pattern="([^&#34;\n{]*)(:)(\n)">
115+
<rule pattern="((?:(?!//)[^&#34;\n{])*?)(:)(\n)">
106116
<bygroups>
107117
<token type="NameTag"/>
108118
<token type="Punctuation"/>

lexers/testdata/yaml.actual

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ nested_comment: # this is a comment right next to a key
1111
date_comment: 2010-11-12 # comment after date
1212
single_quote: 'single # quote string' # comment after single quote string
1313
double_quote: "double # quote string" # comment after double quote string
14+
slash_comment: "slash" // comment after double quote string
15+
slash_comment_with_colon: "slash" // comment: after double quote string
16+
slash_comment_after_literal: slash // comment: after literal
1417
key: no comment
1518

1619
################

lexers/testdata/yaml.expected

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,27 @@
6666
{"type":"TextWhitespace","value":" "},
6767
{"type":"Comment","value":"# comment after double quote string"},
6868
{"type":"TextWhitespace","value":"\n "},
69+
{"type":"NameTag","value":"slash_comment"},
70+
{"type":"Punctuation","value":":"},
71+
{"type":"TextWhitespace","value":" "},
72+
{"type":"LiteralStringDouble","value":"\"slash\""},
73+
{"type":"TextWhitespace","value":" "},
74+
{"type":"Comment","value":"// comment after double quote string"},
75+
{"type":"TextWhitespace","value":"\n "},
76+
{"type":"NameTag","value":"slash_comment_with_colon"},
77+
{"type":"Punctuation","value":":"},
78+
{"type":"TextWhitespace","value":" "},
79+
{"type":"LiteralStringDouble","value":"\"slash\""},
80+
{"type":"TextWhitespace","value":" "},
81+
{"type":"Comment","value":"// comment: after double quote string"},
82+
{"type":"TextWhitespace","value":"\n "},
83+
{"type":"NameTag","value":"slash_comment_after_literal"},
84+
{"type":"Punctuation","value":":"},
85+
{"type":"TextWhitespace","value":" "},
86+
{"type":"Literal","value":"slash"},
87+
{"type":"TextWhitespace","value":" "},
88+
{"type":"Comment","value":"// comment: after literal"},
89+
{"type":"TextWhitespace","value":"\n "},
6990
{"type":"NameTag","value":"key"},
7091
{"type":"Punctuation","value":":"},
7192
{"type":"TextWhitespace","value":" "},

0 commit comments

Comments
 (0)