Sublime Forum

Comments in Batch Files

#1

There’s a bug in the comment parsing in Batch File.tmLanguage for both Sublime Text 2 & 3. I was able to fix it in ST2 since the source is available, but I can’t find where the source lives in ST3 so I can’t fix it myself. The required changes to get lines like:

REM this is a comment

to color code properly is… First on line 33:

<!-- <string>\b(?i)(?:append|assoc|at|attrib|break|cacls|cd|chcp|chdir|chkdsk|chkntfs|cls|cmd|color|comp|compact|convert|copy|date|del|dir|diskcomp|diskcopy|doskey|echo|endlocal|erase|fc|find|findstr|format|ftype|graftabl|help|keyb|label|md|mkdir|mode|more|move|path|pause|popd|print|prompt|pushd|rd|recover|rem|ren|rename|replace|restore|rmdir|set|setlocal|shift|sort|start|subst|time|title|tree|type|ver|verify|vol|xcopy)\b</string> -->
     <string>\b(?i)(?:append|assoc|at|attrib|break|cacls|cd|chcp|chdir|chkdsk|chkntfs|cls|cmd|color|comp|compact|convert|copy|date|del|dir|diskcomp|diskcopy|doskey|echo|endlocal|erase|fc|find|findstr|format|ftype|graftabl|help|keyb|label|md|mkdir|mode|more|move|path|pause|popd|print|prompt|pushd|rd|recover|ren|rename|replace|restore|rmdir|set|setlocal|shift|sort|start|subst|time|title|tree|type|ver|verify|vol|xcopy)\b</string>

And next on line 57:

<!-- <string>(?:^|\s)((?i)rem)(?:$|\s.*$)</string> -->
     <string>(?:^|\s)(?i:rem)(?:$|\s.*$)</string>

Note: Tried to attach the update Batch File.tmLanguage language, but .tmLanguage files are not allowed to be attached (seems strange to me)

0 Likes

#2

Well Rem are kind of comments, but not really. I modified my batch syntax to handle comments proper, and to use the toggle comment shortcut to comment lines with the proper comments. You can see that I handle the text after REM as comments though.

[pre=#2D2D2D]:: This is a comment

REM This is not really a comment, but people like to use it as such.[/pre]

Batch File.tmLanguage

[code]<?xml version="1.0" encoding="UTF-8" ?>

uuid E07EC438-7B75-4437-8AA1-DA94C1E6EACC patterns name keyword.command.dosbatch match \b(?i)(?:append|assoc|at|attrib|break|cacls|cd|chcp|chdir|chkdsk|chkntfs|cls|cmd|color|comp|compact|convert|copy|date|del|dir|diskcomp|diskcopy|doskey|echo|endlocal|erase|fc|find|findstr|format|ftype|graftabl|help|keyb|label|md|mkdir|mode|more|move|path|pause|popd|print|prompt|pushd|rd|recover|ren|rename|replace|restore|rmdir|set|setlocal|shift|sort|start|subst|time|title|tree|type|ver|verify|vol|xcopy)\b name keyword.control.statement.dosbatch match \b(?i)(?:goto|call|exit)\b name keyword.control.conditional.if.dosbatch match \b(?i)if\s+((not)\s+)((?:exist|defined|errorlevel|cmdextversion)\b)? name keyword.control.conditional.dosbatch match \b(?i)(?:if|else)\b begin \b(?i)(for)\b beginCaptures 1 name keyword.control.repeat.dosbatch end \b(do)\b endCaptures 1 name keyword.control.repeat.dosbatch name patterns include source.dosbatch name keyword.control.repeat.dosbatch match (?<= )\b(?i)(in)\b(?= ) name keyword.operator.dosbatch match \b(?:EQU|NEQ|LSS|LEQ|GTR|GEQ)\b include #variables captures 1 name keyword.command.rem.dosbatch 2 name comment.line.rem.dosbatch name match (?:^|\s)@?((?i)rem)($|\s.*$) name comment.line.colons.dosbatch match \s*:\s*:.*$ name entity.name.function.dosbatch match ^\s*:\s*.*$ begin " endCaptures 0 name punctuation.definition.string.end.shell beginCaptures 0 name punctuation.definition.string.begin.shell name string.quoted.double.dosbatch end " patterns include #variables name keyword.operator.pipe.dosbatch match |] name keyword.operator.redirect.shell match &>|\d*>&\d*|\d*(>>|>|<)|\d*<&|\d*<> repository variables patterns name variable.parameter.function.dosbatch match ((?<!\^)%\d\*]|((?<!\^)%){2}[A-Za-z\d]|((?<!\^)%){1,2}~[A-Za-z_\d]+|(?<!\^)%[A-Za-z_\d]+(?<!\^)%) name Batch File scopeName source.dosbatch fileTypes bat [/code]

Comment.tmPreferences

[code]<?xml version="1.0" encoding="UTF-8"?>

name Comment scope source.dosbatch settings shellVariables name TM_COMMENT_START value :: uuid C831BAED-BA65-4F98-AE9A-553CE36EF008 [/code]
0 Likes

#3

How are REM (Remarks) not comments?? They were actually the only way to add comments to code for a very long time. It wasn’t until sometime around Windows NT when :: was allowed as a pseudo-comment (it’s really just an invalid label).
(Chris)

0 Likes

#4

Oh, one other omission I noticed in the Batch File.tmLanguage, “cmd” should be added as a fileType.
(Chris)

0 Likes

#5

You are correct REM (remark) is technically a comment (I withdraw my claim), but I have always found it annoying that when echo is on, REM statements appear. REM is actually a command that does nothing that allows you to insert remarks/comments. I guess :: is a label that cannot be called, but I have always found its behavior more like what I expect from a comment. When echo is on, :: comments are still just comments.

0 Likes

#6

Is there a way to update the language parsers in ST3? It was pretty easy to fix the problem in ST2 because the source was immediately editable, but with ST3 I can only find the binary versions.
(Chris)

0 Likes

#7

It is not really a binary, just a zip file. You can unzip it in the Packages Folder. So if it is called Batch.sublime-package you unzip in Packages/Batch.

0 Likes

#8

Awesome, thanks.
(Chris)

0 Likes

#9

Just to be clear for any who are following along at home, you have to unzip the \Packages\Batch File.sublime-package file into a “Batch File” sub-directory of your ST3 User Directory (Preferences -> Browse Packages…). Once that’s done, any saved changes to the file should affect the color coding of the batch files immediately.
(Chris)

0 Likes

#10

I downloaded the latest Sublime 3 beta; but REM lines are still not treated as comments.

0 Likes