Skip to content

Commit

Permalink
[Batch File] Fix line continuation
Browse files Browse the repository at this point in the history
This commit applies the following rule:

If `^` appears at the end of a line, the linefeed is stripped and the next
character is escaped or uses its special meaning, if any.
If the next character is also a linefeed, it is treated literal.

REM command seems to be working special by line continuation working only
directly after the first token.
  • Loading branch information
deathaxe committed Jun 22, 2024
1 parent 23c715e commit b82c178
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
27 changes: 23 additions & 4 deletions Batch File/Batch File.sublime-syntax
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
%YAML 1.2
---
# https://www.sublimetext.com/docs/syntax.html
# https://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts/4095133#4095133
name: Batch File
scope: source.dosbatch
version: 2
Expand Down Expand Up @@ -836,7 +837,19 @@ contexts:
- include: cmd-arg-help
- include: unquoted-eol-pop
- match: (?=\S)
set: cmd-rem-comment-body
set:
- cmd-rem-comment-body
- cmd-rem-comment-begin

cmd-rem-comment-begin:
# In REM comments only the first continuation directly following
# the first token is taken into account.
- meta_include_prototype: false
- match: \^\n
scope: punctuation.separator.continuation.line.dosbatch
set: line-continuation-body
- match: (?={{metachar}})
pop: 1

cmd-rem-comment-body:
- meta_include_prototype: false
Expand Down Expand Up @@ -1967,8 +1980,7 @@ contexts:
###[ PROTOTYPES ]#############################################################

bol-pop:
# Note: empty lines are ignored
- match: ^(?!$)
- match: ^
pop: 1

else-pop:
Expand All @@ -1983,7 +1995,14 @@ contexts:
line-continuations:
- match: \^\n
scope: punctuation.separator.continuation.line.dosbatch
push: bol-pop
push: line-continuation-body

line-continuation-body:
# The first linefeed diretly following a continuation is treated literal.
# Note: Must consume \n to work.
- match: ^\n
set: bol-pop
- include: bol-pop

eoc-pop:
# Note: An end of command appears in unquoted regions only!
Expand Down
46 changes: 46 additions & 0 deletions Batch File/tests/syntax_test_batch_file.bat
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
not a comment
:: ^^^^^^^^^^^^^^ - comment

REM ^
Line^
continuation after first token
:: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.command.rem.dosbatch comment.line.rem.dosbatch

REM
not a comment
:: ^^^^^^^^^^^^^^ - comment
Expand All @@ -59,6 +64,31 @@ REM This & and | echo "is commented out" ^
:: <- keyword.declaration.rem.dosbatch
:: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.command.rem.dosbatch comment.line.rem.dosbatch

REM Line^
continuation
:: <- meta.command.rem.dosbatch comment.line.rem.dosbatch
:: ^^^^^^^^^^ meta.command.rem.dosbatch comment.line.rem.dosbatch

REM Line^

continuation
:: <- meta.command.rem.dosbatch comment.line.rem.dosbatch
:: ^^^^^^^^^^ meta.command.rem.dosbatch comment.line.rem.dosbatch

REM Line^

continuation^
not a comment
:: <- - comment
:: ^^^^^^^^^^ - comment

REM Line^


not a comment
:: <- - comment
:: ^^^^^^^ - comment

REM No line ^
continuation
:: <- - comment
Expand Down Expand Up @@ -481,6 +511,22 @@ ECHO : Not a comment ^
:: ^ punctuation.definition.variable.dosbatch
:: ^^^^ variable.label.dosbatch - keyword

CALL ^

:EOF
:: ^^^^ meta.function-call.identifier.dosbatch
:: ^ - meta.function-call
:: ^ punctuation.definition.variable.dosbatch
:: ^^^^ variable.label.dosbatch - keyword

CALL ^


:EOF
:: ^^^^ - meta.function-call
:: ^ punctuation.definition.label.dosbatch
:: ^^^^ entity.name.label.dosbatch

CALL :foo 10 %1
::^ - meta.function-call
:: ^^^^^ meta.function-call.dosbatch
Expand Down

0 comments on commit b82c178

Please sign in to comment.