File: kotlin.go

package info (click to toggle)
golang-github-alecthomas-chroma 0.10.0-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,652 kB
  • sloc: python: 426; javascript: 79; makefile: 34; sh: 32
file content (98 lines) | stat: -rw-r--r-- 3,822 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package k

import (
	. "github.com/alecthomas/chroma" // nolint
	"github.com/alecthomas/chroma/lexers/internal"
)

// Kotlin lexer.
var Kotlin = internal.Register(MustNewLazyLexer(
	&Config{
		Name:      "Kotlin",
		Aliases:   []string{"kotlin"},
		Filenames: []string{"*.kt"},
		MimeTypes: []string{"text/x-kotlin"},
		DotAll:    true,
	},
	kotlinRules,
))

func kotlinRules() Rules {
	const kotlinIdentifier = "(?:[_\\p{L}][\\p{L}\\p{N}]*|`@?[_\\p{L}][\\p{L}\\p{N}]+`)"

	return Rules{
		"root": {
			{`^\s*\[.*?\]`, NameAttribute, nil},
			{`[^\S\n]+`, Text, nil},
			{`\\\n`, Text, nil},
			{`//[^\n]*\n?`, CommentSingle, nil},
			{`/[*].*?[*]/`, CommentMultiline, nil},
			{`\n`, Text, nil},
			{`!==|!in|!is|===`, Operator, nil},
			{`%=|&&|\*=|\+\+|\+=|--|-=|->|\.\.|\/=|::|<=|==|>=|!!|!=|\|\||\?[:.]`, Operator, nil},
			{`[~!%^&*()+=|\[\]:;,.<>\/?-]`, Punctuation, nil},
			{`[{}]`, Punctuation, nil},
			{`"""`, LiteralString, Push("rawstring")},
			{`"`, LiteralStringDouble, Push("string")},
			{`(')(\\u[0-9a-fA-F]{4})(')`, ByGroups(LiteralStringChar, LiteralStringEscape, LiteralStringChar), nil},
			{`'\\.'|'[^\\]'`, 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")},
			{`(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},
		},
		"package": {
			{`\S+`, NameNamespace, Pop(1)},
		},
		"class": {
			// \x60 is the back tick character (`)
			{`\x60[^\x60]+?\x60`, NameClass, Pop(1)},
			{kotlinIdentifier, NameClass, Pop(1)},
		},
		"property": {
			{`\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},
		},
		"function": {
			{`<`, 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},
		},
	}
}