-
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.
Merge pull request #808 from Golmote/prism-parser
Add support for Parser
- Loading branch information
Showing
19 changed files
with
866 additions
and
5 deletions.
There are no files selected for viewing
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
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,66 @@ | ||
Prism.languages.parser = Prism.languages.extend('markup', { | ||
'keyword': { | ||
pattern: /(^|[^^])(?:\^(?:case|eval|for|if|switch|throw)\b|@(?:BASE|CLASS|GET(?:_DEFAULT)?|OPTIONS|SET_DEFAULT|USE)\b)/, | ||
lookbehind: true | ||
}, | ||
'variable': { | ||
pattern: /(^|[^^])\B\$(?:\w+|(?=[.\{]))(?:(?:\.|::?)\w+)*(?:\.|::?)?/, | ||
lookbehind: true, | ||
inside: { | ||
'punctuation': /\.|:+/ | ||
} | ||
}, | ||
'function': { | ||
pattern: /(^|[^^])\B[@^]\w+(?:(?:\.|::?)\w+)*(?:\.|::?)?/, | ||
lookbehind: true, | ||
inside: { | ||
'keyword': { | ||
pattern: /(^@)(?:GET_|SET_)/, | ||
lookbehind: true | ||
}, | ||
'punctuation': /\.|:+/ | ||
} | ||
}, | ||
'escape': { | ||
pattern: /\^(?:[$^;@()\[\]{}"':]|#[a-f\d]*)/i, | ||
alias: 'builtin' | ||
}, | ||
'punctuation': /[\[\](){};]/ | ||
}); | ||
Prism.languages.insertBefore('parser', 'keyword', { | ||
'parser-comment': { | ||
pattern: /(\s)#.*/, | ||
lookbehind: true, | ||
alias: 'comment' | ||
}, | ||
'expression': { | ||
// Allow for 3 levels of depth | ||
pattern: /(^|[^^])\((?:[^()]|\((?:[^()]|\((?:[^()])*\))*\))*\)/, | ||
lookbehind: true, | ||
inside: { | ||
'string': { | ||
pattern: /(^|[^^])(["'])(?:(?!\2)[^^]|\^[\s\S])*\2/, | ||
lookbehind: true | ||
}, | ||
'keyword': Prism.languages.parser.keyword, | ||
'variable': Prism.languages.parser.variable, | ||
'function': Prism.languages.parser.function, | ||
'boolean': /\b(?:true|false)\b/, | ||
'number': /\b(?:0x[a-f\d]+|\d+\.?\d*(?:e[+-]?\d+)?)\b/i, | ||
'escape': Prism.languages.parser.escape, | ||
'operator': /[~+*\/\\%]|!(?:\|\|?|=)?|&&?|\|\|?|==|<[<=]?|>[>=]?|-[fd]?|\b(?:def|eq|ge|gt|in|is|le|lt|ne)\b/, | ||
'punctuation': Prism.languages.parser.punctuation | ||
} | ||
} | ||
}); | ||
Prism.languages.insertBefore('inside', 'punctuation', { | ||
'expression': Prism.languages.parser.expression, | ||
'keyword': Prism.languages.parser.keyword, | ||
'variable': Prism.languages.parser.variable, | ||
'function': Prism.languages.parser.function, | ||
'escape': Prism.languages.parser.escape, | ||
'parser-punctuation': { | ||
pattern: Prism.languages.parser.punctuation, | ||
alias: 'punctuation' | ||
} | ||
}, Prism.languages.parser['tag'].inside['attr-value']); |
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,97 @@ | ||
<h1>Parser</h1> | ||
<p>To use this language, use the class "language-parser".</p> | ||
|
||
<h2>Comments</h2> | ||
<pre><code>$foo[bar] # Some comment</code></pre> | ||
|
||
<h2>Variables and functions</h2> | ||
<pre><code>@navigation[] | ||
$sections[^table::load[sections.cfg]] | ||
$sections.uri</code></pre> | ||
|
||
<h2>Literals</h2> | ||
<pre><code>$foo(3+$bar) | ||
^switch[$sMode]{ | ||
^case[def]{$result(true)} | ||
} | ||
^if(in "/news/"){}</code></pre> | ||
|
||
<h2>Escape sequences</h2> | ||
<pre><code>^^ | ||
^" | ||
^;</code></pre> | ||
|
||
<h2>Embedded in markup</h2> | ||
<pre><code><nav> | ||
<ul> | ||
^sections.menu{ | ||
<li> | ||
<a href="$sections.uri">$sections.name</a> | ||
</li> | ||
} | ||
</ul> | ||
</nav></code></pre> | ||
|
||
<h2>Full example</h2> | ||
<pre><code>@CLASS | ||
MyTable | ||
|
||
@create[uParam] | ||
^switch[$uParam.CLASS_NAME]{ | ||
^case[string;void]{$t[^table::create{$uParam}]} | ||
^case[table;MyTable]{$t[^table::create[$uParam]]} | ||
^case[DEFAULT]{^throw[MyTable;Unsupported type $uParam.CLASS_NAME]} | ||
} | ||
|
||
# method will return value in different calling contexts | ||
@GET[sMode] | ||
^switch[$sMode]{ | ||
^case[table]{$result[$t]} | ||
^case[bool]{$result($t!=0)} | ||
^case[def]{$result(true)} | ||
^case[expression;double]{$result($t)} | ||
^case[DEFAULT]{^throw[MyTable;Unsupported mode '$sMode']} | ||
} | ||
|
||
|
||
# method will handle access to the "columns" | ||
@GET_DEFAULT[sName] | ||
$result[$t.$sName] | ||
|
||
|
||
# wrappers for all existing methods are required | ||
@count[] | ||
^t.count[] | ||
|
||
@menu[jCode;sSeparator] | ||
^t.menu{$jCode}[$sSeparator] | ||
|
||
|
||
# new functionality | ||
@remove[iOffset;iLimit] | ||
$iLimit(^iLimit.int(0)) | ||
$t[^t.select(^t.offset[]<$iOffset || ^t.offset[]>=$iOffset+$iLimit)]</code></pre> | ||
|
||
<h2>Known failures</h2> | ||
<p>There are certain edge cases where Prism will fail. | ||
There are always such cases in every regex-based syntax highlighter. | ||
However, Prism dares to be open and honest about them. | ||
If a failure is listed here, it doesn’t mean it will never be fixed. This is more of a “known bugs” list, just with a certain type of bug. | ||
</p> | ||
|
||
<h3>Comment-like substrings</h3> | ||
<pre><code>("fo #o")</code></pre> | ||
|
||
<h3>Tag-like substrings</h3> | ||
<pre><code>("fo<div>o")</code></pre> | ||
|
||
<h3>Code block starting with a comment</h3> | ||
<pre><code># Doesn't work | ||
# Does work</code></pre> | ||
<pre><code> # Does work when prefixed with a space</code></pre> | ||
|
||
<h3>Comments inside expressions break literals and operators</h3> | ||
<pre><code>^if( | ||
$age>=4 # not too young | ||
&& $age<=80 # and not too old | ||
)</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
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,21 @@ | ||
(true) | ||
(false) | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["expression", [ | ||
["punctuation", "("], | ||
["boolean", "true"], | ||
["punctuation", ")"] | ||
]], | ||
["expression", [ | ||
["punctuation", "("], | ||
["boolean", "false"], | ||
["punctuation", ")"] | ||
]] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for booleans inside expressions. |
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,68 @@ | ||
^$ | ||
^^ | ||
^; | ||
^@ | ||
^( | ||
^) | ||
^[ | ||
^] | ||
^{ | ||
^} | ||
^" | ||
^' | ||
^: | ||
^# | ||
^#20 | ||
^#af | ||
^#AF | ||
|
||
^^date::now | ||
^$foobar | ||
|
||
<div class="foo^^bar"> | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["escape", "^$"], | ||
["escape", "^^"], | ||
["escape", "^;"], | ||
["escape", "^@"], | ||
["escape", "^("], | ||
["escape", "^)"], | ||
["escape", "^["], | ||
["escape", "^]"], | ||
["escape", "^{"], | ||
["escape", "^}"], | ||
["escape", "^\""], | ||
["escape", "^'"], | ||
["escape", "^:"], | ||
["escape", "^#"], | ||
["escape", "^#20"], | ||
["escape", "^#af"], | ||
["escape", "^#AF"], | ||
|
||
["escape", "^^"], "date::now\r\n", | ||
["escape", "^$"], "foobar\r\n\r\n", | ||
|
||
["tag", [ | ||
["tag", [ | ||
["punctuation", "<"], | ||
"div" | ||
]], | ||
["attr-name", ["class"]], | ||
["attr-value", [ | ||
["punctuation", "="], | ||
["punctuation", "\""], | ||
"foo", | ||
["escape", "^^"], | ||
"bar", | ||
["punctuation", "\""] | ||
]], | ||
["punctuation", ">"] | ||
]] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for escapes. |
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,58 @@ | ||
((3-(9-2))*4) | ||
^eval(4+2) | ||
|
||
<div class="foo-^eval(4+2)"> | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["expression", [ | ||
["punctuation", "("], | ||
["punctuation", "("], | ||
["number", "3"], | ||
["operator", "-"], | ||
["punctuation", "("], | ||
["number", "9"], | ||
["operator", "-"], | ||
["number", "2"], | ||
["punctuation", ")"], | ||
["punctuation", ")"], | ||
["operator", "*"], | ||
["number", "4"], | ||
["punctuation", ")"] | ||
]], | ||
["keyword", "^eval"], | ||
|
||
["expression", [ | ||
["punctuation", "("], | ||
["number", "4"], | ||
["operator", "+"], | ||
["number", "2"], | ||
["punctuation", ")"] | ||
]], | ||
|
||
["tag", [ | ||
["tag", [ | ||
["punctuation", "<"], | ||
"div" | ||
]], | ||
["attr-name", ["class"]], | ||
["attr-value", [ | ||
["punctuation", "="], | ||
["punctuation", "\""], | ||
"foo-", | ||
["keyword", "^eval"], | ||
["expression", [ | ||
["punctuation", "("], | ||
["number", "4"], ["operator", "+"], ["number", "2"], | ||
["punctuation", ")"] | ||
]], | ||
["punctuation", "\""] | ||
]], | ||
["punctuation", ">"] | ||
]] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for expressions, up to 3 levels of depth. |
Oops, something went wrong.