Strange double syntax error

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Strange double syntax error

#1 Post by jeb » 14 Jun 2019 01:29

Hi,

I found a strange behaviour in the syntax error handling.

I forced a syntax error with a simple %VAR:=WRONG SYNTAX%

Normally you see something like
Output wrote:"=WRONG SYNTAX%
" kann syntaktisch an dieser Stelle nicht verarbeitet werden.
But if the preceding line contains one of the internal-internal commands REM,IF, FOR (with a special parser), then the error message changes a little bit (One more line is appended)

Code: Select all

set var=CONTENT
REM
%var:=WRONG SYNTAX%
Output wrote:"=WRONG SYNTAX%
" kann syntaktisch an dieser Stelle nicht verarbeitet werden.
Syntaxfehler.
But when I change "REM" to "REM." the error message changes again

Code: Select all

setlocal
set var=CONTENT
set "PATH=%PATH%;C:\UNEXPECTED"
REM.
%var:=WRONG SYNTAX%
The second error message takes the last part of the PATH variable and concatenate the last command.
This even works when I changed the command to something different like "echo.HELLO"
Output wrote: "=WRONG SYNTAX%
" kann syntaktisch an dieser Stelle nicht verarbeitet werden.
"C:\UNEXPECTED\REM..*" kann syntaktisch an dieser Stelle nicht verarbeitet werden.
When the problematic code is just behind a called label the output changes again

Code: Select all

@echo off

setlocal
set var=CONTENT
goto :label

:label You can see the whole line
%var:=WRONG SYNTAX%
Output wrote:"=WRONG SYNTAX%
" kann syntaktisch an dieser Stelle nicht verarbeitet werden.
":label You can see the whole line
" kann syntaktisch an dieser Stelle nicht verarbeitet werden.
But why I show these observations :!:
Because, I hope to find a way to execute some commands after a syntax error occours.

jeb

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Strange double syntax error

#2 Post by dbenham » 14 Jun 2019 06:45

:shock: Very interesting

But I do not get the same results on my Windows 10 64 bit machine. :?

What version are you using?

The only situation that gives different behavior for me is if the fatal syntax error follows immediately after a CALLed :label (GOTO does not work)
And even in this case, only the label itself is shown, not the entire label line.

For my tests below, it is important that the last line of the script does not have a newline.

My Test 10 and Test 11 show something interesting. The fatal syntax error message prints the entire remainder of the line. The message split for all but test 11 indicates to me that Phase 0 preserves the newline at the end of the line. So it makes sense that Phase 1 has code to truncate at the first newline. But the newline tests at Examination of Linefeeds with CALL show that Phase 1 does not truncate at newlines between expanding percents. I am liking the recent rule changes to Phase 1. :D

Code: Select all

@echo off
if "%~1" neq "" goto %1
setlocal
set var=CONTENT

for /l %%N in (1 1 11) do (
  echo ====== TEST %%N ======
  cmd /c ^""%~f0" test%%N^"
  echo --------------------
)
exit /b

:test1
echo after ECHO
%var:=WRONG SYNTAX%

:test2
if 1==1 echo after IF
%var:=WRONG SYNTAX%

:test3
for %%a in (.) do echo after FOR
%var:=WRONG SYNTAX%

:test4
echo within FOR
for %%a in (.) do (
  %var:=WRONG SYNTAX%
)

:test5
echo after REM
rem Doesn't make a difference for me
%var:=WRONG SYNTAX%

:test6
echo after REM.
rem.Doesn't make a difference for me
%var:=WRONG SYNTAX%

:test7
echo after GOTO no colon
goto afterGotoNoColon
:afterGotoNoColon
%var:=WRONG SYNTAX%

:test8
echo after GOTO with colon
goto :afterGotoWithColon
:afterGotoWithColon
%var:=WRONG SYNTAX%

:test9
echo The :label only is shown, not the entire :label line
call :afterCall
=====:afterCall THIS IS NOT SHOWN
%var:=WRONG SYNTAX%

:test10
%var:=WRONG SYNTAX% WITH NEWLINE

:test11
%var:=WRONG SYNTAX% WITHOUT NEWLINE
OUTPUT wrote: ====== TEST 1 ======
after ECHO
=WRONG SYNTAX%
was unexpected at this time.
The syntax of the command is incorrect.
--------------------
====== TEST 2 ======
after IF
=WRONG SYNTAX%
was unexpected at this time.
The syntax of the command is incorrect.
--------------------
====== TEST 3 ======
after FOR
=WRONG SYNTAX%
was unexpected at this time.
The syntax of the command is incorrect.
--------------------
====== TEST 4 ======
within FOR
=WRONG SYNTAX%
was unexpected at this time.
The syntax of the command is incorrect.
--------------------
====== TEST 5 ======
after REM
=WRONG SYNTAX%
was unexpected at this time.
The syntax of the command is incorrect.
--------------------
====== TEST 6 ======
after REM.
=WRONG SYNTAX%
was unexpected at this time.
The syntax of the command is incorrect.
--------------------
====== TEST 7 ======
after GOTO no colon
=WRONG SYNTAX%
was unexpected at this time.
The syntax of the command is incorrect.
--------------------
====== TEST 8 ======
after GOTO with colon
=WRONG SYNTAX%
was unexpected at this time.
The syntax of the command is incorrect.
--------------------
====== TEST 9 ======
The :label only is shown, not the entire :label line
=WRONG SYNTAX%
was unexpected at this time.
:afterCall was unexpected at this time.
--------------------
====== TEST 10 ======
=WRONG SYNTAX% WITH NEWLINE
was unexpected at this time.
The syntax of the command is incorrect.
--------------------
====== TEST 11 ======
=WRONG SYNTAX% WITHOUT NEWLINE was unexpected at this time.
The syntax of the command is incorrect.
--------------------

Dave Benham

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Strange double syntax error

#3 Post by penpen » 14 Jun 2019 07:10

(Edit: Too slow.)
I always get the same error message in all your cases (using win 10, x64 home; cmd shell says Version 10.0.17134.829)!

Code: Select all

"=WRONG SYNTAX%" kann syntaktisch an dieser Stelle nicht verarbeitet werden.
Syntaxfehler.
jeb wrote:But why I show these observations :!:
Because, I hope to find a way to execute some commands after a syntax error occours.
Haven't you present a solution to that (or was it dave or antonio or ...) in another thread (but i'm sorry, can't remember which one)?

"test.bat"

Code: Select all

@echo off
setlocal enableExtensions
if "%~1" == "" (
	call;
	cmd /ccall "test.bat" "0"
	if errorlevel 1 call echo errorlevel: %%errorlevel%%
	echo(===

	call;
	cmd /ccall "test.bat" "1"
	if errorlevel 1 call echo errorlevel: %%errorlevel%%
	echo(===

	call;
	cmd /ccall "test.bat" "2"
	if errorlevel 1 call echo errorlevel: %%errorlevel%%
	echo(===

	call;
	cmd /ccall "test.bat" "3"
	if errorlevel 1 call echo errorlevel: %%errorlevel%%
	echo(===
) else if %~1. geq 0. if %~1. leq 3. (
	echo(Test %~1
	goto :error%~1
)
goto :eof

:error0
set var=CONTENT

%var:=WRONG SYNTAX%

goto :eof


:error1
set var=CONTENT
REM
%var:=WRONG SYNTAX%

goto :eof


:error2
setlocal
set var=CONTENT
set "PATH=%PATH%;C:\UNEXPECTED"
REM.
%var:=WRONG SYNTAX%

goto :eof


:error3
setlocal
set var=CONTENT
goto :label

:label You can see the whole line
%var:=WRONG SYNTAX%

goto :eof
Result:

Code: Select all

Test 0
"=WRONG SYNTAX%
" kann syntaktisch an dieser Stelle nicht verarbeitet werden.
Syntaxfehler.
errorlevel: 255
===
Test 1
"=WRONG SYNTAX%
" kann syntaktisch an dieser Stelle nicht verarbeitet werden.
Syntaxfehler.
errorlevel: 255
===
Test 2
"=WRONG SYNTAX%
" kann syntaktisch an dieser Stelle nicht verarbeitet werden.
Syntaxfehler.
errorlevel: 255
===
Test 3
"=WRONG SYNTAX%
" kann syntaktisch an dieser Stelle nicht verarbeitet werden.
Syntaxfehler.
errorlevel: 255
===
Or do you want to execute a 'local' command (directly after the syntax error)?


penpen

Post Reply