Skip to content

Commit 388ad99

Browse files
authored
Add support for NaniScript (#2494)
1 parent 187c8a6 commit 388ad99

15 files changed

+707
-3
lines changed

components.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,11 @@
728728
"title": "Nand To Tetris HDL",
729729
"owner": "stephanmax"
730730
},
731+
"naniscript": {
732+
"title": "Naninovel Script",
733+
"owner": "Elringus",
734+
"alias": "nani"
735+
},
731736
"nasm": {
732737
"title": "NASM",
733738
"owner": "rbmj"

components/prism-naniscript.js

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
(function (Prism) {
2+
3+
var expressionDef = /\{[^\r\n\[\]{}]*\}/;
4+
5+
var params = {
6+
'quoted-string': {
7+
pattern: /"(?:[^"\\]|\\.)*"/,
8+
alias: 'operator'
9+
},
10+
'command-param-id': {
11+
pattern: /(\s)\w+:/,
12+
lookbehind: true,
13+
alias: 'property'
14+
},
15+
'command-param-value': [
16+
{
17+
pattern: expressionDef,
18+
alias: 'selector',
19+
},
20+
{
21+
pattern: /([\t ])\S+/,
22+
lookbehind: true,
23+
greedy: true,
24+
alias: 'operator',
25+
},
26+
{
27+
pattern: /\S(?:.*\S)?/,
28+
alias: 'operator',
29+
}
30+
]
31+
};
32+
33+
Prism.languages.naniscript = {
34+
// ; ...
35+
'comment': {
36+
pattern: /^([\t ]*);.*/m,
37+
lookbehind: true,
38+
},
39+
// > ...
40+
// Define is a control line starting with '>' followed by a word, a space and a text.
41+
'define': {
42+
pattern: /^>.+/m,
43+
alias: 'tag',
44+
inside: {
45+
'value': {
46+
pattern: /(^>\w+[\t ]+)(?!\s)[^{}\r\n]+/,
47+
lookbehind: true,
48+
alias: 'operator'
49+
},
50+
'key': {
51+
pattern: /(^>)\w+/,
52+
lookbehind: true,
53+
}
54+
}
55+
},
56+
// # ...
57+
'label': {
58+
pattern: /^([\t ]*)#[\t ]*\w+[\t ]*$/m,
59+
lookbehind: true,
60+
alias: 'regex'
61+
},
62+
'command': {
63+
pattern: /^([\t ]*)@\w+(?=[\t ]|$).*/m,
64+
lookbehind: true,
65+
alias: 'function',
66+
inside: {
67+
'command-name': /^@\w+/,
68+
'expression': {
69+
pattern: expressionDef,
70+
greedy: true,
71+
alias: 'selector'
72+
},
73+
'command-params': {
74+
pattern: /[\s\S]*\S[\s\S]*/,
75+
inside: params
76+
},
77+
}
78+
},
79+
// Generic is any line that doesn't start with operators: ;>#@
80+
'generic-text': {
81+
pattern: /(^[ \t]*)[^#@>;\s].*/m,
82+
lookbehind: true,
83+
alias: 'punctuation',
84+
inside: {
85+
// \{ ... \} ... \[ ... \] ... \"
86+
'escaped-char': /\\[{}\[\]"]/,
87+
'expression': {
88+
pattern: expressionDef,
89+
greedy: true,
90+
alias: 'selector'
91+
},
92+
'inline-command': {
93+
pattern: /\[[\t ]*\w+[^\r\n\[\]]*\]/,
94+
greedy: true,
95+
alias: 'function',
96+
inside: {
97+
'command-params': {
98+
pattern: /(^\[[\t ]*\w+\b)[\s\S]+(?=\]$)/,
99+
lookbehind: true,
100+
inside: params
101+
},
102+
'command-param-name': {
103+
pattern: /^(\[[\t ]*)\w+/,
104+
lookbehind: true,
105+
alias: 'name',
106+
},
107+
'start-stop-char': /[\[\]]/,
108+
}
109+
},
110+
}
111+
}
112+
};
113+
Prism.languages.nani = Prism.languages['naniscript'];
114+
115+
/** @typedef {InstanceType<import("./prism-core")["Token"]>} Token */
116+
117+
/**
118+
* This hook is used to validate generic-text tokens for balanced brackets.
119+
* Mark token as bad-line when contains not balanced brackets: {},[]
120+
*/
121+
Prism.hooks.add('after-tokenize', function (env) {
122+
/** @type {(Token | string)[]} */
123+
var tokens = env.tokens;
124+
tokens.forEach(function (token) {
125+
if (typeof token !== "string" && token.type === 'generic-text') {
126+
var content = getTextContent(token);
127+
if (!isBracketsBalanced(content)) {
128+
token.type = 'bad-line';
129+
token.content = content;
130+
}
131+
}
132+
});
133+
});
134+
135+
/**
136+
* @param {string} input
137+
* @returns {boolean}
138+
*/
139+
function isBracketsBalanced(input) {
140+
var brackets = "[]{}";
141+
var stack = [];
142+
for (var i = 0; i < input.length; i++) {
143+
var bracket = input[i];
144+
var bracketsIndex = brackets.indexOf(bracket);
145+
if (bracketsIndex !== -1) {
146+
if (bracketsIndex % 2 === 0) {
147+
stack.push(bracketsIndex + 1);
148+
} else if (stack.pop() !== bracketsIndex) {
149+
return false;
150+
}
151+
}
152+
}
153+
return stack.length === 0;
154+
};
155+
156+
/**
157+
* @param {string | Token | (string | Token)[]} token
158+
* @returns {string}
159+
*/
160+
function getTextContent(token) {
161+
if (typeof token === 'string') {
162+
return token;
163+
} else if (Array.isArray(token)) {
164+
return token.map(getTextContent).join('');
165+
} else {
166+
return getTextContent(token.content);
167+
}
168+
}
169+
170+
})(Prism);

components/prism-naniscript.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/prism-naniscript.html

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
<h2>Comments</h2>
3+
<pre><code>;Text of Comment
4+
; Comment with tabs before
5+
</code></pre>
6+
7+
<h2>Define</h2>
8+
<pre><code>
9+
&gt;DefineKey define 12 super usefull lines
10+
</code></pre>
11+
12+
<h2>Label</h2>
13+
<pre><code># Section
14+
#Section without whitespace
15+
# Section with whitespace
16+
# SectionWithTab
17+
</code></pre>
18+
19+
<h2>Command</h2>
20+
<pre><code>
21+
@
22+
@ cmdWithWhiteSpaceBefore
23+
@cmdWithTrailingSemicolon:
24+
@paramlessCmd
25+
@cmdWithNoParamsAndWhitespaceBefore
26+
@cmdWithNoParamsAndTabBefore
27+
@cmdWithNoParamsAndTabAndSpacesBefore
28+
@cmdWithNoParamsWrappedInWhitespaces
29+
@cmdWithNoParamWithTrailingSpace
30+
@cmdWithNoParamWithMultipleTrailingSpaces
31+
@cmdWithNoParamWithTrailingTab
32+
@cmdWithNoParamWithTrailingTabAndSpaces
33+
@cmdWithPositiveIntParam 1
34+
@cmdWithNegativeIntParam -1
35+
@cmdWithPositiveFloatParamAndNoFraction 1.
36+
@cmdWithPositiveFloatParamAndFraction 1.10
37+
@cmdWithPositiveHegativeFloatParamAndNoFraction -1.
38+
@cmdWithPositiveHegativeFloatParamAndFraction -1.10
39+
@cmdWithBoolParamAndPositive true
40+
@cmdWithBoolParamAndNegative false
41+
@cmdWithStringParam hello$co\:mma"d"
42+
@cmdWithQuotedStringNamelessParameter "hello grizzly"
43+
@cmdWithQuotedStringNamelessParameterWithEscapedQuotesInTheValue "hello \"grizzly\""
44+
@set choice="moe"
45+
@command hello.grizzly
46+
@command one,two,three
47+
@command 1,2,3
48+
@command true,false,true
49+
@command hi:grizzly
50+
@command hi:1
51+
@command hi:true
52+
@command 1 in:forest danger:true
53+
@char 1 pos:0.25,-0.75 look:right
54+
</code></pre>
55+
56+
<h2>Generic Text</h2>
57+
<pre><code>Generic text with inlined commands[i] example[command 1 danger:true] more text here [act danger:false true:false]
58+
"Integer: a = {a} malesuada a + b = {a + b}", Random(a, b) = {Random(a, b)}, Random("foo", "bar", "foobar") = {Random("foo", "bar", "foobar")}
59+
UnclosedExpression{ab{cndum dui dolor tincidu{nt [s[fa]sdf [
60+
"Integer: a = {a} malesuada a + b = {a + b}", Random(a, b) = {Random(a, b)}, Random("foo", "bar", "foobar") = {Random("foo", "bar", "foobar")},}
61+
</code></pre>
62+
63+
<h2>Expressions</h2>
64+
<pre><code>{}
65+
{ Abs(a, d) + 12 - 1 / -230.0 + "Lol ipsum" }
66+
Expressions inside a generic text line: Loreim ipsu,{ Abs(a, d) + 12 - 1 / -230.0 + "Lol ipsum" } doler sit amen {¯\_(ツ)_/¯}.
67+
@ExpressionInsteadOfNamelessParameterValue {x > 0}
68+
@ExpressionBlendedWithNamelessParameterValue sdf{x > 0}df
69+
@ExpressionsInsideNamedParameterValueWrappedInQuotes text:"{a} &lt; {b}"
70+
@ExpressionsBlendedWithNamedParameterValue param:32r2f,df{x > 0},d.{Abs(0) + 12.24 > 0}ff
71+
@ExpressionsInsteadOfNamelessParameterAndQuotedParameter {remark} if:remark=="Saying \\"Stop { "the" } car\\" was a mistake."
72+
</code></pre>

plugins/autoloader/prism-autoloader.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@
187187
"md": "markdown",
188188
"moon": "moonscript",
189189
"n4jsd": "n4js",
190+
"nani": "naniscript",
190191
"objc": "objectivec",
191192
"objectpascal": "pascal",
192193
"px": "pcaxis",

plugins/autoloader/prism-autoloader.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/show-language/prism-show-language.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@
119119
"n4js": "N4JS",
120120
"n4jsd": "N4JS",
121121
"nand2tetris-hdl": "Nand To Tetris HDL",
122+
"naniscript": "Naninovel Script",
123+
"nani": "Naninovel Script",
122124
"nasm": "NASM",
123125
"neon": "NEON",
124126
"nginx": "nginx",

0 commit comments

Comments
 (0)