LexTCMD.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // Scintilla\ source code edit control
  2. /** @file LexTCMD.cxx
  3. ** Lexer for Take Command / TCC batch scripts (.bat, .btm, .cmd).
  4. **/
  5. // Written by Rex Conn (rconn [at] jpsoft [dot] com)
  6. // based on the CMD lexer
  7. // The License.txt file describes the conditions under which this software may be distributed.
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <stdarg.h>
  12. #include <assert.h>
  13. #include <ctype.h>
  14. #include "ILexer.h"
  15. #include "Scintilla.h"
  16. #include "SciLexer.h"
  17. #include "WordList.h"
  18. #include "LexAccessor.h"
  19. #include "Accessor.h"
  20. #include "StyleContext.h"
  21. #include "CharacterSet.h"
  22. #include "LexerModule.h"
  23. #ifdef SCI_NAMESPACE
  24. using namespace Scintilla;
  25. #endif
  26. static bool IsAlphabetic(int ch) {
  27. return IsASCII(ch) && isalpha(ch);
  28. }
  29. static inline bool AtEOL(Accessor &styler, Sci_PositionU i) {
  30. return (styler[i] == '\n') || ((styler[i] == '\r') && (styler.SafeGetCharAt(i + 1) != '\n'));
  31. }
  32. // Tests for BATCH Operators
  33. static bool IsBOperator(char ch) {
  34. return (ch == '=') || (ch == '+') || (ch == '>') || (ch == '<') || (ch == '|') || (ch == '&') || (ch == '!') || (ch == '?') || (ch == '*') || (ch == '(') || (ch == ')');
  35. }
  36. // Tests for BATCH Separators
  37. static bool IsBSeparator(char ch) {
  38. return (ch == '\\') || (ch == '.') || (ch == ';') || (ch == ' ') || (ch == '\t') || (ch == '[') || (ch == ']') || (ch == '\"') || (ch == '\'') || (ch == '/');
  39. }
  40. // Find length of CMD FOR variable with modifier (%~...) or return 0
  41. static unsigned int GetBatchVarLen( char *wordBuffer )
  42. {
  43. int nLength = 0;
  44. if ( wordBuffer[0] == '%' ) {
  45. if ( wordBuffer[1] == '~' )
  46. nLength = 2;
  47. else if (( wordBuffer[1] == '%' ) && ( wordBuffer[2] == '~' ))
  48. nLength++;
  49. else
  50. return 0;
  51. for ( ; ( wordBuffer[nLength] ); nLength++ ) {
  52. switch ( toupper(wordBuffer[nLength]) ) {
  53. case 'A':
  54. // file attributes
  55. case 'D':
  56. // drive letter only
  57. case 'F':
  58. // fully qualified path name
  59. case 'N':
  60. // filename only
  61. case 'P':
  62. // path only
  63. case 'S':
  64. // short name
  65. case 'T':
  66. // date / time of file
  67. case 'X':
  68. // file extension only
  69. case 'Z':
  70. // file size
  71. break;
  72. default:
  73. return nLength;
  74. }
  75. }
  76. }
  77. return nLength;
  78. }
  79. static void ColouriseTCMDLine( char *lineBuffer, Sci_PositionU lengthLine, Sci_PositionU startLine, Sci_PositionU endPos, WordList *keywordlists[], Accessor &styler)
  80. {
  81. Sci_PositionU offset = 0; // Line Buffer Offset
  82. char wordBuffer[260]; // Word Buffer - large to catch long paths
  83. Sci_PositionU wbl; // Word Buffer Length
  84. Sci_PositionU wbo; // Word Buffer Offset - also Special Keyword Buffer Length
  85. WordList &keywords = *keywordlists[0]; // Internal Commands
  86. // WordList &keywords2 = *keywordlists[1]; // Aliases (optional)
  87. bool isDelayedExpansion = 1; // !var!
  88. bool continueProcessing = true; // Used to toggle Regular Keyword Checking
  89. // Special Keywords are those that allow certain characters without whitespace after the command
  90. // Examples are: cd. cd\ echo: echo. path=
  91. bool inString = false; // Used for processing while ""
  92. // Special Keyword Buffer used to determine if the first n characters is a Keyword
  93. char sKeywordBuffer[260] = ""; // Special Keyword Buffer
  94. bool sKeywordFound; // Exit Special Keyword for-loop if found
  95. // Skip leading whitespace
  96. while ((offset < lengthLine) && (isspacechar(lineBuffer[offset]))) {
  97. offset++;
  98. }
  99. // Colorize Default Text
  100. styler.ColourTo(startLine + offset - 1, SCE_TCMD_DEFAULT);
  101. if ( offset >= lengthLine )
  102. return;
  103. // Check for Fake Label (Comment) or Real Label - return if found
  104. if (lineBuffer[offset] == ':') {
  105. if (lineBuffer[offset + 1] == ':') {
  106. // Colorize Fake Label (Comment) - :: is the same as REM
  107. styler.ColourTo(endPos, SCE_TCMD_COMMENT);
  108. } else {
  109. // Colorize Real Label
  110. styler.ColourTo(endPos, SCE_TCMD_LABEL);
  111. }
  112. return;
  113. // Check for Comment - return if found
  114. } else if (( CompareNCaseInsensitive(lineBuffer+offset, "rem", 3) == 0 ) && (( lineBuffer[offset+3] == 0 ) || ( isspace(lineBuffer[offset+3] )))) {
  115. styler.ColourTo(endPos, SCE_TCMD_COMMENT);
  116. return;
  117. // Check for Drive Change (Drive Change is internal command) - return if found
  118. } else if ((IsAlphabetic(lineBuffer[offset])) &&
  119. (lineBuffer[offset + 1] == ':') &&
  120. ((isspacechar(lineBuffer[offset + 2])) ||
  121. (((lineBuffer[offset + 2] == '\\')) &&
  122. (isspacechar(lineBuffer[offset + 3]))))) {
  123. // Colorize Regular Keyword
  124. styler.ColourTo(endPos, SCE_TCMD_WORD);
  125. return;
  126. }
  127. // Check for Hide Command (@ECHO OFF/ON)
  128. if (lineBuffer[offset] == '@') {
  129. styler.ColourTo(startLine + offset, SCE_TCMD_HIDE);
  130. offset++;
  131. }
  132. // Skip whitespace
  133. while ((offset < lengthLine) && (isspacechar(lineBuffer[offset]))) {
  134. offset++;
  135. }
  136. // Read remainder of line word-at-a-time or remainder-of-word-at-a-time
  137. while (offset < lengthLine) {
  138. if (offset > startLine) {
  139. // Colorize Default Text
  140. styler.ColourTo(startLine + offset - 1, SCE_TCMD_DEFAULT);
  141. }
  142. // Copy word from Line Buffer into Word Buffer
  143. wbl = 0;
  144. for (; offset < lengthLine && ( wbl < 260 ) && !isspacechar(lineBuffer[offset]); wbl++, offset++) {
  145. wordBuffer[wbl] = static_cast<char>(tolower(lineBuffer[offset]));
  146. }
  147. wordBuffer[wbl] = '\0';
  148. wbo = 0;
  149. // Check for Separator
  150. if (IsBSeparator(wordBuffer[0])) {
  151. // Reset Offset to re-process remainder of word
  152. offset -= (wbl - 1);
  153. // Colorize Default Text
  154. styler.ColourTo(startLine + offset - 1, SCE_BAT_DEFAULT);
  155. if (wordBuffer[0] == '"')
  156. inString = !inString;
  157. // Check for Regular expression
  158. } else if (( wordBuffer[0] == ':' ) && ( wordBuffer[1] == ':' ) && (continueProcessing)) {
  159. // Colorize Regular exoressuin
  160. styler.ColourTo(startLine + offset - 1, SCE_TCMD_DEFAULT);
  161. // No need to Reset Offset
  162. // Check for Labels in text (... :label)
  163. } else if (wordBuffer[0] == ':' && isspacechar(lineBuffer[offset - wbl - 1])) {
  164. // Colorize Default Text
  165. styler.ColourTo(startLine + offset - 1 - wbl, SCE_TCMD_DEFAULT);
  166. // Colorize Label
  167. styler.ColourTo(startLine + offset - 1, SCE_TCMD_CLABEL);
  168. // No need to Reset Offset
  169. // Check for delayed expansion Variable (!x...!)
  170. } else if (isDelayedExpansion && wordBuffer[0] == '!') {
  171. // Colorize Default Text
  172. styler.ColourTo(startLine + offset - 1 - wbl, SCE_TCMD_DEFAULT);
  173. wbo++;
  174. // Search to end of word for second !
  175. while ((wbo < wbl) && (wordBuffer[wbo] != '!') && (!IsBOperator(wordBuffer[wbo])) && (!IsBSeparator(wordBuffer[wbo]))) {
  176. wbo++;
  177. }
  178. if (wordBuffer[wbo] == '!') {
  179. wbo++;
  180. // Colorize Environment Variable
  181. styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_TCMD_EXPANSION);
  182. } else {
  183. wbo = 1;
  184. // Colorize Symbol
  185. styler.ColourTo(startLine + offset - 1 - (wbl - 1), SCE_TCMD_DEFAULT);
  186. }
  187. // Reset Offset to re-process remainder of word
  188. offset -= (wbl - wbo);
  189. // Check for Regular Keyword in list
  190. } else if ((keywords.InList(wordBuffer)) && (!inString) && (continueProcessing)) {
  191. // ECHO, PATH, and PROMPT require no further Regular Keyword Checking
  192. if ((CompareCaseInsensitive(wordBuffer, "echo") == 0) ||
  193. (CompareCaseInsensitive(sKeywordBuffer, "echos") == 0) ||
  194. (CompareCaseInsensitive(sKeywordBuffer, "echoerr") == 0) ||
  195. (CompareCaseInsensitive(sKeywordBuffer, "echoserr") == 0) ||
  196. (CompareCaseInsensitive(wordBuffer, "path") == 0) ||
  197. (CompareCaseInsensitive(wordBuffer, "prompt") == 0)) {
  198. continueProcessing = false;
  199. }
  200. // Colorize Regular keyword
  201. styler.ColourTo(startLine + offset - 1, SCE_TCMD_WORD);
  202. // No need to Reset Offset
  203. } else if ((wordBuffer[0] != '%') && (wordBuffer[0] != '!') && (!IsBOperator(wordBuffer[0])) && (!inString) && (continueProcessing)) {
  204. // a few commands accept "illegal" syntax -- cd\, echo., etc.
  205. sscanf( wordBuffer, "%[^.<>|&=\\/]", sKeywordBuffer );
  206. sKeywordFound = false;
  207. if ((CompareCaseInsensitive(sKeywordBuffer, "echo") == 0) ||
  208. (CompareCaseInsensitive(sKeywordBuffer, "echos") == 0) ||
  209. (CompareCaseInsensitive(sKeywordBuffer, "echoerr") == 0) ||
  210. (CompareCaseInsensitive(sKeywordBuffer, "echoserr") == 0) ||
  211. (CompareCaseInsensitive(sKeywordBuffer, "cd") == 0) ||
  212. (CompareCaseInsensitive(sKeywordBuffer, "path") == 0) ||
  213. (CompareCaseInsensitive(sKeywordBuffer, "prompt") == 0)) {
  214. // no further Regular Keyword Checking
  215. continueProcessing = false;
  216. sKeywordFound = true;
  217. wbo = (Sci_PositionU)strlen( sKeywordBuffer );
  218. // Colorize Special Keyword as Regular Keyword
  219. styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_TCMD_WORD);
  220. // Reset Offset to re-process remainder of word
  221. offset -= (wbl - wbo);
  222. }
  223. // Check for Default Text
  224. if (!sKeywordFound) {
  225. wbo = 0;
  226. // Read up to %, Operator or Separator
  227. while ((wbo < wbl) && (wordBuffer[wbo] != '%') && (!isDelayedExpansion || wordBuffer[wbo] != '!') && (!IsBOperator(wordBuffer[wbo])) && (!IsBSeparator(wordBuffer[wbo]))) {
  228. wbo++;
  229. }
  230. // Colorize Default Text
  231. styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_TCMD_DEFAULT);
  232. // Reset Offset to re-process remainder of word
  233. offset -= (wbl - wbo);
  234. }
  235. // Check for Argument (%n), Environment Variable (%x...%) or Local Variable (%%a)
  236. } else if (wordBuffer[0] == '%') {
  237. unsigned int varlen;
  238. unsigned int n = 1;
  239. // Colorize Default Text
  240. styler.ColourTo(startLine + offset - 1 - wbl, SCE_TCMD_DEFAULT);
  241. wbo++;
  242. // check for %[nn] syntax
  243. if ( wordBuffer[1] == '[' ) {
  244. n++;
  245. while ((n < wbl) && (wordBuffer[n] != ']')) {
  246. n++;
  247. }
  248. if ( wordBuffer[n] == ']' )
  249. n++;
  250. goto ColorizeArg;
  251. }
  252. // Search to end of word for second % or to the first terminator (can be a long path)
  253. while ((wbo < wbl) && (wordBuffer[wbo] != '%') && (!IsBOperator(wordBuffer[wbo])) && (!IsBSeparator(wordBuffer[wbo]))) {
  254. wbo++;
  255. }
  256. // Check for Argument (%n) or (%*)
  257. if (((isdigit(wordBuffer[1])) || (wordBuffer[1] == '*')) && (wordBuffer[wbo] != '%')) {
  258. while (( wordBuffer[n] ) && ( strchr( "%0123456789*#$", wordBuffer[n] ) != NULL ))
  259. n++;
  260. ColorizeArg:
  261. // Colorize Argument
  262. styler.ColourTo(startLine + offset - 1 - (wbl - n), SCE_TCMD_IDENTIFIER);
  263. // Reset Offset to re-process remainder of word
  264. offset -= (wbl - n);
  265. // Check for Variable with modifiers (%~...)
  266. } else if ((varlen = GetBatchVarLen(wordBuffer)) != 0) {
  267. // Colorize Variable
  268. styler.ColourTo(startLine + offset - 1 - (wbl - varlen), SCE_TCMD_IDENTIFIER);
  269. // Reset Offset to re-process remainder of word
  270. offset -= (wbl - varlen);
  271. // Check for Environment Variable (%x...%)
  272. } else if (( wordBuffer[1] ) && ( wordBuffer[1] != '%')) {
  273. if ( wordBuffer[wbo] == '%' )
  274. wbo++;
  275. // Colorize Environment Variable
  276. styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_TCMD_ENVIRONMENT);
  277. // Reset Offset to re-process remainder of word
  278. offset -= (wbl - wbo);
  279. // Check for Local Variable (%%a)
  280. } else if ( (wbl > 2) && (wordBuffer[1] == '%') && (wordBuffer[2] != '%') && (!IsBOperator(wordBuffer[2])) && (!IsBSeparator(wordBuffer[2]))) {
  281. n = 2;
  282. while (( wordBuffer[n] ) && (!IsBOperator(wordBuffer[n])) && (!IsBSeparator(wordBuffer[n])))
  283. n++;
  284. // Colorize Local Variable
  285. styler.ColourTo(startLine + offset - 1 - (wbl - n), SCE_TCMD_IDENTIFIER);
  286. // Reset Offset to re-process remainder of word
  287. offset -= (wbl - n);
  288. // Check for %%
  289. } else if ((wbl > 1) && (wordBuffer[1] == '%')) {
  290. // Colorize Symbols
  291. styler.ColourTo(startLine + offset - 1 - (wbl - 2), SCE_TCMD_DEFAULT);
  292. // Reset Offset to re-process remainder of word
  293. offset -= (wbl - 2);
  294. } else {
  295. // Colorize Symbol
  296. styler.ColourTo(startLine + offset - 1 - (wbl - 1), SCE_TCMD_DEFAULT);
  297. // Reset Offset to re-process remainder of word
  298. offset -= (wbl - 1);
  299. }
  300. // Check for Operator
  301. } else if (IsBOperator(wordBuffer[0])) {
  302. // Colorize Default Text
  303. styler.ColourTo(startLine + offset - 1 - wbl, SCE_TCMD_DEFAULT);
  304. // Check for Pipe, compound, or conditional Operator
  305. if ((wordBuffer[0] == '|') || (wordBuffer[0] == '&')) {
  306. // Colorize Pipe Operator
  307. styler.ColourTo(startLine + offset - 1 - (wbl - 1), SCE_TCMD_OPERATOR);
  308. // Reset Offset to re-process remainder of word
  309. offset -= (wbl - 1);
  310. continueProcessing = true;
  311. // Check for Other Operator
  312. } else {
  313. // Check for > Operator
  314. if ((wordBuffer[0] == '>') || (wordBuffer[0] == '<')) {
  315. // Turn Keyword and External Command / Program checking back on
  316. continueProcessing = true;
  317. }
  318. // Colorize Other Operator
  319. if (!inString || !(wordBuffer[0] == '(' || wordBuffer[0] == ')'))
  320. styler.ColourTo(startLine + offset - 1 - (wbl - 1), SCE_TCMD_OPERATOR);
  321. // Reset Offset to re-process remainder of word
  322. offset -= (wbl - 1);
  323. }
  324. // Check for Default Text
  325. } else {
  326. // Read up to %, Operator or Separator
  327. while ((wbo < wbl) && (wordBuffer[wbo] != '%') && (!isDelayedExpansion || wordBuffer[wbo] != '!') && (!IsBOperator(wordBuffer[wbo])) && (!IsBSeparator(wordBuffer[wbo]))) {
  328. wbo++;
  329. }
  330. // Colorize Default Text
  331. styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_TCMD_DEFAULT);
  332. // Reset Offset to re-process remainder of word
  333. offset -= (wbl - wbo);
  334. }
  335. // Skip whitespace - nothing happens if Offset was Reset
  336. while ((offset < lengthLine) && (isspacechar(lineBuffer[offset]))) {
  337. offset++;
  338. }
  339. }
  340. // Colorize Default Text for remainder of line - currently not lexed
  341. styler.ColourTo(endPos, SCE_TCMD_DEFAULT);
  342. }
  343. static void ColouriseTCMDDoc( Sci_PositionU startPos, Sci_Position length, int /*initStyle*/, WordList *keywordlists[], Accessor &styler )
  344. {
  345. char lineBuffer[16384];
  346. styler.StartAt(startPos);
  347. styler.StartSegment(startPos);
  348. Sci_PositionU linePos = 0;
  349. Sci_PositionU startLine = startPos;
  350. for (Sci_PositionU i = startPos; i < startPos + length; i++) {
  351. lineBuffer[linePos++] = styler[i];
  352. if (AtEOL(styler, i) || (linePos >= sizeof(lineBuffer) - 1)) {
  353. // End of line (or of line buffer) met, colourise it
  354. lineBuffer[linePos] = '\0';
  355. ColouriseTCMDLine(lineBuffer, linePos, startLine, i, keywordlists, styler);
  356. linePos = 0;
  357. startLine = i + 1;
  358. }
  359. }
  360. if (linePos > 0) { // Last line does not have ending characters
  361. lineBuffer[linePos] = '\0';
  362. ColouriseTCMDLine(lineBuffer, linePos, startLine, startPos + length - 1, keywordlists, styler);
  363. }
  364. }
  365. // Convert string to upper case
  366. static void StrUpr(char *s) {
  367. while (*s) {
  368. *s = MakeUpperCase(*s);
  369. s++;
  370. }
  371. }
  372. // Folding support (for DO, IFF, SWITCH, TEXT, and command groups)
  373. static void FoldTCMDDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler)
  374. {
  375. Sci_Position line = styler.GetLine(startPos);
  376. int level = styler.LevelAt(line);
  377. int levelIndent = 0;
  378. Sci_PositionU endPos = startPos + length;
  379. char s[16] = "";
  380. char chPrev = styler.SafeGetCharAt(startPos - 1);
  381. // Scan for ( and )
  382. for (Sci_PositionU i = startPos; i < endPos; i++) {
  383. int c = styler.SafeGetCharAt(i, '\n');
  384. int style = styler.StyleAt(i);
  385. bool bLineStart = ((chPrev == '\r') || (chPrev == '\n')) || i == 0;
  386. if (style == SCE_TCMD_OPERATOR) {
  387. // CheckFoldPoint
  388. if (c == '(') {
  389. levelIndent += 1;
  390. } else if (c == ')') {
  391. levelIndent -= 1;
  392. }
  393. }
  394. if (( bLineStart ) && ( style == SCE_TCMD_WORD )) {
  395. for (Sci_PositionU j = 0; j < 10; j++) {
  396. if (!iswordchar(styler[i + j])) {
  397. break;
  398. }
  399. s[j] = styler[i + j];
  400. s[j + 1] = '\0';
  401. }
  402. StrUpr( s );
  403. if ((strcmp(s, "DO") == 0) || (strcmp(s, "IFF") == 0) || (strcmp(s, "SWITCH") == 0) || (strcmp(s, "TEXT") == 0)) {
  404. levelIndent++;
  405. } else if ((strcmp(s, "ENDDO") == 0) || (strcmp(s, "ENDIFF") == 0) || (strcmp(s, "ENDSWITCH") == 0) || (strcmp(s, "ENDTEXT") == 0)) {
  406. levelIndent--;
  407. }
  408. }
  409. if (c == '\n') { // line end
  410. if (levelIndent > 0) {
  411. level |= SC_FOLDLEVELHEADERFLAG;
  412. }
  413. if (level != styler.LevelAt(line))
  414. styler.SetLevel(line, level);
  415. level += levelIndent;
  416. if ((level & SC_FOLDLEVELNUMBERMASK) < SC_FOLDLEVELBASE)
  417. level = SC_FOLDLEVELBASE;
  418. line++;
  419. // reset state
  420. levelIndent = 0;
  421. level &= ~SC_FOLDLEVELHEADERFLAG;
  422. level &= ~SC_FOLDLEVELWHITEFLAG;
  423. }
  424. chPrev = c;
  425. }
  426. }
  427. static const char *const tcmdWordListDesc[] = {
  428. "Internal Commands",
  429. "Aliases",
  430. 0
  431. };
  432. LexerModule lmTCMD(SCLEX_TCMD, ColouriseTCMDDoc, "tcmd", FoldTCMDDoc, tcmdWordListDesc);