Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(swift) New string grammar #2902

Merged
merged 9 commits into from
Dec 5, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,4 @@ Contributors:
- Kyle Brown <kylebrown9@github>
- Marcus Ortiz <[email protected]>
- Guillaume Grossetie <[email protected]>
- Steven Van Impe <[email protected]>
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ New Languages:
Language improvements:

- enh(makefile): Add `make` as an alias (#2883) [tripleee][]
- enh(swift) Improved grammar for strings (#2819) [Steven Van Impe][]
joshgoebel marked this conversation as resolved.
Show resolved Hide resolved

Grammar improvements:

Expand Down
64 changes: 48 additions & 16 deletions src/languages/swift.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
Language: Swift
Description: Swift is a general-purpose programming language built using a modern approach to safety, performance, and software design patterns.
Author: Chris Eidhof <[email protected]>
Contributors: Nate Cook <[email protected]>, Alexander Lichter <[email protected]>
Contributors: Nate Cook <[email protected]>, Alexander Lichter <[email protected]>, Steven Van Impe <[email protected]>
Website: https://swift.org
Category: common, system
*/

import { concat } from "../lib/regex";

export default function(hljs) {
var SWIFT_KEYWORDS = {
Expand Down Expand Up @@ -58,20 +59,6 @@ export default function(hljs) {
contains: ['self']
}
);
var SUBST = {
className: 'subst',
begin: /\\\(/, end: '\\)',
keywords: SWIFT_KEYWORDS,
contains: [] // assigned later
};
var STRING = {
className: 'string',
contains: [hljs.BACKSLASH_ESCAPE, SUBST],
variants: [
{begin: /"""/, end: /"""/},
{begin: /"/, end: /"/},
]
};

// https://docs.swift.org/swift-book/ReferenceManual/LexicalStructure.html#grammar_numeric-literal
// TODO: Update for leading `-` after lookbehind is supported everywhere
Expand All @@ -96,7 +83,52 @@ export default function(hljs) {
{ begin: /\b0b([01]_*)+\b/ },
]
};
SUBST.contains = [NUMBER];

// https://docs.swift.org/swift-book/ReferenceManual/LexicalStructure.html#grammar_string-literal
var ESCAPED_CHARACTER = (rawDelimiter = "") => ({
className: 'subst',
variants: [
{ begin: concat(/\\/, rawDelimiter, /[0\\tnr"']/) },
{ begin: concat(/\\/, rawDelimiter, /u\{[0-9a-fA-F]{1,8}\}/) }
]
});
var ESCAPED_NEWLINE = (rawDelimiter = "") => ({
className: 'subst',
begin: concat(/\\/, rawDelimiter, /[\t ]*(?:[\r\n]|\r\n)/)
joshgoebel marked this conversation as resolved.
Show resolved Hide resolved
});
var INTERPOLATION = (rawDelimiter = "") => ({
className: 'subst',
begin: concat(/\\/, rawDelimiter, /\(/),
end: /\)/
svanimpe marked this conversation as resolved.
Show resolved Hide resolved
});
var MULTILINE_STRING = (rawDelimiter = "") => ({
begin: concat(rawDelimiter, /"""/),
end: concat(/"""/, rawDelimiter),
contains: [ESCAPED_CHARACTER(rawDelimiter), ESCAPED_NEWLINE(rawDelimiter), INTERPOLATION(rawDelimiter)]
});
var SINGLE_LINE_STRING = (rawDelimiter = "") => ({
begin: concat(rawDelimiter, /"/),
end: concat(/"/, rawDelimiter),
contains: [ESCAPED_CHARACTER(rawDelimiter), INTERPOLATION(rawDelimiter)]
});
var STRING = {
className: 'string',
variants: [
MULTILINE_STRING(),
MULTILINE_STRING("#"),
MULTILINE_STRING("##"),
MULTILINE_STRING("###"),
SINGLE_LINE_STRING(),
SINGLE_LINE_STRING("#"),
SINGLE_LINE_STRING("##"),
SINGLE_LINE_STRING("###"),
]
};
// TODO: Interpolation can contain any expression, so there's room for improvement here.
for (const variant of STRING.variants) {
const interpolation = variant.contains[variant.contains.length - 1]
interpolation.contains = [STRING, NUMBER];
}

return {
name: 'Swift',
Expand Down
3 changes: 0 additions & 3 deletions test/markup/swift/multiline-string.expect.txt

This file was deleted.

3 changes: 0 additions & 3 deletions test/markup/swift/multiline-string.txt

This file was deleted.

72 changes: 72 additions & 0 deletions test/markup/swift/strings.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<span class="hljs-string">&quot;escaped characters <span class="hljs-subst">\0</span><span class="hljs-subst">\\</span><span class="hljs-subst">\t</span><span class="hljs-subst">\n</span><span class="hljs-subst">\r</span><span class="hljs-subst">\&quot;</span><span class="hljs-subst">\&#x27;</span>&quot;</span>
<span class="hljs-string">&quot;escaped unicode <span class="hljs-subst">\u{0}</span><span class="hljs-subst">\u{12}</span><span class="hljs-subst">\u{345}</span><span class="hljs-subst">\u{6789}</span><span class="hljs-subst">\u{abcde}</span><span class="hljs-subst">\u{fABCDE}</span><span class="hljs-subst">\u{F012345}</span><span class="hljs-subst">\u{67890abc}</span>&quot;</span>
<span class="hljs-string">#&quot;escaped characters <span class="hljs-subst">\#0</span><span class="hljs-subst">\#\</span><span class="hljs-subst">\#t</span><span class="hljs-subst">\#n</span><span class="hljs-subst">\#r</span><span class="hljs-subst">\#&quot;</span><span class="hljs-subst">\#&#x27;</span>&quot;#</span>
<span class="hljs-string">#&quot;escaped unicode <span class="hljs-subst">\#u{0}</span><span class="hljs-subst">\#u{12}</span><span class="hljs-subst">\#u{345}</span><span class="hljs-subst">\#u{6789}</span><span class="hljs-subst">\#u{abcde}</span><span class="hljs-subst">\#u{fABCDE}</span><span class="hljs-subst">\#u{F012345}</span><span class="hljs-subst">\#u{67890abc}</span>&quot;#</span>
<span class="hljs-string">#&quot;raw characters \0\\\t\n\r\&quot;\&#x27;\u{6789}&quot;#</span>
<span class="hljs-string">##&quot;escaped characters <span class="hljs-subst">\##0</span><span class="hljs-subst">\##\</span><span class="hljs-subst">\##t</span><span class="hljs-subst">\##n</span><span class="hljs-subst">\##r</span><span class="hljs-subst">\##&quot;</span><span class="hljs-subst">\##&#x27;</span>&quot;##</span>
<span class="hljs-string">##&quot;escaped unicode <span class="hljs-subst">\##u{0}</span><span class="hljs-subst">\##u{12}</span><span class="hljs-subst">\##u{345}</span><span class="hljs-subst">\##u{6789}</span><span class="hljs-subst">\##u{abcde}</span><span class="hljs-subst">\##u{fABCDE}</span><span class="hljs-subst">\##u{F012345}</span><span class="hljs-subst">\##u{67890abc}</span>&quot;##</span>
<span class="hljs-string">##&quot;raw characters \0\\\t\n\r\&quot;\&#x27;\u{6789} \#0\#\\#t\#n\#r\#&quot;\#&#x27;\#u{6789}&quot;##</span>
<span class="hljs-string">###&quot;escaped characters <span class="hljs-subst">\###0</span><span class="hljs-subst">\###\</span><span class="hljs-subst">\###t</span><span class="hljs-subst">\###n</span><span class="hljs-subst">\###r</span><span class="hljs-subst">\###&quot;</span><span class="hljs-subst">\###&#x27;</span>&quot;###</span>
<span class="hljs-string">###&quot;escaped unicode <span class="hljs-subst">\###u{0}</span><span class="hljs-subst">\###u{12}</span><span class="hljs-subst">\###u{345}</span><span class="hljs-subst">\###u{6789}</span><span class="hljs-subst">\###u{abcde}</span><span class="hljs-subst">\###u{fABCDE}</span><span class="hljs-subst">\###u{F012345}</span><span class="hljs-subst">\###u{67890abc}</span>&quot;###</span>
<span class="hljs-string">###&quot;raw characters \0\\\t\n\r\&quot;\&#x27;\u{6789} \#0\#\\#t\#n\#r\#&quot;\#&#x27;\#u{6789} \##0\##\\##t\##n\##r\##&quot;\##&#x27;\##u{6789}&quot;###</span>

<span class="hljs-string">&quot;&quot;&quot;
escaped characters <span class="hljs-subst">\0</span><span class="hljs-subst">\\</span><span class="hljs-subst">\t</span><span class="hljs-subst">\n</span><span class="hljs-subst">\r</span><span class="hljs-subst">\&quot;</span><span class="hljs-subst">\&#x27;</span>
escaped unicode <span class="hljs-subst">\u{0}</span><span class="hljs-subst">\u{12}</span><span class="hljs-subst">\u{345}</span><span class="hljs-subst">\u{6789}</span><span class="hljs-subst">\u{abcde}</span><span class="hljs-subst">\u{fABCDE}</span><span class="hljs-subst">\u{F012345}</span><span class="hljs-subst">\u{67890abc}</span>
&quot;&quot;&quot;</span>
<span class="hljs-string">#&quot;&quot;&quot;
escaped characters <span class="hljs-subst">\#0</span><span class="hljs-subst">\#\</span><span class="hljs-subst">\#t</span><span class="hljs-subst">\#n</span><span class="hljs-subst">\#r</span><span class="hljs-subst">\#&quot;</span><span class="hljs-subst">\#&#x27;</span>
escaped unicode <span class="hljs-subst">\#u{0}</span><span class="hljs-subst">\#u{12}</span><span class="hljs-subst">\#u{345}</span><span class="hljs-subst">\#u{6789}</span><span class="hljs-subst">\#u{abcde}</span><span class="hljs-subst">\#u{fABCDE}</span><span class="hljs-subst">\#u{F012345}</span><span class="hljs-subst">\#u{67890abc}</span>
raw characters \0\\\t\n\r\&quot;\&#x27;\u{6789}
&quot;&quot;&quot;#</span>
<span class="hljs-string">##&quot;&quot;&quot;
escaped characters <span class="hljs-subst">\##0</span><span class="hljs-subst">\##\</span><span class="hljs-subst">\##t</span><span class="hljs-subst">\##n</span><span class="hljs-subst">\##r</span><span class="hljs-subst">\##&quot;</span><span class="hljs-subst">\##&#x27;</span>
escaped unicode <span class="hljs-subst">\##u{0}</span><span class="hljs-subst">\##u{12}</span><span class="hljs-subst">\##u{345}</span><span class="hljs-subst">\##u{6789}</span><span class="hljs-subst">\##u{abcde}</span><span class="hljs-subst">\##u{fABCDE}</span><span class="hljs-subst">\##u{F012345}</span><span class="hljs-subst">\##u{67890abc}</span>
raw characters \0\\\t\n\r\&quot;\&#x27;\u{6789} \#0\#\\#t\#n\#r\#&quot;\#&#x27;\#u{6789}
&quot;&quot;&quot;##</span>
<span class="hljs-string">###&quot;&quot;&quot;
escaped characters <span class="hljs-subst">\###0</span><span class="hljs-subst">\###\</span><span class="hljs-subst">\###t</span><span class="hljs-subst">\###n</span><span class="hljs-subst">\###r</span><span class="hljs-subst">\###&quot;</span><span class="hljs-subst">\###&#x27;</span>
escaped unicode <span class="hljs-subst">\###u{0}</span><span class="hljs-subst">\###u{12}</span><span class="hljs-subst">\###u{345}</span><span class="hljs-subst">\###u{6789}</span><span class="hljs-subst">\###u{abcde}</span><span class="hljs-subst">\###u{fABCDE}</span><span class="hljs-subst">\###u{F012345}</span><span class="hljs-subst">\###u{67890abc}</span>
raw characters \0\\\t\n\r\&quot;\&#x27;\u{6789} \#0\#\\#t\#n\#r\#&quot;\#&#x27;\#u{6789} \##0\##\\##t\##n\##r\##&quot;\##&#x27;\##u{6789}
&quot;&quot;&quot;###</span>

<span class="hljs-string">&quot;&quot;&quot;
escaped newline <span class="hljs-subst">\
</span>same line
&quot;&quot;&quot;</span>
<span class="hljs-string">#&quot;&quot;&quot;
escaped newline <span class="hljs-subst">\#
</span>same line
&quot;&quot;&quot;#</span>
<span class="hljs-string">##&quot;&quot;&quot;
escaped newline <span class="hljs-subst">\##
</span>same line
&quot;&quot;&quot;##</span>
<span class="hljs-string">###&quot;&quot;&quot;
escaped newline <span class="hljs-subst">\###
</span>same line
&quot;&quot;&quot;###</span>

<span class="hljs-string">&quot;interpolation <span class="hljs-subst">\(x)</span>&quot;</span>
<span class="hljs-string">#&quot;interpolation <span class="hljs-subst">\#(<span class="hljs-number">123</span>)</span> raw \(123)&quot;#</span>
<span class="hljs-string">##&quot;interpolation <span class="hljs-subst">\##(<span class="hljs-number">1.23</span>)</span> raw \#(1.23) raw \(1.23)&quot;##</span>
<span class="hljs-string">###&quot;interpolation <span class="hljs-subst">\###(<span class="hljs-string">&quot;string&quot;</span>)</span> raw \##(&quot;string&quot;) raw \#(&quot;string&quot;) raw \(&quot;string&quot;)&quot;###</span>

<span class="hljs-string">&quot;&quot;&quot;
interpolation <span class="hljs-subst">\(x)</span>
&quot;&quot;&quot;</span>
<span class="hljs-string">#&quot;&quot;&quot;
interpolation <span class="hljs-subst">\#(<span class="hljs-number">123</span>)</span>
raw \(123)
&quot;&quot;&quot;#</span>
<span class="hljs-string">##&quot;&quot;&quot;
interpolation <span class="hljs-subst">\##(<span class="hljs-number">1.23</span>)</span>
raw \#(1.23)
raw \(1.23)
&quot;&quot;&quot;##</span>
<span class="hljs-string">###&quot;&quot;&quot;
interpolation <span class="hljs-subst">\###(<span class="hljs-string">&quot;string&quot;</span>)</span>
raw \##(&quot;string&quot;)
raw \#(&quot;string&quot;)
raw \(&quot;string&quot;)
&quot;&quot;&quot;###</span>
72 changes: 72 additions & 0 deletions test/markup/swift/strings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"escaped characters \0\\\t\n\r\"\'"
"escaped unicode \u{0}\u{12}\u{345}\u{6789}\u{abcde}\u{fABCDE}\u{F012345}\u{67890abc}"
#"escaped characters \#0\#\\#t\#n\#r\#"\#'"#
#"escaped unicode \#u{0}\#u{12}\#u{345}\#u{6789}\#u{abcde}\#u{fABCDE}\#u{F012345}\#u{67890abc}"#
#"raw characters \0\\\t\n\r\"\'\u{6789}"#
##"escaped characters \##0\##\\##t\##n\##r\##"\##'"##
##"escaped unicode \##u{0}\##u{12}\##u{345}\##u{6789}\##u{abcde}\##u{fABCDE}\##u{F012345}\##u{67890abc}"##
##"raw characters \0\\\t\n\r\"\'\u{6789} \#0\#\\#t\#n\#r\#"\#'\#u{6789}"##
###"escaped characters \###0\###\\###t\###n\###r\###"\###'"###
###"escaped unicode \###u{0}\###u{12}\###u{345}\###u{6789}\###u{abcde}\###u{fABCDE}\###u{F012345}\###u{67890abc}"###
###"raw characters \0\\\t\n\r\"\'\u{6789} \#0\#\\#t\#n\#r\#"\#'\#u{6789} \##0\##\\##t\##n\##r\##"\##'\##u{6789}"###

"""
escaped characters \0\\\t\n\r\"\'
escaped unicode \u{0}\u{12}\u{345}\u{6789}\u{abcde}\u{fABCDE}\u{F012345}\u{67890abc}
"""
#"""
escaped characters \#0\#\\#t\#n\#r\#"\#'
escaped unicode \#u{0}\#u{12}\#u{345}\#u{6789}\#u{abcde}\#u{fABCDE}\#u{F012345}\#u{67890abc}
raw characters \0\\\t\n\r\"\'\u{6789}
"""#
##"""
escaped characters \##0\##\\##t\##n\##r\##"\##'
escaped unicode \##u{0}\##u{12}\##u{345}\##u{6789}\##u{abcde}\##u{fABCDE}\##u{F012345}\##u{67890abc}
raw characters \0\\\t\n\r\"\'\u{6789} \#0\#\\#t\#n\#r\#"\#'\#u{6789}
"""##
###"""
escaped characters \###0\###\\###t\###n\###r\###"\###'
escaped unicode \###u{0}\###u{12}\###u{345}\###u{6789}\###u{abcde}\###u{fABCDE}\###u{F012345}\###u{67890abc}
raw characters \0\\\t\n\r\"\'\u{6789} \#0\#\\#t\#n\#r\#"\#'\#u{6789} \##0\##\\##t\##n\##r\##"\##'\##u{6789}
"""###

"""
escaped newline \
same line
"""
#"""
escaped newline \#
same line
"""#
##"""
escaped newline \##
same line
"""##
###"""
escaped newline \###
same line
"""###

"interpolation \(x)"
#"interpolation \#(123) raw \(123)"#
##"interpolation \##(1.23) raw \#(1.23) raw \(1.23)"##
###"interpolation \###("string") raw \##("string") raw \#("string") raw \("string")"###

"""
interpolation \(x)
"""
#"""
interpolation \#(123)
raw \(123)
"""#
##"""
interpolation \##(1.23)
raw \#(1.23)
raw \(1.23)
"""##
###"""
interpolation \###("string")
raw \##("string")
raw \#("string")
raw \("string")
"""###