Skip to content

Commit

Permalink
Merge pull request #333 from novusnota-forks/main
Browse files Browse the repository at this point in the history
feat: Updated Tact language highlighting for Prism.js
  • Loading branch information
SwiftAdviser authored Aug 23, 2023
2 parents 327ec1d + 9ecb64b commit 45cb016
Showing 1 changed file with 126 additions and 49 deletions.
175 changes: 126 additions & 49 deletions src/theme/prism/prism-tact.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,130 @@
(function (Prism) {
// 1. Does not start from "
// 2. Can start from ` and end with `, containing any character
// 3. Starts with underscore or { or } and have more than 1 character after it
// 4. Starts with letter, contains letters, numbers and underscores
var identifier = /(?!")(`([^`]+)`|((?=_)_|(?=\{)\{|(?=\})\}|(?![_`{}]))([^;,\[\]\(\)\s~.]+))/;
var string = /"[^\n"]+"[Hhcusa]?/;
var number = /\b([\d_]+|0x[\d_a-fA-F]+|0b[1_0]+)\b/;

Prism.languages.tact = {
'include': {
pattern: /#include(.*);/,
inside: {
'keyword': /#include/,
'string': string,
'punctuation': /;/
},
(function(Prism) {
Prism.languages.tact = {
// reserved keywords
'keyword': [
{
pattern: /\b(?:abstract|as|const|contract(?!:)|do|else|extend|extends|fun|get|if|import|initOf|inline|let|message(?!:)|mutates|native|override|primitive|public|repeat|return|self|struct(?!:)|trait(?!:)|until|virtual|while|with)\b/,
},
'pragma': {
pattern: /#pragma(.*);/,
inside: {
'keyword': /#pragma|not-version|version/,
'number': /(\d+)(.\d+)?(.\d+)?/,
'operator': [/>=/, /<=/, /=/, />/, /</, /\^/],
'punctuation': /;/
}
},

'comment': [
{
pattern: /;;.*/,
lookbehind: true,
greedy: true
},
{
pattern: /\{-[\s\S]*?(?:-\}|$)/,
lookbehind: true,
greedy: true
},
],
{ // keyword after as
pattern: /(\bas\s+)\w+/,
lookbehind: true,
greedy: true,
},
{ // reserved function names
pattern: /\b(?:init|receive|bounced|external)\b/
},
],

'keyword': /\b(?:_(?=\s*:)|asm|const|do|else|elseif|elseifnot|forall|global|if|ifnot|impure|inline|inline_ref|method_id|repeat|return|until|while)\b/,
'boolean': /\b(?:false|true)\b/,
'builtin': /\b(?:_|builder|cell|cont|int|slice|tuple|var)\b/,
// built-in types
'builtin': {
pattern: /\b(?:Int|Bool|Address|Slice|Cell|Builder|String|StringBuilder)\b/,
},

'string': string,
'number': number,
'variable': identifier,
// SCREAMING_SNAKE_CASE for null values and names of constants
'constant': [
{
pattern: /\bnull\b/,
},
{
pattern: /\b[A-Z][A-Z0-9_]*\b/,
},
],

'operator': /(<=>|>=|<=|!=|==|~>>=|~>>|\/%|\^%=|\^%|~%|\^\/=|\^\/|~\/=|~\/|\+=|-=|\*=|\/=|%=|<<=|>>=|\^>>=|\^>>|&=|>>|<<|\^=|\|=|\^|=|~|\/|%|-|\*|\+|>|<|&|\||:|\?)/,
'punctuation': /[\.;\(\),\[\]~\{\}]/,
};
}(Prism));
// UpperCamelCase for names of contracts, traits, structs, messages
'class-name': {
pattern: /\b[A-Z][\w]*\b/,
},

// native FunC functions mapping
'attribute': {
pattern: /@name/,
inside: {
'function': /.+/,
},
},

'function': {
pattern: /\b[\w]+(?=\()/,
},

'boolean': {
pattern: /\b(?:false|true)\b/,
},

'number': [
{ // hexadecimal, case-insensitive /i
pattern: /\b0x[0-9a-f]+\b/i,
},
{ // decimal integers
pattern: /\b[0-9]+\b/,
}
],

'string': undefined,

'punctuation': {
pattern: /[{}[\]();,.:?]/,
},

'comment': [
{ // single-line
pattern: /(^|[^\\:])\/\/.*/,
lookbehind: true,
greedy: true,
},
{ // multi-line
pattern: /(^|[^\\])\/\*[\s\S]*?(?:\*\/|$)/,
lookbehind: true,
greedy: true,
}
],

'operator': {
'pattern': /![!=]?|\*|\/|%|-|\+|==?|[<>]=|<<?|>>?|\|\|?|&&?/,
},

};

// strings, made this way to not collide with other entities
Prism.languages.insertBefore('tact', 'string', {
'string-literal': {
pattern: /(?:(")(?:\\.|(?!\1)[^\\\r\n])*\1(?!\1))/,
greedy: true,
inside: {
'string': {
pattern: /[\s\S]+/,
},
},
},
});

// map and bounced message generic type modifiers
Prism.languages.insertBefore('tact', 'keyword', {
'generics': {
pattern: /(?:\b(?:map|bounced)\b<[^\\\r\n]*>)/,
greedy: true,
inside: {
'builtin': [
Prism.languages['tact']['builtin'],
{
pattern: /\b(?:map(?=<)|bounced(?=<))\b/
},
],
'class-name': Prism.languages['tact']['class-name'],
'punctuation': {
pattern: /[<>(),.?]/,
},
'keyword': [
{
pattern: /\bas\b/,
},
{
pattern: /(\bas\s+)\w+/,
lookbehind: true,
greedy: true,
},
],
},
},
});
}(Prism));

0 comments on commit 45cb016

Please sign in to comment.