Home Download Buy Blog Forum Support

Comments in Batch Files

Comments in Batch Files

Postby Anodyzed on Thu Apr 25, 2013 7:15 pm

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:

Code: Select all
REM this is a comment


to color code properly is... First on line 33:

Code: Select all
<!-- <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:

Code: Select all
<!-- <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)
Anodyzed
 
Posts: 6
Joined: Thu Apr 25, 2013 7:06 pm

Re: Comments in Batch Files

Postby facelessuser on Thu Apr 25, 2013 7:22 pm

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.

:: This is a comment

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


Batch File.tmLanguage
Code: Select all
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>uuid</key>
        <string>E07EC438-7B75-4437-8AA1-DA94C1E6EACC</string>
        <key>patterns</key>
        <array>
            <dict>
                <key>name</key>
                <string>keyword.command.dosbatch</string>
                <key>match</key>
                <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>
            </dict>
            <dict>
                <key>name</key>
                <string>keyword.control.statement.dosbatch</string>
                <key>match</key>
                <string>\b(?i)(?:goto|call|exit)\b</string>
            </dict>
            <dict>
                <key>name</key>
                <string>keyword.control.conditional.if.dosbatch</string>
                <key>match</key>
                <string>\b(?i)if\s+((not)\s+)((?:exist|defined|errorlevel|cmdextversion)\b)?</string>
            </dict>
            <dict>
                <key>name</key>
                <string>keyword.control.conditional.dosbatch</string>
                <key>match</key>
                <string>\b(?i)(?:if|else)\b</string>
            </dict>
            <dict>
                <key>begin</key>
                <string>\b(?i)(for)\b</string>
                <key>beginCaptures</key>
                <dict>
                    <key>1</key>
                    <dict>
                        <key>name</key>
                        <string>keyword.control.repeat.dosbatch</string>
                    </dict>
                </dict>
                <key>end</key>
                <string>\b(do)\b</string>
                <key>endCaptures</key>
                <dict>
                    <key>1</key>
                    <dict>
                        <key>name</key>
                        <string>keyword.control.repeat.dosbatch</string>
                    </dict>
                </dict>
                <key>name</key>
                <string></string>
                <key>patterns</key>
                <array>
                    <dict>
                        <key>include</key>
                        <string>source.dosbatch</string>
                    </dict>
                    <dict>
                        <key>name</key>
                        <string>keyword.control.repeat.dosbatch</string>
                        <key>match</key>
                        <string>(?&lt;= )\b(?i)(in)\b(?= )</string>
                    </dict>
                </array>
            </dict>
            <dict>
                <key>name</key>
                <string>keyword.operator.dosbatch</string>
                <key>match</key>
                <string>\b(?:EQU|NEQ|LSS|LEQ|GTR|GEQ)\b</string>
            </dict>
            <dict>
                <key>include</key>
                <string>#variables</string>
            </dict>
            <dict>
                <key>captures</key>
                <dict>
                    <key>1</key>
                    <dict>
                        <key>name</key>
                        <string>keyword.command.rem.dosbatch</string>
                    </dict>
                    <key>2</key>
                    <dict>
                        <key>name</key>
                        <string>comment.line.rem.dosbatch</string>
                    </dict>
                </dict>
                <key>name</key>
                <string></string>
                <key>match</key>
                <string>(?:^|\s)@?((?i)rem)($|\s.*$)</string>
            </dict>
            <dict>
                <key>name</key>
                <string>comment.line.colons.dosbatch</string>
                <key>match</key>
                <string>\s*:\s*:.*$</string>
            </dict>
            <dict>
                <key>name</key>
                <string>entity.name.function.dosbatch</string>
                <key>match</key>
                <string>^\s*:\s*.*$</string>
            </dict>
            <dict>
                <key>begin</key>
                <string>"</string>
                <key>endCaptures</key>
                <dict>
                    <key>0</key>
                    <dict>
                        <key>name</key>
                        <string>punctuation.definition.string.end.shell</string>
                    </dict>
                </dict>
                <key>beginCaptures</key>
                <dict>
                    <key>0</key>
                    <dict>
                        <key>name</key>
                        <string>punctuation.definition.string.begin.shell</string>
                    </dict>
                </dict>
                <key>name</key>
                <string>string.quoted.double.dosbatch</string>
                <key>end</key>
                <string>"</string>
                <key>patterns</key>
                <array>
                    <dict>
                        <key>include</key>
                        <string>#variables</string>
                    </dict>
                </array>
            </dict>
            <dict>
                <key>name</key>
                <string>keyword.operator.pipe.dosbatch</string>
                <key>match</key>
                <string>[|]</string>
            </dict>
            <dict>
                <key>name</key>
                <string>keyword.operator.redirect.shell</string>
                <key>match</key>
                <string>&amp;&gt;|\d*&gt;&amp;\d*|\d*(&gt;&gt;|&gt;|&lt;)|\d*&lt;&amp;|\d*&lt;&gt;</string>
            </dict>
        </array>
        <key>repository</key>
        <dict>
            <key>variables</key>
            <dict>
                <key>patterns</key>
                <array>
                    <dict>
                        <key>name</key>
                        <string>variable.parameter.function.dosbatch</string>
                        <key>match</key>
                        <string>((?&lt;!\^)%[\d\*]|((?&lt;!\^)%){2}[A-Za-z\d]|((?&lt;!\^)%){1,2}~[A-Za-z_\d]+|(?&lt;!\^)%[A-Za-z_\d]+(?&lt;!\^)%)</string>
                    </dict>
                </array>
            </dict>
        </dict>
        <key>name</key>
        <string>Batch File</string>
        <key>scopeName</key>
        <string>source.dosbatch</string>
        <key>fileTypes</key>
        <array>
            <string>bat</string>
        </array>
    </dict>
</plist>


Comment.tmPreferences
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
   <key>name</key>
   <string>Comment</string>
   <key>scope</key>
   <string>source.dosbatch</string>
   <key>settings</key>
   <dict>
      <key>shellVariables</key>
      <array>
         <dict>
            <key>name</key>
            <string>TM_COMMENT_START</string>
            <key>value</key>
            <string>:: </string>
         </dict>
      </array>
   </dict>
   <key>uuid</key>
   <string>C831BAED-BA65-4F98-AE9A-553CE36EF008</string>
</dict>
</plist>
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm

Re: Comments in Batch Files

Postby Anodyzed on Thu Apr 25, 2013 7:55 pm

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*)
Anodyzed
 
Posts: 6
Joined: Thu Apr 25, 2013 7:06 pm

Re: Comments in Batch Files

Postby Anodyzed on Thu Apr 25, 2013 7:59 pm

Oh, one other omission I noticed in the Batch File.tmLanguage, "cmd" should be added as a fileType.
(*Chris*)
Anodyzed
 
Posts: 6
Joined: Thu Apr 25, 2013 7:06 pm

Re: Comments in Batch Files

Postby facelessuser on Thu Apr 25, 2013 8:22 pm

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.
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm

Re: Comments in Batch Files

Postby Anodyzed on Thu Apr 25, 2013 8:52 pm

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*)
Anodyzed
 
Posts: 6
Joined: Thu Apr 25, 2013 7:06 pm

Re: Comments in Batch Files

Postby facelessuser on Thu Apr 25, 2013 8:55 pm

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.
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm

Re: Comments in Batch Files

Postby Anodyzed on Thu Apr 25, 2013 9:36 pm

Awesome, thanks.
(*Chris*)
Anodyzed
 
Posts: 6
Joined: Thu Apr 25, 2013 7:06 pm

Re: Comments in Batch Files

Postby Anodyzed on Thu Apr 25, 2013 9:55 pm

Just to be clear for any who are following along at home, you have to unzip the <ST3 Install Dir>\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*)
Anodyzed
 
Posts: 6
Joined: Thu Apr 25, 2013 7:06 pm

Re: Comments in Batch Files

Postby MKANET on Sat Aug 31, 2013 4:12 am

I downloaded the latest Sublime 3 beta; but REM lines are still not treated as comments.
MKANET
 
Posts: 56
Joined: Fri Aug 30, 2013 5:06 pm


Return to Technical Support

Who is online

Users browsing this forum: Ation, Exabot [Bot], Google [Bot] and 25 guests