From eb7b8d986b04bd02858a4af728d656d5901cd532 Mon Sep 17 00:00:00 2001 From: Fela Date: Sat, 31 Oct 2026 13:20:35 +0400 Subject: [PATCH] Replace Prism's Kotlin grammar with one of our own --- components/prism-kotlin.js | 220 +++++++++++++++++++++++++++++++++++++ generate.js | 1 + tools/corpus.txt | 2 + 3 files changed, 223 insertions(+) create mode 100644 components/prism-kotlin.js diff --git a/components/prism-kotlin.js b/components/prism-kotlin.js new file mode 100644 index 0000000..53ce2b8 --- /dev/null +++ b/components/prism-kotlin.js @@ -0,0 +1,220 @@ +/** + * Kotlin, replacing Prism's grammar. Tokens are matched in the order they are declared, and a + * lookbehind or lookahead only sees the text no earlier token has taken, so the order below is + * what makes the context rules work: names of types are found before keywords take the + * keyword in front of them, and modifiers before the declaration keyword after them. + */ +(function (Prism) { + // Completes a pattern that refers to itself through , nesting it depth times. + function nested(pattern, depth) { + for (var i = 0; i < depth; i++) { + pattern = pattern.replace(//g, function () { return '(?:' + pattern + ')'; }); + } + return pattern.replace(//g, '[^\\s\\S]'); + } + + function re(pattern) { + return RegExp(pattern.replace(/<(\w+)>/g, function (m, name) { return parts[name]; })); + } + + var parts = {}; + + // Kotlin's block comments nest; two levels inside the outer one cover real code. + parts.block = nested(/\/\*(?:[^*/]|\*(?!\/)|\/(?!\*)|)*\*\//.source, 2); + + // The text of a line before a comment starts: code, and strings, characters, backticked + // names and block comments that close on it. A "//" or "/*" inside one of those is not a + // comment, which matching comments first would otherwise make it. Lazy, or it would take + // in the first of two comments on a line and match the second. + parts.code = re(/(?:[^"'`\/\r\n]|\/(?![\/*])|"(?:\\.|[^"\\\r\n])*"|'(?:\\.|[^'\\\r\n])*'|`[^`\r\n]*`|)*?/.source).source; + + // a template's braces, which may hold one more level of braces, a lambda's + parts.braces = /\{(?:[^{}]|\{[^{}]*\})*\}/.source; + + // the package and the outer classes a type may be qualified by + parts.qualifier = /(?:[a-z]\w*\s*\.\s*)*(?:[A-Z]\w*\s*\.\s*)*/.source; + + // type arguments in a call, nested twice; a function type in them is not recognised + parts.arguments = /<(?:[\w\s,.?*]|<(?:[\w\s,.?*]|<[\w\s,.?*]*>)*>)*>/.source; + + parts.call = re(/\s*(?:)?\s*\(/.source).source; + + var modifiers = 'abstract|actual|annotation|companion|const|data|enum|expect|external|final|infix|inline|inner|internal|open|operator|override|private|protected|public|sealed|value'; + + var type = { + 'namespace': { + pattern: /^[a-z]\w*(?:\s*\.\s*[a-z]\w*)*(?:\s*\.)?/, + inside: { + 'punctuation': /\./ + } + }, + 'punctuation': /\./ + }; + + var interpolation = { + 'interpolation-punctuation': { + pattern: /^\$\{?|\}$/, + alias: 'punctuation' + }, + 'expression': { + pattern: /[\s\S]+/ + } + }; + + var kotlin = { + // Not greedy: each match is searched for in the text after the comment before it, so "^" + // is a place outside any string, which a line start may not be. + 'comment': [ + { + pattern: re(/((?:^|[\r\n]))/.source), + lookbehind: true + }, + { + pattern: re(/((?:^|[\r\n]))\/\/.*/.source), + lookbehind: true + } + ], + 'string-literal': [ + { + // a raw string may end in quotes of its own: """a"""" holds a" + pattern: re(/"""(?:[^$]|\$(?!\{)|\$)*?"""(?!")/.source), + alias: 'multiline', + greedy: true, + inside: { + 'interpolation': { + pattern: re(/\$(?:[a-zA-Z_]\w*|)/.source), + inside: interpolation + }, + 'string': /[\s\S]+/ + } + }, + { + pattern: re(/"(?:[^"\\\r\n$]|\\.|\$(?!\{)|\$)*"/.source), + alias: 'singleline', + greedy: true, + inside: { + 'interpolation': { + pattern: re(/((?:^|[^\\])(?:\\{2})*)\$(?:[a-zA-Z_]\w*|)/.source), + lookbehind: true, + inside: interpolation + }, + 'string': /[\s\S]+/ + } + } + ], + 'char': { + pattern: /'(?:[^'\\\r\n]|\\(?:.|u[a-fA-F0-9]{0,4}))'/, + greedy: true + }, + 'annotation': { + // "@get:Inject", "@file:JvmName", "@kotlin.jvm.JvmStatic", "@field:[A B]" + pattern: /\B@(?:\w+:)?(?:(?:[a-z]\w*\.)*[A-Z]\w*|\[[^\]]+\])/, + alias: 'builtin' + }, + 'label': [ + { + // "loop@ for", and not "return@forEach" + pattern: /\b\w+@(?!\w)/, + alias: 'symbol' + }, + { + pattern: /(\b(?:break|continue|return|super|this))@\w+/, + lookbehind: true, + alias: 'symbol' + } + ], + 'class-name': [ + { + // the name a declaration, a type check or a cast gives + pattern: re(/(\b(?:as\??|class|interface|is|object|typealias)\s+)[A-Z]\w*\b/.source), + lookbehind: true, + inside: type + }, + { + // A type annotation, a supertype or a bound. A colon after another one or after + // "?" is "::" or the Elvis operator, which no type follows. + pattern: re(/((?:^|[^:?]):\s*)[A-Z]\w*\b/.source), + lookbehind: true, + inside: type + } + ], + 'keyword': [ + { + // a modifier is one only in front of what it modifies; "data" and "open" are + // names everywhere else + pattern: RegExp('(^|[^.])\\b(?:' + modifiers + ')\\b(?=\\s+(?:@|(?:' + modifiers + '|class|constructor|fun|get|init|interface|lateinit|object|set|suspend|tailrec|typealias|val|var)\\b))'), + lookbehind: true + }, + { + pattern: /(^|[^.])\b(?:as|break|by|catch|class|continue|crossinline|do|else|finally|for|fun|if|import|in|interface|is|lateinit|noinline|null|object|package|reified|return|super|suspend|tailrec|this|throw|try|typealias|val|var|vararg|when|where|while)\b/, + lookbehind: true + }, + { + // variance, "Array"; a parameter or a variable named "out" is common + pattern: /([<,]\s*)out\b(?=\s+[\w*@])/, + lookbehind: true + }, + { + pattern: /(^|[^.])\b(?:init(?=\s*\{)|constructor(?=\s*\())/, + lookbehind: true + }, + { + // property accessors: "get() = x", "set(value) {", and "private set" ending its line + pattern: /(^|[^.\w])(?:get|set)(?=[ \t]*(?:[\r\n]|$|\(\s*\w*\s*(?::[^()=]*)?\)\s*(?:[{:]|=(?!=))))/, + lookbehind: true + } + ], + 'boolean': /\b(?:false|true)\b/, + 'function': [ + { + pattern: re(/`[^\r\n`]+`(?=|\s*\{)/.source), + greedy: true + }, + re(/\b\w+(?=)/.source), + { + // a call with a trailing lambda: "list.map {", "remember {" + pattern: /(^|[^\w])[a-z_]\w*(?=\s*\{)/, + lookbehind: true + }, + { + // a reference, "::println" + pattern: /(::\s*)[a-z_]\w*/, + lookbehind: true + } + ], + // After 'function', which has to see a call's type arguments before their names are + // taken, and which makes a constructor or a composable call a function. + 'type-name': { + // a name capitalised and with a lower-case letter + pattern: re(/(^|[^\w.])[A-Z](?:[\d_A-Z]*[a-z]\w*)?\b/.source), + lookbehind: true, + inside: type, + alias: 'class-name' + }, + 'number': [ + { + // ".5f", first so its digits are not taken alone; not the second dot of "0..5" + pattern: /(^|[^\w.])\.\d+(?:_+\d+)*(?:[eE][+-]?\d+(?:_+\d+)*)?[fF]?\b/, + lookbehind: true + }, + /\b0[xX][\da-fA-F]+(?:_+[\da-fA-F]+)*(?:[uU]L?|L)?\b|\b0[bB][01]+(?:_+[01]+)*(?:[uU]L?|L)?\b|\b\d+(?:_+\d+)*(?:\.\d+(?:_+\d+)*)?(?:[eE][+-]?\d+(?:_+\d+)*)?(?:[fF]|[uU]L?|L)?\b/ + ], + 'constant': /\b[A-Z][A-Z_\d]+\b/, + 'operator': [ + { + // Infix functions, only between two operands: "val or = 1" names a variable. An + // operand may already be a token, a string or a number, so the text may end there. + pattern: /((?:^|[\w)\]}])[ \t]+)(?:and|downTo|or|shl|shr|step|to|until|ushr|xor)(?=[ \t]+(?:[\w"'(\[{@-]|$))/, + lookbehind: true + }, + /\+[+=]?|-[-=>]?|==?=?|!(?:!|==?)?|[\/*%<>]=?|[?:]:?|\.\. { if (unsupported.includes(lng) || !components.languages[lng]) { diff --git a/tools/corpus.txt b/tools/corpus.txt index 787bdc1..3eeaf77 100644 --- a/tools/corpus.txt +++ b/tools/corpus.txt @@ -17,3 +17,5 @@ key = value ; ini @@@ -1 -1 +1 @@@ int f(void) --- /dev/null +++ b/sub.d/Makefile +f(/* a */ 1, /* b /* c */ */ 2) // "x" val s = "a // b ${x.map { it }} \$y" + """r"""" + .5f + 0xFFuL + 0..(), Api { private set; get() = a to b; loop@ return@loop }