Skip to content

Commit

Permalink
[swift mode] Support multi-line strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dierksen authored and marijnh committed Nov 20, 2018
1 parent 606498c commit 3619ae2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
11 changes: 11 additions & 0 deletions mode/swift/index.html
Expand Up @@ -71,6 +71,17 @@ <h2>Swift mode</h2>

}

func funWithStrings() {
var numLines = 3
print("This is a string!")
print("""
This is a
multi-line
string!
""")
print("The preceding string had \(numLines) lines!")
}

}
</textarea></form>

Expand Down
15 changes: 9 additions & 6 deletions mode/swift/swift.js
Expand Up @@ -73,9 +73,8 @@
stream.match("..")
return "punctuation"
}
if (ch == '"' || ch == "'") {
stream.next()
var tokenize = tokenString(ch)
if (ch = stream.match(/("{3}|"|')/)) {
var tokenize = tokenString(ch[0])
state.tokenize.push(tokenize)
return tokenize(stream, state)
}
Expand Down Expand Up @@ -117,6 +116,7 @@
}

function tokenString(quote) {
var singleLine = quote.length == 1
return function(stream, state) {
var ch, escaped = false
while (ch = stream.next()) {
Expand All @@ -126,13 +126,16 @@
return "string"
}
escaped = false
} else if (ch == quote) {
break
} else if (stream.match(quote)) {
state.tokenize.pop()
return "string"
} else {
escaped = ch == "\\"
}
}
state.tokenize.pop()
if (singleLine) {
state.tokenize.pop()
}
return "string"
}
}
Expand Down
7 changes: 6 additions & 1 deletion mode/swift/test.js
Expand Up @@ -35,7 +35,12 @@
// Strings and string interpolation.
MT("strings",
"[keyword var] [def a][punctuation :] [variable-2 String] [operator =] [string \"test\"]",
"[keyword var] [def b][punctuation :] [variable-2 String] [operator =] [string \"\\(][variable a][string )\"]");
"[keyword var] [def b][punctuation :] [variable-2 String] [operator =] [string \"\\(][variable a][string )\"]",
"[keyword var] [def c] [operator =] [string \"\"\"]",
"[string multi]",
"[string line]",
"[string \"test\"]",
"[string \"\"\"]");

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

0 comments on commit 3619ae2

Please sign in to comment.