2024-01-16 21:26:16 +08:00
|
|
|
/*
|
|
|
|
Language: Crystal
|
|
|
|
Author: TSUYUSATO Kitsune <make.just.on@gmail.com>
|
|
|
|
Website: https://crystal-lang.org
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @type LanguageFn */
|
|
|
|
function crystal(hljs) {
|
|
|
|
const INT_SUFFIX = '(_?[ui](8|16|32|64|128))?';
|
|
|
|
const FLOAT_SUFFIX = '(_?f(32|64))?';
|
|
|
|
const CRYSTAL_IDENT_RE = '[a-zA-Z_]\\w*[!?=]?';
|
|
|
|
const CRYSTAL_METHOD_RE = '[a-zA-Z_]\\w*[!?=]?|[-+~]@|<<|>>|[=!]~|===?|<=>|[<>]=?|\\*\\*|[-/+%^&*~|]|//|//=|&[-+*]=?|&\\*\\*|\\[\\][=?]?';
|
|
|
|
const CRYSTAL_PATH_RE = '[A-Za-z_]\\w*(::\\w+)*(\\?|!)?';
|
|
|
|
const CRYSTAL_KEYWORDS = {
|
|
|
|
$pattern: CRYSTAL_IDENT_RE,
|
2023-12-18 13:12:25 +08:00
|
|
|
keyword:
|
|
|
|
'abstract alias annotation as as? asm begin break case class def do else elsif end ensure enum extend for fun if ' +
|
|
|
|
'include instance_sizeof is_a? lib macro module next nil? of out pointerof private protected rescue responds_to? ' +
|
|
|
|
'return require select self sizeof struct super then type typeof union uninitialized unless until verbatim when while with yield ' +
|
|
|
|
'__DIR__ __END_LINE__ __FILE__ __LINE__',
|
|
|
|
literal: 'false nil true'
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const SUBST = {
|
2023-12-18 13:12:25 +08:00
|
|
|
className: 'subst',
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: /#\{/,
|
|
|
|
end: /\}/,
|
2023-12-18 13:12:25 +08:00
|
|
|
keywords: CRYSTAL_KEYWORDS
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const EXPANSION = {
|
2023-12-18 13:12:25 +08:00
|
|
|
className: 'template-variable',
|
|
|
|
variants: [
|
2024-01-16 21:26:16 +08:00
|
|
|
{
|
|
|
|
begin: '\\{\\{',
|
|
|
|
end: '\\}\\}'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '\\{%',
|
|
|
|
end: '%\\}'
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
],
|
|
|
|
keywords: CRYSTAL_KEYWORDS
|
|
|
|
};
|
|
|
|
|
|
|
|
function recursiveParen(begin, end) {
|
2024-01-16 21:26:16 +08:00
|
|
|
const
|
|
|
|
contains = [
|
|
|
|
{
|
|
|
|
begin: begin,
|
|
|
|
end: end
|
|
|
|
}
|
|
|
|
];
|
2023-12-18 13:12:25 +08:00
|
|
|
contains[0].contains = contains;
|
|
|
|
return contains;
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
const STRING = {
|
2023-12-18 13:12:25 +08:00
|
|
|
className: 'string',
|
2024-01-16 21:26:16 +08:00
|
|
|
contains: [
|
|
|
|
hljs.BACKSLASH_ESCAPE,
|
|
|
|
SUBST
|
|
|
|
],
|
2023-12-18 13:12:25 +08:00
|
|
|
variants: [
|
2024-01-16 21:26:16 +08:00
|
|
|
{
|
|
|
|
begin: /'/,
|
|
|
|
end: /'/
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: /"/,
|
|
|
|
end: /"/
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: /`/,
|
|
|
|
end: /`/
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '%[Qwi]?\\(',
|
|
|
|
end: '\\)',
|
|
|
|
contains: recursiveParen('\\(', '\\)')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '%[Qwi]?\\[',
|
|
|
|
end: '\\]',
|
|
|
|
contains: recursiveParen('\\[', '\\]')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '%[Qwi]?\\{',
|
|
|
|
end: /\}/,
|
|
|
|
contains: recursiveParen(/\{/, /\}/)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '%[Qwi]?<',
|
|
|
|
end: '>',
|
|
|
|
contains: recursiveParen('<', '>')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '%[Qwi]?\\|',
|
|
|
|
end: '\\|'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: /<<-\w+$/,
|
|
|
|
end: /^\s*\w+$/
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
],
|
2024-01-16 21:26:16 +08:00
|
|
|
relevance: 0
|
2023-12-18 13:12:25 +08:00
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const Q_STRING = {
|
2023-12-18 13:12:25 +08:00
|
|
|
className: 'string',
|
|
|
|
variants: [
|
2024-01-16 21:26:16 +08:00
|
|
|
{
|
|
|
|
begin: '%q\\(',
|
|
|
|
end: '\\)',
|
|
|
|
contains: recursiveParen('\\(', '\\)')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '%q\\[',
|
|
|
|
end: '\\]',
|
|
|
|
contains: recursiveParen('\\[', '\\]')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '%q\\{',
|
|
|
|
end: /\}/,
|
|
|
|
contains: recursiveParen(/\{/, /\}/)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '%q<',
|
|
|
|
end: '>',
|
|
|
|
contains: recursiveParen('<', '>')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '%q\\|',
|
|
|
|
end: '\\|'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: /<<-'\w+'$/,
|
|
|
|
end: /^\s*\w+$/
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
],
|
2024-01-16 21:26:16 +08:00
|
|
|
relevance: 0
|
2023-12-18 13:12:25 +08:00
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const REGEXP = {
|
|
|
|
begin: '(?!%\\})(' + hljs.RE_STARTERS_RE + '|\\n|\\b(case|if|select|unless|until|when|while)\\b)\\s*',
|
2023-12-18 13:12:25 +08:00
|
|
|
keywords: 'case if select unless until when while',
|
|
|
|
contains: [
|
|
|
|
{
|
|
|
|
className: 'regexp',
|
2024-01-16 21:26:16 +08:00
|
|
|
contains: [
|
|
|
|
hljs.BACKSLASH_ESCAPE,
|
|
|
|
SUBST
|
|
|
|
],
|
2023-12-18 13:12:25 +08:00
|
|
|
variants: [
|
2024-01-16 21:26:16 +08:00
|
|
|
{
|
|
|
|
begin: '//[a-z]*',
|
|
|
|
relevance: 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '/(?!\\/)',
|
|
|
|
end: '/[a-z]*'
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
relevance: 0
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const REGEXP2 = {
|
2023-12-18 13:12:25 +08:00
|
|
|
className: 'regexp',
|
2024-01-16 21:26:16 +08:00
|
|
|
contains: [
|
|
|
|
hljs.BACKSLASH_ESCAPE,
|
|
|
|
SUBST
|
|
|
|
],
|
2023-12-18 13:12:25 +08:00
|
|
|
variants: [
|
2024-01-16 21:26:16 +08:00
|
|
|
{
|
|
|
|
begin: '%r\\(',
|
|
|
|
end: '\\)',
|
|
|
|
contains: recursiveParen('\\(', '\\)')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '%r\\[',
|
|
|
|
end: '\\]',
|
|
|
|
contains: recursiveParen('\\[', '\\]')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '%r\\{',
|
|
|
|
end: /\}/,
|
|
|
|
contains: recursiveParen(/\{/, /\}/)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '%r<',
|
|
|
|
end: '>',
|
|
|
|
contains: recursiveParen('<', '>')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '%r\\|',
|
|
|
|
end: '\\|'
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
],
|
|
|
|
relevance: 0
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const ATTRIBUTE = {
|
2023-12-18 13:12:25 +08:00
|
|
|
className: 'meta',
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: '@\\[',
|
|
|
|
end: '\\]',
|
2023-12-18 13:12:25 +08:00
|
|
|
contains: [
|
2024-01-16 21:26:16 +08:00
|
|
|
hljs.inherit(hljs.QUOTE_STRING_MODE, {
|
|
|
|
className: 'meta-string'
|
|
|
|
})
|
2023-12-18 13:12:25 +08:00
|
|
|
]
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const CRYSTAL_DEFAULT_CONTAINS = [
|
2023-12-18 13:12:25 +08:00
|
|
|
EXPANSION,
|
|
|
|
STRING,
|
|
|
|
Q_STRING,
|
|
|
|
REGEXP2,
|
|
|
|
REGEXP,
|
|
|
|
ATTRIBUTE,
|
|
|
|
hljs.HASH_COMMENT_MODE,
|
|
|
|
{
|
|
|
|
className: 'class',
|
2024-01-16 21:26:16 +08:00
|
|
|
beginKeywords: 'class module struct',
|
|
|
|
end: '$|;',
|
2023-12-18 13:12:25 +08:00
|
|
|
illegal: /=/,
|
|
|
|
contains: [
|
|
|
|
hljs.HASH_COMMENT_MODE,
|
2024-01-16 21:26:16 +08:00
|
|
|
hljs.inherit(hljs.TITLE_MODE, {
|
|
|
|
begin: CRYSTAL_PATH_RE
|
|
|
|
}),
|
|
|
|
{ // relevance booster for inheritance
|
|
|
|
begin: '<'
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
className: 'class',
|
2024-01-16 21:26:16 +08:00
|
|
|
beginKeywords: 'lib enum union',
|
|
|
|
end: '$|;',
|
2023-12-18 13:12:25 +08:00
|
|
|
illegal: /=/,
|
|
|
|
contains: [
|
|
|
|
hljs.HASH_COMMENT_MODE,
|
2024-01-16 21:26:16 +08:00
|
|
|
hljs.inherit(hljs.TITLE_MODE, {
|
|
|
|
begin: CRYSTAL_PATH_RE
|
|
|
|
})
|
|
|
|
]
|
2023-12-18 13:12:25 +08:00
|
|
|
},
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
beginKeywords: 'annotation',
|
|
|
|
end: '$|;',
|
2023-12-18 13:12:25 +08:00
|
|
|
illegal: /=/,
|
|
|
|
contains: [
|
|
|
|
hljs.HASH_COMMENT_MODE,
|
2024-01-16 21:26:16 +08:00
|
|
|
hljs.inherit(hljs.TITLE_MODE, {
|
|
|
|
begin: CRYSTAL_PATH_RE
|
|
|
|
})
|
2023-12-18 13:12:25 +08:00
|
|
|
],
|
2024-01-16 21:26:16 +08:00
|
|
|
relevance: 2
|
2023-12-18 13:12:25 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
className: 'function',
|
2024-01-16 21:26:16 +08:00
|
|
|
beginKeywords: 'def',
|
|
|
|
end: /\B\b/,
|
2023-12-18 13:12:25 +08:00
|
|
|
contains: [
|
|
|
|
hljs.inherit(hljs.TITLE_MODE, {
|
|
|
|
begin: CRYSTAL_METHOD_RE,
|
|
|
|
endsParent: true
|
|
|
|
})
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
className: 'function',
|
2024-01-16 21:26:16 +08:00
|
|
|
beginKeywords: 'fun macro',
|
|
|
|
end: /\B\b/,
|
2023-12-18 13:12:25 +08:00
|
|
|
contains: [
|
|
|
|
hljs.inherit(hljs.TITLE_MODE, {
|
|
|
|
begin: CRYSTAL_METHOD_RE,
|
|
|
|
endsParent: true
|
|
|
|
})
|
|
|
|
],
|
2024-01-16 21:26:16 +08:00
|
|
|
relevance: 2
|
2023-12-18 13:12:25 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
className: 'symbol',
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: hljs.UNDERSCORE_IDENT_RE + '(!|\\?)?:',
|
2023-12-18 13:12:25 +08:00
|
|
|
relevance: 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
className: 'symbol',
|
|
|
|
begin: ':',
|
2024-01-16 21:26:16 +08:00
|
|
|
contains: [
|
|
|
|
STRING,
|
|
|
|
{
|
|
|
|
begin: CRYSTAL_METHOD_RE
|
|
|
|
}
|
|
|
|
],
|
2023-12-18 13:12:25 +08:00
|
|
|
relevance: 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
className: 'number',
|
|
|
|
variants: [
|
2024-01-16 21:26:16 +08:00
|
|
|
{
|
|
|
|
begin: '\\b0b([01_]+)' + INT_SUFFIX
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '\\b0o([0-7_]+)' + INT_SUFFIX
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '\\b0x([A-Fa-f0-9_]+)' + INT_SUFFIX
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '\\b([1-9][0-9_]*[0-9]|[0-9])(\\.[0-9][0-9_]*)?([eE]_?[-+]?[0-9_]*)?' + FLOAT_SUFFIX + '(?!_)'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '\\b([1-9][0-9_]*|0)' + INT_SUFFIX
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
],
|
|
|
|
relevance: 0
|
|
|
|
}
|
|
|
|
];
|
|
|
|
SUBST.contains = CRYSTAL_DEFAULT_CONTAINS;
|
|
|
|
EXPANSION.contains = CRYSTAL_DEFAULT_CONTAINS.slice(1); // without EXPANSION
|
|
|
|
|
|
|
|
return {
|
2024-01-16 21:26:16 +08:00
|
|
|
name: 'Crystal',
|
|
|
|
aliases: [ 'cr' ],
|
2023-12-18 13:12:25 +08:00
|
|
|
keywords: CRYSTAL_KEYWORDS,
|
|
|
|
contains: CRYSTAL_DEFAULT_CONTAINS
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = crystal;
|