This commit is contained in:
Steven Degutis 2025-02-05 04:26:58 +07:00 committed by GitHub
commit 89f98119c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -75,138 +75,47 @@ export const conf: languages.LanguageConfiguration = {
}; };
export const language = { export const language = {
// Set defaultToken to invalid to see what you do not tokenize yet
defaultToken: 'invalid', defaultToken: 'invalid',
tokenPostfix: '.ts', tokenPostfix: '.ts',
typeKeywords: [
'any', 'bigint', 'boolean', 'number', 'object', 'string', 'unknown', 'void',
],
ctrlKeywords: [
'export', 'default', 'return', 'as', 'if', 'break', 'case', 'catch', 'continue',
'do', 'else', 'finally', 'for', 'throw', 'try', 'with', 'yield', 'await',
'import', 'from', 'type',
],
alwaysKeyword: [
'constructor', 'super',
],
keywords: [ keywords: [
// Should match the keys of textToKeywordObj in // Should match the keys of textToKeywordObj in
// https://github.com/microsoft/TypeScript/blob/master/src/compiler/scanner.ts // https://github.com/microsoft/TypeScript/blob/master/src/compiler/scanner.ts
'abstract', 'abstract', 'asserts',
'any', 'class', 'const', 'debugger',
'as', 'declare', 'delete', 'enum',
'asserts', 'extends', 'false', 'function', 'get',
'bigint', 'implements', 'in', 'infer', 'instanceof', 'interface',
'boolean', 'is', 'keyof', 'let', 'module', 'namespace', 'never', 'new',
'break', 'null', 'out', 'package', 'private', 'protected',
'case', 'public', 'override', 'readonly', 'require', 'global', 'satisfies',
'catch', 'set', 'static', 'switch', 'symbol', 'this',
'class', 'true', 'typeof', 'undefined', 'unique',
'continue', 'var', 'while', 'async', 'of'
'const',
'constructor',
'debugger',
'declare',
'default',
'delete',
'do',
'else',
'enum',
'export',
'extends',
'false',
'finally',
'for',
'from',
'function',
'get',
'if',
'implements',
'import',
'in',
'infer',
'instanceof',
'interface',
'is',
'keyof',
'let',
'module',
'namespace',
'never',
'new',
'null',
'number',
'object',
'out',
'package',
'private',
'protected',
'public',
'override',
'readonly',
'require',
'global',
'return',
'satisfies',
'set',
'static',
'string',
'super',
'switch',
'symbol',
'this',
'throw',
'true',
'try',
'type',
'typeof',
'undefined',
'unique',
'unknown',
'var',
'void',
'while',
'with',
'yield',
'async',
'await',
'of'
], ],
operators: [ operators: [
'<=', '<=', '>=', '==', '!=', '===', '!==', '=>',
'>=', '+', '-', '**', '*', '/', '%', '++',
'==', '--', '<<', '</', '>>', '>>>', '&', '|',
'!=', '^', '!', '~', '&&', '||', '??', '?',
'===', ':', '=', '+=', '-=', '*=', '**=', '/=',
'!==', '%=', '<<=', '>>=', '>>>=', '&=', '|=', '^=', '@'
'=>',
'+',
'-',
'**',
'*',
'/',
'%',
'++',
'--',
'<<',
'</',
'>>',
'>>>',
'&',
'|',
'^',
'!',
'~',
'&&',
'||',
'??',
'?',
':',
'=',
'+=',
'-=',
'*=',
'**=',
'/=',
'%=',
'<<=',
'>>=',
'>>>=',
'&=',
'|=',
'^=',
'@'
], ],
// we include these common regular expressions // we include these common regular expressions
@ -222,143 +131,249 @@ export const language = {
// The main tokenizer for our languages // The main tokenizer for our languages
tokenizer: { tokenizer: {
root: [[/[{}]/, 'delimiter.bracket'], { include: 'common' }], root: [
[/}/, {
cases: {
'$S2==INJSX': { token: '@brackets', next: '@pop' },
'@default': '@brackets',
}
}],
common: [ [/{/, 'delimiter.bracket'],
// identifiers and keywords
[
/#?[a-z_$][\w$]*/,
{
cases: {
'@keywords': 'keyword',
'@default': 'identifier'
}
}
],
[/[A-Z][\w\$]*/, 'type.identifier'], // to show class names nicely
// [/[A-Z][\w\$]*/, 'identifier'],
// whitespace // highlight class field-properties
{ include: '@whitespace' }, [/^\s+#?[\w$]+(?=\s*[;=:])/, 'variable.property'],
// regular expression: ensure it is terminated before beginning (otherwise it is an opeator) // highlight function/class defs
[ [/(function|class|new)(\s+)(#?[\w$]+)(\s*)([<(]?)/, ['keyword', '',
/\/(?=([^\\\/]|\\.)+\/([dgimsuy]*)(\s*)(\.|;|,|\)|\]|\}|$))/, {
{ token: 'regexp', bracket: '@open', next: '@regexp' } cases: {
], '$1==function': 'method',
'$1==class': 'type.identifier',
'$1==new': 'type.identifier',
}
}, '', {
cases: {
'<': { token: '@brackets', next: '@typeparams' },
'@default': '@rematch',
}
},
]],
// delimiters and operators // highlight var/const/let defs
[/[()\[\]]/, '@brackets'], [/(const|let|var)(\s+)(#?[\w$]+)/, ['keyword', '', {
[/[<>](?!@symbols)/, '@brackets'], cases: {
[/!(?=([^=]|$))/, 'delimiter'], '$1==const': 'constant',
[ '@default': 'variable',
/@symbols/, }
{ }]],
cases: {
'@operators': 'delimiter',
'@default': ''
}
}
],
// numbers { include: 'jsxReady' },
[/(@digits)[eE]([\-+]?(@digits))?/, 'number.float'],
[/(@digits)\.(@digits)([eE][\-+]?(@digits))?/, 'number.float'],
[/0[xX](@hexdigits)n?/, 'number.hex'],
[/0[oO]?(@octaldigits)n?/, 'number.octal'],
[/0[bB](@binarydigits)n?/, 'number.binary'],
[/(@digits)n?/, 'number'],
// delimiter: after number because of .\d floats { include: 'common' },
[/[;,.]/, 'delimiter'], ],
// strings common: [
[/"([^"\\]|\\.)*$/, 'string.invalid'], // non-teminated string
[/'([^'\\]|\\.)*$/, 'string.invalid'], // non-teminated string // identifiers and keywords
[/"/, 'string', '@string_double'], [/(#?[a-zA-Z_$][\w$]*)([<(]?)/, [
[/'/, 'string', '@string_single'], {
[/`/, 'string', '@string_backtick'] cases: {
'@typeKeywords': 'type.identifier',
'@alwaysKeyword': 'keyword',
'$1~#?[A-Z].*': 'type.identifier',
'$2': 'method',
'@ctrlKeywords': 'keyword.flow',
'@keywords': 'keyword',
'@default': 'identifier',
}
},
{
cases: {
'$2==<': { token: '@brackets', next: '@typeparams' },
'@default': '@rematch',
}
},
]],
// whitespace
{ include: '@whitespace' },
// regular expression: ensure it is terminated before beginning (otherwise it is an opeator)
[
/\/(?=([^\\\/]|\\.)+\/([dgimsuy]*)(\s*)(\.|;|,|\)|\]|\}|$))/,
{ token: 'regexp', bracket: '@open', next: '@regexp' }
], ],
whitespace: [ // delimiters and operators
[/[ \t\r\n]+/, ''], [/[()\[\]]/, '@brackets'],
[/\/\*\*(?!\/)/, 'comment.doc', '@jsdoc'], [/[<>](?!@symbols)/, '@brackets'],
[/\/\*/, 'comment', '@comment'], [/!(?=([^=]|$))/, 'delimiter'],
[/\/\/.*$/, 'comment'] [
/@symbols/,
{
cases: {
'@operators': 'delimiter',
'@default': ''
}
}
], ],
comment: [ [/\.\.\./, 'keyword'],
[/[^\/*]+/, 'comment'],
[/\*\//, 'comment', '@pop'],
[/[\/*]/, 'comment']
],
jsdoc: [ // numbers
[/[^\/*]+/, 'comment.doc'], [/(@digits)[eE]([\-+]?(@digits))?/, 'number.float'],
[/\*\//, 'comment.doc', '@pop'], [/(@digits)\.(@digits)([eE][\-+]?(@digits))?/, 'number.float'],
[/[\/*]/, 'comment.doc'] [/0[xX](@hexdigits)n?/, 'number.hex'],
], [/0[oO]?(@octaldigits)n?/, 'number.octal'],
[/0[bB](@binarydigits)n?/, 'number.binary'],
[/(@digits)n?/, 'number'],
// We match regular expression quite precisely // delimiter: after number because of .\d floats
regexp: [ [/[;,.]/, 'delimiter'],
[
/(\{)(\d+(?:,\d*)?)(\})/,
['regexp.escape.control', 'regexp.escape.control', 'regexp.escape.control']
],
[
/(\[)(\^?)(?=(?:[^\]\\\/]|\\.)+)/,
['regexp.escape.control', { token: 'regexp.escape.control', next: '@regexrange' }]
],
[/(\()(\?:|\?=|\?!)/, ['regexp.escape.control', 'regexp.escape.control']],
[/[()]/, 'regexp.escape.control'],
[/@regexpctl/, 'regexp.escape.control'],
[/[^\\\/]/, 'regexp'],
[/@regexpesc/, 'regexp.escape'],
[/\\\./, 'regexp.invalid'],
[/(\/)([dgimsuy]*)/, [{ token: 'regexp', bracket: '@close', next: '@pop' }, 'keyword.other']]
],
regexrange: [ // strings
[/-/, 'regexp.escape.control'], [/"([^"\\]|\\.)*$/, 'string.invalid'], // non-teminated string
[/\^/, 'regexp.invalid'], [/'([^'\\]|\\.)*$/, 'string.invalid'], // non-teminated string
[/@regexpesc/, 'regexp.escape'], [/"/, 'string', '@string_double'],
[/[^\]]/, 'regexp'], [/'/, 'string', '@string_single'],
[ [/`/, 'string', '@string_backtick']
/\]/, ],
{
token: 'regexp.escape.control',
next: '@pop',
bracket: '@close'
}
]
],
string_double: [ typeparams: [
[/[^\\"]+/, 'string'], [/>/, '@brackets', '@pop'],
[/@escapes/, 'string.escape'], { include: 'common' }
[/\\./, 'string.escape.invalid'], ],
[/"/, 'string', '@pop']
],
string_single: [ jsxReady: [
[/[^\\']+/, 'string'], [/<>/, 'delimiter.html', '@jsxText.FRAGMENT'],
[/@escapes/, 'string.escape'], [/(<)([A-Z][\w$]*\s*(?:,|extends|implements))/, ['@brackets', { token: '@rematch', next: '@typeparams' }]],
[/\\./, 'string.escape.invalid'], [/(<)(\s*)([\w$])/, ['delimiter.html', '',
[/'/, 'string', '@pop'] { token: '@rematch', next: '@jsxIdent.jsxOpen.' },
], ]],
],
string_backtick: [ jsxIdent: [
[/\$\{/, { token: 'delimiter.bracket', next: '@bracketCounting' }], [/\./, { token: 'delimiter', switchTo: '$S0^' }],
[/[^\\`$]+/, 'string'], [/[A-Z][\w$]*/, { token: 'type.identifier', switchTo: '$S0$0' }],
[/@escapes/, 'string.escape'], [/[\w$-]+/, { token: 'tag', switchTo: '$S0$0' }],
[/\\./, 'string.escape.invalid'], [/.+/, { token: '@rematch', switchTo: '@$S2.$S3.$S4' }],
[/`/, 'string', '@pop'] ],
],
bracketCounting: [ jsxOpen: [
[/\{/, 'delimiter.bracket', '@bracketCounting'], [/{/, { token: 'keyword', next: '@root.INJSX', bracket: '@open' }],
[/\}/, 'delimiter.bracket', '@pop'], [/>/, { token: 'delimiter.html', switchTo: '@jsxText.$S2' }],
{ include: 'common' } [/\/>/, { token: 'delimiter.html', next: '@pop' }],
[/ +([\w-$]+)/, 'attribute.name'],
[/(=)(')/, ['delimiter', { token: 'string', next: '@string_single' }]],
[/(=)(")/, ['delimiter', { token: 'string', next: '@string_double' }]],
[/(=)({)/, ['delimiter', { token: '@brackets', next: '@root.INJSX' }]],
],
jsxText: [
[/{/, { token: 'keyword', next: '@root.INJSX', bracket: '@open' }],
[/<\/>/, {
cases: {
'$S2==FRAGMENT': { token: 'delimiter.html', next: '@pop' },
'@default': { token: 'invalid', next: '@pop' },
}
}],
[/(<\/)(\s*)([\w$])/, ['delimiter.html', '',
{ token: '@rematch', switchTo: '@jsxIdent.jsxClose.$S2.' },
]],
{ include: 'jsxReady' },
[/./, 'string'],
],
jsxClose: [
[/>/, {
cases: {
'$S2==$S3': { token: 'delimiter.html', next: '@pop' },
'@default': { token: 'invalid', next: '@pop' },
}
}],
],
whitespace: [
[/[ \t\r\n]+/, ''],
[/\/\*\*(?!\/)/, 'comment.doc', '@jsdoc'],
[/\/\*/, 'comment', '@comment'],
[/\/\/.*$/, 'comment']
],
comment: [
[/[^\/*]+/, 'comment'],
[/\*\//, 'comment', '@pop'],
[/[\/*]/, 'comment']
],
jsdoc: [
[/[^\/*]+/, 'comment.doc'],
[/\*\//, 'comment.doc', '@pop'],
[/[\/*]/, 'comment.doc']
],
// We match regular expression quite precisely
regexp: [
[
/(\{)(\d+(?:,\d*)?)(\})/,
['regexp.escape.control', 'regexp.escape.control', 'regexp.escape.control']
],
[
/(\[)(\^?)(?=(?:[^\]\\\/]|\\.)+)/,
['regexp.escape.control', { token: 'regexp.escape.control', next: '@regexrange' }]
],
[/(\()(\?:|\?=|\?!)/, ['regexp.escape.control', 'regexp.escape.control']],
[/[()]/, 'regexp.escape.control'],
[/@regexpctl/, 'regexp.escape.control'],
[/[^\\\/]/, 'regexp'],
[/@regexpesc/, 'regexp.escape'],
[/\\\./, 'regexp.invalid'],
[/(\/)([dgimsuy]*)/, [{ token: 'regexp', bracket: '@close', next: '@pop' }, 'keyword.other']]
],
regexrange: [
[/-/, 'regexp.escape.control'],
[/\^/, 'regexp.invalid'],
[/@regexpesc/, 'regexp.escape'],
[/[^\]]/, 'regexp'],
[
/\]/,
{
token: 'regexp.escape.control',
next: '@pop',
bracket: '@close'
}
] ]
],
string_double: [
[/[^\\"]+/, 'string'],
[/@escapes/, 'string.escape'],
[/\\./, 'string.escape.invalid'],
[/"/, 'string', '@pop']
],
string_single: [
[/[^\\']+/, 'string'],
[/@escapes/, 'string.escape'],
[/\\./, 'string.escape.invalid'],
[/'/, 'string', '@pop']
],
string_backtick: [
[/\$\{/, { token: 'delimiter.bracket', next: '@bracketCounting' }],
[/[^\\`$]+/, 'string'],
[/@escapes/, 'string.escape'],
[/\\./, 'string.escape.invalid'],
[/`/, 'string', '@pop']
],
bracketCounting: [
[/\{/, 'delimiter.bracket', '@bracketCounting'],
[/\}/, 'delimiter.bracket', '@pop'],
{ include: 'common' }
]
} }
}; };