-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
707 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
>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} < {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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.