Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update Prism.js definitions of Tact and FunC for syntax highlighting #768

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 123 additions & 33 deletions src/theme/prism/prism-func.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,143 @@
(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/;
/**
* @file Prism.js definition for FunC
* @link https://docs.ton.org/develop/func/overview
* @version 0.4.4
* @author Novus Nota (https://github.com/novusnota)
* @author Nikita Sobolev (https://github.com/sobolevn)
* @license MIT
*/
(function(Prism) {
/** @type {Prism.GrammarValue} */
var number = /-?\b(?:\d+|0x[\da-fA-F]+)\b(?![^\.~,;\[\]\(\)\s])/;

Prism.languages.func = {
'include': {
pattern: /#include(.*);/,
/** @type {Prism.GrammarValue} */
var string = [
{ // multi-line
pattern: /"""[\s\S]*?"""[Hhcusa]?/,
greedy: true,
inside: {
'keyword': /#include/,
'string': string,
'punctuation': /;/
// string type
'symbol': {
pattern: /("""[\s\S]*?""")[Hhcusa]/,
lookbehind: true,
greedy: true,
}
},
},
'pragma': {
pattern: /#pragma(.*);/,
{ // single-line
pattern: /"[^\n"]*"[Hhcusa]?/,
greedy: true,
inside: {
'keyword': /#pragma|not-version|version/,
'number': /(\d+)(.\d+)?(.\d+)?/,
'operator': [/>=/, /<=/, /=/, />/, /</, /\^/],
'punctuation': /;/
}
// string type
'symbol': {
pattern: /("[^\n"]*")[Hhcusa]/,
lookbehind: true,
greedy: true,
}
},
},
];

/** @type {Prism.GrammarValue} */
var operator = /(?:!=|\?|:|%=|%|&=|&|\*=|\*|\+=|\+|->|-=|-|\/%|\/=|\/|<=>|<<=|<<|<=|<|==|=|>>=|>>|>=|>|\^>>=|\^>>|\^=|\^\/=|\^\/|\^%=|\^%|\^|\|=|\||~>>=|~>>|~\/=|~\/|~%|~)(?=\s)/;

/** @type {RegExp[]} */
var var_identifier = [
// quoted
/`[^`\n]+`/,
// plain
/[^\.~,;\[\]\(\)\s]+/
];

/** Prism.js definition for FunC */
Prism.languages.func = {
'comment': [
{
{ // single-line
pattern: /;;.*/,
lookbehind: true,
greedy: true
greedy: true,
},
{
{ // multi-line, not nested (TODO: nesting)
// . isn't used, because it only applies to the single line
pattern: /\{-[\s\S]*?(?:-\}|$)/,
lookbehind: true,
greedy: true
greedy: true,
},
{
// unused variable identifier
pattern: /\b_\b(?![^\.~,;\[\]\(\)\s])/
},
],

'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/,
// Custom token for #pragma's
'pragma': {
pattern: /#pragma(.*);/,
inside: {
'keyword': /(?:#pragma|test-version-set|not-version|version|allow-post-modification|compute-asm-ltr)\b/,
'number': /(?:\d+)(?:.\d+)?(?:.\d+)?/,
'operator': /=|\^|<=|>=|<|>/,
'string': string,
'punctuation': /;/,
}
},

// Custom token for #include's
'include': {
pattern: /#include(.*);/,
inside: {
'keyword': /#include\b/,
'string': string,
'punctuation': /;/,
}
},

// builtin types
'builtin': /\b(?:int|cell|slice|builder|cont|tuple|type)\b/,

// builtin constants
'boolean': /\b(?:false|true|nil|Nil)\b/,

'keyword': /\b(?:forall|extern|global|asm|impure|inline_ref|inline|auto_apply|method_id|operator|infixl|infixr|infix|const|return|var|repeat|do|while|until|try|catch|ifnot|if|then|elseifnot|elseif|else)\b/,

'string': string,
'number': number,
'variable': identifier,

'operator': /(<=>|>=|<=|!=|==|~>>=|~>>|\/%|\^%=|\^%|~%|\^\/=|\^\/|~\/=|~\/|\+=|-=|\*=|\/=|%=|<<=|>>=|\^>>=|\^>>|&=|>>|<<|\^=|\|=|\^|=|~|\/|%|-|\*|\+|>|<|&|\||:|\?)/,
'punctuation': /[\.;\(\),\[\]~\{\}]/,
'string': string,

'operator': operator,

'punctuation': [/[\.,;\(\)\[\]]/, /[\{\}](?![^\.~,;\[\]\(\)\s])/],

// Function and method names in declarations, definitions and calls
'function': [
{ // quoted
pattern: new RegExp(var_identifier[0].source + /(?=\s*\()/.source),
greedy: true,
},
{ // bitwise not operator
pattern: /~_/,
greedy: true,
},
{ // remaining operators
pattern: new RegExp(/\^?_/.source + operator.source + /_/.source),
greedy: true,
},
{ // plain function or method name
pattern: new RegExp(/[\.~]?/.source + var_identifier[1].source + /(?=\s*\()/.source),
greedy: true,
},
{ // builtin functions and methods
pattern: /\b(?:divmod|moddiv|muldiv|muldivr|muldivc|muldivmod|null\?|throw|throw_if|throw_unless|throw_arg|throw_arg_if|throw_arg_unless|load_int|load_uint|preload_int|preload_uint|store_int|store_uint|load_bits|preload_bits|int_at|cell_at|slice_at|tuple_at|at|touch|touch2|run_method0|run_method1|run_method2|run_method3|~divmod|~moddiv|~store_int|~store_uint|~touch|~touch2|~dump|~stdump)\b/,
greedy: true,
},
],

// Parametric polymorphism
'class-name': /[A-Z][^\.~,;\[\]\(\)\s]*/,

// All the rest of identifiers
'variable': var_identifier.map(x => { return { pattern: x, greedy: true } }),
};
}(Prism));

// Adding a fc alias
Prism.languages.fc = Prism.languages.func;
}(Prism));
70 changes: 47 additions & 23 deletions src/theme/prism/prism-tact.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
(function (Prism) {
/**
* @file Prism.js definition for Tact
* @link https://tact-lang.org
* @version 1.5.0
* @author Novus Nota (https://github.com/novusnota)
* @license MIT
*/
(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/,
},
{ // keyword after as
pattern: /(\bas\s+)\w+/,
lookbehind: true,
greedy: true,
pattern: /\b(?:abstract|asm|as|catch|const|contract(?!:)|do|else|extend|extends|foreach|fun|get|if|in|import|initOf|inline|let|message(?!:)|mutates|native|override|primitive|public|repeat|return|self|struct(?!:)|trait(?!:)|try|until|virtual|while|with)\b/,
},
{ // reserved function names
pattern: /\b(?:bounced|external|init|receive)\b/
pattern: /\b(?:bounced|external|init|receive)\b(?=\()/
},
],

// built-in types
'builtin': {
pattern: /\b(?:Address|Bool|Builder|Cell|Int|Slice|String|StringBuilder)\b/,
},
'builtin': [
{
pattern: /\b(?:Address|Bool|Builder|Cell|Int|Slice|String|StringBuilder)\b/,
},
{ // keyword after as, see: https://prismjs.com/extending.html#object-notation
pattern: /(\bas\s+)(?:coins|remaining|bytes32|bytes64|int257|u?int(?:2[0-5][0-6]|1[0-9][0-9]|[1-9][0-9]?))\b/,
lookbehind: true,
greedy: true,
},
],

// SCREAMING_SNAKE_CASE for null values and names of constants
'constant': [
Expand Down Expand Up @@ -93,11 +102,19 @@
pattern: /(^|[^\\])\/\*[\s\S]*?(?:\*\/|$)/,
lookbehind: true,
greedy: true,
},
{
// unused variable identifier
pattern: /\b_\b/
}
],

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

'variable': {
'pattern': /\b[a-zA-Z_]\w*\b/,
},

};
Expand All @@ -108,6 +125,20 @@
pattern: /(?:(")(?:\\.|(?!\1)[^\\\r\n])*\1(?!\1))/,
greedy: true,
inside: {
'regex': [
{ // \\ \" \n \r \t \v \b \f
pattern: /\\[\\"nrtvbf]/,
},
{ // hexEscape, \x00 through \xFF
pattern: /\\x[0-9a-fA-F]{2}/,
},
{ // unicodeEscape, \u0000 through \uFFFF
pattern: /\\u[0-9a-fA-F]{4}/,
},
{ // unicodeCodePoint, \u{0} through \u{FFFFFF}
pattern: /\\u\{[0-9a-fA-F]{1,6}\}/,
},
],
'string': {
pattern: /[\s\S]+/,
},
Expand All @@ -122,7 +153,7 @@
greedy: true,
inside: {
'builtin': [
Prism.languages['tact']['builtin'],
...Prism.languages['tact']['builtin'],
{
pattern: /\b(?:bounced(?=<)|map(?=<))\b/
},
Expand All @@ -131,16 +162,9 @@
'punctuation': {
pattern: /[<>(),.?]/,
},
'keyword': [
{
pattern: /\bas\b/,
},
{
pattern: /(\bas\s+)\w+/,
lookbehind: true,
greedy: true,
},
],
'keyword': {
pattern: /\bas\b/,
},
},
},
});
Expand Down
Loading