Skip to content

Commit

Permalink
Swift: Add support for string interpolation. Fix #448
Browse files Browse the repository at this point in the history
  • Loading branch information
Golmote committed Sep 5, 2015
1 parent 85ed18f commit 89cd5d0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
18 changes: 17 additions & 1 deletion components/prism-swift.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
// issues: nested multiline comments, highlighting inside string interpolations
// issues: nested multiline comments
Prism.languages.swift = Prism.languages.extend('clike', {
'string': {
pattern: /("|')(\\(?:\((?:[^()]|\([^)]+\))+\)|\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
inside: {
'interpolation': {
pattern: /\\\((?:[^()]|\([^)]+\))+\)/,
inside: {
delimiter: {
pattern: /^\\\(|\)$/,
alias: 'variable'
}
// See rest below
}
}
}
},
'keyword': /\b(as|associativity|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|nonmutating|operator|optional|override|postfix|precedence|prefix|private|Protocol|public|repeat|required|rethrows|return|right|safe|self|Self|set|static|struct|subscript|super|switch|throws?|try|Type|typealias|unowned|unsafe|var|weak|where|while|willSet|__(?:COLUMN__|FILE__|FUNCTION__|LINE__))\b/,
'number': /\b([\d_]+(\.[\de_]+)?|0x[a-f0-9_]+(\.[a-f0-9p_]+)?|0b[01_]+|0o[0-7_]+)\b/i,
'constant': /\b(nil|[A-Z_]{2,}|k[A-Z][A-Za-z_]+)\b/,
'atrule': /@\b(IB(?:Outlet|Designable|Action|Inspectable)|class_protocol|exported|noreturn|NS(?:Copying|Managed)|objc|UIApplicationMain|auto_closure)\b/,
'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/
});
Prism.languages.swift['string'].inside['interpolation'].inside.rest = Prism.util.clone(Prism.languages.swift);
2 changes: 1 addition & 1 deletion components/prism-swift.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion examples/prism-swift.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ <h2>Numbers</h2>

<h2>Strings</h2>
<pre><code>let someString = "Some string literal value"
var emptyString = ""</code></pre>
var emptyString = ""
// String interpolation
let multiplier = 3
"\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"</code></pre>

<h2>Control flow</h2>
<pre><code>for index in 1...5 {
Expand Down

0 comments on commit 89cd5d0

Please sign in to comment.