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

Add support for NaniScript #2494

Merged
merged 25 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
097674e
initial commit
elringus Aug 4, 2020
b239b2d
feat: add command param value token; fix: highlight inline command na…
vladdancer Aug 4, 2020
3f4fbe0
Merge pull request #1 from vladdancer/master
elringus Aug 4, 2020
86df911
minor changes
elringus Aug 4, 2020
4861fdf
fix: CI issues for build https://travis-ci.org/github/PrismJS/prism/b…
vladdancer Aug 4, 2020
0459f1d
Merge branch 'master' into master
vladdancer Aug 4, 2020
83ded98
Merge pull request #2 from vladdancer/master
elringus Aug 4, 2020
246150b
Update prism-naniscript.min.js
elringus Aug 4, 2020
ea142ca
fix inline comand value
elringus Aug 5, 2020
9ec0503
fix generic line wrapped in whitespace
elringus Aug 5, 2020
30f27ec
Naniscript improvements
RunDevelopment Aug 6, 2020
321b6ee
Merge branch 'master' into nani
RunDevelopment Aug 6, 2020
d2e15be
Figured it out.
RunDevelopment Aug 6, 2020
4c81348
Merge pull request #3 from RunDevelopment/nani
elringus Aug 6, 2020
5adf916
Update prism-naniscript.min.js
elringus Aug 6, 2020
ff3ba32
fix: use right tests descriptions
vladdancer Aug 7, 2020
05d0b64
fix: add new line
vladdancer Aug 7, 2020
2a25185
fix: add missing ']' char to the regex
vladdancer Aug 7, 2020
3082c2a
improve: remove odd part of the regex
vladdancer Aug 7, 2020
00aacec
Merge pull request #4 from vladdancer/master
elringus Aug 7, 2020
2faa612
Update prism-naniscript.min.js
elringus Aug 7, 2020
ff4bc3e
Update components/prism-naniscript.js
elringus Aug 7, 2020
782a6e3
Update components/prism-naniscript.js
elringus Aug 7, 2020
a3625d9
Update prism-naniscript.min.js
elringus Aug 7, 2020
76d5500
fix pattern ref
elringus Aug 7, 2020
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
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
172 changes: 172 additions & 0 deletions components/prism-naniscript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
(function (Prism) {

function expressionDef(isMultiline) {
return RegExp(/\{[^\r\n\[\]{}]*\}/, isMultiline ? 'm': '');
};
elringus marked this conversation as resolved.
Show resolved Hide resolved

var params = {
'quoted-string': {
pattern: /"(?:[^"\\]|\\.)*"/,
alias: 'operator'
},
'command-param-id': {
pattern: /(\s)\w+:/,
lookbehind: true,
alias: 'property'
},
'command-param-value': [
{
pattern: expressionDef(false),
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,
elringus marked this conversation as resolved.
Show resolved Hide resolved
lookbehind: true,
alias: 'regex'
},
'command': {
pattern: /^([\t ]*)@\w+(?=[\t ]|$).*/m,
lookbehind: true,
alias: 'function',
inside: {
'command-name': /^@\w+/,
'expression': {
pattern: expressionDef(true),
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
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(true),
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