Skip to content

Commit 1c2b083

Browse files
grauemarijnh
authored andcommitted
[swift mode] Properly handle empty strings
1 parent 97f6acc commit 1c2b083

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

mode/swift/swift.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@
7373
stream.match("..")
7474
return "punctuation"
7575
}
76-
if (ch = stream.match(/("{3}|"|')/)) {
77-
var tokenize = tokenString(ch[0])
76+
var stringMatch
77+
if (stringMatch = stream.match(/("""|"|')/)) {
78+
var tokenize = tokenString.bind(null, stringMatch[0])
7879
state.tokenize.push(tokenize)
7980
return tokenize(stream, state)
8081
}
@@ -115,29 +116,29 @@
115116
}
116117
}
117118

118-
function tokenString(quote) {
119-
var singleLine = quote.length == 1
120-
return function(stream, state) {
121-
var ch, escaped = false
122-
while (ch = stream.next()) {
123-
if (escaped) {
124-
if (ch == "(") {
125-
state.tokenize.push(tokenUntilClosingParen())
126-
return "string"
127-
}
128-
escaped = false
129-
} else if (stream.match(quote)) {
130-
state.tokenize.pop()
119+
function tokenString(openQuote, stream, state) {
120+
var singleLine = openQuote.length == 1
121+
var ch, escaped = false
122+
while (ch = stream.peek()) {
123+
if (escaped) {
124+
stream.next()
125+
if (ch == "(") {
126+
state.tokenize.push(tokenUntilClosingParen())
131127
return "string"
132-
} else {
133-
escaped = ch == "\\"
134128
}
135-
}
136-
if (singleLine) {
129+
escaped = false
130+
} else if (stream.match(openQuote)) {
137131
state.tokenize.pop()
132+
return "string"
133+
} else {
134+
stream.next()
135+
escaped = ch == "\\"
138136
}
139-
return "string"
140137
}
138+
if (singleLine) {
139+
state.tokenize.pop()
140+
}
141+
return "string"
141142
}
142143

143144
function tokenComment(stream, state) {

mode/swift/test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"[string multi]",
4141
"[string line]",
4242
"[string \"test\"]",
43-
"[string \"\"\"]");
43+
"[string \"\"\"]",
44+
"[variable print][punctuation (][string \"\"][punctuation )]");
4445

4546
// Comments.
4647
MT("comments",

0 commit comments

Comments
 (0)