Skip to content

Commit

Permalink
[swift mode] Properly handle empty strings
Browse files Browse the repository at this point in the history
  • Loading branch information
graue authored and marijnh committed Apr 11, 2019
1 parent 97f6acc commit 1c2b083
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
41 changes: 21 additions & 20 deletions mode/swift/swift.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@
stream.match("..")
return "punctuation"
}
if (ch = stream.match(/("{3}|"|')/)) {
var tokenize = tokenString(ch[0])
var stringMatch
if (stringMatch = stream.match(/("""|"|')/)) {
var tokenize = tokenString.bind(null, stringMatch[0])
state.tokenize.push(tokenize)
return tokenize(stream, state)
}
Expand Down Expand Up @@ -115,29 +116,29 @@
}
}

function tokenString(quote) {
var singleLine = quote.length == 1
return function(stream, state) {
var ch, escaped = false
while (ch = stream.next()) {
if (escaped) {
if (ch == "(") {
state.tokenize.push(tokenUntilClosingParen())
return "string"
}
escaped = false
} else if (stream.match(quote)) {
state.tokenize.pop()
function tokenString(openQuote, stream, state) {
var singleLine = openQuote.length == 1
var ch, escaped = false
while (ch = stream.peek()) {
if (escaped) {
stream.next()
if (ch == "(") {
state.tokenize.push(tokenUntilClosingParen())
return "string"
} else {
escaped = ch == "\\"
}
}
if (singleLine) {
escaped = false
} else if (stream.match(openQuote)) {
state.tokenize.pop()
return "string"
} else {
stream.next()
escaped = ch == "\\"
}
return "string"
}
if (singleLine) {
state.tokenize.pop()
}
return "string"
}

function tokenComment(stream, state) {
Expand Down
3 changes: 2 additions & 1 deletion mode/swift/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"[string multi]",
"[string line]",
"[string \"test\"]",
"[string \"\"\"]");
"[string \"\"\"]",
"[variable print][punctuation (][string \"\"][punctuation )]");

// Comments.
MT("comments",
Expand Down

0 comments on commit 1c2b083

Please sign in to comment.