Skip to content

Commit

Permalink
Add initial support for raw string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
dkearns committed May 31, 2022
1 parent 246b1f5 commit 467724f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
13 changes: 13 additions & 0 deletions syntax/cs.vim
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ syn match csUnicodeNumber +\\U00\x\{6}+ contained contains=csUnicodeSpecifier di
syn match csUnicodeSpecifier +\\[uUx]+ contained display

syn region csString matchgroup=csQuote start=+"+ end=+"+ end=+$+ extend contains=csSpecialChar,csSpecialError,csUnicodeNumber,@Spell
syn region csRawString matchgroup=csQuote start=+\z("""\+\)+ end=+\z1+
syn match csCharacter "'[^']*'" contains=csSpecialChar,csSpecialCharError,csUnicodeNumber display
syn match csCharacter "'\\''" contains=csSpecialChar display
syn match csCharacter "'[^\\]'" display
Expand All @@ -210,6 +211,17 @@ syn match csInterpolationFormat +:[^}]\+}+ contained contains=csInterpolationFor
syn match csInterpolationAlignDel +,+ contained display
syn match csInterpolationFormatDel +:+ contained display

" Interpolated raw string literals
for i in range(1, get(g:, "cs_raw_string_interpolation_brace_count", 5))
exe 'syn region csInterpolatedRawString' .. i .. ' matchgroup=csQuote start=+$\{' .. i .. '}\z("""\+\)+ end=+\z1+ extend contains=csInterpolation' .. i .. ',csInterpolationDelimiterError' .. i .. ',@Spell'
exe 'syn match csInterpolationDelimiterError' .. i .. ' "}\{' .. i .. '}" contained'
exe 'syn match csInterpolationDelimiterError' .. i .. ' "{\{' .. 2 * i .. ',}" contained'
exe 'syn match csInterpolationDelimiterError' .. i .. ' "}\{' .. 2 * i .. ',}" contained'
exe 'syn region csInterpolation' .. i .. ' matchgroup=csInterpolationDelimiter start=+\%({\{' .. i .. '}\)\@' .. i .. '<!{\{' .. i .. '}{\@!+ end=+}\@<!}\{' .. i .. '}\%(}\{' .. i .. '}\)\@!+ keepend contained contains=@csAll,csBraced,csBracketed,csInterpolationAlign,csInterpolationFormat,csInterpolationDelimiterError' .. i
exe 'hi def link csInterpolationDelimiterError' .. i .. ' Error'
exe 'hi def link csInterpolatedRawString' .. i .. ' csRawString'
endfor

syn region csVerbatimString matchgroup=csQuote start=+@"+ end=+"+ skip=+""+ extend contains=csVerbatimQuote,@Spell
syn match csVerbatimQuote +""+ contained

Expand Down Expand Up @@ -275,6 +287,7 @@ hi def link csLogicSymbols Operator
hi def link csSpecialError Error
hi def link csSpecialCharError Error
hi def link csString String
hi def link csRawString String
hi def link csQuote String
hi def link csInterpolatedString String
hi def link csVerbatimString String
Expand Down
77 changes: 77 additions & 0 deletions test/strings.vader
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,80 @@ Execute:
AssertEqual 'csQuote', SyntaxAt(2)
AssertEqual 'csQuote', SyntaxAt(3)
AssertEqual 'csInterVerbString', SyntaxAt(4)

Given cs (a single line raw string):
"""a"""

Execute:
AssertEqual 'csQuote', SyntaxOf('"""', 1)
AssertEqual 'csQuote', SyntaxOf('"""', 2)
AssertEqual 'csRawString', SyntaxOf('a')

Given cs (a multi-line raw string):
"""
a
"""

Execute:
AssertEqual 'csQuote', SyntaxOf('"""', 1)
AssertEqual 'csQuote', SyntaxOf('"""', 2)
AssertEqual 'csRawString', SyntaxOf('a')

Given cs (an interpolated raw string - 1 dollar):
$"""
a
{42}
{{42}}
{{{42}}}
b
"""

Execute:
AssertEqual 'csQuote', SyntaxOf('$"""')
AssertEqual 'csInterpolatedRawString1', SyntaxOf('a')
AssertEqual 'csInterpolationDelimiter', SyntaxOf('{')
AssertEqual 'csInteger', SyntaxOf('42')
AssertEqual 'csInterpolationDelimiter', SyntaxOf('}')
AssertEqual 'csInterpolationDelimiterError1', SyntaxOf('{{')
AssertEqual 'csInterpolatedRawString1', SyntaxOf('42', 2)
AssertEqual 'csInterpolationDelimiterError1', SyntaxOf('}}')
AssertEqual 'csInterpolationDelimiterError1', SyntaxOf('{{{')
AssertEqual 'csInterpolatedRawString1', SyntaxOf('42', 3)
AssertEqual 'csInterpolationDelimiterError1', SyntaxOf('}}}')
AssertEqual 'csInterpolatedRawString1', SyntaxOf('b')
AssertEqual 'csQuote', SyntaxOf('"""', 2)

Given cs (an interpolated raw string - 2 dollar):
$$"""
a
{42}
{{42}}
{{{42}}}
{{{{42}}}}
{{{{{42}}}}}
b
"""

Execute:
AssertEqual 'csQuote', SyntaxOf('$"""')
AssertEqual 'csInterpolatedRawString2', SyntaxOf('a')
AssertEqual 'csInterpolatedRawString2', SyntaxOf('{')
AssertEqual 'csInterpolatedRawString2', SyntaxOf('42')
AssertEqual 'csInterpolatedRawString2', SyntaxOf('}')
AssertEqual 'csInterpolationDelimiter', SyntaxOf('{{')
AssertEqual 'csInteger', SyntaxOf('42', 2)
AssertEqual 'csInterpolationDelimiter', SyntaxOf('}}')
AssertEqual 'csInterpolatedRawString2', SyntaxOf('{\ze{{')
AssertEqual 'csInterpolationDelimiter', SyntaxOf('{\zs{{')
AssertEqual 'csInteger', SyntaxOf('42', 3)
AssertEqual 'csInterpolationDelimiter', SyntaxOf('}}\ze}')
AssertEqual 'csInterpolatedRawString2', SyntaxOf('}}\zs}')
AssertEqual 'csInterpolationDelimiterError2', SyntaxOf('{{{{')
AssertEqual 'csInterpolatedRawString2', SyntaxOf('42', 4)
AssertEqual 'csInterpolationDelimiterError2', SyntaxOf('}}}}')
AssertEqual 'csInterpolationDelimiterError2', SyntaxOf('{{{{{')
AssertEqual 'csInterpolatedRawString2', SyntaxOf('42', 5)
AssertEqual 'csInterpolationDelimiterError2', SyntaxOf('}}}}}')
AssertEqual 'csInterpolatedRawString2', SyntaxOf('b')
AssertEqual 'csQuote', SyntaxOf('"""', 2)

0 comments on commit 467724f

Please sign in to comment.