2020-09-19 18:00:50 +02:00
|
|
|
package k
|
|
|
|
|
|
|
|
import (
|
|
|
|
. "github.com/alecthomas/chroma" // nolint
|
|
|
|
"github.com/alecthomas/chroma/lexers/internal"
|
|
|
|
)
|
|
|
|
|
|
|
|
var kotlinIdentifier = "_A-Z\u00c0-\u00d6\u00d8-\u00de\u0100\u0102\u0104\u0106\u0108\u010a\u010c\u010e\u0110\u0112\u0114\u0116\u0118\u011a\u011c\u011e\u0120\u0122\u0124\u0126\u0128\u012a\u012c\u012e\u0130\u0132\u0134\u0136\u0139\u013b\u013d\u013f\u0141\u0143\u0145\u0147\u014a\u014c\u014e\u0150\u0152\u0154\u0156\u0158\u015a\u015c\u015e\u0160\u0162\u0164\u0166\u0168\u016a\u016c\u016e\u0170\u0172\u0174\u0176\u0178-\u0179\u017b\u017d\u0181-\u0182\u0184\u0186-\u0187\u0189-\u018b\u018e-\u0191\u0193-\u0194\u0196-\u0198\u019c-\u019d\u019f-\u01a0\u01a2\u01a4\u01a6-\u01a7\u01a9\u01ac\u01ae-\u01af\u01b1-\u01b3\u01b5\u01b7-\u01b8\u01bc\u01c4\u01c7\u01ca\u01cd\u01cf\u01d1\u01d3\u01d5\u01d7\u01d9\u01db\u01de\u01e0\u01e2\u01e4\u01e6\u01e8\u01ea\u01ec\u01ee\u01f1\u01f4\u01f6-\u01f8\u01fa\u01fc\u01fe\u0200\u0202\u0204\u0206\u0208\u020a\u020c\u020e\u0210\u0212\u0214\u0216\u0218\u021a\u021c\u021e\u0220\u0222\u0224\u0226\u0228\u022a\u022c\u022e\u0230\u0232\u023a-\u023b\u023d-\u023e\u0241\u0243-\u0246\u0248\u024a\u024c\u024e\u0370\u0372\u0376\u0386\u0388-\u038a\u038c\u038e-\u038f\u0391-\u03a1\u03a3-\u03ab\u03cf\u03d2-\u03d4\u03d8\u03da\u03dc\u03de\u03e0\u03e2\u03e4\u03e6\u03e8\u03ea\u03ec\u03ee\u03f4\u03f7\u03f9-\u03fa\u03fd-\u042f\u0460\u0462\u0464\u0466\u0468\u046a\u046c\u046e\u0470\u0472\u0474\u0476\u0478\u047a\u047c\u047e\u0480\u048a\u048c\u048e\u0490\u0492\u0494\u0496\u0498\u049a\u049c\u049e\u04a0\u04a2\u04a4\u04a6\u04a8\u04aa\u04ac\u04ae\u04b0\u04b2\u04b4\u04b6\u04b8\u04ba\u04bc\u04be\u04c0-\u04c1\u04c3\u04c5\u04c7\u04c9\u04cb\u04cd\u04d0\u04d2\u04d4\u04d6\u04d8\u04da\u04dc\u04de\u04e0\u04e2\u04e4\u04e6\u04e8\u04ea\u04ec\u04ee\u04f0\u04f2\u04f4\u04f6\u04f8\u04fa\u04fc\u04fe\u0500\u0502\u0504\u0506\u0508\u050a\u050c\u050e\u0510\u0512\u0514\u0516\u0518\u051a\u051c\u051e\u0520\u0522\u0524\u0526\u0531-\u0556\u10a0-\u10c5\u10c7\u10cd\u1e00\u1e02\u1e04\u1e06\u1e08\u1e0a\u1e0c\u1e0e\u1e10\u1e12\u1e14\u1e16\u1e18\u1e1a\u1e1c\u1e1e\u1e20\u1e22\u1e24\u1e26\u1e28\u1e2a\u1e2c\u1e2e\u1e30\u1e32\u1e34\u1e36\u1e38\u1e3a\u1e3c\u1e3e\u1e40\u1e42\u1e44\u1e46\u1e48\u1e4a\u1e4c\u1e4e\u1e50\u1e52\u1e54\u1e56\u1e58\u1e5a\u1e5c\u1e5e\u1e60\u1e62\u1e64\u1e66\u1e68\u1e6a\u1e6c\u1e6e\u1e70\u1e72\u1e74\u1e76\u1e78\u1e7a\u1e7c\u1e7e\u1e80\u1e82\u1e84\u1e86\u1e88\u1e8a\u1e8c\u1e8e\u1e90\u1e92\u1e94\u1e9e\u1ea0\u1ea2\u1ea4\u1ea6\u1ea8\u1eaa\u1eac\u1eae\u1eb0\u1eb2\u1eb4\u1eb6\u1eb8\u1eba\u1ebc\u1ebe\u1ec0\u1ec2\u1ec4\u1ec6\u1ec8\u1eca\u1ecc\u1ece\u1ed0\u1ed2\u1ed4\u1ed6\u1ed8\u1eda\u1edc\u1ede\u1ee0\u1ee2\u1ee4\u1ee6\u1ee8\u1eea\u1eec\u1eee\u1ef0\u1ef2\u1ef4\u1ef6\u1ef8\u1efa\u1efc\u1efe\u1f08-\u1f0f\u1f18-\u1f1d\u1f28-\u1f2f\u1f38-\u1f3f\u1f48-\u1f4d\u1f59\u1f5b\u1f5d\u1f5f\u1f68-\u1f6f\u1fb8-\u1fbb\u1fc8-\u1fcb\u1fd8-\u1fdb\u1fe8-\u1fec\u1ff8-\u1ffb\u2102\u2107\u210b-\u210d\u2110-\u2112\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u2130-\u2133\u213e-\u213f\u2145\u2183\u2c00-\u2c2e\u2c60\u2c62-\u2c64\u2c67\u2c69\u2c6b\u2c6d-\u2c70\u2c72\u2c75\u2c7e-\u2c80\u2c82\u2c84\u2c86\u2c88\u2c8a\u2c8c\u2c8e\u2c90\u2c92\u2c94\u2c96\u2c98\u2c9a\u2c9c\u2c9e\u2ca0\u2ca2\u2ca4\u2ca6\u2ca8\u2caa\u2cac\u2cae\u2cb0\u2cb2\u2cb4\u2cb6\u2cb8\u2cba\u2cbc\u2cbe\u2cc0\u2cc2\u2cc4\u2cc6\u2cc8\u2cca\u2ccc\u2cce\u2cd0\u2cd2\u2cd4\u2cd6\u2cd8\u2cda\u2cdc\u2cde\u2ce0\u2ce2\u2ceb\u2ced\u2cf2\ua640\ua642\ua644\ua646\ua648\ua64a\ua64c\ua64e\ua650\ua652\ua654\ua656\ua658\ua65a\ua65c\ua65e\ua660\ua662\ua664\ua666\ua668\ua66a\ua66c\ua680\ua682\ua684\ua686\ua688\ua68a\ua68c\ua68e\ua690\ua692\ua694\ua696\ua722\ua724\ua726\ua728\ua72a\ua72c\ua72e\ua732\ua734\ua736\ua738\ua73a\ua73c\ua73e\ua740\ua742\ua744\ua746\ua748\ua74a\ua74c\ua74e\ua750\ua752\ua754\ua756\ua758\ua75a\ua75c\ua75e\ua760\ua762\ua764\ua766\ua768\ua76a\ua76c\ua76e\ua779\ua77b\ua77d-\ua77e\ua780\ua782\ua784\ua786\ua78b\ua78d\ua790\ua792\ua7a0\ua7a2\ua7a4\ua7a6\ua7a8\ua7aa\uff21-\uff3aa-z\u00b5\u00df-\u00f6\u00f8-\u00ff\u0101\u0103\u0105\u0107\u0109\u010b\u010d\u010f\u0111\u0113\u0115\u0117\u0119\u011b\u011d\u011f\u0121\u0123\u0125\u0127\u0129\u012b\u012d\u012f\u0131\u0133\u0135\u0137-\u0138\u013a\u013c\u013e\u0140\u0142\u0144\u0146\
|
|
|
|
|
|
|
|
// Kotlin lexer.
|
|
|
|
var Kotlin = internal.Register(MustNewLexer(
|
|
|
|
&Config{
|
|
|
|
Name: "Kotlin",
|
|
|
|
Aliases: []string{"kotlin"},
|
|
|
|
Filenames: []string{"*.kt"},
|
|
|
|
MimeTypes: []string{"text/x-kotlin"},
|
|
|
|
DotAll: true,
|
|
|
|
},
|
|
|
|
Rules{
|
|
|
|
"root": {
|
|
|
|
{`^\s*\[.*?\]`, NameAttribute, nil},
|
|
|
|
{`[^\S\n]+`, Text, nil},
|
|
|
|
{`\\\n`, Text, nil},
|
|
|
|
{`//[^\n]*\n?`, CommentSingle, nil},
|
|
|
|
{`/[*].*?[*]/`, CommentMultiline, nil},
|
|
|
|
{`\n`, Text, nil},
|
2020-11-09 16:25:54 +01:00
|
|
|
{`!==|!in|!is|===`, Operator, nil},
|
|
|
|
{`%=|&&|\*=|\+\+|\+=|--|-=|->|\.\.|\/=|::|<=|==|>=|!!|!=|\|\||\?[:.]`, Operator, nil},
|
|
|
|
{`[~!%^&*()+=|\[\]:;,.<>\/?-]`, Punctuation, nil},
|
2020-09-19 18:00:50 +02:00
|
|
|
{`[{}]`, Punctuation, nil},
|
2020-11-09 16:25:54 +01:00
|
|
|
{`"""`, LiteralString, Push("rawstring")},
|
|
|
|
{`"`, LiteralStringDouble, Push("string")},
|
|
|
|
{`(')(\\u[0-9a-fA-F]{4})(')`, ByGroups(LiteralStringChar, LiteralStringEscape, LiteralStringChar), nil},
|
2020-09-19 18:00:50 +02:00
|
|
|
{`'\\.'|'[^\\]'`, LiteralStringChar, nil},
|
|
|
|
{`0[xX][0-9a-fA-F]+[Uu]?[Ll]?|[0-9]+(\.[0-9]*)?([eE][+-][0-9]+)?[fF]?[Uu]?[Ll]?`, LiteralNumber, nil},
|
|
|
|
{`(companion)(\s+)(object)`, ByGroups(Keyword, Text, Keyword), nil},
|
|
|
|
{`(class|interface|object)(\s+)`, ByGroups(Keyword, Text), Push("class")},
|
|
|
|
{`(package|import)(\s+)`, ByGroups(Keyword, Text), Push("package")},
|
|
|
|
{`(val|var)(\s+)`, ByGroups(Keyword, Text), Push("property")},
|
2020-11-09 16:25:54 +01:00
|
|
|
{`(fun)(\s+)`, ByGroups(Keyword, Text), Push("function")},
|
|
|
|
{`(abstract|actual|annotation|as|as\?|break|by|catch|class|companion|const|constructor|continue|crossinline|data|delegate|do|dynamic|else|enum|expect|external|false|field|file|final|finally|for|fun|get|if|import|in|infix|init|inline|inner|interface|internal|is|it|lateinit|noinline|null|object|open|operator|out|override|package|param|private|property|protected|public|receiver|reified|return|sealed|set|setparam|super|suspend|tailrec|this|throw|true|try|typealias|typeof|val|var|vararg|when|where|while)\b`, Keyword, nil},
|
|
|
|
{`@[` + kotlinIdentifier + `]+`, NameDecorator, nil},
|
|
|
|
{`[` + kotlinIdentifier + `]+`, Name, nil},
|
2020-09-19 18:00:50 +02:00
|
|
|
},
|
|
|
|
"package": {
|
|
|
|
{`\S+`, NameNamespace, Pop(1)},
|
|
|
|
},
|
|
|
|
"class": {
|
2020-11-09 16:25:54 +01:00
|
|
|
// \x60 is the back tick character (`)
|
|
|
|
{`\x60[^\x60]+?\x60`, NameClass, Pop(1)},
|
|
|
|
{`[` + kotlinIdentifier + `]+`, NameClass, Pop(1)},
|
2020-09-19 18:00:50 +02:00
|
|
|
},
|
|
|
|
"property": {
|
2020-11-09 16:25:54 +01:00
|
|
|
{`\x60[^\x60]+?\x60`, NameProperty, Pop(1)},
|
|
|
|
{`[` + kotlinIdentifier + `]+`, NameProperty, Pop(1)},
|
|
|
|
},
|
|
|
|
"generics-specification": {
|
|
|
|
{`<`, Punctuation, Push("generics-specification")}, // required for generics inside generics e.g. <T : List<Int> >
|
|
|
|
{`>`, Punctuation, Pop(1)},
|
|
|
|
{`[,:*?]`, Punctuation, nil},
|
|
|
|
{`(in|out|reified)`, Keyword, nil},
|
|
|
|
{`\x60[^\x60]+?\x60`, NameClass, nil},
|
|
|
|
{`[` + kotlinIdentifier + `]+`, NameClass, nil},
|
|
|
|
{`\s+`, Text, nil},
|
2020-09-19 18:00:50 +02:00
|
|
|
},
|
|
|
|
"function": {
|
2020-11-09 16:25:54 +01:00
|
|
|
{`<`, Punctuation, Push("generics-specification")},
|
|
|
|
{`\x60[^\x60]+?\x60`, NameFunction, Pop(1)},
|
|
|
|
{`[` + kotlinIdentifier + `]+`, NameFunction, Pop(1)},
|
|
|
|
{`\s+`, Text, nil},
|
|
|
|
},
|
|
|
|
"rawstring": {
|
|
|
|
// raw strings don't allow character escaping
|
|
|
|
{`"""`, LiteralString, Pop(1)},
|
|
|
|
{`(?:[^$"]+|\"{1,2}[^"])+`, LiteralString, nil},
|
|
|
|
Include("string-interpol"),
|
|
|
|
// remaining dollar signs are just a string
|
|
|
|
{`\$`, LiteralString, nil},
|
|
|
|
},
|
|
|
|
"string": {
|
|
|
|
{`\\[tbnr'"\\\$]`, LiteralStringEscape, nil},
|
|
|
|
{`\\u[0-9a-fA-F]{4}`, LiteralStringEscape, nil},
|
|
|
|
{`"`, LiteralStringDouble, Pop(1)},
|
|
|
|
Include("string-interpol"),
|
|
|
|
{`[^\n\\"$]+`, LiteralStringDouble, nil},
|
|
|
|
// remaining dollar signs are just a string
|
|
|
|
{`\$`, LiteralStringDouble, nil},
|
|
|
|
},
|
|
|
|
"string-interpol": {
|
|
|
|
{`\$[` + kotlinIdentifier + `]+`, LiteralStringInterpol, nil},
|
|
|
|
{`\${[^}\n]*}`, LiteralStringInterpol, nil},
|
2020-09-19 18:00:50 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
))
|