| 1 |
// Scintilla source code edit control |
| 2 |
/** @file LexBatch.cxx |
| 3 |
** Lexer for batch files. |
| 4 |
**/ |
| 5 |
// Copyright 1998-2001 by Neil Hodgson <[email protected]> |
| 6 |
// The License.txt file describes the conditions under which this software may be distributed. |
| 7 |
|
| 8 |
#include <cstdlib> |
| 9 |
#include <cassert> |
| 10 |
#include <cstring> |
| 11 |
#include <cctype> |
| 12 |
#include <cstdio> |
| 13 |
#include <cstdarg> |
| 14 |
|
| 15 |
#include <string> |
| 16 |
#include <string_view> |
| 17 |
#include <initializer_list> |
| 18 |
|
| 19 |
#include "ILexer.h" |
| 20 |
#include "Scintilla.h" |
| 21 |
#include "SciLexer.h" |
| 22 |
|
| 23 |
#include "InList.h" |
| 24 |
#include "WordList.h" |
| 25 |
#include "LexAccessor.h" |
| 26 |
#include "Accessor.h" |
| 27 |
#include "StyleContext.h" |
| 28 |
#include "CharacterSet.h" |
| 29 |
#include "LexerModule.h" |
| 30 |
|
| 31 |
using namespace Lexilla; |
| 32 |
|
| 33 |
namespace { |
| 34 |
|
| 35 |
constexpr bool Is0To9(char ch) noexcept { |
| 36 |
return (ch >= '0') && (ch <= '9'); |
| 37 |
} |
| 38 |
|
| 39 |
bool IsAlphabetic(int ch) noexcept { |
| 40 |
return IsASCII(ch) && isalpha(ch); |
| 41 |
} |
| 42 |
|
| 43 |
inline bool AtEOL(Accessor &styler, Sci_PositionU i) { |
| 44 |
return (styler[i] == '\n') || |
| 45 |
((styler[i] == '\r') && (styler.SafeGetCharAt(i + 1) != '\n')); |
| 46 |
} |
| 47 |
|
| 48 |
// Tests for BATCH Operators |
| 49 |
constexpr bool IsBOperator(char ch) noexcept { |
| 50 |
return (ch == '=') || (ch == '+') || (ch == '>') || (ch == '<') || |
| 51 |
(ch == '|') || (ch == '?') || (ch == '*')|| |
| 52 |
(ch == '&') || (ch == '(') || (ch == ')'); |
| 53 |
} |
| 54 |
|
| 55 |
// Tests for BATCH Separators |
| 56 |
constexpr bool IsBSeparator(char ch) noexcept { |
| 57 |
return (ch == '\\') || (ch == '.') || (ch == ';') || |
| 58 |
(ch == '\"') || (ch == '\'') || (ch == '/'); |
| 59 |
} |
| 60 |
|
| 61 |
// Tests for escape character |
| 62 |
constexpr bool IsEscaped(const char* wordStr, Sci_PositionU pos) noexcept { |
| 63 |
bool isQoted=false; |
| 64 |
while (pos>0){ |
| 65 |
pos--; |
| 66 |
if (wordStr[pos]=='^') |
| 67 |
isQoted=!isQoted; |
| 68 |
else |
| 69 |
break; |
| 70 |
} |
| 71 |
return isQoted; |
| 72 |
} |
| 73 |
|
| 74 |
constexpr bool IsQuotedBy(std::string_view svBuffer, char quote) noexcept { |
| 75 |
bool CurrentStatus = false; |
| 76 |
size_t pQuote = svBuffer.find(quote); |
| 77 |
while (pQuote != std::string_view::npos) { |
| 78 |
if (!IsEscaped(svBuffer.data(), pQuote)) { |
| 79 |
CurrentStatus = !CurrentStatus; |
| 80 |
} |
| 81 |
pQuote = svBuffer.find(quote, pQuote + 1); |
| 82 |
} |
| 83 |
return CurrentStatus; |
| 84 |
} |
| 85 |
|
| 86 |
// Tests for quote character |
| 87 |
constexpr bool textQuoted(const char *lineBuffer, Sci_PositionU endPos) noexcept { |
| 88 |
const std::string_view svBuffer(lineBuffer, endPos); |
| 89 |
return IsQuotedBy(svBuffer, '\"') || IsQuotedBy(svBuffer, '\''); |
| 90 |
} |
| 91 |
|
| 92 |
void ColouriseBatchDoc( |
| 93 |
Sci_PositionU startPos, |
| 94 |
Sci_Position length, |
| 95 |
int /*initStyle*/, |
| 96 |
WordList *keywordlists[], |
| 97 |
Accessor &styler) { |
| 98 |
// Always backtracks to the start of a line that is not a continuation |
| 99 |
// of the previous line |
| 100 |
if (startPos > 0) { |
| 101 |
Sci_Position ln = styler.GetLine(startPos); // Current line number |
| 102 |
while (startPos > 0) { |
| 103 |
ln--; |
| 104 |
if ((styler.SafeGetCharAt(startPos-3) == '^' && styler.SafeGetCharAt(startPos-2) == '\r' && styler.SafeGetCharAt(startPos-1) == '\n') |
| 105 |
|| styler.SafeGetCharAt(startPos-2) == '^') { // handle '^' line continuation |
| 106 |
// When the line continuation is found, |
| 107 |
// set the Start Position to the Start of the previous line |
| 108 |
length+=startPos-styler.LineStart(ln); |
| 109 |
startPos=styler.LineStart(ln); |
| 110 |
} |
| 111 |
else |
| 112 |
break; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
char lineBuffer[1024] {}; |
| 117 |
|
| 118 |
styler.StartAt(startPos); |
| 119 |
styler.StartSegment(startPos); |
| 120 |
Sci_PositionU linePos = 0; |
| 121 |
Sci_PositionU startLine = startPos; |
| 122 |
bool continueProcessing = true; // Used to toggle Regular Keyword Checking |
| 123 |
bool isNotAssigned=false; // Used to flag Assignment in Set operation |
| 124 |
|
| 125 |
for (Sci_PositionU i = startPos; i < startPos + length; i++) { |
| 126 |
lineBuffer[linePos++] = styler[i]; |
| 127 |
if (AtEOL(styler, i) || (linePos >= sizeof(lineBuffer) - 1) || (i==startPos + length-1)) { |
| 128 |
// End of line (or of line buffer) (or End of Last Line) met, colourise it |
| 129 |
lineBuffer[linePos] = '\0'; |
| 130 |
const Sci_PositionU lengthLine=linePos; |
| 131 |
const Sci_PositionU endPos=i; |
| 132 |
const WordList &keywords = *keywordlists[0]; // Internal Commands |
| 133 |
const WordList &keywords2 = *keywordlists[1]; // External Commands (optional) |
| 134 |
|
| 135 |
// CHOICE, ECHO, GOTO, PROMPT and SET have Default Text that may contain Regular Keywords |
| 136 |
// Toggling Regular Keyword Checking off improves readability |
| 137 |
// Other Regular Keywords and External Commands / Programs might also benefit from toggling |
| 138 |
// Need a more robust algorithm to properly toggle Regular Keyword Checking |
| 139 |
bool stopLineProcessing=false; // Used to stop line processing if Comment or Drive Change found |
| 140 |
|
| 141 |
Sci_PositionU offset = 0; // Line Buffer Offset |
| 142 |
// Skip initial spaces |
| 143 |
while ((offset < lengthLine) && (isspacechar(lineBuffer[offset]))) { |
| 144 |
offset++; |
| 145 |
} |
| 146 |
// Colorize Default Text |
| 147 |
styler.ColourTo(startLine + offset - 1, SCE_BAT_DEFAULT); |
| 148 |
// Set External Command / Program Location |
| 149 |
Sci_PositionU cmdLoc = offset; |
| 150 |
|
| 151 |
// Check for Fake Label (Comment) or Real Label - return if found |
| 152 |
if (lineBuffer[offset] == ':') { |
| 153 |
if (lineBuffer[offset + 1] == ':') { |
| 154 |
// Colorize Fake Label (Comment) - :: is similar to REM, see http://content.techweb.com/winmag/columns/explorer/2000/21.htm |
| 155 |
styler.ColourTo(endPos, SCE_BAT_COMMENT); |
| 156 |
} else { |
| 157 |
// Colorize Real Label |
| 158 |
// :[\t ]*[^\t &+:<>|]+ |
| 159 |
const char *startLabelName = lineBuffer + offset + 1; |
| 160 |
const size_t whitespaceLength = strspn(startLabelName, "\t "); |
| 161 |
// Set of label-terminating characters determined experimentally |
| 162 |
const char *endLabel = strpbrk(startLabelName + whitespaceLength, "\t &+:<>|"); |
| 163 |
if (endLabel) { |
| 164 |
styler.ColourTo(startLine + offset + endLabel - startLabelName, SCE_BAT_LABEL); |
| 165 |
styler.ColourTo(endPos, SCE_BAT_AFTER_LABEL); // New style |
| 166 |
} else { |
| 167 |
styler.ColourTo(endPos, SCE_BAT_LABEL); |
| 168 |
} |
| 169 |
} |
| 170 |
stopLineProcessing=true; |
| 171 |
// Check for Drive Change (Drive Change is internal command) - return if found |
| 172 |
} else if ((IsAlphabetic(lineBuffer[offset])) && |
| 173 |
(lineBuffer[offset + 1] == ':') && |
| 174 |
((isspacechar(lineBuffer[offset + 2])) || |
| 175 |
(((lineBuffer[offset + 2] == '\\')) && |
| 176 |
(isspacechar(lineBuffer[offset + 3]))))) { |
| 177 |
// Colorize Regular Keyword |
| 178 |
styler.ColourTo(endPos, SCE_BAT_WORD); |
| 179 |
stopLineProcessing=true; |
| 180 |
} |
| 181 |
|
| 182 |
// Check for Hide Command (@ECHO OFF/ON) |
| 183 |
if (lineBuffer[offset] == '@') { |
| 184 |
styler.ColourTo(startLine + offset, SCE_BAT_HIDE); |
| 185 |
offset++; |
| 186 |
} |
| 187 |
// Skip next spaces |
| 188 |
while ((offset < lengthLine) && (isspacechar(lineBuffer[offset]))) { |
| 189 |
offset++; |
| 190 |
} |
| 191 |
|
| 192 |
// Read remainder of line word-at-a-time or remainder-of-word-at-a-time |
| 193 |
while (offset < lengthLine && !stopLineProcessing) { |
| 194 |
if (offset > startLine) { |
| 195 |
// Colorize Default Text |
| 196 |
styler.ColourTo(startLine + offset - 1, SCE_BAT_DEFAULT); |
| 197 |
} |
| 198 |
char wordBuffer[81]{}; // Word Buffer - large to catch long paths |
| 199 |
// Copy word from Line Buffer into Word Buffer and convert to lower case |
| 200 |
Sci_PositionU wbl = 0; // Word Buffer Length |
| 201 |
for (; offset < lengthLine && wbl < 80 && |
| 202 |
!isspacechar(lineBuffer[offset]); wbl++, offset++) { |
| 203 |
wordBuffer[wbl] = MakeLowerCase(lineBuffer[offset]); |
| 204 |
} |
| 205 |
wordBuffer[wbl] = '\0'; |
| 206 |
const std::string_view wordView(wordBuffer); |
| 207 |
Sci_PositionU wbo = 0; // Word Buffer Offset - also Special Keyword Buffer Length |
| 208 |
|
| 209 |
// Check for Comment - return if found |
| 210 |
if (continueProcessing) { |
| 211 |
if ((wordView == "rem") || (wordBuffer[0] == ':' && wordBuffer[1] == ':')) { |
| 212 |
if ((offset == wbl) || !textQuoted(lineBuffer, offset - wbl)) { |
| 213 |
styler.ColourTo(startLine + offset - wbl - 1, SCE_BAT_DEFAULT); |
| 214 |
styler.ColourTo(endPos, SCE_BAT_COMMENT); |
| 215 |
break; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
// Check for Separator |
| 220 |
if (IsBSeparator(wordBuffer[0])) { |
| 221 |
// Check for External Command / Program |
| 222 |
if ((cmdLoc == offset - wbl) && |
| 223 |
((wordBuffer[0] == ':') || |
| 224 |
(wordBuffer[0] == '\\') || |
| 225 |
(wordBuffer[0] == '.'))) { |
| 226 |
// Reset Offset to re-process remainder of word |
| 227 |
offset -= (wbl - 1); |
| 228 |
// Colorize External Command / Program |
| 229 |
if (!keywords2) { |
| 230 |
styler.ColourTo(startLine + offset - 1, SCE_BAT_COMMAND); |
| 231 |
} else if (keywords2.InList(wordBuffer)) { |
| 232 |
styler.ColourTo(startLine + offset - 1, SCE_BAT_COMMAND); |
| 233 |
} else { |
| 234 |
styler.ColourTo(startLine + offset - 1, SCE_BAT_DEFAULT); |
| 235 |
} |
| 236 |
// Reset External Command / Program Location |
| 237 |
cmdLoc = offset; |
| 238 |
} else { |
| 239 |
// Reset Offset to re-process remainder of word |
| 240 |
offset -= (wbl - 1); |
| 241 |
// Colorize Default Text |
| 242 |
styler.ColourTo(startLine + offset - 1, SCE_BAT_DEFAULT); |
| 243 |
} |
| 244 |
// Check for Regular Keyword in list |
| 245 |
} else if ((keywords.InList(wordBuffer)) && |
| 246 |
(continueProcessing)) { |
| 247 |
// ECHO, GOTO, PROMPT and SET require no further Regular Keyword Checking |
| 248 |
if (InList(wordView, {"echo", "goto", "prompt"})) { |
| 249 |
continueProcessing = false; |
| 250 |
} |
| 251 |
// SET requires additional processing for the assignment operator |
| 252 |
if (wordView == "set") { |
| 253 |
continueProcessing = false; |
| 254 |
isNotAssigned=true; |
| 255 |
} |
| 256 |
// Identify External Command / Program Location for ERRORLEVEL, and EXIST |
| 257 |
if (InList(wordView, {"errorlevel", "exist"})) { |
| 258 |
// Reset External Command / Program Location |
| 259 |
cmdLoc = offset; |
| 260 |
// Skip next spaces |
| 261 |
while ((cmdLoc < lengthLine) && |
| 262 |
(isspacechar(lineBuffer[cmdLoc]))) { |
| 263 |
cmdLoc++; |
| 264 |
} |
| 265 |
// Skip comparison |
| 266 |
while ((cmdLoc < lengthLine) && |
| 267 |
(!isspacechar(lineBuffer[cmdLoc]))) { |
| 268 |
cmdLoc++; |
| 269 |
} |
| 270 |
// Skip next spaces |
| 271 |
while ((cmdLoc < lengthLine) && |
| 272 |
(isspacechar(lineBuffer[cmdLoc]))) { |
| 273 |
cmdLoc++; |
| 274 |
} |
| 275 |
// Identify External Command / Program Location for CALL, DO, LOADHIGH and LH |
| 276 |
} else if (InList(wordView, {"call", "do", "loadhigh", "lh"})) { |
| 277 |
// Reset External Command / Program Location |
| 278 |
cmdLoc = offset; |
| 279 |
// Skip next spaces |
| 280 |
while ((cmdLoc < lengthLine) && |
| 281 |
(isspacechar(lineBuffer[cmdLoc]))) { |
| 282 |
cmdLoc++; |
| 283 |
} |
| 284 |
// Check if call is followed by a label |
| 285 |
if ((lineBuffer[cmdLoc] == ':') && |
| 286 |
(wordView == "call")) { |
| 287 |
continueProcessing = false; |
| 288 |
} |
| 289 |
} |
| 290 |
// Colorize Regular keyword |
| 291 |
styler.ColourTo(startLine + offset - 1, SCE_BAT_WORD); |
| 292 |
// No need to Reset Offset |
| 293 |
// Check for Special Keyword in list, External Command / Program, or Default Text |
| 294 |
} else if ((wordBuffer[0] != '%') && |
| 295 |
(wordBuffer[0] != '!') && |
| 296 |
(!IsBOperator(wordBuffer[0])) && |
| 297 |
(continueProcessing)) { |
| 298 |
// Check for Special Keyword |
| 299 |
// Affected Commands are in Length range 2-6 |
| 300 |
// Good that ERRORLEVEL, EXIST, CALL, DO, LOADHIGH, and LH are unaffected |
| 301 |
bool sKeywordFound = false; // Exit Special Keyword for-loop if found |
| 302 |
for (Sci_PositionU keywordLength = 2; keywordLength < wbl && keywordLength < 7 && !sKeywordFound; keywordLength++) { |
| 303 |
// Special Keywords are those that allow certain characters without whitespace after the command |
| 304 |
// Examples are: cd. cd\ md. rd. dir| dir> echo: echo. path= |
| 305 |
// Special Keyword Buffer used to determine if the first n characters is a Keyword |
| 306 |
char sKeywordBuffer[10]{}; // Special Keyword Buffer |
| 307 |
wbo = 0; |
| 308 |
// Copy Keyword Length from Word Buffer into Special Keyword Buffer |
| 309 |
for (; wbo < keywordLength; wbo++) { |
| 310 |
sKeywordBuffer[wbo] = wordBuffer[wbo]; |
| 311 |
} |
| 312 |
sKeywordBuffer[wbo] = '\0'; |
| 313 |
// Check for Special Keyword in list |
| 314 |
if ((keywords.InList(sKeywordBuffer)) && |
| 315 |
((IsBOperator(wordBuffer[wbo])) || |
| 316 |
(IsBSeparator(wordBuffer[wbo])) || |
| 317 |
(wordBuffer[wbo] == ':' && |
| 318 |
(InList(sKeywordBuffer, {"call", "echo", "goto"}) )))) { |
| 319 |
sKeywordFound = true; |
| 320 |
// ECHO requires no further Regular Keyword Checking |
| 321 |
if (std::string_view(sKeywordBuffer) == "echo") { |
| 322 |
continueProcessing = false; |
| 323 |
} |
| 324 |
// Colorize Special Keyword as Regular Keyword |
| 325 |
styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_WORD); |
| 326 |
// Reset Offset to re-process remainder of word |
| 327 |
offset -= (wbl - wbo); |
| 328 |
} |
| 329 |
} |
| 330 |
// Check for External Command / Program or Default Text |
| 331 |
if (!sKeywordFound) { |
| 332 |
wbo = 0; |
| 333 |
// Check for External Command / Program |
| 334 |
if (cmdLoc == offset - wbl) { |
| 335 |
// Read up to %, Operator or Separator |
| 336 |
while ((wbo < wbl) && |
| 337 |
(((wordBuffer[wbo] != '%') && |
| 338 |
(wordBuffer[wbo] != '!') && |
| 339 |
(!IsBOperator(wordBuffer[wbo])) && |
| 340 |
(!IsBSeparator(wordBuffer[wbo]))))) { |
| 341 |
wbo++; |
| 342 |
} |
| 343 |
// Reset External Command / Program Location |
| 344 |
cmdLoc = offset - (wbl - wbo); |
| 345 |
// Reset Offset to re-process remainder of word |
| 346 |
offset -= (wbl - wbo); |
| 347 |
// CHOICE requires no further Regular Keyword Checking |
| 348 |
if (wordView == "choice") { |
| 349 |
continueProcessing = false; |
| 350 |
} |
| 351 |
// Check for START (and its switches) - What follows is External Command \ Program |
| 352 |
if (wordView == "start") { |
| 353 |
// Reset External Command / Program Location |
| 354 |
cmdLoc = offset; |
| 355 |
// Skip next spaces |
| 356 |
while ((cmdLoc < lengthLine) && |
| 357 |
(isspacechar(lineBuffer[cmdLoc]))) { |
| 358 |
cmdLoc++; |
| 359 |
} |
| 360 |
// Reset External Command / Program Location if command switch detected |
| 361 |
if (lineBuffer[cmdLoc] == '/') { |
| 362 |
// Skip command switch |
| 363 |
while ((cmdLoc < lengthLine) && |
| 364 |
(!isspacechar(lineBuffer[cmdLoc]))) { |
| 365 |
cmdLoc++; |
| 366 |
} |
| 367 |
// Skip next spaces |
| 368 |
while ((cmdLoc < lengthLine) && |
| 369 |
(isspacechar(lineBuffer[cmdLoc]))) { |
| 370 |
cmdLoc++; |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
// Colorize External Command / Program |
| 375 |
if (!keywords2) { |
| 376 |
styler.ColourTo(startLine + offset - 1, SCE_BAT_COMMAND); |
| 377 |
} else if (keywords2.InList(wordBuffer)) { |
| 378 |
styler.ColourTo(startLine + offset - 1, SCE_BAT_COMMAND); |
| 379 |
} else { |
| 380 |
styler.ColourTo(startLine + offset - 1, SCE_BAT_DEFAULT); |
| 381 |
} |
| 382 |
// No need to Reset Offset |
| 383 |
// Check for Default Text |
| 384 |
} else { |
| 385 |
// Read up to %, Operator or Separator |
| 386 |
while ((wbo < wbl) && |
| 387 |
(((wordBuffer[wbo] != '%') && |
| 388 |
(wordBuffer[wbo] != '!') && |
| 389 |
(!IsBOperator(wordBuffer[wbo])) && |
| 390 |
(!IsBSeparator(wordBuffer[wbo]))))) { |
| 391 |
wbo++; |
| 392 |
} |
| 393 |
// Colorize Default Text |
| 394 |
styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_DEFAULT); |
| 395 |
// Reset Offset to re-process remainder of word |
| 396 |
offset -= (wbl - wbo); |
| 397 |
} |
| 398 |
} |
| 399 |
// Check for Argument (%n), Environment Variable (%x...%) or Local Variable (%%a) |
| 400 |
} else if (wordBuffer[0] == '%') { |
| 401 |
// Colorize Default Text |
| 402 |
styler.ColourTo(startLine + offset - 1 - wbl, SCE_BAT_DEFAULT); |
| 403 |
wbo++; |
| 404 |
// Search to end of word for second % (can be a long path) |
| 405 |
while ((wbo < wbl) && |
| 406 |
(wordBuffer[wbo] != '%')) { |
| 407 |
wbo++; |
| 408 |
} |
| 409 |
// Check for Argument (%n) or (%*) |
| 410 |
if (((Is0To9(wordBuffer[1])) || (wordBuffer[1] == '*')) && |
| 411 |
(wordBuffer[wbo] != '%')) { |
| 412 |
// Check for External Command / Program |
| 413 |
if (cmdLoc == offset - wbl) { |
| 414 |
cmdLoc = offset - (wbl - 2); |
| 415 |
} |
| 416 |
// Colorize Argument |
| 417 |
styler.ColourTo(startLine + offset - 1 - (wbl - 2), SCE_BAT_IDENTIFIER); |
| 418 |
// Reset Offset to re-process remainder of word |
| 419 |
offset -= (wbl - 2); |
| 420 |
// Check for Expanded Argument (%~...) / Variable (%%~...) |
| 421 |
// Expanded Argument: %~[<path-operators>]<single digit> |
| 422 |
// Expanded Variable: %%~[<path-operators>]<single identifier character> |
| 423 |
// Path operators are exclusively alphabetic. |
| 424 |
// Expanded arguments have a single digit at the end. |
| 425 |
// Expanded variables have a single identifier character as variable name. |
| 426 |
} else if (((wbl > 1) && (wordBuffer[1] == '~')) || |
| 427 |
((wbl > 2) && (wordBuffer[1] == '%') && (wordBuffer[2] == '~'))) { |
| 428 |
// Check for External Command / Program |
| 429 |
if (cmdLoc == offset - wbl) { |
| 430 |
cmdLoc = offset - (wbl - wbo); |
| 431 |
} |
| 432 |
const bool isArgument = (wordBuffer[1] == '~'); |
| 433 |
if (isArgument) { |
| 434 |
Sci_PositionU expansionStopOffset = 2; |
| 435 |
bool isValid = false; |
| 436 |
for (; expansionStopOffset < wbl; expansionStopOffset++) { |
| 437 |
if (Is0To9(wordBuffer[expansionStopOffset])) { |
| 438 |
expansionStopOffset++; |
| 439 |
isValid = true; |
| 440 |
wbo = expansionStopOffset; |
| 441 |
// Colorize Expanded Argument |
| 442 |
styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_IDENTIFIER); |
| 443 |
break; |
| 444 |
} |
| 445 |
} |
| 446 |
if (!isValid) { |
| 447 |
// not a valid expanded argument or variable |
| 448 |
styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_DEFAULT); |
| 449 |
} |
| 450 |
// Expanded Variable |
| 451 |
} else { |
| 452 |
// start after ~ |
| 453 |
wbo = 3; |
| 454 |
// Search to end of word for another % (can be a long path) |
| 455 |
while ((wbo < wbl) && |
| 456 |
(wordBuffer[wbo] != '%') && |
| 457 |
(!IsBOperator(wordBuffer[wbo])) && |
| 458 |
(!IsBSeparator(wordBuffer[wbo]))) { |
| 459 |
wbo++; |
| 460 |
} |
| 461 |
if (wbo > 3) { |
| 462 |
// Colorize Expanded Variable |
| 463 |
styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_IDENTIFIER); |
| 464 |
} else { |
| 465 |
// not a valid expanded argument or variable |
| 466 |
styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_DEFAULT); |
| 467 |
} |
| 468 |
} |
| 469 |
// Reset Offset to re-process remainder of word |
| 470 |
offset -= (wbl - wbo); |
| 471 |
// Check for Environment Variable (%x...%) |
| 472 |
} else if ((wordBuffer[1] != '%') && |
| 473 |
(wordBuffer[wbo] == '%')) { |
| 474 |
wbo++; |
| 475 |
// Check for External Command / Program |
| 476 |
if (cmdLoc == offset - wbl) { |
| 477 |
cmdLoc = offset - (wbl - wbo); |
| 478 |
} |
| 479 |
// Colorize Environment Variable |
| 480 |
styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_IDENTIFIER); |
| 481 |
// Reset Offset to re-process remainder of word |
| 482 |
offset -= (wbl - wbo); |
| 483 |
// Check for Local Variable (%%a) |
| 484 |
} else if ( |
| 485 |
(wbl > 2) && |
| 486 |
(wordBuffer[1] == '%') && |
| 487 |
(wordBuffer[2] != '%') && |
| 488 |
(!IsBOperator(wordBuffer[2])) && |
| 489 |
(!IsBSeparator(wordBuffer[2]))) { |
| 490 |
// Check for External Command / Program |
| 491 |
if (cmdLoc == offset - wbl) { |
| 492 |
cmdLoc = offset - (wbl - 3); |
| 493 |
} |
| 494 |
// Colorize Local Variable |
| 495 |
styler.ColourTo(startLine + offset - 1 - (wbl - 3), SCE_BAT_IDENTIFIER); |
| 496 |
// Reset Offset to re-process remainder of word |
| 497 |
offset -= (wbl - 3); |
| 498 |
// escaped % |
| 499 |
} else if ( |
| 500 |
(wbl > 1) && |
| 501 |
(wordBuffer[1] == '%')) { |
| 502 |
|
| 503 |
// Reset Offset to re-process remainder of word |
| 504 |
styler.ColourTo(startLine + offset - 1 - (wbl - 2), SCE_BAT_DEFAULT); |
| 505 |
offset -= (wbl - 2); |
| 506 |
} |
| 507 |
// Check for Environment Variable (!x...!) |
| 508 |
} else if (wordBuffer[0] == '!') { |
| 509 |
// Colorize Default Text |
| 510 |
styler.ColourTo(startLine + offset - 1 - wbl, SCE_BAT_DEFAULT); |
| 511 |
wbo++; |
| 512 |
// Search to end of word for second ! (can be a long path) |
| 513 |
while ((wbo < wbl) && |
| 514 |
(wordBuffer[wbo] != '!')) { |
| 515 |
wbo++; |
| 516 |
} |
| 517 |
if (wordBuffer[wbo] == '!') { |
| 518 |
wbo++; |
| 519 |
// Check for External Command / Program |
| 520 |
if (cmdLoc == offset - wbl) { |
| 521 |
cmdLoc = offset - (wbl - wbo); |
| 522 |
} |
| 523 |
// Colorize Environment Variable |
| 524 |
styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_IDENTIFIER); |
| 525 |
// Reset Offset to re-process remainder of word |
| 526 |
offset -= (wbl - wbo); |
| 527 |
} |
| 528 |
// Check for Operator |
| 529 |
} else if (IsBOperator(wordBuffer[0])) { |
| 530 |
// Colorize Default Text |
| 531 |
styler.ColourTo(startLine + offset - 1 - wbl, SCE_BAT_DEFAULT); |
| 532 |
// Check for Comparison Operator |
| 533 |
if ((wordBuffer[0] == '=') && (wordBuffer[1] == '=')) { |
| 534 |
// Identify External Command / Program Location for IF |
| 535 |
cmdLoc = offset; |
| 536 |
// Skip next spaces |
| 537 |
while ((cmdLoc < lengthLine) && |
| 538 |
(isspacechar(lineBuffer[cmdLoc]))) { |
| 539 |
cmdLoc++; |
| 540 |
} |
| 541 |
// Colorize Comparison Operator |
| 542 |
if (continueProcessing) |
| 543 |
styler.ColourTo(startLine + offset - 1 - (wbl - 2), SCE_BAT_OPERATOR); |
| 544 |
else |
| 545 |
styler.ColourTo(startLine + offset - 1 - (wbl - 2), SCE_BAT_DEFAULT); |
| 546 |
// Reset Offset to re-process remainder of word |
| 547 |
offset -= (wbl - 2); |
| 548 |
// Check for Pipe Operator |
| 549 |
} else if ((wordBuffer[0] == '|') && |
| 550 |
!(IsEscaped(lineBuffer,offset - wbl + wbo) || textQuoted(lineBuffer, offset - wbl) )) { |
| 551 |
// Reset External Command / Program Location |
| 552 |
cmdLoc = offset - wbl + 1; |
| 553 |
// Skip next spaces |
| 554 |
while ((cmdLoc < lengthLine) && |
| 555 |
(isspacechar(lineBuffer[cmdLoc]))) { |
| 556 |
cmdLoc++; |
| 557 |
} |
| 558 |
// Colorize Pipe Operator |
| 559 |
styler.ColourTo(startLine + offset - 1 - (wbl - 1), SCE_BAT_OPERATOR); |
| 560 |
// Reset Offset to re-process remainder of word |
| 561 |
offset -= (wbl - 1); |
| 562 |
continueProcessing = true; |
| 563 |
// Check for Other Operator |
| 564 |
} else { |
| 565 |
// Check for Operators: >, |, & |
| 566 |
if (((wordBuffer[0] == '>')|| |
| 567 |
(wordBuffer[0] == ')')|| |
| 568 |
(wordBuffer[0] == '(')|| |
| 569 |
(wordBuffer[0] == '&' )) && |
| 570 |
!(!continueProcessing && (IsEscaped(lineBuffer,offset - wbl + wbo) |
| 571 |
|| textQuoted(lineBuffer, offset - wbl) ))){ |
| 572 |
// Turn Keyword and External Command / Program checking back on |
| 573 |
continueProcessing = true; |
| 574 |
isNotAssigned=false; |
| 575 |
} |
| 576 |
// Colorize Other Operators |
| 577 |
// Do not Colorize Parenthesis, quoted text and escaped operators |
| 578 |
if (((wordBuffer[0] != ')') && (wordBuffer[0] != '(') |
| 579 |
&& !textQuoted(lineBuffer, offset - wbl) && !IsEscaped(lineBuffer,offset - wbl + wbo)) |
| 580 |
&& !((wordBuffer[0] == '=') && !isNotAssigned )) |
| 581 |
styler.ColourTo(startLine + offset - 1 - (wbl - 1), SCE_BAT_OPERATOR); |
| 582 |
else |
| 583 |
styler.ColourTo(startLine + offset - 1 - (wbl - 1), SCE_BAT_DEFAULT); |
| 584 |
// Reset Offset to re-process remainder of word |
| 585 |
offset -= (wbl - 1); |
| 586 |
|
| 587 |
if ((wordBuffer[0] == '=') && isNotAssigned ){ |
| 588 |
isNotAssigned=false; |
| 589 |
} |
| 590 |
} |
| 591 |
// Check for Default Text |
| 592 |
} else { |
| 593 |
// Read up to %, Operator or Separator |
| 594 |
while ((wbo < wbl) && |
| 595 |
((wordBuffer[wbo] != '%') && |
| 596 |
(wordBuffer[wbo] != '!') && |
| 597 |
(!IsBOperator(wordBuffer[wbo])) && |
| 598 |
(!IsBSeparator(wordBuffer[wbo])))) { |
| 599 |
wbo++; |
| 600 |
} |
| 601 |
// Colorize Default Text |
| 602 |
styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_DEFAULT); |
| 603 |
// Reset Offset to re-process remainder of word |
| 604 |
offset -= (wbl - wbo); |
| 605 |
} |
| 606 |
// Skip next spaces - nothing happens if Offset was Reset |
| 607 |
while ((offset < lengthLine) && (isspacechar(lineBuffer[offset]))) { |
| 608 |
offset++; |
| 609 |
} |
| 610 |
} |
| 611 |
// Colorize Default Text for remainder of line - currently not lexed |
| 612 |
styler.ColourTo(endPos, SCE_BAT_DEFAULT); |
| 613 |
|
| 614 |
// handle line continuation for SET and ECHO commands except the last line |
| 615 |
if (!continueProcessing && (i<startPos + length-1)) { |
| 616 |
if (linePos==1 || (linePos==2 && lineBuffer[1]=='\r')) // empty line on Unix and Mac or on Windows |
| 617 |
continueProcessing=true; |
| 618 |
else { |
| 619 |
Sci_PositionU lineContinuationPos; |
| 620 |
if ((linePos>2) && lineBuffer[linePos-2]=='\r') // Windows EOL |
| 621 |
lineContinuationPos=linePos-3; |
| 622 |
else |
| 623 |
lineContinuationPos=linePos-2; // Unix or Mac EOL |
| 624 |
// Reset continueProcessing if line continuation was not found |
| 625 |
if ((lineBuffer[lineContinuationPos]!='^') |
| 626 |
|| IsEscaped(lineBuffer, lineContinuationPos) |
| 627 |
|| textQuoted(lineBuffer, lineContinuationPos)) |
| 628 |
continueProcessing=true; |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
linePos = 0; |
| 633 |
startLine = i + 1; |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
const char *const batchWordListDesc[] = { |
| 639 |
"Internal Commands", |
| 640 |
"External Commands", |
| 641 |
nullptr |
| 642 |
}; |
| 643 |
|
| 644 |
} |
| 645 |
|
| 646 |
extern const LexerModule lmBatch(SCLEX_BATCH, ColouriseBatchDoc, "batch", nullptr, batchWordListDesc); |