Skip to content
Permalink
Browse files
Kotlin: Added char token and improved string interpolation (#3225)
  • Loading branch information
RunDevelopment committed Dec 5, 2021
1 parent 6b168a3 commit 563cd73ea232e2100fea9233956c1564d6dc5214
@@ -22,47 +22,67 @@

delete Prism.languages.kotlin['class-name'];

var interpolationInside = {
'interpolation-punctuation': {
pattern: /^\$\{?|\}$/,
alias: 'punctuation'
},
'expression': {
pattern: /[\s\S]+/,
inside: Prism.languages.kotlin
}
};

Prism.languages.insertBefore('kotlin', 'string', {
'raw-string': {
pattern: /("""|''')[\s\S]*?\1/,
alias: 'string'
// See interpolation below
// https://kotlinlang.org/spec/expressions.html#string-interpolation-expressions
'string-literal': [
{
pattern: /"""(?:[^$]|\$(?:(?!\{)|\{[^{}]*\}))*?"""/,
alias: 'multiline',
inside: {
'interpolation': {
pattern: /\$(?:[a-z_]\w*|\{[^{}]*\})/i,
inside: interpolationInside
},
'string': /[\s\S]+/
}
},
{
pattern: /"(?:[^"\\\r\n$]|\\.|\$(?:(?!\{)|\{[^{}]*\}))*"/,
alias: 'singleline',
inside: {
'interpolation': {
pattern: /((?:^|[^\\])(?:\\{2})*)\$(?:[a-z_]\w*|\{[^{}]*\})/i,
lookbehind: true,
inside: interpolationInside
},
'string': /[\s\S]+/
}
}
],
'char': {
// https://kotlinlang.org/spec/expressions.html#character-literals
pattern: /'(?:[^'\\\r\n]|\\(?:.|u[a-fA-F0-9]{0,4}))'/,
greedy: true
}
});

delete Prism.languages.kotlin['string'];

Prism.languages.insertBefore('kotlin', 'keyword', {
'annotation': {
pattern: /\B@(?:\w+:)?(?:[A-Z]\w*|\[[^\]]+\])/,
alias: 'builtin'
}
});

Prism.languages.insertBefore('kotlin', 'function', {
'label': {
pattern: /\b\w+@|@\w+\b/,
alias: 'symbol'
}
});

var interpolation = [
{
pattern: /\$\{[^}]+\}/,
inside: {
'delimiter': {
pattern: /^\$\{|\}$/,
alias: 'variable'
},
rest: Prism.languages.kotlin
}
},
{
pattern: /\$\w+/,
alias: 'variable'
}
];

Prism.languages.kotlin['string'].inside = Prism.languages.kotlin['raw-string'].inside = {
interpolation: interpolation
};

Prism.languages.kt = Prism.languages.kotlin;
Prism.languages.kts = Prism.languages.kotlin;
}(Prism));

Some generated files are not rendered by default. Learn more.

@@ -0,0 +1,11 @@
'a'
'\n'
'\uFFFF'

----------------------------------------------------

[
["char", "'a'"],
["char", "'\\n'"],
["char", "'\\uFFFF'"]
]

This file was deleted.

This file was deleted.

@@ -0,0 +1,113 @@
""
"foo"
"\"\""

""""""
"""Foo "bar"" baz"""
"""
"Foo"
bar
"""

// interpolation

"$foo ${bar} ${'$'} ${foobar()}"
"""
$foo ${bar}
${'$'} ${foobar()}
"""

----------------------------------------------------

[
["string-literal", [
["string", "\"\""]
]],
["string-literal", [
["string", "\"foo\""]
]],
["string-literal", [
["string", "\"\\\"\\\"\""]
]],

["string-literal", [
["string", "\"\"\"\"\"\""]
]],
["string-literal", [
["string", "\"\"\"Foo \"bar\"\" baz\"\"\""]
]],
["string-literal", [
["string", "\"\"\"\r\n\"Foo\"\r\nbar\r\n\"\"\""]
]],

["comment", "// interpolation"],

["string-literal", [
["string", "\""],
["interpolation", [
["interpolation-punctuation", "$"],
["expression", ["foo"]]
]],
["string", " "],
["interpolation", [
["interpolation-punctuation", "${"],
["expression", ["bar"]],
["interpolation-punctuation", "}"]
]],
["string", " "],
["interpolation", [
["interpolation-punctuation", "${"],
["expression", [
["char", "'$'"]
]],
["interpolation-punctuation", "}"]
]],
["string", " "],
["interpolation", [
["interpolation-punctuation", "${"],
["expression", [
["function", "foobar"],
["punctuation", "("],
["punctuation", ")"]
]],
["interpolation-punctuation", "}"]
]],
["string", "\""]
]],
["string-literal", [
["string", "\"\"\"\r\n"],
["interpolation", [
["interpolation-punctuation", "$"],
["expression", ["foo"]]
]],
["string", " "],
["interpolation", [
["interpolation-punctuation", "${"],
["expression", ["bar"]],
["interpolation-punctuation", "}"]
]],
["string", "\r\n"],
["interpolation", [
["interpolation-punctuation", "${"],
["expression", [
["char", "'$'"]
]],
["interpolation-punctuation", "}"]
]],
["string", " "],
["interpolation", [
["interpolation-punctuation", "${"],
["expression", [
["function", "foobar"],
["punctuation", "("],
["punctuation", ")"]
]],
["interpolation-punctuation", "}"]
]],
["string", "\r\n\"\"\""]
]]
]

----------------------------------------------------

Checks for raw strings.

0 comments on commit 563cd73

Please sign in to comment.