Skip to content

Commit 8541db2

Browse files
authoredAug 9, 2021
Swift: Major improvements (#3022)
1 parent 748bb9a commit 8541db2

22 files changed

+883
-168
lines changed
 

‎components.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎components.json

-1
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,6 @@
12211221
},
12221222
"swift": {
12231223
"title": "Swift",
1224-
"require": "clike",
12251224
"owner": "chrischares"
12261225
},
12271226
"t4-templating": {

‎components/prism-swift.js

+143-20
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,148 @@
1-
// issues: nested multiline comments
2-
Prism.languages.swift = Prism.languages.extend('clike', {
3-
'string': {
4-
pattern: /("|')(?:\\(?:\((?:[^()]|\([^)]+\))+\)|\r\n|[^(])|(?!\1)[^\\\r\n])*\1/,
5-
greedy: true,
6-
inside: {
7-
'interpolation': {
8-
pattern: /\\\((?:[^()]|\([^)]+\))+\)/,
9-
inside: {
10-
delimiter: {
11-
pattern: /^\\\(|\)$/,
12-
alias: 'variable'
13-
}
14-
// See rest below
15-
}
1+
Prism.languages.swift = {
2+
'comment': {
3+
// Nested comments are supported up to 2 levels
4+
pattern: /(^|[^\\:])(?:\/\/.*|\/\*(?:[^/*]|\/(?!\*)|\*(?!\/)|\/\*(?:[^*]|\*(?!\/))*\*\/)*\*\/)/,
5+
lookbehind: true,
6+
greedy: true
7+
},
8+
'string-literal': [
9+
// https://docs.swift.org/swift-book/LanguageGuide/StringsAndCharacters.html
10+
{
11+
pattern: RegExp(
12+
/(^|[^"#])/.source
13+
+ '(?:'
14+
// single-line string
15+
+ /"(?:\\(?:\((?:[^()]|\([^()]*\))*\)|\r\n|[^(])|[^\\\r\n"])*"/.source
16+
+ '|'
17+
// multi-line string
18+
+ /"""(?:\\(?:\((?:[^()]|\([^()]*\))*\)|[^(])|[^\\"]|"(?!""))*"""/.source
19+
+ ')'
20+
+ /(?!["#])/.source
21+
),
22+
lookbehind: true,
23+
greedy: true,
24+
inside: {
25+
'interpolation': {
26+
pattern: /(\\\()(?:[^()]|\([^()]*\))*(?=\))/,
27+
lookbehind: true,
28+
inside: null // see below
29+
},
30+
'interpolation-punctuation': {
31+
pattern: /^\)|\\\($/,
32+
alias: 'punctuation'
33+
},
34+
'punctuation': /\\(?=[\r\n])/,
35+
'string': /[\s\S]+/
36+
}
37+
},
38+
{
39+
pattern: RegExp(
40+
/(^|[^"#])(#+)/.source
41+
+ '(?:'
42+
// single-line string
43+
+ /"(?:\\(?:#+\((?:[^()]|\([^()]*\))*\)|\r\n|[^#])|[^\\\r\n])*?"/.source
44+
+ '|'
45+
// multi-line string
46+
+ /"""(?:\\(?:#+\((?:[^()]|\([^()]*\))*\)|[^#])|[^\\])*?"""/.source
47+
+ ')'
48+
+ '\\2'
49+
),
50+
lookbehind: true,
51+
greedy: true,
52+
inside: {
53+
'interpolation': {
54+
pattern: /(\\#+\()(?:[^()]|\([^()]*\))*(?=\))/,
55+
lookbehind: true,
56+
inside: null // see below
57+
},
58+
'interpolation-punctuation': {
59+
pattern: /^\)|\\#+\($/,
60+
alias: 'punctuation'
61+
},
62+
'string': /[\s\S]+/
1663
}
64+
},
65+
],
66+
67+
'directive': {
68+
// directives with conditions
69+
pattern: RegExp(
70+
/#/.source
71+
+ '(?:'
72+
+ (
73+
/(?:elseif|if)\b/.source
74+
+ '(?:[ \t]*'
75+
// This regex is a little complex. It's equivalent to this:
76+
// (?:![ \t]*)?(?:\b\w+\b(?:[ \t]*<round>)?|<round>)(?:[ \t]*(?:&&|\|\|))?
77+
// where <round> is a general parentheses expression.
78+
+ /(?:![ \t]*)?(?:\b\w+\b(?:[ \t]*\((?:[^()]|\([^()]*\))*\))?|\((?:[^()]|\([^()]*\))*\))(?:[ \t]*(?:&&|\|\|))?/.source
79+
+ ')+'
80+
)
81+
+ '|'
82+
+ /(?:else|endif)\b/.source
83+
+ ')'
84+
),
85+
alias: 'property',
86+
inside: {
87+
'directive-name': /^#\w+/,
88+
'boolean': /\b(?:true|false)\b/,
89+
'number': /\b\d+(?:\.\d+)*\b/,
90+
'operator': /!|&&|\|\||[<>]=?/,
91+
'punctuation': /[(),]/
1792
}
1893
},
19-
'keyword': /\b(?:actor|as|associativity|async|await|break|case|catch|class|continue|convenience|default|defer|deinit|didSet|do|dynamic(?:Type)?|else|enum|extension|fallthrough|final|for|func|get|guard|if|import|in|infix|init|inout|internal|is|lazy|left|let|mutating|new|none|nonisolated|nonmutating|operator|optional|override|postfix|precedence|prefix|private|protocol|public|repeat|required|rethrows|return|right|safe|self|Self|set|some|static|struct|subscript|super|switch|throws?|try|Type|typealias|unowned|unsafe|var|weak|where|while|willSet|__(?:COLUMN__|FILE__|FUNCTION__|LINE__))\b/,
94+
'literal': {
95+
pattern: /#(?:colorLiteral|column|dsohandle|file(?:ID|Literal|Path)?|function|imageLiteral|line)\b/,
96+
alias: 'constant'
97+
},
98+
'other-directive': {
99+
pattern: /#\w+\b/,
100+
alias: 'property'
101+
},
102+
103+
'attribute': {
104+
pattern: /@\w+/,
105+
alias: 'atrule'
106+
},
107+
108+
'function-definition': {
109+
pattern: /(\bfunc\s+)\w+/,
110+
lookbehind: true,
111+
alias: 'function'
112+
},
113+
'label': {
114+
// https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID141
115+
pattern: /\b(break|continue)\s+\w+|\b[a-zA-Z_]\w*(?=\s*:\s*(?:for|repeat|while)\b)/,
116+
lookbehind: true,
117+
alias: 'important'
118+
},
119+
120+
'keyword': /\b(?:Any|Protocol|Self|Type|actor|as|assignment|associatedtype|associativity|async|await|break|case|catch|class|continue|convenience|default|defer|deinit|didSet|do|dynamic|else|enum|extension|fallthrough|fileprivate|final|for|func|get|guard|higherThan|if|import|in|indirect|infix|init|inout|internal|is|lazy|left|let|lowerThan|mutating|none|nonisolated|nonmutating|open|operator|optional|override|postfix|precedencegroup|prefix|private|protocol|public|repeat|required|rethrows|return|right|safe|self|set|some|static|struct|subscript|super|switch|throw|throws|try|typealias|unowned|unsafe|var|weak|where|while|willSet)\b/,
121+
'boolean': /\b(?:true|false)\b/,
122+
'nil': {
123+
pattern: /\bnil\b/,
124+
alias: 'constant'
125+
},
126+
127+
'short-argument': /\$\d+\b/,
128+
'omit': {
129+
pattern: /\b_\b/,
130+
alias: 'keyword'
131+
},
20132
'number': /\b(?:[\d_]+(?:\.[\de_]+)?|0x[a-f0-9_]+(?:\.[a-f0-9p_]+)?|0b[01_]+|0o[0-7_]+)\b/i,
21-
'constant': /\b(?:nil|[A-Z_]{2,}|k[A-Z][A-Za-z_]+)\b/,
22-
'atrule': /@\b(?:IB(?:Outlet|Designable|Action|Inspectable)|class_protocol|exported|globalActor|MainActor|noreturn|NS(?:Copying|Managed)|objc|propertyWrapper|UIApplicationMain|auto_closure)\b/,
23-
'builtin': /\b(?:[A-Z]\S+|abs|advance|alignof(?:Value)?|assert|contains|count(?:Elements)?|debugPrint(?:ln)?|distance|drop(?:First|Last)|dump|enumerate|equal|filter|find|first|getVaList|indices|isEmpty|join|last|lexicographicalCompare|map|max(?:Element)?|min(?:Element)?|numericCast|overlaps|partition|print(?:ln)?|reduce|reflect|reverse|sizeof(?:Value)?|sort(?:ed)?|split|startsWith|stride(?:of(?:Value)?)?|suffix|swap|toDebugString|toString|transcode|underestimateCount|unsafeBitCast|with(?:ExtendedLifetime|Unsafe(?:MutablePointers?|Pointers?)|VaList))\b/
133+
134+
// A class name must start with an upper-case letter and be either 1 letter long or contain a lower-case letter.
135+
'class-name': /\b[A-Z](?:[A-Z_\d]*[a-z]\w*)?\b/,
136+
'function': /\b[a-z_]\w*(?=\s*\()/i,
137+
'constant': /\b(?:[A-Z_]{2,}|k[A-Z][A-Za-z_]+)\b/,
138+
139+
// Operators are generic in Swift. Developers can even create new operators (e.g. +++).
140+
// https://docs.swift.org/swift-book/ReferenceManual/zzSummaryOfTheGrammar.html#ID481
141+
// This regex only supports ASCII operators.
142+
'operator': /[-+*/%=!<>&|^~?]+|\.[.\-+*/%=!<>&|^~?]+/,
143+
'punctuation': /[{}[\]();,.:\\]/
144+
};
145+
146+
Prism.languages.swift['string-literal'].forEach(function (rule) {
147+
rule.inside['interpolation'].inside = Prism.languages.swift;
24148
});
25-
Prism.languages.swift['string'].inside['interpolation'].inside.rest = Prism.languages.swift;

‎components/prism-swift.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎plugins/autoloader/prism-autoloader.js

-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@
135135
"sparql": "turtle",
136136
"sqf": "clike",
137137
"squirrel": "clike",
138-
"swift": "clike",
139138
"t4-cs": [
140139
"t4-templating",
141140
"csharp"

‎plugins/autoloader/prism-autoloader.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/languages/swift/atrule_feature.test

-39
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
@IBOutlet
2+
@IBDesignable
3+
@IBAction
4+
@IBInspectable
5+
@class_protocol
6+
@exported
7+
@globalActor
8+
@MainActor
9+
@noreturn
10+
@NSCopying
11+
@NSManaged
12+
@objc
13+
@propertyWrapper
14+
@UIApplicationMain
15+
@auto_closure
16+
17+
@SomeCustomName
18+
19+
----------------------------------------------------
20+
21+
[
22+
["attribute", "@IBOutlet"],
23+
["attribute", "@IBDesignable"],
24+
["attribute", "@IBAction"],
25+
["attribute", "@IBInspectable"],
26+
["attribute", "@class_protocol"],
27+
["attribute", "@exported"],
28+
["attribute", "@globalActor"],
29+
["attribute", "@MainActor"],
30+
["attribute", "@noreturn"],
31+
["attribute", "@NSCopying"],
32+
["attribute", "@NSManaged"],
33+
["attribute", "@objc"],
34+
["attribute", "@propertyWrapper"],
35+
["attribute", "@UIApplicationMain"],
36+
["attribute", "@auto_closure"],
37+
38+
["attribute", "@SomeCustomName"]
39+
]

‎tests/languages/swift/builtin_feature.test

-53
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
struct SomeStructure {}
2+
enum SomeEnumeration {}
3+
class SomeClass: SomeSuperclass {
4+
class var overrideableComputedTypeProperty: Int {
5+
return 107
6+
}
7+
}
8+
9+
----------------------------------------------------
10+
11+
[
12+
["keyword", "struct"],
13+
["class-name", "SomeStructure"],
14+
["punctuation", "{"],
15+
["punctuation", "}"],
16+
17+
["keyword", "enum"],
18+
["class-name", "SomeEnumeration"],
19+
["punctuation", "{"],
20+
["punctuation", "}"],
21+
22+
["keyword", "class"],
23+
["class-name", "SomeClass"],
24+
["punctuation", ":"],
25+
["class-name", "SomeSuperclass"],
26+
["punctuation", "{"],
27+
28+
["keyword", "class"],
29+
["keyword", "var"],
30+
" overrideableComputedTypeProperty",
31+
["punctuation", ":"],
32+
["class-name", "Int"],
33+
["punctuation", "{"],
34+
35+
["keyword", "return"],
36+
["number", "107"],
37+
38+
["punctuation", "}"],
39+
40+
["punctuation", "}"]
41+
]
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// foo
2+
/**/
3+
/* foo */
4+
/*
5+
foo
6+
*/
7+
8+
/*
9+
/*
10+
foo
11+
*/
12+
*/
13+
14+
----------------------------------------------------
15+
16+
[
17+
["comment", "// foo"],
18+
["comment", "/**/"],
19+
["comment", "/* foo */"],
20+
["comment", "/*\r\n foo\r\n*/"],
21+
22+
["comment", "/*\r\n/*\r\n foo\r\n*/\r\n*/"]
23+
]

‎tests/languages/swift/constant_feature.test

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ kFoo_bar
77
----------------------------------------------------
88

99
[
10-
["constant", "nil"],
10+
["nil", "nil"],
1111
["constant", "AB"],
1212
["constant", "FOO_BAR"],
1313
["constant", "kAb"],
@@ -16,4 +16,4 @@ kFoo_bar
1616

1717
----------------------------------------------------
1818

19-
Checks for constants.
19+
Checks for constants.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#if os(tvOS)
2+
#if !DEBUG && ENABLE_INTERNAL_TOOLS
3+
#if SWIFTUI_PROFILE
4+
#if compiler(>=5)
5+
#if compiler(>=5) && swift(<5)
6+
7+
#elseif compiler(>=5)
8+
#else
9+
#endif
10+
11+
#sourceLocation(file: "foo", line: 42)
12+
#sourceLocation()
13+
14+
#error("error message")
15+
#warning("warning message")
16+
17+
#available(iOS 13, *)
18+
19+
#selector(SomeClass.doSomething(_:))
20+
21+
#keyPath(SomeClass.someProperty)
22+
23+
----------------------------------------------------
24+
25+
[
26+
["directive", [
27+
["directive-name", "#if"],
28+
" os",
29+
["punctuation", "("],
30+
"tvOS",
31+
["punctuation", ")"]
32+
]],
33+
["directive", [
34+
["directive-name", "#if"],
35+
["operator", "!"],
36+
"DEBUG ",
37+
["operator", "&&"],
38+
" ENABLE_INTERNAL_TOOLS"
39+
]],
40+
["directive", [
41+
["directive-name", "#if"],
42+
" SWIFTUI_PROFILE"
43+
]],
44+
["directive", [
45+
["directive-name", "#if"],
46+
" compiler",
47+
["punctuation", "("],
48+
["operator", ">="],
49+
["number", "5"],
50+
["punctuation", ")"]
51+
]],
52+
["directive", [
53+
["directive-name", "#if"],
54+
" compiler",
55+
["punctuation", "("],
56+
["operator", ">="],
57+
["number", "5"],
58+
["punctuation", ")"],
59+
["operator", "&&"],
60+
" swift",
61+
["punctuation", "("],
62+
["operator", "<"],
63+
["number", "5"],
64+
["punctuation", ")"]
65+
]],
66+
67+
["directive", [
68+
["directive-name", "#elseif"],
69+
" compiler",
70+
["punctuation", "("],
71+
["operator", ">="],
72+
["number", "5"],
73+
["punctuation", ")"]
74+
]],
75+
["directive", [
76+
["directive-name", "#else"]
77+
]],
78+
["directive", [
79+
["directive-name", "#endif"]
80+
]],
81+
82+
["other-directive", "#sourceLocation"],
83+
["punctuation", "("],
84+
"file",
85+
["punctuation", ":"],
86+
["string-literal", [
87+
["string", "\"foo\""]
88+
]],
89+
["punctuation", ","],
90+
" line",
91+
["punctuation", ":"],
92+
["number", "42"],
93+
["punctuation", ")"],
94+
95+
["other-directive", "#sourceLocation"],
96+
["punctuation", "("],
97+
["punctuation", ")"],
98+
99+
["other-directive", "#error"],
100+
["punctuation", "("],
101+
["string-literal", [
102+
["string", "\"error message\""]
103+
]],
104+
["punctuation", ")"],
105+
106+
["other-directive", "#warning"],
107+
["punctuation", "("],
108+
["string-literal", [
109+
["string", "\"warning message\""]
110+
]],
111+
["punctuation", ")"],
112+
113+
["other-directive", "#available"],
114+
["punctuation", "("],
115+
"iOS ",
116+
["number", "13"],
117+
["punctuation", ","],
118+
["operator", "*"],
119+
["punctuation", ")"],
120+
121+
["other-directive", "#selector"],
122+
["punctuation", "("],
123+
["class-name", "SomeClass"],
124+
["punctuation", "."],
125+
["function", "doSomething"],
126+
["punctuation", "("],
127+
["omit", "_"],
128+
["punctuation", ":"],
129+
["punctuation", ")"],
130+
["punctuation", ")"],
131+
132+
["other-directive", "#keyPath"],
133+
["punctuation", "("],
134+
["class-name", "SomeClass"],
135+
["punctuation", "."],
136+
"someProperty",
137+
["punctuation", ")"]
138+
]
+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
func greetAgain(person: String) -> String {
2+
return "Hello again, " + person + "!"
3+
}
4+
print(greetAgain(person: "Anna"))
5+
func someFunction<T: SomeClass, U: SomeProtocol>(someT: T, someU: U) {
6+
// function body goes here
7+
}
8+
9+
10+
// none of the below are functions
11+
subscript(index: Int) -> Int {
12+
get {}
13+
set(newValue) {}
14+
}
15+
16+
----------------------------------------------------
17+
18+
[
19+
["keyword", "func"],
20+
["function-definition", "greetAgain"],
21+
["punctuation", "("],
22+
"person",
23+
["punctuation", ":"],
24+
["class-name", "String"],
25+
["punctuation", ")"],
26+
["operator", "->"],
27+
["class-name", "String"],
28+
["punctuation", "{"],
29+
30+
["keyword", "return"],
31+
["string-literal", [
32+
["string", "\"Hello again, \""]
33+
]],
34+
["operator", "+"],
35+
" person ",
36+
["operator", "+"],
37+
["string-literal", [
38+
["string", "\"!\""]
39+
]],
40+
41+
["punctuation", "}"],
42+
43+
["function", "print"],
44+
["punctuation", "("],
45+
["function", "greetAgain"],
46+
["punctuation", "("],
47+
"person",
48+
["punctuation", ":"],
49+
["string-literal", [
50+
["string", "\"Anna\""]
51+
]],
52+
["punctuation", ")"],
53+
["punctuation", ")"],
54+
55+
["keyword", "func"],
56+
["function-definition", "someFunction"],
57+
["operator", "<"],
58+
["class-name", "T"],
59+
["punctuation", ":"],
60+
["class-name", "SomeClass"],
61+
["punctuation", ","],
62+
["class-name", "U"],
63+
["punctuation", ":"],
64+
["class-name", "SomeProtocol"],
65+
["operator", ">"],
66+
["punctuation", "("],
67+
"someT",
68+
["punctuation", ":"],
69+
["class-name", "T"],
70+
["punctuation", ","],
71+
" someU",
72+
["punctuation", ":"],
73+
["class-name", "U"],
74+
["punctuation", ")"],
75+
["punctuation", "{"],
76+
77+
["comment", "// function body goes here"],
78+
79+
["punctuation", "}"],
80+
81+
["comment", "// none of the below are functions"],
82+
83+
["keyword", "subscript"],
84+
["punctuation", "("],
85+
"index",
86+
["punctuation", ":"],
87+
["class-name", "Int"],
88+
["punctuation", ")"],
89+
["operator", "->"],
90+
["class-name", "Int"],
91+
["punctuation", "{"],
92+
93+
["keyword", "get"],
94+
["punctuation", "{"],
95+
["punctuation", "}"],
96+
97+
["keyword", "set"],
98+
["punctuation", "("],
99+
"newValue",
100+
["punctuation", ")"],
101+
["punctuation", "{"],
102+
["punctuation", "}"],
103+
104+
["punctuation", "}"]
105+
]

‎tests/languages/swift/keyword_feature.test

+31-25
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
1+
Any
2+
Protocol
3+
Self
4+
Type
15
actor
26
as
7+
assignment
8+
associatedtype
39
associativity
410
async
511
await
6-
break
12+
break;
713
case
814
catch
915
class;
10-
continue
16+
continue;
1117
convenience
1218
default
1319
defer
1420
deinit
1521
didSet
1622
do
1723
dynamic
18-
dynamicType
1924
else
2025
enum
2126
extension
2227
fallthrough
28+
fileprivate
2329
final
2430
for
25-
func
31+
func;
2632
get
2733
guard
34+
higherThan
2835
if
2936
import
3037
in
38+
indirect
3139
infix
3240
init
3341
inout
@@ -36,16 +44,17 @@ is
3644
lazy
3745
left
3846
let
47+
lowerThan
3948
mutating
40-
new;
4149
none
4250
nonisolated
4351
nonmutating
52+
open
4453
operator
4554
optional
4655
override
4756
postfix
48-
precedence
57+
precedencegroup
4958
prefix
5059
private
5160
protocol
@@ -57,7 +66,6 @@ return
5766
right
5867
safe
5968
self
60-
Self
6169
set
6270
some
6371
static
@@ -68,7 +76,6 @@ switch
6876
throw
6977
throws
7078
try
71-
Type
7279
typealias
7380
unowned
7481
unsafe
@@ -77,44 +84,48 @@ weak
7784
where
7885
while
7986
willSet
80-
__COLUMN__
81-
__FILE__
82-
__FUNCTION__
83-
__LINE__
8487

8588
----------------------------------------------------
8689

8790
[
91+
["keyword", "Any"],
92+
["keyword", "Protocol"],
93+
["keyword", "Self"],
94+
["keyword", "Type"],
8895
["keyword", "actor"],
8996
["keyword", "as"],
97+
["keyword", "assignment"],
98+
["keyword", "associatedtype"],
9099
["keyword", "associativity"],
91100
["keyword", "async"],
92101
["keyword", "await"],
93-
["keyword", "break"],
102+
["keyword", "break"], ["punctuation", ";"],
94103
["keyword", "case"],
95104
["keyword", "catch"],
96105
["keyword", "class"], ["punctuation", ";"],
97-
["keyword", "continue"],
106+
["keyword", "continue"], ["punctuation", ";"],
98107
["keyword", "convenience"],
99108
["keyword", "default"],
100109
["keyword", "defer"],
101110
["keyword", "deinit"],
102111
["keyword", "didSet"],
103112
["keyword", "do"],
104113
["keyword", "dynamic"],
105-
["keyword", "dynamicType"],
106114
["keyword", "else"],
107115
["keyword", "enum"],
108116
["keyword", "extension"],
109117
["keyword", "fallthrough"],
118+
["keyword", "fileprivate"],
110119
["keyword", "final"],
111120
["keyword", "for"],
112-
["keyword", "func"],
121+
["keyword", "func"], ["punctuation", ";"],
113122
["keyword", "get"],
114123
["keyword", "guard"],
124+
["keyword", "higherThan"],
115125
["keyword", "if"],
116126
["keyword", "import"],
117127
["keyword", "in"],
128+
["keyword", "indirect"],
118129
["keyword", "infix"],
119130
["keyword", "init"],
120131
["keyword", "inout"],
@@ -123,16 +134,17 @@ __LINE__
123134
["keyword", "lazy"],
124135
["keyword", "left"],
125136
["keyword", "let"],
137+
["keyword", "lowerThan"],
126138
["keyword", "mutating"],
127-
["keyword", "new"], ["punctuation", ";"],
128139
["keyword", "none"],
129140
["keyword", "nonisolated"],
130141
["keyword", "nonmutating"],
142+
["keyword", "open"],
131143
["keyword", "operator"],
132144
["keyword", "optional"],
133145
["keyword", "override"],
134146
["keyword", "postfix"],
135-
["keyword", "precedence"],
147+
["keyword", "precedencegroup"],
136148
["keyword", "prefix"],
137149
["keyword", "private"],
138150
["keyword", "protocol"],
@@ -144,7 +156,6 @@ __LINE__
144156
["keyword", "right"],
145157
["keyword", "safe"],
146158
["keyword", "self"],
147-
["keyword", "Self"],
148159
["keyword", "set"],
149160
["keyword", "some"],
150161
["keyword", "static"],
@@ -155,19 +166,14 @@ __LINE__
155166
["keyword", "throw"],
156167
["keyword", "throws"],
157168
["keyword", "try"],
158-
["keyword", "Type"],
159169
["keyword", "typealias"],
160170
["keyword", "unowned"],
161171
["keyword", "unsafe"],
162172
["keyword", "var"],
163173
["keyword", "weak"],
164174
["keyword", "where"],
165175
["keyword", "while"],
166-
["keyword", "willSet"],
167-
["keyword", "__COLUMN__"],
168-
["keyword", "__FILE__"],
169-
["keyword", "__FUNCTION__"],
170-
["keyword", "__LINE__"]
176+
["keyword", "willSet"]
171177
]
172178

173179
----------------------------------------------------
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
gameLoop: while square != finalSquare {
2+
break gameLoop
3+
continue gameLoop
4+
}
5+
6+
----------------------------------------------------
7+
8+
[
9+
["label", "gameLoop"],
10+
["punctuation", ":"],
11+
["keyword", "while"],
12+
" square ",
13+
["operator", "!="],
14+
" finalSquare ",
15+
["punctuation", "{"],
16+
17+
["keyword", "break"],
18+
["label", " gameLoop"],
19+
20+
["keyword", "continue"],
21+
["label", " gameLoop"],
22+
23+
["punctuation", "}"]
24+
]
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#file
2+
#fileID
3+
#filePath
4+
#line
5+
#column
6+
#function
7+
#dsohandle
8+
9+
#colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
10+
#fileLiteral(resourceName: "foo")
11+
#imageLiteral(resourceName: "foo")
12+
13+
----------------------------------------------------
14+
15+
[
16+
["literal", "#file"],
17+
["literal", "#fileID"],
18+
["literal", "#filePath"],
19+
["literal", "#line"],
20+
["literal", "#column"],
21+
["literal", "#function"],
22+
["literal", "#dsohandle"],
23+
24+
["literal", "#colorLiteral"],
25+
["punctuation", "("],
26+
"red",
27+
["punctuation", ":"],
28+
["number", "1.0"],
29+
["punctuation", ","],
30+
" green",
31+
["punctuation", ":"],
32+
["number", "1.0"],
33+
["punctuation", ","],
34+
" blue",
35+
["punctuation", ":"],
36+
["number", "1.0"],
37+
["punctuation", ","],
38+
" alpha",
39+
["punctuation", ":"],
40+
["number", "1.0"],
41+
["punctuation", ")"],
42+
43+
["literal", "#fileLiteral"],
44+
["punctuation", "("],
45+
"resourceName",
46+
["punctuation", ":"],
47+
["string-literal", [
48+
["string", "\"foo\""]
49+
]],
50+
["punctuation", ")"],
51+
52+
["literal", "#imageLiteral"],
53+
["punctuation", "("],
54+
"resourceName",
55+
["punctuation", ":"],
56+
["string-literal", [
57+
["string", "\"foo\""]
58+
]],
59+
["punctuation", ")"]
60+
]
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
_
2+
3+
----------------------------------------------------
4+
5+
[
6+
["omit", "_"]
7+
]
+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
+ - * / %
2+
+= -= *= /= %=
3+
4+
~ & | ^ << >>
5+
~= &= |= ^= <<= >>=
6+
7+
&+ &- &* &<< &>>
8+
&+= &-= &*= &<<= &>>=
9+
10+
=
11+
== != === !== <= >= < >
12+
! && ||
13+
14+
..< ...
15+
16+
->
17+
18+
??
19+
20+
// custom operators
21+
+++
22+
prefix func +++ (vector: inout Vector2D) -> Vector2D {}
23+
24+
// dot operators (SIMD)
25+
.!= .== .< .> .<= .>=
26+
27+
----------------------------------------------------
28+
29+
[
30+
["operator", "+"],
31+
["operator", "-"],
32+
["operator", "*"],
33+
["operator", "/"],
34+
["operator", "%"],
35+
36+
["operator", "+="],
37+
["operator", "-="],
38+
["operator", "*="],
39+
["operator", "/="],
40+
["operator", "%="],
41+
42+
["operator", "~"],
43+
["operator", "&"],
44+
["operator", "|"],
45+
["operator", "^"],
46+
["operator", "<<"],
47+
["operator", ">>"],
48+
49+
["operator", "~="],
50+
["operator", "&="],
51+
["operator", "|="],
52+
["operator", "^="],
53+
["operator", "<<="],
54+
["operator", ">>="],
55+
56+
["operator", "&+"],
57+
["operator", "&-"],
58+
["operator", "&*"],
59+
["operator", "&<<"],
60+
["operator", "&>>"],
61+
62+
["operator", "&+="],
63+
["operator", "&-="],
64+
["operator", "&*="],
65+
["operator", "&<<="],
66+
["operator", "&>>="],
67+
68+
["operator", "="],
69+
70+
["operator", "=="],
71+
["operator", "!="],
72+
["operator", "==="],
73+
["operator", "!=="],
74+
["operator", "<="],
75+
["operator", ">="],
76+
["operator", "<"],
77+
["operator", ">"],
78+
79+
["operator", "!"],
80+
["operator", "&&"],
81+
["operator", "||"],
82+
83+
["operator", "..<"], ["operator", "..."],
84+
85+
["operator", "->"],
86+
87+
["operator", "??"],
88+
89+
["comment", "// custom operators"],
90+
91+
["operator", "+++"],
92+
93+
["keyword", "prefix"],
94+
["keyword", "func"],
95+
["operator", "+++"],
96+
["punctuation", "("],
97+
"vector",
98+
["punctuation", ":"],
99+
["keyword", "inout"],
100+
["class-name", "Vector2D"],
101+
["punctuation", ")"],
102+
["operator", "->"],
103+
["class-name", "Vector2D"],
104+
["punctuation", "{"],
105+
["punctuation", "}"],
106+
107+
["comment", "// dot operators (SIMD)"],
108+
109+
["operator", ".!="],
110+
["operator", ".=="],
111+
["operator", ".<"],
112+
["operator", ".>"],
113+
["operator", ".<="],
114+
["operator", ".>="]
115+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{ } [ ] ( )
2+
; , . :
3+
\
4+
5+
----------------------------------------------------
6+
7+
[
8+
["punctuation", "{"],
9+
["punctuation", "}"],
10+
["punctuation", "["],
11+
["punctuation", "]"],
12+
["punctuation", "("],
13+
["punctuation", ")"],
14+
15+
["punctuation", ";"],
16+
["punctuation", ","],
17+
["punctuation", "."],
18+
["punctuation", ":"],
19+
20+
["punctuation", "\\"]
21+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
reversedNames = names.sorted(by: { $0 > $1 } )
2+
3+
----------------------------------------------------
4+
5+
[
6+
"reversedNames ",
7+
["operator", "="],
8+
" names",
9+
["punctuation", "."],
10+
["function", "sorted"],
11+
["punctuation", "("],
12+
"by",
13+
["punctuation", ":"],
14+
["punctuation", "{"],
15+
["short-argument", "$0"],
16+
["operator", ">"],
17+
["short-argument", "$1"],
18+
["punctuation", "}"],
19+
["punctuation", ")"]
20+
]

‎tests/languages/swift/string_feature.test

+111-24
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,133 @@
22
"fo\"o"
33
"foo\
44
bar"
5-
"foo \(42)"
6-
"foo \(f("bar"))"
7-
"foo /* comment */ bar"
8-
'foo // bar'
5+
6+
"foo /* not a comment */ bar"
97
"foo\
10-
/* comment */\
8+
/* not a comment */\
119
bar"
1210

11+
let softWrappedQuotation = """
12+
The White Rabbit put on his spectacles. "Where shall I begin, \
13+
please your Majesty?" he asked.
14+
15+
"Begin at the beginning," the King said gravely, "and go on \
16+
till you come to the end; then stop."
17+
"""
18+
19+
let threeMoreDoubleQuotationMarks = #"""
20+
Here are three more double quotes: """
21+
"""#
22+
#"Write an interpolated string in Swift using \(multiplier)."#
23+
24+
25+
"foo \(42)"
26+
"foo \(f("bar"))"
27+
"\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
28+
#"6 times 7 is \#(6 * 7)."#
29+
1330
----------------------------------------------------
1431

1532
[
16-
["string", ["\"\""]],
17-
["string", ["\"fo\\\"o\""]],
18-
["string", ["\"foo\\\r\nbar\""]],
19-
["string", [
20-
"\"foo ",
33+
["string-literal", [
34+
["string", "\"\""]
35+
]],
36+
["string-literal", [
37+
["string", "\"fo\\\"o\""]
38+
]],
39+
["string-literal", [
40+
["string", "\"foo"],
41+
["punctuation", "\\"],
42+
["string", "\r\nbar\""]
43+
]],
44+
45+
["string-literal", [
46+
["string", "\"foo /* not a comment */ bar\""]
47+
]],
48+
["string-literal", [
49+
["string", "\"foo"],
50+
["punctuation", "\\"],
51+
["string", "\r\n/* not a comment */"],
52+
["punctuation", "\\"],
53+
["string", "\r\nbar\""]
54+
]],
55+
56+
["keyword", "let"],
57+
" softWrappedQuotation ",
58+
["operator", "="],
59+
["string-literal", [
60+
["string", "\"\"\"\r\nThe White Rabbit put on his spectacles. \"Where shall I begin, "],
61+
["punctuation", "\\"],
62+
["string", "\r\nplease your Majesty?\" he asked.\r\n\r\n\"Begin at the beginning,\" the King said gravely, \"and go on "],
63+
["punctuation", "\\"],
64+
["string", "\r\ntill you come to the end; then stop.\"\r\n\"\"\""]
65+
]],
66+
67+
["keyword", "let"],
68+
" threeMoreDoubleQuotationMarks ",
69+
["operator", "="],
70+
["string-literal", [
71+
["string", "#\"\"\"\r\nHere are three more double quotes: \"\"\"\r\n\"\"\"#"]
72+
]],
73+
74+
["string-literal", [
75+
["string", "#\"Write an interpolated string in Swift using \\(multiplier).\"#"]
76+
]],
77+
78+
["string-literal", [
79+
["string", "\"foo "],
80+
["interpolation-punctuation", "\\("],
2181
["interpolation", [
22-
["delimiter", "\\("],
23-
["number", "42"],
24-
["delimiter", ")"]
82+
["number", "42"]
2583
]],
26-
"\""
84+
["interpolation-punctuation", ")"],
85+
["string", "\""]
2786
]],
28-
["string", [
29-
"\"foo ",
87+
["string-literal", [
88+
["string", "\"foo "],
89+
["interpolation-punctuation", "\\("],
3090
["interpolation", [
31-
["delimiter", "\\("],
3291
["function", "f"],
3392
["punctuation", "("],
34-
["string", ["\"bar\""]],
93+
["string-literal", [
94+
["string", "\"bar\""]
95+
]],
96+
["punctuation", ")"]
97+
]],
98+
["interpolation-punctuation", ")"],
99+
["string", "\""]
100+
]],
101+
["string-literal", [
102+
["string", "\""],
103+
["interpolation-punctuation", "\\("],
104+
["interpolation", ["multiplier"]],
105+
["interpolation-punctuation", ")"],
106+
["string", " times 2.5 is "],
107+
["interpolation-punctuation", "\\("],
108+
["interpolation", [
109+
["class-name", "Double"],
110+
["punctuation", "("],
111+
"multiplier",
35112
["punctuation", ")"],
36-
["delimiter", ")"]
113+
["operator", "*"],
114+
["number", "2.5"]
37115
]],
38-
"\""
116+
["interpolation-punctuation", ")"],
117+
["string", "\""]
39118
]],
40-
["string", ["\"foo /* comment */ bar\""]],
41-
["string", ["'foo // bar'"]],
42-
["string", ["\"foo\\\r\n/* comment */\\\r\nbar\""]]
119+
["string-literal", [
120+
["string", "#\"6 times 7 is "],
121+
["interpolation-punctuation", "\\#("],
122+
["interpolation", [
123+
["number", "6"],
124+
["operator", "*"],
125+
["number", "7"]
126+
]],
127+
["interpolation-punctuation", ")"],
128+
["string", ".\"#"]
129+
]]
43130
]
44131

45132
----------------------------------------------------
46133

47-
Checks for strings and string interpolation.
134+
Checks for strings and string interpolation.

0 commit comments

Comments
 (0)
Please sign in to comment.