PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Tools/MaterialEditor/wxscintilla_1.69.2/src/scintilla/src/LexBash.cxx

https://bitbucket.org/CaptainOz/ogre
C++ | 663 lines | 593 code | 32 blank | 38 comment | 312 complexity | 6616a6a1b2a9abd23218b3e87839e1f3 MD5 | raw file
Possible License(s): LGPL-2.1, MIT
  1. // Scintilla source code edit control
  2. /** @file LexBash.cxx
  3. ** Lexer for Bash.
  4. **/
  5. // Copyright 2004-2005 by Neil Hodgson <neilh@scintilla.org>
  6. // Adapted from LexPerl by Kein-Hong Man <mkh@pl.jaring.my> 2004
  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 <ctype.h>
  11. #include <stdio.h>
  12. #include <stdarg.h>
  13. #include "Platform.h"
  14. #include "PropSet.h"
  15. #include "Accessor.h"
  16. #include "KeyWords.h"
  17. #include "Scintilla.h"
  18. #include "SciLexer.h"
  19. #define BASH_BASE_ERROR 65
  20. #define BASH_BASE_DECIMAL 66
  21. #define BASH_BASE_HEX 67
  22. #define BASH_BASE_OCTAL 68
  23. #define BASH_BASE_OCTAL_ERROR 69
  24. #define HERE_DELIM_MAX 256
  25. static inline int translateBashDigit(char ch) {
  26. if (ch >= '0' && ch <= '9') {
  27. return ch - '0';
  28. } else if (ch >= 'a' && ch <= 'z') {
  29. return ch - 'a' + 10;
  30. } else if (ch >= 'A' && ch <= 'Z') {
  31. return ch - 'A' + 36;
  32. } else if (ch == '@') {
  33. return 62;
  34. } else if (ch == '_') {
  35. return 63;
  36. }
  37. return BASH_BASE_ERROR;
  38. }
  39. static inline bool isEOLChar(char ch) {
  40. return (ch == '\r') || (ch == '\n');
  41. }
  42. static bool isSingleCharOp(char ch) {
  43. char strCharSet[2];
  44. strCharSet[0] = ch;
  45. strCharSet[1] = '\0';
  46. return (NULL != strstr("rwxoRWXOezsfdlpSbctugkTBMACahGLNn", strCharSet));
  47. }
  48. static inline bool isBashOperator(char ch) {
  49. if (ch == '^' || ch == '&' || ch == '\\' || ch == '%' ||
  50. ch == '(' || ch == ')' || ch == '-' || ch == '+' ||
  51. ch == '=' || ch == '|' || ch == '{' || ch == '}' ||
  52. ch == '[' || ch == ']' || ch == ':' || ch == ';' ||
  53. ch == '>' || ch == ',' || ch == '/' || ch == '<' ||
  54. ch == '?' || ch == '!' || ch == '.' || ch == '~' ||
  55. ch == '@')
  56. return true;
  57. return false;
  58. }
  59. static int classifyWordBash(unsigned int start, unsigned int end, WordList &keywords, Accessor &styler) {
  60. char s[100];
  61. for (unsigned int i = 0; i < end - start + 1 && i < 30; i++) {
  62. s[i] = styler[start + i];
  63. s[i + 1] = '\0';
  64. }
  65. char chAttr = SCE_SH_IDENTIFIER;
  66. if (keywords.InList(s))
  67. chAttr = SCE_SH_WORD;
  68. styler.ColourTo(end, chAttr);
  69. return chAttr;
  70. }
  71. static inline int getBashNumberBase(unsigned int start, unsigned int end, Accessor &styler) {
  72. int base = 0;
  73. for (unsigned int i = 0; i < end - start + 1 && i < 10; i++) {
  74. base = base * 10 + (styler[start + i] - '0');
  75. }
  76. if (base > 64 || (end - start) > 1) {
  77. return BASH_BASE_ERROR;
  78. }
  79. return base;
  80. }
  81. static inline bool isEndVar(char ch) {
  82. return !isalnum(ch) && ch != '$' && ch != '_';
  83. }
  84. static inline bool isNonQuote(char ch) {
  85. return isalnum(ch) || ch == '_';
  86. }
  87. static bool isMatch(Accessor &styler, int lengthDoc, int pos, const char *val) {
  88. if ((pos + static_cast<int>(strlen(val))) >= lengthDoc) {
  89. return false;
  90. }
  91. while (*val) {
  92. if (*val != styler[pos++]) {
  93. return false;
  94. }
  95. val++;
  96. }
  97. return true;
  98. }
  99. static char opposite(char ch) {
  100. if (ch == '(')
  101. return ')';
  102. if (ch == '[')
  103. return ']';
  104. if (ch == '{')
  105. return '}';
  106. if (ch == '<')
  107. return '>';
  108. return ch;
  109. }
  110. static void ColouriseBashDoc(unsigned int startPos, int length, int initStyle,
  111. WordList *keywordlists[], Accessor &styler) {
  112. // Lexer for bash often has to backtrack to start of current style to determine
  113. // which characters are being used as quotes, how deeply nested is the
  114. // start position and what the termination string is for here documents
  115. WordList &keywords = *keywordlists[0];
  116. class HereDocCls {
  117. public:
  118. int State; // 0: '<<' encountered
  119. // 1: collect the delimiter
  120. // 2: here doc text (lines after the delimiter)
  121. char Quote; // the char after '<<'
  122. bool Quoted; // true if Quote in ('\'','"','`')
  123. bool Indent; // indented delimiter (for <<-)
  124. int DelimiterLength; // strlen(Delimiter)
  125. char *Delimiter; // the Delimiter, 256: sizeof PL_tokenbuf
  126. HereDocCls() {
  127. State = 0;
  128. Quote = 0;
  129. Quoted = false;
  130. Indent = 0;
  131. DelimiterLength = 0;
  132. Delimiter = new char[HERE_DELIM_MAX];
  133. Delimiter[0] = '\0';
  134. }
  135. ~HereDocCls() {
  136. delete []Delimiter;
  137. }
  138. };
  139. HereDocCls HereDoc;
  140. class QuoteCls {
  141. public:
  142. int Rep;
  143. int Count;
  144. char Up;
  145. char Down;
  146. QuoteCls() {
  147. this->New(1);
  148. }
  149. void New(int r) {
  150. Rep = r;
  151. Count = 0;
  152. Up = '\0';
  153. Down = '\0';
  154. }
  155. void Open(char u) {
  156. Count++;
  157. Up = u;
  158. Down = opposite(Up);
  159. }
  160. };
  161. QuoteCls Quote;
  162. int state = initStyle;
  163. int numBase = 0;
  164. unsigned int lengthDoc = startPos + length;
  165. // If in a long distance lexical state, seek to the beginning to find quote characters
  166. // Bash strings can be multi-line with embedded newlines, so backtrack.
  167. // Bash numbers have additional state during lexing, so backtrack too.
  168. if (state == SCE_SH_HERE_Q) {
  169. while ((startPos > 1) && (styler.StyleAt(startPos) != SCE_SH_HERE_DELIM)) {
  170. startPos--;
  171. }
  172. startPos = styler.LineStart(styler.GetLine(startPos));
  173. state = styler.StyleAt(startPos - 1);
  174. }
  175. if (state == SCE_SH_STRING
  176. || state == SCE_SH_BACKTICKS
  177. || state == SCE_SH_CHARACTER
  178. || state == SCE_SH_NUMBER
  179. || state == SCE_SH_IDENTIFIER
  180. || state == SCE_SH_COMMENTLINE
  181. ) {
  182. while ((startPos > 1) && (styler.StyleAt(startPos - 1) == state)) {
  183. startPos--;
  184. }
  185. state = SCE_SH_DEFAULT;
  186. }
  187. styler.StartAt(startPos);
  188. char chPrev = styler.SafeGetCharAt(startPos - 1);
  189. if (startPos == 0)
  190. chPrev = '\n';
  191. char chNext = styler[startPos];
  192. styler.StartSegment(startPos);
  193. for (unsigned int i = startPos; i < lengthDoc; i++) {
  194. char ch = chNext;
  195. // if the current character is not consumed due to the completion of an
  196. // earlier style, lexing can be restarted via a simple goto
  197. restartLexer:
  198. chNext = styler.SafeGetCharAt(i + 1);
  199. char chNext2 = styler.SafeGetCharAt(i + 2);
  200. if (styler.IsLeadByte(ch)) {
  201. chNext = styler.SafeGetCharAt(i + 2);
  202. chPrev = ' ';
  203. i += 1;
  204. continue;
  205. }
  206. if ((chPrev == '\r' && ch == '\n')) { // skip on DOS/Windows
  207. styler.ColourTo(i, state);
  208. chPrev = ch;
  209. continue;
  210. }
  211. if (HereDoc.State == 1 && isEOLChar(ch)) {
  212. // Begin of here-doc (the line after the here-doc delimiter):
  213. // Lexically, the here-doc starts from the next line after the >>, but the
  214. // first line of here-doc seem to follow the style of the last EOL sequence
  215. HereDoc.State = 2;
  216. if (HereDoc.Quoted) {
  217. if (state == SCE_SH_HERE_DELIM) {
  218. // Missing quote at end of string! We are stricter than bash.
  219. // Colour here-doc anyway while marking this bit as an error.
  220. state = SCE_SH_ERROR;
  221. }
  222. styler.ColourTo(i - 1, state);
  223. // HereDoc.Quote always == '\''
  224. state = SCE_SH_HERE_Q;
  225. } else {
  226. styler.ColourTo(i - 1, state);
  227. // always switch
  228. state = SCE_SH_HERE_Q;
  229. }
  230. }
  231. if (state == SCE_SH_DEFAULT) {
  232. if (ch == '\\') { // escaped character
  233. if (i < lengthDoc - 1)
  234. i++;
  235. ch = chNext;
  236. chNext = chNext2;
  237. styler.ColourTo(i, SCE_SH_IDENTIFIER);
  238. } else if (isdigit(ch)) {
  239. state = SCE_SH_NUMBER;
  240. numBase = BASH_BASE_DECIMAL;
  241. if (ch == '0') { // hex,octal
  242. if (chNext == 'x' || chNext == 'X') {
  243. numBase = BASH_BASE_HEX;
  244. i++;
  245. ch = chNext;
  246. chNext = chNext2;
  247. } else if (isdigit(chNext)) {
  248. numBase = BASH_BASE_OCTAL;
  249. }
  250. }
  251. } else if (iswordstart(ch)) {
  252. state = SCE_SH_WORD;
  253. if (!iswordchar(chNext) && chNext != '+' && chNext != '-') {
  254. // We need that if length of word == 1!
  255. // This test is copied from the SCE_SH_WORD handler.
  256. classifyWordBash(styler.GetStartSegment(), i, keywords, styler);
  257. state = SCE_SH_DEFAULT;
  258. }
  259. } else if (ch == '#') {
  260. state = SCE_SH_COMMENTLINE;
  261. } else if (ch == '\"') {
  262. state = SCE_SH_STRING;
  263. Quote.New(1);
  264. Quote.Open(ch);
  265. } else if (ch == '\'') {
  266. state = SCE_SH_CHARACTER;
  267. Quote.New(1);
  268. Quote.Open(ch);
  269. } else if (ch == '`') {
  270. state = SCE_SH_BACKTICKS;
  271. Quote.New(1);
  272. Quote.Open(ch);
  273. } else if (ch == '$') {
  274. if (chNext == '{') {
  275. state = SCE_SH_PARAM;
  276. goto startQuote;
  277. } else if (chNext == '\'') {
  278. state = SCE_SH_CHARACTER;
  279. goto startQuote;
  280. } else if (chNext == '"') {
  281. state = SCE_SH_STRING;
  282. goto startQuote;
  283. } else if (chNext == '(' && chNext2 == '(') {
  284. styler.ColourTo(i, SCE_SH_OPERATOR);
  285. state = SCE_SH_DEFAULT;
  286. goto skipChar;
  287. } else if (chNext == '(' || chNext == '`') {
  288. state = SCE_SH_BACKTICKS;
  289. startQuote:
  290. Quote.New(1);
  291. Quote.Open(chNext);
  292. goto skipChar;
  293. } else {
  294. state = SCE_SH_SCALAR;
  295. skipChar:
  296. i++;
  297. ch = chNext;
  298. chNext = chNext2;
  299. }
  300. } else if (ch == '*') {
  301. if (chNext == '*') { // exponentiation
  302. i++;
  303. ch = chNext;
  304. chNext = chNext2;
  305. }
  306. styler.ColourTo(i, SCE_SH_OPERATOR);
  307. } else if (ch == '<' && chNext == '<') {
  308. state = SCE_SH_HERE_DELIM;
  309. HereDoc.State = 0;
  310. HereDoc.Indent = false;
  311. } else if (ch == '-' // file test operators
  312. && isSingleCharOp(chNext)
  313. && !isalnum((chNext2 = styler.SafeGetCharAt(i+2)))) {
  314. styler.ColourTo(i + 1, SCE_SH_WORD);
  315. state = SCE_SH_DEFAULT;
  316. i++;
  317. ch = chNext;
  318. chNext = chNext2;
  319. } else if (isBashOperator(ch)) {
  320. styler.ColourTo(i, SCE_SH_OPERATOR);
  321. } else {
  322. // keep colouring defaults to make restart easier
  323. styler.ColourTo(i, SCE_SH_DEFAULT);
  324. }
  325. } else if (state == SCE_SH_NUMBER) {
  326. int digit = translateBashDigit(ch);
  327. if (numBase == BASH_BASE_DECIMAL) {
  328. if (ch == '#') {
  329. numBase = getBashNumberBase(styler.GetStartSegment(), i - 1, styler);
  330. if (numBase == BASH_BASE_ERROR) // take the rest as comment
  331. goto numAtEnd;
  332. } else if (!isdigit(ch))
  333. goto numAtEnd;
  334. } else if (numBase == BASH_BASE_HEX) {
  335. if ((digit < 16) || (digit >= 36 && digit <= 41)) {
  336. // hex digit 0-9a-fA-F
  337. } else
  338. goto numAtEnd;
  339. } else if (numBase == BASH_BASE_OCTAL ||
  340. numBase == BASH_BASE_OCTAL_ERROR) {
  341. if (digit > 7) {
  342. if (digit <= 9) {
  343. numBase = BASH_BASE_OCTAL_ERROR;
  344. } else
  345. goto numAtEnd;
  346. }
  347. } else if (numBase == BASH_BASE_ERROR) {
  348. if (digit > 9)
  349. goto numAtEnd;
  350. } else { // DD#DDDD number style handling
  351. if (digit != BASH_BASE_ERROR) {
  352. if (numBase <= 36) {
  353. // case-insensitive if base<=36
  354. if (digit >= 36) digit -= 26;
  355. }
  356. if (digit >= numBase) {
  357. if (digit <= 9) {
  358. numBase = BASH_BASE_ERROR;
  359. } else
  360. goto numAtEnd;
  361. }
  362. } else {
  363. numAtEnd:
  364. if (numBase == BASH_BASE_ERROR ||
  365. numBase == BASH_BASE_OCTAL_ERROR)
  366. state = SCE_SH_ERROR;
  367. styler.ColourTo(i - 1, state);
  368. state = SCE_SH_DEFAULT;
  369. goto restartLexer;
  370. }
  371. }
  372. } else if (state == SCE_SH_WORD) {
  373. if (!iswordchar(chNext) && chNext != '+' && chNext != '-') {
  374. // "." never used in Bash variable names
  375. // but used in file names
  376. classifyWordBash(styler.GetStartSegment(), i, keywords, styler);
  377. state = SCE_SH_DEFAULT;
  378. ch = ' ';
  379. }
  380. } else if (state == SCE_SH_IDENTIFIER) {
  381. if (!iswordchar(chNext) && chNext != '+' && chNext != '-') {
  382. styler.ColourTo(i, SCE_SH_IDENTIFIER);
  383. state = SCE_SH_DEFAULT;
  384. ch = ' ';
  385. }
  386. } else {
  387. if (state == SCE_SH_COMMENTLINE) {
  388. if (ch == '\\' && isEOLChar(chNext)) {
  389. // comment continuation
  390. if (chNext == '\r' && chNext2 == '\n') {
  391. i += 2;
  392. ch = styler.SafeGetCharAt(i);
  393. chNext = styler.SafeGetCharAt(i + 1);
  394. } else {
  395. i++;
  396. ch = chNext;
  397. chNext = chNext2;
  398. }
  399. } else if (isEOLChar(ch)) {
  400. styler.ColourTo(i - 1, state);
  401. state = SCE_SH_DEFAULT;
  402. goto restartLexer;
  403. } else if (isEOLChar(chNext)) {
  404. styler.ColourTo(i, state);
  405. state = SCE_SH_DEFAULT;
  406. }
  407. } else if (state == SCE_SH_HERE_DELIM) {
  408. //
  409. // From Bash info:
  410. // ---------------
  411. // Specifier format is: <<[-]WORD
  412. // Optional '-' is for removal of leading tabs from here-doc.
  413. // Whitespace acceptable after <<[-] operator
  414. //
  415. if (HereDoc.State == 0) { // '<<' encountered
  416. HereDoc.State = 1;
  417. HereDoc.Quote = chNext;
  418. HereDoc.Quoted = false;
  419. HereDoc.DelimiterLength = 0;
  420. HereDoc.Delimiter[HereDoc.DelimiterLength] = '\0';
  421. if (chNext == '\'' || chNext == '\"') { // a quoted here-doc delimiter (' or ")
  422. i++;
  423. ch = chNext;
  424. chNext = chNext2;
  425. HereDoc.Quoted = true;
  426. } else if (!HereDoc.Indent && chNext == '-') { // <<- indent case
  427. HereDoc.Indent = true;
  428. HereDoc.State = 0;
  429. } else if (isalpha(chNext) || chNext == '_' || chNext == '\\'
  430. || chNext == '-' || chNext == '+' || chNext == '!') {
  431. // an unquoted here-doc delimiter, no special handling
  432. // TODO check what exactly bash considers part of the delim
  433. } else if (chNext == '<') { // HERE string <<<
  434. i++;
  435. ch = chNext;
  436. chNext = chNext2;
  437. styler.ColourTo(i, SCE_SH_HERE_DELIM);
  438. state = SCE_SH_DEFAULT;
  439. HereDoc.State = 0;
  440. } else if (isspacechar(chNext)) {
  441. // eat whitespace
  442. HereDoc.State = 0;
  443. } else if (isdigit(chNext) || chNext == '=' || chNext == '$') {
  444. // left shift << or <<= operator cases
  445. styler.ColourTo(i, SCE_SH_OPERATOR);
  446. state = SCE_SH_DEFAULT;
  447. HereDoc.State = 0;
  448. } else {
  449. // symbols terminates; deprecated zero-length delimiter
  450. }
  451. } else if (HereDoc.State == 1) { // collect the delimiter
  452. if (HereDoc.Quoted) { // a quoted here-doc delimiter
  453. if (ch == HereDoc.Quote) { // closing quote => end of delimiter
  454. styler.ColourTo(i, state);
  455. state = SCE_SH_DEFAULT;
  456. } else {
  457. if (ch == '\\' && chNext == HereDoc.Quote) { // escaped quote
  458. i++;
  459. ch = chNext;
  460. chNext = chNext2;
  461. }
  462. HereDoc.Delimiter[HereDoc.DelimiterLength++] = ch;
  463. HereDoc.Delimiter[HereDoc.DelimiterLength] = '\0';
  464. }
  465. } else { // an unquoted here-doc delimiter
  466. if (isalnum(ch) || ch == '_' || ch == '-' || ch == '+' || ch == '!') {
  467. HereDoc.Delimiter[HereDoc.DelimiterLength++] = ch;
  468. HereDoc.Delimiter[HereDoc.DelimiterLength] = '\0';
  469. } else if (ch == '\\') {
  470. // skip escape prefix
  471. } else {
  472. styler.ColourTo(i - 1, state);
  473. state = SCE_SH_DEFAULT;
  474. goto restartLexer;
  475. }
  476. }
  477. if (HereDoc.DelimiterLength >= HERE_DELIM_MAX - 1) {
  478. styler.ColourTo(i - 1, state);
  479. state = SCE_SH_ERROR;
  480. goto restartLexer;
  481. }
  482. }
  483. } else if (HereDoc.State == 2) {
  484. // state == SCE_SH_HERE_Q
  485. if (isMatch(styler, lengthDoc, i, HereDoc.Delimiter)) {
  486. if (!HereDoc.Indent && isEOLChar(chPrev)) {
  487. endHereDoc:
  488. // standard HERE delimiter
  489. i += HereDoc.DelimiterLength;
  490. chPrev = styler.SafeGetCharAt(i - 1);
  491. ch = styler.SafeGetCharAt(i);
  492. if (isEOLChar(ch)) {
  493. styler.ColourTo(i - 1, state);
  494. state = SCE_SH_DEFAULT;
  495. HereDoc.State = 0;
  496. goto restartLexer;
  497. }
  498. chNext = styler.SafeGetCharAt(i + 1);
  499. } else if (HereDoc.Indent) {
  500. // indented HERE delimiter
  501. unsigned int bk = (i > 0)? i - 1: 0;
  502. while (i > 0) {
  503. ch = styler.SafeGetCharAt(bk--);
  504. if (isEOLChar(ch)) {
  505. goto endHereDoc;
  506. } else if (!isspacechar(ch)) {
  507. break; // got leading non-whitespace
  508. }
  509. }
  510. }
  511. }
  512. } else if (state == SCE_SH_SCALAR) { // variable names
  513. if (isEndVar(ch)) {
  514. if ((state == SCE_SH_SCALAR)
  515. && i == (styler.GetStartSegment() + 1)) {
  516. // Special variable: $(, $_ etc.
  517. styler.ColourTo(i, state);
  518. state = SCE_SH_DEFAULT;
  519. } else {
  520. styler.ColourTo(i - 1, state);
  521. state = SCE_SH_DEFAULT;
  522. goto restartLexer;
  523. }
  524. }
  525. } else if (state == SCE_SH_STRING
  526. || state == SCE_SH_CHARACTER
  527. || state == SCE_SH_BACKTICKS
  528. || state == SCE_SH_PARAM
  529. ) {
  530. if (!Quote.Down && !isspacechar(ch)) {
  531. Quote.Open(ch);
  532. } else if (ch == '\\' && Quote.Up != '\\') {
  533. i++;
  534. ch = chNext;
  535. chNext = styler.SafeGetCharAt(i + 1);
  536. } else if (ch == Quote.Down) {
  537. Quote.Count--;
  538. if (Quote.Count == 0) {
  539. Quote.Rep--;
  540. if (Quote.Rep <= 0) {
  541. styler.ColourTo(i, state);
  542. state = SCE_SH_DEFAULT;
  543. ch = ' ';
  544. }
  545. if (Quote.Up == Quote.Down) {
  546. Quote.Count++;
  547. }
  548. }
  549. } else if (ch == Quote.Up) {
  550. Quote.Count++;
  551. }
  552. }
  553. }
  554. if (state == SCE_SH_ERROR) {
  555. break;
  556. }
  557. chPrev = ch;
  558. }
  559. styler.ColourTo(lengthDoc - 1, state);
  560. }
  561. static bool IsCommentLine(int line, Accessor &styler) {
  562. int pos = styler.LineStart(line);
  563. int eol_pos = styler.LineStart(line + 1) - 1;
  564. for (int i = pos; i < eol_pos; i++) {
  565. char ch = styler[i];
  566. if (ch == '#')
  567. return true;
  568. else if (ch != ' ' && ch != '\t')
  569. return false;
  570. }
  571. return false;
  572. }
  573. static void FoldBashDoc(unsigned int startPos, int length, int, WordList *[],
  574. Accessor &styler) {
  575. bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
  576. bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
  577. unsigned int endPos = startPos + length;
  578. int visibleChars = 0;
  579. int lineCurrent = styler.GetLine(startPos);
  580. int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
  581. int levelCurrent = levelPrev;
  582. char chNext = styler[startPos];
  583. int styleNext = styler.StyleAt(startPos);
  584. for (unsigned int i = startPos; i < endPos; i++) {
  585. char ch = chNext;
  586. chNext = styler.SafeGetCharAt(i + 1);
  587. int style = styleNext;
  588. styleNext = styler.StyleAt(i + 1);
  589. bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
  590. // Comment folding
  591. if (foldComment && atEOL && IsCommentLine(lineCurrent, styler))
  592. {
  593. if (!IsCommentLine(lineCurrent - 1, styler)
  594. && IsCommentLine(lineCurrent + 1, styler))
  595. levelCurrent++;
  596. else if (IsCommentLine(lineCurrent - 1, styler)
  597. && !IsCommentLine(lineCurrent+1, styler))
  598. levelCurrent--;
  599. }
  600. if (style == SCE_SH_OPERATOR) {
  601. if (ch == '{') {
  602. levelCurrent++;
  603. } else if (ch == '}') {
  604. levelCurrent--;
  605. }
  606. }
  607. if (atEOL) {
  608. int lev = levelPrev;
  609. if (visibleChars == 0 && foldCompact)
  610. lev |= SC_FOLDLEVELWHITEFLAG;
  611. if ((levelCurrent > levelPrev) && (visibleChars > 0))
  612. lev |= SC_FOLDLEVELHEADERFLAG;
  613. if (lev != styler.LevelAt(lineCurrent)) {
  614. styler.SetLevel(lineCurrent, lev);
  615. }
  616. lineCurrent++;
  617. levelPrev = levelCurrent;
  618. visibleChars = 0;
  619. }
  620. if (!isspacechar(ch))
  621. visibleChars++;
  622. }
  623. // Fill in the real level of the next line, keeping the current flags as they will be filled in later
  624. int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
  625. styler.SetLevel(lineCurrent, levelPrev | flagsNext);
  626. }
  627. static const char * const bashWordListDesc[] = {
  628. "Keywords",
  629. 0
  630. };
  631. LexerModule lmBash(SCLEX_BASH, ColouriseBashDoc, "bash", FoldBashDoc, bashWordListDesc);