Skip to content

Commit

Permalink
Add support for Batch (Fix #398)
Browse files Browse the repository at this point in the history
  • Loading branch information
Golmote committed Sep 28, 2015
1 parent 792e35c commit eab5b06
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 0 deletions.
4 changes: 4 additions & 0 deletions components.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ var components = {
"title": "BASIC",
"owner": "Golmote"
},
"batch": {
"title": "Batch",
"owner": "Golmote"
},
"bison": {
"title": "Bison",
"require": "c",
Expand Down
99 changes: 99 additions & 0 deletions components/prism-batch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
(function (Prism) {
var variable = /%%?[~:\w]+%?|!\S+!/;
var parameter = {
pattern: /\/[a-z?]+(?=[ :]|$):?|-[a-z]\b|--[a-z-]+\b/im,
alias: 'attr-name',
inside: {
'punctuation': /:/
}
};
var string = /"[^"]*"/;
var number = /(?:\b|-)\d+\b/;

Prism.languages.batch = {
'comment': [
/^::.*/m,
{
pattern: /((?:^|[&(])[ \t]*)rem\b(?:[^^&)\r\n]|\^(?:\r\n|[\s\S]))*/im,
lookbehind: true
}
],
'label': {
pattern: /^:.*/m,
alias: 'property'
},
'command': [
{
// FOR command
pattern: /((?:^|[&(])[ \t]*)for(?: ?\/[a-z?](?:[ :](?:"[^"]*"|\S+))?)* \S+ in \([^)]+\) do/im,
lookbehind: true,
inside: {
'keyword': /^for\b|\b(?:in|do)\b/i,
'string': string,
'parameter': parameter,
'variable': variable,
'number': number,
'punctuation': /[()',]/
}
},
{
// IF command
pattern: /((?:^|[&(])[ \t]*)if(?: ?\/[a-z?](?:[ :](?:"[^"]*"|\S+))?)* (?:not )?(?:cmdextversion \d+|defined \w+|errorlevel \d+|exist \S+|(?:"[^"]*"|\S+)?(?:==| (?:equ|neq|lss|leq|gtr|geq) )(?:"[^"]*"|\S+))/im,
lookbehind: true,
inside: {
'keyword': /^if\b|\b(?:not|cmdextversion|defined|errorlevel|exist)\b/i,
'string': string,
'parameter': parameter,
'variable': variable,
'number': number,
'operator': /\^|==|\b(?:equ|neq|lss|leq|gtr|geq)\b/i
}
},
{
// ELSE command
pattern: /((?:^|[&()])[ \t]*)else\b/im,
lookbehind: true,
inside: {
'keyword': /^else\b/i
}
},
{
// SET command
pattern: /((?:^|[&(])[ \t]*)set(?: ?\/[a-z](?:[ :](?:"[^"]*"|\S+))?)* (?:[^^&)\r\n]|\^(?:\r\n|[\s\S]))*/im,
lookbehind: true,
inside: {
'keyword': /^set\b/i,
'string': string,
'parameter': parameter,
'variable': [
variable,
/\w+(?=(?:[*\/%+\-&^|]|<<|>>)?=)/
],
'number': number,
'operator': /[*\/%+\-&^|]=?|<<=?|>>=?|[!~_=]/,
'punctuation': /[()',]/
}
},
{
// Other commands
pattern: /((?:^|[&(])[ \t]*@?)\w+\b(?:[^^&)\r\n]|\^(?:\r\n|[\s\S]))*/im,
lookbehind: true,
inside: {
'keyword': /^\w+\b/i,
'string': string,
'parameter': parameter,
'label': {
pattern: /(^\s*):\S+/m,
lookbehind: true,
alias: 'property'
},
'variable': variable,
'number': number,
'operator': /\^/
}
}
],
'operator': /[&@]/,
'punctuation': /[()']/
};
}(Prism));
1 change: 1 addition & 0 deletions components/prism-batch.min.js

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

20 changes: 20 additions & 0 deletions examples/prism-batch.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<h1>Batch</h1>
<p>To use this language, use the class "language-batch".</p>

<h2>Comments</h2>
<pre><code>::
:: Foo bar
REM This is a comment too
REM Multi-line ^
comment</code></pre>

<h2>Labels</h2>
<pre><code>:foobar
GOTO :EOF</code></pre>

<h2>Commands</h2>
<pre><code>@ECHO OFF
FOR /l %%a in (5,-1,1) do (TITLE %title% -- closing in %%as)
SET title=%~n0
if /i "%InstSize:~0,1%"=="M" set maxcnt=3
ping -n 2 -w 1 127.0.0.1</code></pre>
103 changes: 103 additions & 0 deletions tests/languages/batch/command_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
FOR /l %%a in (5,-1,1) do (TITLE %title% -- closing in %%as)
SET title=%~n0
echo.Hello World
@ECHO OFF
if not defined ProgressFormat set "ProgressFormat=[PPPP]"
EXIT /b
set /a ProgressCnt+=1
IF "%~1" NEQ "" (SET %~1=%new%) ELSE (echo.%new%)

----------------------------------------------------

[
["command", [
["keyword", "FOR"],
["parameter", ["/l"]],
["variable", "%%a"],
["keyword", "in"],
["punctuation", "("],
["number", "5"], ["punctuation", ","],
["number", "-1"], ["punctuation", ","],
["number", "1"], ["punctuation", ")"],
["keyword", "do"]
]],
["punctuation", "("],
["command", [
["keyword", "TITLE"],
["variable", "%title%"],
" -- closing in ",
["variable", "%%as"]
]],
["punctuation", ")"],

["command", [
["keyword", "SET"],
["variable", "title"],
["operator", "="],
["variable", "%~n0"]
]],

["command", [
["keyword", "echo"],
".Hello World"
]],

["operator", "@"],
["command", [
["keyword", "ECHO"],
" OFF"
]],

["command", [
["keyword", "if"],
["keyword", "not"],
["keyword", "defined"],
" ProgressFormat"
]],
["command", [
["keyword", "set"],
["string", "\"ProgressFormat=[PPPP]\""]
]],

["command", [
["keyword", "EXIT"],
["parameter", ["/b"]]
]],

["command", [
["keyword", "set"],
["parameter", ["/a"]],
["variable", "ProgressCnt"],
["operator", "+="],
["number", "1"]
]],

["command", [
["keyword", "IF"],
["string", "\"%~1\""],
["operator", "NEQ"],
["string", "\"\""]
]],
["punctuation", "("],
["command", [
["keyword", "SET"],
["variable", "%~1"],
["operator", "="],
["variable", "%new%"]
]],
["punctuation", ")"],
["command", [
["keyword", "ELSE"]
]],
["punctuation", "("],
["command", [
["keyword", "echo"],
".",
["variable", "%new%"]
]],
["punctuation", ")"]
]

----------------------------------------------------

Checks for commands.
18 changes: 18 additions & 0 deletions tests/languages/batch/comment_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
::
:: Foobar
REM Foobar
rem foo^
bar

----------------------------------------------------

[
["comment", "::"],
["comment", ":: Foobar"],
["comment", "REM Foobar"],
["comment", "rem foo^\r\nbar"]
]

----------------------------------------------------

Checks for comments.
13 changes: 13 additions & 0 deletions tests/languages/batch/label_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
:foo
:Foo_Bar

----------------------------------------------------

[
["label", ":foo"],
["label", ":Foo_Bar"]
]

----------------------------------------------------

Checks for labels.

0 comments on commit eab5b06

Please sign in to comment.