Skip to content

Commit

Permalink
Add support for NaniScript (#2494)
Browse files Browse the repository at this point in the history
  • Loading branch information
elringus authored Aug 7, 2020
1 parent 187c8a6 commit 388ad99
Show file tree
Hide file tree
Showing 15 changed files with 707 additions and 3 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,11 @@
"title": "Nand To Tetris HDL",
"owner": "stephanmax"
},
"naniscript": {
"title": "Naninovel Script",
"owner": "Elringus",
"alias": "nani"
},
"nasm": {
"title": "NASM",
"owner": "rbmj"
Expand Down
170 changes: 170 additions & 0 deletions components/prism-naniscript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
(function (Prism) {

var expressionDef = /\{[^\r\n\[\]{}]*\}/;

var params = {
'quoted-string': {
pattern: /"(?:[^"\\]|\\.)*"/,
alias: 'operator'
},
'command-param-id': {
pattern: /(\s)\w+:/,
lookbehind: true,
alias: 'property'
},
'command-param-value': [
{
pattern: expressionDef,
alias: 'selector',
},
{
pattern: /([\t ])\S+/,
lookbehind: true,
greedy: true,
alias: 'operator',
},
{
pattern: /\S(?:.*\S)?/,
alias: 'operator',
}
]
};

Prism.languages.naniscript = {
// ; ...
'comment': {
pattern: /^([\t ]*);.*/m,
lookbehind: true,
},
// > ...
// Define is a control line starting with '>' followed by a word, a space and a text.
'define': {
pattern: /^>.+/m,
alias: 'tag',
inside: {
'value': {
pattern: /(^>\w+[\t ]+)(?!\s)[^{}\r\n]+/,
lookbehind: true,
alias: 'operator'
},
'key': {
pattern: /(^>)\w+/,
lookbehind: true,
}
}
},
// # ...
'label': {
pattern: /^([\t ]*)#[\t ]*\w+[\t ]*$/m,
lookbehind: true,
alias: 'regex'
},
'command': {
pattern: /^([\t ]*)@\w+(?=[\t ]|$).*/m,
lookbehind: true,
alias: 'function',
inside: {
'command-name': /^@\w+/,
'expression': {
pattern: expressionDef,
greedy: true,
alias: 'selector'
},
'command-params': {
pattern: /[\s\S]*\S[\s\S]*/,
inside: params
},
}
},
// Generic is any line that doesn't start with operators: ;>#@
'generic-text': {
pattern: /(^[ \t]*)[^#@>;\s].*/m,
lookbehind: true,
alias: 'punctuation',
inside: {
// \{ ... \} ... \[ ... \] ... \"
'escaped-char': /\\[{}\[\]"]/,
'expression': {
pattern: expressionDef,
greedy: true,
alias: 'selector'
},
'inline-command': {
pattern: /\[[\t ]*\w+[^\r\n\[\]]*\]/,
greedy: true,
alias: 'function',
inside: {
'command-params': {
pattern: /(^\[[\t ]*\w+\b)[\s\S]+(?=\]$)/,
lookbehind: true,
inside: params
},
'command-param-name': {
pattern: /^(\[[\t ]*)\w+/,
lookbehind: true,
alias: 'name',
},
'start-stop-char': /[\[\]]/,
}
},
}
}
};
Prism.languages.nani = Prism.languages['naniscript'];

/** @typedef {InstanceType<import("./prism-core")["Token"]>} Token */

/**
* This hook is used to validate generic-text tokens for balanced brackets.
* Mark token as bad-line when contains not balanced brackets: {},[]
*/
Prism.hooks.add('after-tokenize', function (env) {
/** @type {(Token | string)[]} */
var tokens = env.tokens;
tokens.forEach(function (token) {
if (typeof token !== "string" && token.type === 'generic-text') {
var content = getTextContent(token);
if (!isBracketsBalanced(content)) {
token.type = 'bad-line';
token.content = content;
}
}
});
});

/**
* @param {string} input
* @returns {boolean}
*/
function isBracketsBalanced(input) {
var brackets = "[]{}";
var stack = [];
for (var i = 0; i < input.length; i++) {
var bracket = input[i];
var bracketsIndex = brackets.indexOf(bracket);
if (bracketsIndex !== -1) {
if (bracketsIndex % 2 === 0) {
stack.push(bracketsIndex + 1);
} else if (stack.pop() !== bracketsIndex) {
return false;
}
}
}
return stack.length === 0;
};

/**
* @param {string | Token | (string | Token)[]} token
* @returns {string}
*/
function getTextContent(token) {
if (typeof token === 'string') {
return token;
} else if (Array.isArray(token)) {
return token.map(getTextContent).join('');
} else {
return getTextContent(token.content);
}
}

})(Prism);
1 change: 1 addition & 0 deletions components/prism-naniscript.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions examples/prism-naniscript.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

<h2>Comments</h2>
<pre><code>;Text of Comment
; Comment with tabs before
</code></pre>

<h2>Define</h2>
<pre><code>
&gt;DefineKey define 12 super usefull lines
</code></pre>

<h2>Label</h2>
<pre><code># Section
#Section without whitespace
# Section with whitespace
# SectionWithTab
</code></pre>

<h2>Command</h2>
<pre><code>
@
@ cmdWithWhiteSpaceBefore
@cmdWithTrailingSemicolon:
@paramlessCmd
@cmdWithNoParamsAndWhitespaceBefore
@cmdWithNoParamsAndTabBefore
@cmdWithNoParamsAndTabAndSpacesBefore
@cmdWithNoParamsWrappedInWhitespaces
@cmdWithNoParamWithTrailingSpace
@cmdWithNoParamWithMultipleTrailingSpaces
@cmdWithNoParamWithTrailingTab
@cmdWithNoParamWithTrailingTabAndSpaces
@cmdWithPositiveIntParam 1
@cmdWithNegativeIntParam -1
@cmdWithPositiveFloatParamAndNoFraction 1.
@cmdWithPositiveFloatParamAndFraction 1.10
@cmdWithPositiveHegativeFloatParamAndNoFraction -1.
@cmdWithPositiveHegativeFloatParamAndFraction -1.10
@cmdWithBoolParamAndPositive true
@cmdWithBoolParamAndNegative false
@cmdWithStringParam hello$co\:mma"d"
@cmdWithQuotedStringNamelessParameter "hello grizzly"
@cmdWithQuotedStringNamelessParameterWithEscapedQuotesInTheValue "hello \"grizzly\""
@set choice="moe"
@command hello.grizzly
@command one,two,three
@command 1,2,3
@command true,false,true
@command hi:grizzly
@command hi:1
@command hi:true
@command 1 in:forest danger:true
@char 1 pos:0.25,-0.75 look:right
</code></pre>

<h2>Generic Text</h2>
<pre><code>Generic text with inlined commands[i] example[command 1 danger:true] more text here [act danger:false true:false]
"Integer: a = {a} malesuada a + b = {a + b}", Random(a, b) = {Random(a, b)}, Random("foo", "bar", "foobar") = {Random("foo", "bar", "foobar")}
UnclosedExpression{ab{cndum dui dolor tincidu{nt [s[fa]sdf [
"Integer: a = {a} malesuada a + b = {a + b}", Random(a, b) = {Random(a, b)}, Random("foo", "bar", "foobar") = {Random("foo", "bar", "foobar")},}
</code></pre>

<h2>Expressions</h2>
<pre><code>{}
{ Abs(a, d) + 12 - 1 / -230.0 + "Lol ipsum" }
Expressions inside a generic text line: Loreim ipsu,{ Abs(a, d) + 12 - 1 / -230.0 + "Lol ipsum" } doler sit amen {¯\_(ツ)_/¯}.
@ExpressionInsteadOfNamelessParameterValue {x > 0}
@ExpressionBlendedWithNamelessParameterValue sdf{x > 0}df
@ExpressionsInsideNamedParameterValueWrappedInQuotes text:"{a} &lt; {b}"
@ExpressionsBlendedWithNamedParameterValue param:32r2f,df{x > 0},d.{Abs(0) + 12.24 > 0}ff
@ExpressionsInsteadOfNamelessParameterAndQuotedParameter {remark} if:remark=="Saying \\"Stop { "the" } car\\" was a mistake."
</code></pre>
1 change: 1 addition & 0 deletions plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@
"md": "markdown",
"moon": "moonscript",
"n4jsd": "n4js",
"nani": "naniscript",
"objc": "objectivec",
"objectpascal": "pascal",
"px": "pcaxis",
Expand Down
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions plugins/show-language/prism-show-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@
"n4js": "N4JS",
"n4jsd": "N4JS",
"nand2tetris-hdl": "Nand To Tetris HDL",
"naniscript": "Naninovel Script",
"nani": "Naninovel Script",
"nasm": "NASM",
"neon": "NEON",
"nginx": "nginx",
Expand Down
Loading

0 comments on commit 388ad99

Please sign in to comment.