idea-batch icon indicating copy to clipboard operation
idea-batch copied to clipboard

Syntax highlighter marking code after "if defined" as "bad characters"

Open juntalis opened this issue 8 years ago • 1 comments

The current lexer doesn't appear to factor in potential command line options for many of the built-in commands. Additionally, the standard if defined ... construct appears to be broken.

I've attached a screenshot showing the plugin's handling of a valid batch file below. (and added the source code of the batch file in question beneath that)

I could probably make some updates and setup a pull request if you feel this warrants a fix - just wanted to make sure I wasn't missing anything with regards to the if defined construct.

Non-Issue: There's also some weird handling of double '%' symbols, but that's probably not easily fixed since availability of and methods for character escaping varies across the the different builtin commands. (^% for some, %% for others) You can see an example of this in the screenshot on lines 57 & 58. I wouldn't expect those lines to be highlighted correctly in any case, but the %%A syntax should only be applied to for commands.

Issue Example

@echo off
setlocal
rem Setup our VC environment
set MAXVERS=14
set BUILDARCH=X64
:getopt
if "%~1x"=="x"           goto setup_msvc
if /i "%~1"=="x86"       (set "BUILDARCH=%~1") & (shift /1) & goto getopt
if /i "%~1"=="amd64"     (set "BUILDARCH=%~1") & (shift /1) & goto getopt
if /i "%~1"=="x64"       (set "BUILDARCH=%~1") & (shift /1) & goto getopt
if /i "%~1"=="ia64"      (set "BUILDARCH=%~1") & (shift /1) & goto getopt
if /i "%~1"=="x86_amd64" (set "BUILDARCH=%~1") & (shift /1) & goto getopt
if /i "%~1"=="x86_ia64"  (set "BUILDARCH=%~1") & (shift /1) & goto getopt
if not "%~1x"=="x"       (set "MAXVERS=%~1")   & (shift /1) & goto getopt

:setup_msvc
if defined INCLUDE goto error_env
call "%~dp0find-msvc.cmd" %MAXVERS% 8
if errorlevel 1 exit /B %ERRORLEVEL%
if not defined VCROOT exit /B 1
call "%VCROOT%\vcvarsall.bat" %BUILDARCH%>nul 2>nul
if errorlevel 1 exit /B %ERRORLEVEL%

:setup_flags
rem Normalize our build architecture
if "x%BUILDARCH:~-2%"=="x64" set BUILDARCH=X64
if not "%BUILDARCH%x"=="X64x" set BUILDARCH=X86
if "%BUILDARCH%"=="X86" set BUILDBITS=32
if "%BUILDARCH%"=="X64" set BUILDBITS=64

rem Used for checking ranges a bit later
set /A VCVERNUM=%VCVERS%

rem Output filename
set "OUTPUTNAME=%~dp0TES5Edit.exe"

rem RC Flags
set RCFLAGS=/nologo /D_NDEBUG /DNDEBUG /D_WIN32 /DWIN32 /D_CONSOLE /DCONSOLE /D_WIN64

rem Shared and year-specific compiler options
set CFLAGS=/nologo /W3 /WX- /fp:precise /Qfast_transcendentals /EHs-c- /Ox /GA /GL /GF /Gm- /GS- /Gy /GT /DNDEBUG=1 /D_NDEBUG=1 /D_CRT_SECURE_NO_DEPRECATE /D_WINDOWS /DWINDOWS /MD
set CFLAGS64=/favor:INTEL64
set CFLAGS2010=/arch:SSE2
set CFLAGS2012=/arch:AVX /Qpar
set "CFLAGS2013=%CFLAGS2012% /Gw /Zc:inline /cgthreads4"
set "CFLAGS2015=%CFLAGS2013%"

rem Shared and year-specific linker options
set LDFLAGS=/nologo /INCREMENTAL:NO /LTCG /OPT:ICF=32 /OPT:REF /MACHINE:%BUILDARCH% /LARGEADDRESSAWARE
set LDFLAGS2010=/MANIFEST
set LDFLAGS2012=/MANIFEST:EMBED
set "LDFLAGS2013=%LDFLAGS2012% /CGTHREADS:4"
set "LDFLAGS2015=%LDFLAGS2013%"

rem Merge our shared toolset options with the appropriate year-specific options
call set "CFLAGS=%%CFLAGS%% %%CFLAGS%VCYEAR%%%"
call set "LDFLAGS=%%LDFLAGS%% %%LDFLAGS%VCYEAR%%%"

rem If we're doing a 64-bit build, replace any arch:SSE2 args with arch:AVX and append our 64-bit options
if "%BUILDARCH%"=="X64" set "CFLAGS=%CFLAGS:SSE2=AVX% %CFLAGS64%"

rem Compile sources
echo cl.exe %CFLAGS% /MD /c /FoFO4EditStub.obj FO4EditStub.c
call cl.exe %CFLAGS% /MD /c "/Fo%~dp0FO4EditStub.obj" "%~dp0FO4EditStub.c"
if errorlevel 1 exit /B %ERRORLEVEL%

rem Compile resources
call rc.exe %RCFLAGS% "/fo%~dp0FO4EditStub.res" "%~dp0FO4EditStub.rc"
if errorlevel 1 exit /B %ERRORLEVEL%

rem Link executable
call link.exe %LDFLAGS% /SUBSYSTEM:WINDOWS /OUT:%OUTPUTNAME% /NODEFAULTLIB "%~dp0FO4EditStub.obj" "%~dp0FO4EditStub.res" kernel32.lib msvcrt.lib
if errorlevel 1 exit /B %ERRORLEVEL%

rem VC2012 and later can embed the manifest into the executable from the linker.
if %VCVERNUM% GEQ 11 goto cleanup

rem Embed manifest into the executable
call mt.exe /nologo -manifest %OUTPUTNAME%.manifest -outputresource:%OUTPUTNAME%
if errorlevel 1 exit /B %ERRORLEVEL%

:cleanup
rem Cleanup temporarey build filds
if exist "%~dp0*.obj" del /F /Q "%~dp0*.obj"
if exist "%~dp0*.res" del /F /Q "%~dp0*.res"
if exist "%~dp0*.manifest" del /F /Q "%~dp0*.manifest"
endlocal
goto :EOF

:error_env
echo ERROR: %~nx0 needs to be invoked from a clean environment, with no existing VC env setup.
exit /B 1

Note: Line 18 in the screenshot is actually invalid - I added it while trying to figure out what syntax the lexer expected.

juntalis avatar Feb 28 '16 09:02 juntalis

https://github.com/t3hnar/CmdSupport/issues/24 This issue also occurs in another similar plugin. And it seems that this problem remains unsolved for years. Any updates?

fz0000 avatar May 10 '22 02:05 fz0000