LexBatch.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // Scintilla source code edit control
  2. /** @file LexBatch.cxx
  3. ** Lexer for batch files.
  4. **/
  5. // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
  6. // The License.txt file describes the conditions under which this software may be distributed.
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <assert.h>
  12. #include <ctype.h>
  13. #include "ILexer.h"
  14. #include "Scintilla.h"
  15. #include "SciLexer.h"
  16. #include "WordList.h"
  17. #include "LexAccessor.h"
  18. #include "Accessor.h"
  19. #include "StyleContext.h"
  20. #include "CharacterSet.h"
  21. #include "LexerModule.h"
  22. #ifdef SCI_NAMESPACE
  23. using namespace Scintilla;
  24. #endif
  25. static bool Is0To9(char ch) {
  26. return (ch >= '0') && (ch <= '9');
  27. }
  28. static bool IsAlphabetic(int ch) {
  29. return IsASCII(ch) && isalpha(ch);
  30. }
  31. static inline bool AtEOL(Accessor &styler, Sci_PositionU i) {
  32. return (styler[i] == '\n') ||
  33. ((styler[i] == '\r') && (styler.SafeGetCharAt(i + 1) != '\n'));
  34. }
  35. // Tests for BATCH Operators
  36. static bool IsBOperator(char ch) {
  37. return (ch == '=') || (ch == '+') || (ch == '>') || (ch == '<') ||
  38. (ch == '|') || (ch == '?') || (ch == '*');
  39. }
  40. // Tests for BATCH Separators
  41. static bool IsBSeparator(char ch) {
  42. return (ch == '\\') || (ch == '.') || (ch == ';') ||
  43. (ch == '\"') || (ch == '\'') || (ch == '/');
  44. }
  45. static void ColouriseBatchLine(
  46. char *lineBuffer,
  47. Sci_PositionU lengthLine,
  48. Sci_PositionU startLine,
  49. Sci_PositionU endPos,
  50. WordList *keywordlists[],
  51. Accessor &styler) {
  52. Sci_PositionU offset = 0; // Line Buffer Offset
  53. Sci_PositionU cmdLoc; // External Command / Program Location
  54. char wordBuffer[81]; // Word Buffer - large to catch long paths
  55. Sci_PositionU wbl; // Word Buffer Length
  56. Sci_PositionU wbo; // Word Buffer Offset - also Special Keyword Buffer Length
  57. WordList &keywords = *keywordlists[0]; // Internal Commands
  58. WordList &keywords2 = *keywordlists[1]; // External Commands (optional)
  59. // CHOICE, ECHO, GOTO, PROMPT and SET have Default Text that may contain Regular Keywords
  60. // Toggling Regular Keyword Checking off improves readability
  61. // Other Regular Keywords and External Commands / Programs might also benefit from toggling
  62. // Need a more robust algorithm to properly toggle Regular Keyword Checking
  63. bool continueProcessing = true; // Used to toggle Regular Keyword Checking
  64. // Special Keywords are those that allow certain characters without whitespace after the command
  65. // Examples are: cd. cd\ md. rd. dir| dir> echo: echo. path=
  66. // Special Keyword Buffer used to determine if the first n characters is a Keyword
  67. char sKeywordBuffer[10]; // Special Keyword Buffer
  68. bool sKeywordFound; // Exit Special Keyword for-loop if found
  69. // Skip initial spaces
  70. while ((offset < lengthLine) && (isspacechar(lineBuffer[offset]))) {
  71. offset++;
  72. }
  73. // Colorize Default Text
  74. styler.ColourTo(startLine + offset - 1, SCE_BAT_DEFAULT);
  75. // Set External Command / Program Location
  76. cmdLoc = offset;
  77. // Check for Fake Label (Comment) or Real Label - return if found
  78. if (lineBuffer[offset] == ':') {
  79. if (lineBuffer[offset + 1] == ':') {
  80. // Colorize Fake Label (Comment) - :: is similar to REM, see http://content.techweb.com/winmag/columns/explorer/2000/21.htm
  81. styler.ColourTo(endPos, SCE_BAT_COMMENT);
  82. } else {
  83. // Colorize Real Label
  84. styler.ColourTo(endPos, SCE_BAT_LABEL);
  85. }
  86. return;
  87. // Check for Drive Change (Drive Change is internal command) - return if found
  88. } else if ((IsAlphabetic(lineBuffer[offset])) &&
  89. (lineBuffer[offset + 1] == ':') &&
  90. ((isspacechar(lineBuffer[offset + 2])) ||
  91. (((lineBuffer[offset + 2] == '\\')) &&
  92. (isspacechar(lineBuffer[offset + 3]))))) {
  93. // Colorize Regular Keyword
  94. styler.ColourTo(endPos, SCE_BAT_WORD);
  95. return;
  96. }
  97. // Check for Hide Command (@ECHO OFF/ON)
  98. if (lineBuffer[offset] == '@') {
  99. styler.ColourTo(startLine + offset, SCE_BAT_HIDE);
  100. offset++;
  101. }
  102. // Skip next spaces
  103. while ((offset < lengthLine) && (isspacechar(lineBuffer[offset]))) {
  104. offset++;
  105. }
  106. // Read remainder of line word-at-a-time or remainder-of-word-at-a-time
  107. while (offset < lengthLine) {
  108. if (offset > startLine) {
  109. // Colorize Default Text
  110. styler.ColourTo(startLine + offset - 1, SCE_BAT_DEFAULT);
  111. }
  112. // Copy word from Line Buffer into Word Buffer
  113. wbl = 0;
  114. for (; offset < lengthLine && wbl < 80 &&
  115. !isspacechar(lineBuffer[offset]); wbl++, offset++) {
  116. wordBuffer[wbl] = static_cast<char>(tolower(lineBuffer[offset]));
  117. }
  118. wordBuffer[wbl] = '\0';
  119. wbo = 0;
  120. // Check for Comment - return if found
  121. if (CompareCaseInsensitive(wordBuffer, "rem") == 0) {
  122. styler.ColourTo(endPos, SCE_BAT_COMMENT);
  123. return;
  124. }
  125. // Check for Separator
  126. if (IsBSeparator(wordBuffer[0])) {
  127. // Check for External Command / Program
  128. if ((cmdLoc == offset - wbl) &&
  129. ((wordBuffer[0] == ':') ||
  130. (wordBuffer[0] == '\\') ||
  131. (wordBuffer[0] == '.'))) {
  132. // Reset Offset to re-process remainder of word
  133. offset -= (wbl - 1);
  134. // Colorize External Command / Program
  135. if (!keywords2) {
  136. styler.ColourTo(startLine + offset - 1, SCE_BAT_COMMAND);
  137. } else if (keywords2.InList(wordBuffer)) {
  138. styler.ColourTo(startLine + offset - 1, SCE_BAT_COMMAND);
  139. } else {
  140. styler.ColourTo(startLine + offset - 1, SCE_BAT_DEFAULT);
  141. }
  142. // Reset External Command / Program Location
  143. cmdLoc = offset;
  144. } else {
  145. // Reset Offset to re-process remainder of word
  146. offset -= (wbl - 1);
  147. // Colorize Default Text
  148. styler.ColourTo(startLine + offset - 1, SCE_BAT_DEFAULT);
  149. }
  150. // Check for Regular Keyword in list
  151. } else if ((keywords.InList(wordBuffer)) &&
  152. (continueProcessing)) {
  153. // ECHO, GOTO, PROMPT and SET require no further Regular Keyword Checking
  154. if ((CompareCaseInsensitive(wordBuffer, "echo") == 0) ||
  155. (CompareCaseInsensitive(wordBuffer, "goto") == 0) ||
  156. (CompareCaseInsensitive(wordBuffer, "prompt") == 0) ||
  157. (CompareCaseInsensitive(wordBuffer, "set") == 0)) {
  158. continueProcessing = false;
  159. }
  160. // Identify External Command / Program Location for ERRORLEVEL, and EXIST
  161. if ((CompareCaseInsensitive(wordBuffer, "errorlevel") == 0) ||
  162. (CompareCaseInsensitive(wordBuffer, "exist") == 0)) {
  163. // Reset External Command / Program Location
  164. cmdLoc = offset;
  165. // Skip next spaces
  166. while ((cmdLoc < lengthLine) &&
  167. (isspacechar(lineBuffer[cmdLoc]))) {
  168. cmdLoc++;
  169. }
  170. // Skip comparison
  171. while ((cmdLoc < lengthLine) &&
  172. (!isspacechar(lineBuffer[cmdLoc]))) {
  173. cmdLoc++;
  174. }
  175. // Skip next spaces
  176. while ((cmdLoc < lengthLine) &&
  177. (isspacechar(lineBuffer[cmdLoc]))) {
  178. cmdLoc++;
  179. }
  180. // Identify External Command / Program Location for CALL, DO, LOADHIGH and LH
  181. } else if ((CompareCaseInsensitive(wordBuffer, "call") == 0) ||
  182. (CompareCaseInsensitive(wordBuffer, "do") == 0) ||
  183. (CompareCaseInsensitive(wordBuffer, "loadhigh") == 0) ||
  184. (CompareCaseInsensitive(wordBuffer, "lh") == 0)) {
  185. // Reset External Command / Program Location
  186. cmdLoc = offset;
  187. // Skip next spaces
  188. while ((cmdLoc < lengthLine) &&
  189. (isspacechar(lineBuffer[cmdLoc]))) {
  190. cmdLoc++;
  191. }
  192. }
  193. // Colorize Regular keyword
  194. styler.ColourTo(startLine + offset - 1, SCE_BAT_WORD);
  195. // No need to Reset Offset
  196. // Check for Special Keyword in list, External Command / Program, or Default Text
  197. } else if ((wordBuffer[0] != '%') &&
  198. (wordBuffer[0] != '!') &&
  199. (!IsBOperator(wordBuffer[0])) &&
  200. (continueProcessing)) {
  201. // Check for Special Keyword
  202. // Affected Commands are in Length range 2-6
  203. // Good that ERRORLEVEL, EXIST, CALL, DO, LOADHIGH, and LH are unaffected
  204. sKeywordFound = false;
  205. for (Sci_PositionU keywordLength = 2; keywordLength < wbl && keywordLength < 7 && !sKeywordFound; keywordLength++) {
  206. wbo = 0;
  207. // Copy Keyword Length from Word Buffer into Special Keyword Buffer
  208. for (; wbo < keywordLength; wbo++) {
  209. sKeywordBuffer[wbo] = static_cast<char>(wordBuffer[wbo]);
  210. }
  211. sKeywordBuffer[wbo] = '\0';
  212. // Check for Special Keyword in list
  213. if ((keywords.InList(sKeywordBuffer)) &&
  214. ((IsBOperator(wordBuffer[wbo])) ||
  215. (IsBSeparator(wordBuffer[wbo])))) {
  216. sKeywordFound = true;
  217. // ECHO requires no further Regular Keyword Checking
  218. if (CompareCaseInsensitive(sKeywordBuffer, "echo") == 0) {
  219. continueProcessing = false;
  220. }
  221. // Colorize Special Keyword as Regular Keyword
  222. styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_WORD);
  223. // Reset Offset to re-process remainder of word
  224. offset -= (wbl - wbo);
  225. }
  226. }
  227. // Check for External Command / Program or Default Text
  228. if (!sKeywordFound) {
  229. wbo = 0;
  230. // Check for External Command / Program
  231. if (cmdLoc == offset - wbl) {
  232. // Read up to %, Operator or Separator
  233. while ((wbo < wbl) &&
  234. (wordBuffer[wbo] != '%') &&
  235. (wordBuffer[wbo] != '!') &&
  236. (!IsBOperator(wordBuffer[wbo])) &&
  237. (!IsBSeparator(wordBuffer[wbo]))) {
  238. wbo++;
  239. }
  240. // Reset External Command / Program Location
  241. cmdLoc = offset - (wbl - wbo);
  242. // Reset Offset to re-process remainder of word
  243. offset -= (wbl - wbo);
  244. // CHOICE requires no further Regular Keyword Checking
  245. if (CompareCaseInsensitive(wordBuffer, "choice") == 0) {
  246. continueProcessing = false;
  247. }
  248. // Check for START (and its switches) - What follows is External Command \ Program
  249. if (CompareCaseInsensitive(wordBuffer, "start") == 0) {
  250. // Reset External Command / Program Location
  251. cmdLoc = offset;
  252. // Skip next spaces
  253. while ((cmdLoc < lengthLine) &&
  254. (isspacechar(lineBuffer[cmdLoc]))) {
  255. cmdLoc++;
  256. }
  257. // Reset External Command / Program Location if command switch detected
  258. if (lineBuffer[cmdLoc] == '/') {
  259. // Skip command switch
  260. while ((cmdLoc < lengthLine) &&
  261. (!isspacechar(lineBuffer[cmdLoc]))) {
  262. cmdLoc++;
  263. }
  264. // Skip next spaces
  265. while ((cmdLoc < lengthLine) &&
  266. (isspacechar(lineBuffer[cmdLoc]))) {
  267. cmdLoc++;
  268. }
  269. }
  270. }
  271. // Colorize External Command / Program
  272. if (!keywords2) {
  273. styler.ColourTo(startLine + offset - 1, SCE_BAT_COMMAND);
  274. } else if (keywords2.InList(wordBuffer)) {
  275. styler.ColourTo(startLine + offset - 1, SCE_BAT_COMMAND);
  276. } else {
  277. styler.ColourTo(startLine + offset - 1, SCE_BAT_DEFAULT);
  278. }
  279. // No need to Reset Offset
  280. // Check for Default Text
  281. } else {
  282. // Read up to %, Operator or Separator
  283. while ((wbo < wbl) &&
  284. (wordBuffer[wbo] != '%') &&
  285. (wordBuffer[wbo] != '!') &&
  286. (!IsBOperator(wordBuffer[wbo])) &&
  287. (!IsBSeparator(wordBuffer[wbo]))) {
  288. wbo++;
  289. }
  290. // Colorize Default Text
  291. styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_DEFAULT);
  292. // Reset Offset to re-process remainder of word
  293. offset -= (wbl - wbo);
  294. }
  295. }
  296. // Check for Argument (%n), Environment Variable (%x...%) or Local Variable (%%a)
  297. } else if (wordBuffer[0] == '%') {
  298. // Colorize Default Text
  299. styler.ColourTo(startLine + offset - 1 - wbl, SCE_BAT_DEFAULT);
  300. wbo++;
  301. // Search to end of word for second % (can be a long path)
  302. while ((wbo < wbl) &&
  303. (wordBuffer[wbo] != '%') &&
  304. (!IsBOperator(wordBuffer[wbo])) &&
  305. (!IsBSeparator(wordBuffer[wbo]))) {
  306. wbo++;
  307. }
  308. // Check for Argument (%n) or (%*)
  309. if (((Is0To9(wordBuffer[1])) || (wordBuffer[1] == '*')) &&
  310. (wordBuffer[wbo] != '%')) {
  311. // Check for External Command / Program
  312. if (cmdLoc == offset - wbl) {
  313. cmdLoc = offset - (wbl - 2);
  314. }
  315. // Colorize Argument
  316. styler.ColourTo(startLine + offset - 1 - (wbl - 2), SCE_BAT_IDENTIFIER);
  317. // Reset Offset to re-process remainder of word
  318. offset -= (wbl - 2);
  319. // Check for Expanded Argument (%~...) / Variable (%%~...)
  320. } else if (((wbl > 1) && (wordBuffer[1] == '~')) ||
  321. ((wbl > 2) && (wordBuffer[1] == '%') && (wordBuffer[2] == '~'))) {
  322. // Check for External Command / Program
  323. if (cmdLoc == offset - wbl) {
  324. cmdLoc = offset - (wbl - wbo);
  325. }
  326. // Colorize Expanded Argument / Variable
  327. styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_IDENTIFIER);
  328. // Reset Offset to re-process remainder of word
  329. offset -= (wbl - wbo);
  330. // Check for Environment Variable (%x...%)
  331. } else if ((wordBuffer[1] != '%') &&
  332. (wordBuffer[wbo] == '%')) {
  333. wbo++;
  334. // Check for External Command / Program
  335. if (cmdLoc == offset - wbl) {
  336. cmdLoc = offset - (wbl - wbo);
  337. }
  338. // Colorize Environment Variable
  339. styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_IDENTIFIER);
  340. // Reset Offset to re-process remainder of word
  341. offset -= (wbl - wbo);
  342. // Check for Local Variable (%%a)
  343. } else if (
  344. (wbl > 2) &&
  345. (wordBuffer[1] == '%') &&
  346. (wordBuffer[2] != '%') &&
  347. (!IsBOperator(wordBuffer[2])) &&
  348. (!IsBSeparator(wordBuffer[2]))) {
  349. // Check for External Command / Program
  350. if (cmdLoc == offset - wbl) {
  351. cmdLoc = offset - (wbl - 3);
  352. }
  353. // Colorize Local Variable
  354. styler.ColourTo(startLine + offset - 1 - (wbl - 3), SCE_BAT_IDENTIFIER);
  355. // Reset Offset to re-process remainder of word
  356. offset -= (wbl - 3);
  357. }
  358. // Check for Environment Variable (!x...!)
  359. } else if (wordBuffer[0] == '!') {
  360. // Colorize Default Text
  361. styler.ColourTo(startLine + offset - 1 - wbl, SCE_BAT_DEFAULT);
  362. wbo++;
  363. // Search to end of word for second ! (can be a long path)
  364. while ((wbo < wbl) &&
  365. (wordBuffer[wbo] != '!') &&
  366. (!IsBOperator(wordBuffer[wbo])) &&
  367. (!IsBSeparator(wordBuffer[wbo]))) {
  368. wbo++;
  369. }
  370. if (wordBuffer[wbo] == '!') {
  371. wbo++;
  372. // Check for External Command / Program
  373. if (cmdLoc == offset - wbl) {
  374. cmdLoc = offset - (wbl - wbo);
  375. }
  376. // Colorize Environment Variable
  377. styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_IDENTIFIER);
  378. // Reset Offset to re-process remainder of word
  379. offset -= (wbl - wbo);
  380. }
  381. // Check for Operator
  382. } else if (IsBOperator(wordBuffer[0])) {
  383. // Colorize Default Text
  384. styler.ColourTo(startLine + offset - 1 - wbl, SCE_BAT_DEFAULT);
  385. // Check for Comparison Operator
  386. if ((wordBuffer[0] == '=') && (wordBuffer[1] == '=')) {
  387. // Identify External Command / Program Location for IF
  388. cmdLoc = offset;
  389. // Skip next spaces
  390. while ((cmdLoc < lengthLine) &&
  391. (isspacechar(lineBuffer[cmdLoc]))) {
  392. cmdLoc++;
  393. }
  394. // Colorize Comparison Operator
  395. styler.ColourTo(startLine + offset - 1 - (wbl - 2), SCE_BAT_OPERATOR);
  396. // Reset Offset to re-process remainder of word
  397. offset -= (wbl - 2);
  398. // Check for Pipe Operator
  399. } else if (wordBuffer[0] == '|') {
  400. // Reset External Command / Program Location
  401. cmdLoc = offset - wbl + 1;
  402. // Skip next spaces
  403. while ((cmdLoc < lengthLine) &&
  404. (isspacechar(lineBuffer[cmdLoc]))) {
  405. cmdLoc++;
  406. }
  407. // Colorize Pipe Operator
  408. styler.ColourTo(startLine + offset - 1 - (wbl - 1), SCE_BAT_OPERATOR);
  409. // Reset Offset to re-process remainder of word
  410. offset -= (wbl - 1);
  411. // Check for Other Operator
  412. } else {
  413. // Check for > Operator
  414. if (wordBuffer[0] == '>') {
  415. // Turn Keyword and External Command / Program checking back on
  416. continueProcessing = true;
  417. }
  418. // Colorize Other Operator
  419. styler.ColourTo(startLine + offset - 1 - (wbl - 1), SCE_BAT_OPERATOR);
  420. // Reset Offset to re-process remainder of word
  421. offset -= (wbl - 1);
  422. }
  423. // Check for Default Text
  424. } else {
  425. // Read up to %, Operator or Separator
  426. while ((wbo < wbl) &&
  427. (wordBuffer[wbo] != '%') &&
  428. (wordBuffer[wbo] != '!') &&
  429. (!IsBOperator(wordBuffer[wbo])) &&
  430. (!IsBSeparator(wordBuffer[wbo]))) {
  431. wbo++;
  432. }
  433. // Colorize Default Text
  434. styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_DEFAULT);
  435. // Reset Offset to re-process remainder of word
  436. offset -= (wbl - wbo);
  437. }
  438. // Skip next spaces - nothing happens if Offset was Reset
  439. while ((offset < lengthLine) && (isspacechar(lineBuffer[offset]))) {
  440. offset++;
  441. }
  442. }
  443. // Colorize Default Text for remainder of line - currently not lexed
  444. styler.ColourTo(endPos, SCE_BAT_DEFAULT);
  445. }
  446. static void ColouriseBatchDoc(
  447. Sci_PositionU startPos,
  448. Sci_Position length,
  449. int /*initStyle*/,
  450. WordList *keywordlists[],
  451. Accessor &styler) {
  452. char lineBuffer[1024];
  453. styler.StartAt(startPos);
  454. styler.StartSegment(startPos);
  455. Sci_PositionU linePos = 0;
  456. Sci_PositionU startLine = startPos;
  457. for (Sci_PositionU i = startPos; i < startPos + length; i++) {
  458. lineBuffer[linePos++] = styler[i];
  459. if (AtEOL(styler, i) || (linePos >= sizeof(lineBuffer) - 1)) {
  460. // End of line (or of line buffer) met, colourise it
  461. lineBuffer[linePos] = '\0';
  462. ColouriseBatchLine(lineBuffer, linePos, startLine, i, keywordlists, styler);
  463. linePos = 0;
  464. startLine = i + 1;
  465. }
  466. }
  467. if (linePos > 0) { // Last line does not have ending characters
  468. lineBuffer[linePos] = '\0';
  469. ColouriseBatchLine(lineBuffer, linePos, startLine, startPos + length - 1,
  470. keywordlists, styler);
  471. }
  472. }
  473. static const char *const batchWordListDesc[] = {
  474. "Internal Commands",
  475. "External Commands",
  476. 0
  477. };
  478. LexerModule lmBatch(SCLEX_BATCH, ColouriseBatchDoc, "batch", 0, batchWordListDesc);