PageRenderTime 54ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/wx.mod/wxscintilla.mod/src/scintilla/src/LexPerl.cxx

http://wxmax.googlecode.com/
C++ | 1260 lines | 1155 code | 37 blank | 68 comment | 394 complexity | 7aec2130a16b1419cc42b3471c28500d MD5 | raw file
Possible License(s): Unlicense, GPL-2.0
  1. // Scintilla source code edit control
  2. /** @file LexPerl.cxx
  3. ** Lexer for subset of Perl.
  4. **/
  5. // Copyright 1998-2005 by Neil Hodgson <neilh@scintilla.org>
  6. // Lexical analysis fixes by Kein-Hong Man <mkh@pl.jaring.my>
  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. #ifdef SCI_NAMESPACE
  20. using namespace Scintilla;
  21. #endif
  22. #define PERLNUM_BINARY 1 // order is significant: 1-4 cannot have a dot
  23. #define PERLNUM_HEX 2
  24. #define PERLNUM_OCTAL 3
  25. #define PERLNUM_FLOAT 4 // actually exponent part
  26. #define PERLNUM_DECIMAL 5 // 1-5 are numbers; 6-7 are strings
  27. #define PERLNUM_VECTOR 6
  28. #define PERLNUM_V_VECTOR 7
  29. #define PERLNUM_BAD 8
  30. #define BACK_NONE 0 // lookback state for bareword disambiguation:
  31. #define BACK_OPERATOR 1 // whitespace/comments are insignificant
  32. #define BACK_KEYWORD 2 // operators/keywords are needed for disambiguation
  33. #define HERE_DELIM_MAX 256
  34. static inline bool isEOLChar(char ch) {
  35. return (ch == '\r') || (ch == '\n');
  36. }
  37. static bool isSingleCharOp(char ch) {
  38. char strCharSet[2];
  39. strCharSet[0] = ch;
  40. strCharSet[1] = '\0';
  41. return (NULL != strstr("rwxoRWXOezsfdlpSbctugkTBMAC", strCharSet));
  42. }
  43. static inline bool isPerlOperator(char ch) {
  44. if (ch == '^' || ch == '&' || ch == '\\' ||
  45. ch == '(' || ch == ')' || ch == '-' || ch == '+' ||
  46. ch == '=' || ch == '|' || ch == '{' || ch == '}' ||
  47. ch == '[' || ch == ']' || ch == ':' || ch == ';' ||
  48. ch == '>' || ch == ',' ||
  49. ch == '?' || ch == '!' || ch == '.' || ch == '~')
  50. return true;
  51. // these chars are already tested before this call
  52. // ch == '%' || ch == '*' || ch == '<' || ch == '/' ||
  53. return false;
  54. }
  55. static bool isPerlKeyword(unsigned int start, unsigned int end, WordList &keywords, Accessor &styler) {
  56. char s[100];
  57. unsigned int i, len = end - start;
  58. if (len > 30) { len = 30; }
  59. for (i = 0; i < len; i++, start++) s[i] = styler[start];
  60. s[i] = '\0';
  61. return keywords.InList(s);
  62. }
  63. // Note: as lexer uses chars, UTF-8 bytes are considered as <0 values
  64. // Note: iswordchar() was used in only one place in LexPerl, it is
  65. // unnecessary as '.' is processed as the concatenation operator, so
  66. // only isWordStart() is used in LexPerl
  67. static inline bool isWordStart(char ch) {
  68. return !isascii(ch) || isalnum(ch) || ch == '_';
  69. }
  70. static inline bool isEndVar(char ch) {
  71. return isascii(ch) && !isalnum(ch) && ch != '#' && ch != '$' &&
  72. ch != '_' && ch != '\'';
  73. }
  74. static inline bool isNonQuote(char ch) {
  75. return !isascii(ch) || isalnum(ch) || ch == '_';
  76. }
  77. static inline char actualNumStyle(int numberStyle) {
  78. if (numberStyle == PERLNUM_VECTOR || numberStyle == PERLNUM_V_VECTOR) {
  79. return SCE_PL_STRING;
  80. } else if (numberStyle == PERLNUM_BAD) {
  81. return SCE_PL_ERROR;
  82. }
  83. return SCE_PL_NUMBER;
  84. }
  85. static bool isMatch(Accessor &styler, int lengthDoc, int pos, const char *val) {
  86. if ((pos + static_cast<int>(strlen(val))) >= lengthDoc) {
  87. return false;
  88. }
  89. while (*val) {
  90. if (*val != styler[pos++]) {
  91. return false;
  92. }
  93. val++;
  94. }
  95. return true;
  96. }
  97. static char opposite(char ch) {
  98. if (ch == '(')
  99. return ')';
  100. if (ch == '[')
  101. return ']';
  102. if (ch == '{')
  103. return '}';
  104. if (ch == '<')
  105. return '>';
  106. return ch;
  107. }
  108. static void ColourisePerlDoc(unsigned int startPos, int length, int initStyle,
  109. WordList *keywordlists[], Accessor &styler) {
  110. // Lexer for perl often has to backtrack to start of current style to determine
  111. // which characters are being used as quotes, how deeply nested is the
  112. // start position and what the termination string is for here documents
  113. WordList &keywords = *keywordlists[0];
  114. class HereDocCls {
  115. public:
  116. int State; // 0: '<<' encountered
  117. // 1: collect the delimiter
  118. // 2: here doc text (lines after the delimiter)
  119. char Quote; // the char after '<<'
  120. bool Quoted; // true if Quote in ('\'','"','`')
  121. int DelimiterLength; // strlen(Delimiter)
  122. char *Delimiter; // the Delimiter, 256: sizeof PL_tokenbuf
  123. HereDocCls() {
  124. State = 0;
  125. Quote = 0;
  126. Quoted = false;
  127. DelimiterLength = 0;
  128. Delimiter = new char[HERE_DELIM_MAX];
  129. Delimiter[0] = '\0';
  130. }
  131. ~HereDocCls() {
  132. delete []Delimiter;
  133. }
  134. };
  135. HereDocCls HereDoc; // TODO: FIFO for stacked here-docs
  136. class QuoteCls {
  137. public:
  138. int Rep;
  139. int Count;
  140. char Up;
  141. char Down;
  142. QuoteCls() {
  143. this->New(1);
  144. }
  145. void New(int r) {
  146. Rep = r;
  147. Count = 0;
  148. Up = '\0';
  149. Down = '\0';
  150. }
  151. void Open(char u) {
  152. Count++;
  153. Up = u;
  154. Down = opposite(Up);
  155. }
  156. };
  157. QuoteCls Quote;
  158. int state = initStyle;
  159. char numState = PERLNUM_DECIMAL;
  160. int dotCount = 0;
  161. unsigned int lengthDoc = startPos + length;
  162. //int sookedpos = 0; // these have no apparent use, see POD state
  163. //char sooked[100];
  164. //sooked[sookedpos] = '\0';
  165. // If in a long distance lexical state, seek to the beginning to find quote characters
  166. // Perl strings can be multi-line with embedded newlines, so backtrack.
  167. // Perl numbers have additional state during lexing, so backtrack too.
  168. if (state == SCE_PL_HERE_Q || state == SCE_PL_HERE_QQ || state == SCE_PL_HERE_QX) {
  169. while ((startPos > 1) && (styler.StyleAt(startPos) != SCE_PL_HERE_DELIM)) {
  170. startPos--;
  171. }
  172. startPos = styler.LineStart(styler.GetLine(startPos));
  173. state = styler.StyleAt(startPos - 1);
  174. }
  175. if ( state == SCE_PL_STRING_Q
  176. || state == SCE_PL_STRING_QQ
  177. || state == SCE_PL_STRING_QX
  178. || state == SCE_PL_STRING_QR
  179. || state == SCE_PL_STRING_QW
  180. || state == SCE_PL_REGEX
  181. || state == SCE_PL_REGSUBST
  182. || state == SCE_PL_STRING
  183. || state == SCE_PL_BACKTICKS
  184. || state == SCE_PL_CHARACTER
  185. || state == SCE_PL_NUMBER
  186. || state == SCE_PL_IDENTIFIER
  187. || state == SCE_PL_ERROR
  188. ) {
  189. while ((startPos > 1) && (styler.StyleAt(startPos - 1) == state)) {
  190. startPos--;
  191. }
  192. state = SCE_PL_DEFAULT;
  193. }
  194. // lookback at start of lexing to set proper state for backflag
  195. // after this, they are updated when elements are lexed
  196. int backflag = BACK_NONE;
  197. unsigned int backPos = startPos;
  198. if (backPos > 0) {
  199. backPos--;
  200. int sty = SCE_PL_DEFAULT;
  201. while ((backPos > 0) && (sty = styler.StyleAt(backPos),
  202. sty == SCE_PL_DEFAULT || sty == SCE_PL_COMMENTLINE))
  203. backPos--;
  204. if (sty == SCE_PL_OPERATOR)
  205. backflag = BACK_OPERATOR;
  206. else if (sty == SCE_PL_WORD)
  207. backflag = BACK_KEYWORD;
  208. }
  209. styler.StartAt(startPos);
  210. char chPrev = styler.SafeGetCharAt(startPos - 1);
  211. if (startPos == 0)
  212. chPrev = '\n';
  213. char chNext = styler[startPos];
  214. styler.StartSegment(startPos);
  215. for (unsigned int i = startPos; i < lengthDoc; i++) {
  216. char ch = chNext;
  217. // if the current character is not consumed due to the completion of an
  218. // earlier style, lexing can be restarted via a simple goto
  219. restartLexer:
  220. chNext = styler.SafeGetCharAt(i + 1);
  221. char chNext2 = styler.SafeGetCharAt(i + 2);
  222. if (styler.IsLeadByte(ch)) {
  223. chNext = styler.SafeGetCharAt(i + 2);
  224. chPrev = ' ';
  225. i += 1;
  226. continue;
  227. }
  228. if ((chPrev == '\r' && ch == '\n')) { // skip on DOS/Windows
  229. styler.ColourTo(i, state);
  230. chPrev = ch;
  231. continue;
  232. }
  233. if (HereDoc.State == 1 && isEOLChar(ch)) {
  234. // Begin of here-doc (the line after the here-doc delimiter):
  235. // Lexically, the here-doc starts from the next line after the >>, but the
  236. // first line of here-doc seem to follow the style of the last EOL sequence
  237. HereDoc.State = 2;
  238. if (HereDoc.Quoted) {
  239. if (state == SCE_PL_HERE_DELIM) {
  240. // Missing quote at end of string! We are stricter than perl.
  241. // Colour here-doc anyway while marking this bit as an error.
  242. state = SCE_PL_ERROR;
  243. }
  244. styler.ColourTo(i - 1, state);
  245. switch (HereDoc.Quote) {
  246. case '\'':
  247. state = SCE_PL_HERE_Q ;
  248. break;
  249. case '"':
  250. state = SCE_PL_HERE_QQ;
  251. break;
  252. case '`':
  253. state = SCE_PL_HERE_QX;
  254. break;
  255. }
  256. } else {
  257. styler.ColourTo(i - 1, state);
  258. switch (HereDoc.Quote) {
  259. case '\\':
  260. state = SCE_PL_HERE_Q ;
  261. break;
  262. default :
  263. state = SCE_PL_HERE_QQ;
  264. }
  265. }
  266. }
  267. if (state == SCE_PL_DEFAULT) {
  268. if ((isascii(ch) && isdigit(ch)) || (isascii(chNext) && isdigit(chNext) &&
  269. (ch == '.' || ch == 'v'))) {
  270. state = SCE_PL_NUMBER;
  271. backflag = BACK_NONE;
  272. numState = PERLNUM_DECIMAL;
  273. dotCount = 0;
  274. if (ch == '0') { // hex,bin,octal
  275. if (chNext == 'x') {
  276. numState = PERLNUM_HEX;
  277. } else if (chNext == 'b') {
  278. numState = PERLNUM_BINARY;
  279. } else if (isascii(chNext) && isdigit(chNext)) {
  280. numState = PERLNUM_OCTAL;
  281. }
  282. if (numState != PERLNUM_DECIMAL) {
  283. i++;
  284. ch = chNext;
  285. chNext = chNext2;
  286. }
  287. } else if (ch == 'v') { // vector
  288. numState = PERLNUM_V_VECTOR;
  289. }
  290. } else if (isWordStart(ch)) {
  291. // if immediately prefixed by '::', always a bareword
  292. state = SCE_PL_WORD;
  293. if (chPrev == ':' && styler.SafeGetCharAt(i - 2) == ':') {
  294. state = SCE_PL_IDENTIFIER;
  295. }
  296. unsigned int kw = i + 1;
  297. // first check for possible quote-like delimiter
  298. if (ch == 's' && !isNonQuote(chNext)) {
  299. state = SCE_PL_REGSUBST;
  300. Quote.New(2);
  301. } else if (ch == 'm' && !isNonQuote(chNext)) {
  302. state = SCE_PL_REGEX;
  303. Quote.New(1);
  304. } else if (ch == 'q' && !isNonQuote(chNext)) {
  305. state = SCE_PL_STRING_Q;
  306. Quote.New(1);
  307. } else if (ch == 'y' && !isNonQuote(chNext)) {
  308. state = SCE_PL_REGSUBST;
  309. Quote.New(2);
  310. } else if (ch == 't' && chNext == 'r' && !isNonQuote(chNext2)) {
  311. state = SCE_PL_REGSUBST;
  312. Quote.New(2);
  313. kw++;
  314. } else if (ch == 'q' && (chNext == 'q' || chNext == 'r' || chNext == 'w' || chNext == 'x') && !isNonQuote(chNext2)) {
  315. if (chNext == 'q') state = SCE_PL_STRING_QQ;
  316. else if (chNext == 'x') state = SCE_PL_STRING_QX;
  317. else if (chNext == 'r') state = SCE_PL_STRING_QR;
  318. else if (chNext == 'w') state = SCE_PL_STRING_QW;
  319. Quote.New(1);
  320. kw++;
  321. } else if (ch == 'x' && (chNext == '=' || // repetition
  322. !isWordStart(chNext) ||
  323. (isdigit(chPrev) && isdigit(chNext)))) {
  324. state = SCE_PL_OPERATOR;
  325. }
  326. // if potentially a keyword, scan forward and grab word, then check
  327. // if it's really one; if yes, disambiguation test is performed
  328. // otherwise it is always a bareword and we skip a lot of scanning
  329. // note: keywords assumed to be limited to [_a-zA-Z] only
  330. if (state == SCE_PL_WORD) {
  331. while (isWordStart(styler.SafeGetCharAt(kw))) kw++;
  332. if (!isPerlKeyword(styler.GetStartSegment(), kw, keywords, styler)) {
  333. state = SCE_PL_IDENTIFIER;
  334. }
  335. }
  336. // if already SCE_PL_IDENTIFIER, then no ambiguity, skip this
  337. // for quote-like delimiters/keywords, attempt to disambiguate
  338. // to select for bareword, change state -> SCE_PL_IDENTIFIER
  339. if (state != SCE_PL_IDENTIFIER && i > 0) {
  340. unsigned int j = i;
  341. bool moreback = false; // true if passed newline/comments
  342. bool brace = false; // true if opening brace found
  343. char ch2;
  344. // first look backwards past whitespace/comments for EOLs
  345. // if BACK_NONE, neither operator nor keyword, so skip test
  346. if (backflag != BACK_NONE) {
  347. while (--j > backPos) {
  348. if (isEOLChar(styler.SafeGetCharAt(j)))
  349. moreback = true;
  350. }
  351. ch2 = styler.SafeGetCharAt(j);
  352. if (ch2 == '{' && !moreback) {
  353. // {bareword: possible variable spec
  354. brace = true;
  355. } else if ((ch2 == '&' && styler.SafeGetCharAt(j - 1) != '&')
  356. // &bareword: subroutine call
  357. || (ch2 == '>' && styler.SafeGetCharAt(j - 1) == '-')
  358. // ->bareword: part of variable spec
  359. || (ch2 == 'b' && styler.Match(j - 2, "su"))) {
  360. // sub bareword: subroutine declaration
  361. // (implied BACK_KEYWORD, no keywords end in 'sub'!)
  362. state = SCE_PL_IDENTIFIER;
  363. }
  364. // if status still ambiguous, look forward after word past
  365. // tabs/spaces only; if ch2 isn't one of '[{(,' it can never
  366. // match anything, so skip the whole thing
  367. j = kw;
  368. if (state != SCE_PL_IDENTIFIER
  369. && (ch2 == '{' || ch2 == '(' || ch2 == '['|| ch2 == ',')
  370. && kw < lengthDoc) {
  371. while (ch2 = styler.SafeGetCharAt(j),
  372. (ch2 == ' ' || ch2 == '\t') && j < lengthDoc) {
  373. j++;
  374. }
  375. if ((ch2 == '}' && brace)
  376. // {bareword}: variable spec
  377. || (ch2 == '=' && styler.SafeGetCharAt(j + 1) == '>')) {
  378. // [{(, bareword=>: hash literal
  379. state = SCE_PL_IDENTIFIER;
  380. }
  381. }
  382. }
  383. }
  384. backflag = BACK_NONE;
  385. // an identifier or bareword
  386. if (state == SCE_PL_IDENTIFIER) {
  387. if ((!isWordStart(chNext) && chNext != '\'')
  388. || (chNext == '.' && chNext2 == '.')) {
  389. // We need that if length of word == 1!
  390. // This test is copied from the SCE_PL_WORD handler.
  391. styler.ColourTo(i, SCE_PL_IDENTIFIER);
  392. state = SCE_PL_DEFAULT;
  393. }
  394. // a keyword
  395. } else if (state == SCE_PL_WORD) {
  396. i = kw - 1;
  397. if (ch == '_' && chNext == '_' &&
  398. (isMatch(styler, lengthDoc, styler.GetStartSegment(), "__DATA__")
  399. || isMatch(styler, lengthDoc, styler.GetStartSegment(), "__END__"))) {
  400. styler.ColourTo(i, SCE_PL_DATASECTION);
  401. state = SCE_PL_DATASECTION;
  402. } else {
  403. styler.ColourTo(i, SCE_PL_WORD);
  404. state = SCE_PL_DEFAULT;
  405. backflag = BACK_KEYWORD;
  406. backPos = i;
  407. }
  408. ch = styler.SafeGetCharAt(i);
  409. chNext = styler.SafeGetCharAt(i + 1);
  410. // a repetition operator 'x'
  411. } else if (state == SCE_PL_OPERATOR) {
  412. styler.ColourTo(i, SCE_PL_OPERATOR);
  413. state = SCE_PL_DEFAULT;
  414. // quote-like delimiter, skip one char if double-char delimiter
  415. } else {
  416. i = kw - 1;
  417. chNext = styler.SafeGetCharAt(i + 1);
  418. }
  419. } else if (ch == '#') {
  420. state = SCE_PL_COMMENTLINE;
  421. } else if (ch == '\"') {
  422. state = SCE_PL_STRING;
  423. Quote.New(1);
  424. Quote.Open(ch);
  425. backflag = BACK_NONE;
  426. } else if (ch == '\'') {
  427. if (chPrev == '&') {
  428. // Archaic call
  429. styler.ColourTo(i, state);
  430. } else {
  431. state = SCE_PL_CHARACTER;
  432. Quote.New(1);
  433. Quote.Open(ch);
  434. }
  435. backflag = BACK_NONE;
  436. } else if (ch == '`') {
  437. state = SCE_PL_BACKTICKS;
  438. Quote.New(1);
  439. Quote.Open(ch);
  440. backflag = BACK_NONE;
  441. } else if (ch == '$') {
  442. if ((chNext == '{') || isspacechar(chNext)) {
  443. styler.ColourTo(i, SCE_PL_SCALAR);
  444. } else {
  445. state = SCE_PL_SCALAR;
  446. if ((chNext == '`' && chNext2 == '`')
  447. || (chNext == ':' && chNext2 == ':')) {
  448. i += 2;
  449. ch = styler.SafeGetCharAt(i);
  450. chNext = styler.SafeGetCharAt(i + 1);
  451. } else {
  452. i++;
  453. ch = chNext;
  454. chNext = chNext2;
  455. }
  456. }
  457. backflag = BACK_NONE;
  458. } else if (ch == '@') {
  459. if (!isascii(chNext) || isalpha(chNext) || chNext == '#' || chNext == '$'
  460. || chNext == '_' || chNext == '+' || chNext == '-') {
  461. state = SCE_PL_ARRAY;
  462. } else if (chNext == ':' && chNext2 == ':') {
  463. state = SCE_PL_ARRAY;
  464. i += 2;
  465. ch = styler.SafeGetCharAt(i);
  466. chNext = styler.SafeGetCharAt(i + 1);
  467. } else if (chNext != '{' && chNext != '[') {
  468. styler.ColourTo(i, SCE_PL_ARRAY);
  469. } else {
  470. styler.ColourTo(i, SCE_PL_ARRAY);
  471. }
  472. backflag = BACK_NONE;
  473. } else if (ch == '%') {
  474. if (!isascii(chNext) || isalpha(chNext) || chNext == '#' || chNext == '$'
  475. || chNext == '_' || chNext == '!' || chNext == '^') {
  476. state = SCE_PL_HASH;
  477. i++;
  478. ch = chNext;
  479. chNext = chNext2;
  480. } else if (chNext == ':' && chNext2 == ':') {
  481. state = SCE_PL_HASH;
  482. i += 2;
  483. ch = styler.SafeGetCharAt(i);
  484. chNext = styler.SafeGetCharAt(i + 1);
  485. } else if (chNext == '{') {
  486. styler.ColourTo(i, SCE_PL_HASH);
  487. } else {
  488. styler.ColourTo(i, SCE_PL_OPERATOR);
  489. }
  490. backflag = BACK_NONE;
  491. } else if (ch == '*') {
  492. char strch[2];
  493. strch[0] = chNext;
  494. strch[1] = '\0';
  495. if (chNext == ':' && chNext2 == ':') {
  496. state = SCE_PL_SYMBOLTABLE;
  497. i += 2;
  498. ch = styler.SafeGetCharAt(i);
  499. chNext = styler.SafeGetCharAt(i + 1);
  500. } else if (!isascii(chNext) || isalpha(chNext) || chNext == '_'
  501. || NULL != strstr("^/|,\\\";#%^:?<>)[]", strch)) {
  502. state = SCE_PL_SYMBOLTABLE;
  503. i++;
  504. ch = chNext;
  505. chNext = chNext2;
  506. } else if (chNext == '{') {
  507. styler.ColourTo(i, SCE_PL_SYMBOLTABLE);
  508. } else {
  509. if (chNext == '*') { // exponentiation
  510. i++;
  511. ch = chNext;
  512. chNext = chNext2;
  513. }
  514. styler.ColourTo(i, SCE_PL_OPERATOR);
  515. }
  516. backflag = BACK_NONE;
  517. } else if (ch == '/' || (ch == '<' && chNext == '<')) {
  518. // Explicit backward peeking to set a consistent preferRE for
  519. // any slash found, so no longer need to track preferRE state.
  520. // Find first previous significant lexed element and interpret.
  521. // Test for HERE doc start '<<' shares this code, helps to
  522. // determine if it should be an operator.
  523. bool preferRE = false;
  524. bool isHereDoc = (ch == '<');
  525. bool hereDocSpace = false; // these are for corner case:
  526. bool hereDocScalar = false; // SCALAR [whitespace] '<<'
  527. unsigned int bk = (i > 0)? i - 1: 0;
  528. char bkch;
  529. styler.Flush();
  530. if (styler.StyleAt(bk) == SCE_PL_DEFAULT)
  531. hereDocSpace = true;
  532. while ((bk > 0) && (styler.StyleAt(bk) == SCE_PL_DEFAULT ||
  533. styler.StyleAt(bk) == SCE_PL_COMMENTLINE)) {
  534. bk--;
  535. }
  536. if (bk == 0) {
  537. // position 0 won't really be checked; rarely happens
  538. // hard to fix due to an unsigned index i
  539. preferRE = true;
  540. } else {
  541. int bkstyle = styler.StyleAt(bk);
  542. bkch = styler.SafeGetCharAt(bk);
  543. switch(bkstyle) {
  544. case SCE_PL_OPERATOR:
  545. preferRE = true;
  546. if (bkch == ')' || bkch == ']') {
  547. preferRE = false;
  548. } else if (bkch == '}') {
  549. // backtrack further, count balanced brace pairs
  550. // if a brace pair found, see if it's a variable
  551. int braceCount = 1;
  552. while (--bk > 0) {
  553. bkstyle = styler.StyleAt(bk);
  554. if (bkstyle == SCE_PL_OPERATOR) {
  555. bkch = styler.SafeGetCharAt(bk);
  556. if (bkch == ';') { // early out
  557. break;
  558. } else if (bkch == '}') {
  559. braceCount++;
  560. } else if (bkch == '{') {
  561. if (--braceCount == 0)
  562. break;
  563. }
  564. }
  565. }
  566. if (bk == 0) {
  567. // at beginning, true
  568. } else if (braceCount == 0) {
  569. // balanced { found, bk>0, skip more whitespace
  570. if (styler.StyleAt(--bk) == SCE_PL_DEFAULT) {
  571. while (bk > 0) {
  572. bkstyle = styler.StyleAt(--bk);
  573. if (bkstyle != SCE_PL_DEFAULT)
  574. break;
  575. }
  576. }
  577. bkstyle = styler.StyleAt(bk);
  578. if (bkstyle == SCE_PL_SCALAR
  579. || bkstyle == SCE_PL_ARRAY
  580. || bkstyle == SCE_PL_HASH
  581. || bkstyle == SCE_PL_SYMBOLTABLE
  582. || bkstyle == SCE_PL_OPERATOR) {
  583. preferRE = false;
  584. }
  585. }
  586. }
  587. break;
  588. case SCE_PL_IDENTIFIER:
  589. preferRE = true;
  590. if (bkch == '>') { // inputsymbol
  591. preferRE = false;
  592. break;
  593. }
  594. // backtrack to find "->" or "::" before identifier
  595. while (bk > 0 && styler.StyleAt(bk) == SCE_PL_IDENTIFIER) {
  596. bk--;
  597. }
  598. while (bk > 0) {
  599. bkstyle = styler.StyleAt(bk);
  600. if (bkstyle == SCE_PL_DEFAULT ||
  601. bkstyle == SCE_PL_COMMENTLINE) {
  602. } else if (bkstyle == SCE_PL_OPERATOR) {
  603. // gcc 3.2.3 bloats if more compact form used
  604. bkch = styler.SafeGetCharAt(bk);
  605. if (bkch == '>') { // "->"
  606. if (styler.SafeGetCharAt(bk - 1) == '-') {
  607. preferRE = false;
  608. break;
  609. }
  610. } else if (bkch == ':') { // "::"
  611. if (styler.SafeGetCharAt(bk - 1) == ':') {
  612. preferRE = false;
  613. break;
  614. }
  615. }
  616. } else {// bare identifier, usually a function call but Perl
  617. // optimizes them as pseudo-constants, then the next
  618. // '/' will be a divide; favour divide over regex
  619. // if there is a whitespace after the '/'
  620. if (isspacechar(chNext)) {
  621. preferRE = false;
  622. }
  623. break;
  624. }
  625. bk--;
  626. }
  627. break;
  628. case SCE_PL_SCALAR: // for $var<< case
  629. hereDocScalar = true;
  630. break;
  631. // other styles uses the default, preferRE=false
  632. case SCE_PL_WORD:
  633. case SCE_PL_POD:
  634. case SCE_PL_POD_VERB:
  635. case SCE_PL_HERE_Q:
  636. case SCE_PL_HERE_QQ:
  637. case SCE_PL_HERE_QX:
  638. preferRE = true;
  639. break;
  640. }
  641. }
  642. if (isHereDoc) { // handle HERE doc
  643. // if SCALAR whitespace '<<', *always* a HERE doc
  644. if (preferRE || (hereDocSpace && hereDocScalar)) {
  645. state = SCE_PL_HERE_DELIM;
  646. HereDoc.State = 0;
  647. } else { // << operator
  648. i++;
  649. ch = chNext;
  650. chNext = chNext2;
  651. styler.ColourTo(i, SCE_PL_OPERATOR);
  652. }
  653. } else { // handle regexp
  654. if (preferRE) {
  655. state = SCE_PL_REGEX;
  656. Quote.New(1);
  657. Quote.Open(ch);
  658. } else { // / operator
  659. styler.ColourTo(i, SCE_PL_OPERATOR);
  660. }
  661. }
  662. backflag = BACK_NONE;
  663. } else if (ch == '<') {
  664. // looks forward for matching > on same line
  665. unsigned int fw = i + 1;
  666. while (fw < lengthDoc) {
  667. char fwch = styler.SafeGetCharAt(fw);
  668. if (fwch == ' ') {
  669. if (styler.SafeGetCharAt(fw-1) != '\\' ||
  670. styler.SafeGetCharAt(fw-2) != '\\')
  671. break;
  672. } else if (isEOLChar(fwch) || isspacechar(fwch)) {
  673. break;
  674. } else if (fwch == '>') {
  675. if ((fw - i) == 2 && // '<=>' case
  676. styler.SafeGetCharAt(fw-1) == '=') {
  677. styler.ColourTo(fw, SCE_PL_OPERATOR);
  678. } else {
  679. styler.ColourTo(fw, SCE_PL_IDENTIFIER);
  680. }
  681. i = fw;
  682. ch = fwch;
  683. chNext = styler.SafeGetCharAt(i+1);
  684. }
  685. fw++;
  686. }
  687. styler.ColourTo(i, SCE_PL_OPERATOR);
  688. backflag = BACK_NONE;
  689. } else if (ch == '=' // POD
  690. && isalpha(chNext)
  691. && (isEOLChar(chPrev))) {
  692. state = SCE_PL_POD;
  693. backflag = BACK_NONE;
  694. //sookedpos = 0;
  695. //sooked[sookedpos] = '\0';
  696. } else if (ch == '-' // file test operators
  697. && isSingleCharOp(chNext)
  698. && !isalnum((chNext2 = styler.SafeGetCharAt(i+2)))) {
  699. styler.ColourTo(i + 1, SCE_PL_WORD);
  700. state = SCE_PL_DEFAULT;
  701. i++;
  702. ch = chNext;
  703. chNext = chNext2;
  704. backflag = BACK_NONE;
  705. } else if (isPerlOperator(ch)) {
  706. if (ch == '.' && chNext == '.') { // .. and ...
  707. i++;
  708. if (chNext2 == '.') { i++; }
  709. state = SCE_PL_DEFAULT;
  710. ch = styler.SafeGetCharAt(i);
  711. chNext = styler.SafeGetCharAt(i + 1);
  712. }
  713. styler.ColourTo(i, SCE_PL_OPERATOR);
  714. backflag = BACK_OPERATOR;
  715. backPos = i;
  716. } else {
  717. // keep colouring defaults to make restart easier
  718. styler.ColourTo(i, SCE_PL_DEFAULT);
  719. }
  720. } else if (state == SCE_PL_NUMBER) {
  721. if (ch == '.') {
  722. if (chNext == '.') {
  723. // double dot is always an operator
  724. goto numAtEnd;
  725. } else if (numState <= PERLNUM_FLOAT) {
  726. // non-decimal number or float exponent, consume next dot
  727. styler.ColourTo(i - 1, SCE_PL_NUMBER);
  728. styler.ColourTo(i, SCE_PL_OPERATOR);
  729. state = SCE_PL_DEFAULT;
  730. } else { // decimal or vectors allows dots
  731. dotCount++;
  732. if (numState == PERLNUM_DECIMAL) {
  733. if (dotCount > 1) {
  734. if (isdigit(chNext)) { // really a vector
  735. numState = PERLNUM_VECTOR;
  736. } else // number then dot
  737. goto numAtEnd;
  738. }
  739. } else { // vectors
  740. if (!isdigit(chNext)) // vector then dot
  741. goto numAtEnd;
  742. }
  743. }
  744. } else if (ch == '_' && numState == PERLNUM_DECIMAL) {
  745. if (!isdigit(chNext)) {
  746. goto numAtEnd;
  747. }
  748. } else if (!isascii(ch) || isalnum(ch)) {
  749. if (numState == PERLNUM_VECTOR || numState == PERLNUM_V_VECTOR) {
  750. if (!isascii(ch) || isalpha(ch)) {
  751. if (dotCount == 0) { // change to word
  752. state = SCE_PL_IDENTIFIER;
  753. } else { // vector then word
  754. goto numAtEnd;
  755. }
  756. }
  757. } else if (numState == PERLNUM_DECIMAL) {
  758. if (ch == 'E' || ch == 'e') { // exponent
  759. numState = PERLNUM_FLOAT;
  760. if (chNext == '+' || chNext == '-') {
  761. i++;
  762. ch = chNext;
  763. chNext = chNext2;
  764. }
  765. } else if (!isascii(ch) || !isdigit(ch)) { // number then word
  766. goto numAtEnd;
  767. }
  768. } else if (numState == PERLNUM_FLOAT) {
  769. if (!isdigit(ch)) { // float then word
  770. goto numAtEnd;
  771. }
  772. } else if (numState == PERLNUM_OCTAL) {
  773. if (!isdigit(ch))
  774. goto numAtEnd;
  775. else if (ch > '7')
  776. numState = PERLNUM_BAD;
  777. } else if (numState == PERLNUM_BINARY) {
  778. if (!isdigit(ch))
  779. goto numAtEnd;
  780. else if (ch > '1')
  781. numState = PERLNUM_BAD;
  782. } else if (numState == PERLNUM_HEX) {
  783. int ch2 = toupper(ch);
  784. if (!isdigit(ch) && !(ch2 >= 'A' && ch2 <= 'F'))
  785. goto numAtEnd;
  786. } else {//(numState == PERLNUM_BAD) {
  787. if (!isdigit(ch))
  788. goto numAtEnd;
  789. }
  790. } else {
  791. // complete current number or vector
  792. numAtEnd:
  793. styler.ColourTo(i - 1, actualNumStyle(numState));
  794. state = SCE_PL_DEFAULT;
  795. goto restartLexer;
  796. }
  797. } else if (state == SCE_PL_IDENTIFIER) {
  798. if (!isWordStart(chNext) && chNext != '\'') {
  799. styler.ColourTo(i, SCE_PL_IDENTIFIER);
  800. state = SCE_PL_DEFAULT;
  801. ch = ' ';
  802. }
  803. } else {
  804. if (state == SCE_PL_COMMENTLINE) {
  805. if (isEOLChar(ch)) {
  806. styler.ColourTo(i - 1, state);
  807. state = SCE_PL_DEFAULT;
  808. goto restartLexer;
  809. } else if (isEOLChar(chNext)) {
  810. styler.ColourTo(i, state);
  811. state = SCE_PL_DEFAULT;
  812. }
  813. } else if (state == SCE_PL_HERE_DELIM) {
  814. //
  815. // From perldata.pod:
  816. // ------------------
  817. // A line-oriented form of quoting is based on the shell ``here-doc''
  818. // syntax.
  819. // Following a << you specify a string to terminate the quoted material,
  820. // and all lines following the current line down to the terminating
  821. // string are the value of the item.
  822. // The terminating string may be either an identifier (a word),
  823. // or some quoted text.
  824. // If quoted, the type of quotes you use determines the treatment of
  825. // the text, just as in regular quoting.
  826. // An unquoted identifier works like double quotes.
  827. // There must be no space between the << and the identifier.
  828. // (If you put a space it will be treated as a null identifier,
  829. // which is valid, and matches the first empty line.)
  830. // (This is deprecated, -w warns of this syntax)
  831. // The terminating string must appear by itself (unquoted and with no
  832. // surrounding whitespace) on the terminating line.
  833. //
  834. // From Bash info:
  835. // ---------------
  836. // Specifier format is: <<[-]WORD
  837. // Optional '-' is for removal of leading tabs from here-doc.
  838. // Whitespace acceptable after <<[-] operator.
  839. //
  840. if (HereDoc.State == 0) { // '<<' encountered
  841. bool gotspace = false;
  842. unsigned int oldi = i;
  843. if (chNext == ' ' || chNext == '\t') {
  844. // skip whitespace; legal for quoted delimiters
  845. gotspace = true;
  846. do {
  847. i++;
  848. chNext = styler.SafeGetCharAt(i + 1);
  849. } while ((i + 1 < lengthDoc) && (chNext == ' ' || chNext == '\t'));
  850. chNext2 = styler.SafeGetCharAt(i + 2);
  851. }
  852. HereDoc.State = 1;
  853. HereDoc.Quote = chNext;
  854. HereDoc.Quoted = false;
  855. HereDoc.DelimiterLength = 0;
  856. HereDoc.Delimiter[HereDoc.DelimiterLength] = '\0';
  857. if (chNext == '\'' || chNext == '"' || chNext == '`') {
  858. // a quoted here-doc delimiter
  859. i++;
  860. ch = chNext;
  861. chNext = chNext2;
  862. HereDoc.Quoted = true;
  863. } else if (isspacechar(chNext) || isdigit(chNext) || chNext == '\\'
  864. || chNext == '=' || chNext == '$' || chNext == '@'
  865. || ((isalpha(chNext) || chNext == '_') && gotspace)) {
  866. // left shift << or <<= operator cases
  867. // restore position if operator
  868. i = oldi;
  869. styler.ColourTo(i, SCE_PL_OPERATOR);
  870. state = SCE_PL_DEFAULT;
  871. HereDoc.State = 0;
  872. goto restartLexer;
  873. } else {
  874. // an unquoted here-doc delimiter, no special handling
  875. // (cannot be prefixed by spaces/tabs), or
  876. // symbols terminates; deprecated zero-length delimiter
  877. }
  878. } else if (HereDoc.State == 1) { // collect the delimiter
  879. backflag = BACK_NONE;
  880. if (HereDoc.Quoted) { // a quoted here-doc delimiter
  881. if (ch == HereDoc.Quote) { // closing quote => end of delimiter
  882. styler.ColourTo(i, state);
  883. state = SCE_PL_DEFAULT;
  884. } else {
  885. if (ch == '\\' && chNext == HereDoc.Quote) { // escaped quote
  886. i++;
  887. ch = chNext;
  888. chNext = chNext2;
  889. }
  890. HereDoc.Delimiter[HereDoc.DelimiterLength++] = ch;
  891. HereDoc.Delimiter[HereDoc.DelimiterLength] = '\0';
  892. }
  893. } else { // an unquoted here-doc delimiter
  894. if (isalnum(ch) || ch == '_') {
  895. HereDoc.Delimiter[HereDoc.DelimiterLength++] = ch;
  896. HereDoc.Delimiter[HereDoc.DelimiterLength] = '\0';
  897. } else {
  898. styler.ColourTo(i - 1, state);
  899. state = SCE_PL_DEFAULT;
  900. goto restartLexer;
  901. }
  902. }
  903. if (HereDoc.DelimiterLength >= HERE_DELIM_MAX - 1) {
  904. styler.ColourTo(i - 1, state);
  905. state = SCE_PL_ERROR;
  906. goto restartLexer;
  907. }
  908. }
  909. } else if (HereDoc.State == 2) {
  910. // state == SCE_PL_HERE_Q || state == SCE_PL_HERE_QQ || state == SCE_PL_HERE_QX
  911. if (isEOLChar(chPrev) && isMatch(styler, lengthDoc, i, HereDoc.Delimiter)) {
  912. i += HereDoc.DelimiterLength;
  913. chPrev = styler.SafeGetCharAt(i - 1);
  914. ch = styler.SafeGetCharAt(i);
  915. if (isEOLChar(ch)) {
  916. styler.ColourTo(i - 1, state);
  917. state = SCE_PL_DEFAULT;
  918. backflag = BACK_NONE;
  919. HereDoc.State = 0;
  920. goto restartLexer;
  921. }
  922. chNext = styler.SafeGetCharAt(i + 1);
  923. }
  924. } else if (state == SCE_PL_POD
  925. || state == SCE_PL_POD_VERB) {
  926. if (isEOLChar(chPrev)) {
  927. if (ch == ' ' || ch == '\t') {
  928. styler.ColourTo(i - 1, state);
  929. state = SCE_PL_POD_VERB;
  930. } else {
  931. styler.ColourTo(i - 1, state);
  932. state = SCE_PL_POD;
  933. if (ch == '=') {
  934. if (isMatch(styler, lengthDoc, i, "=cut")) {
  935. styler.ColourTo(i - 1 + 4, state);
  936. i += 4;
  937. state = SCE_PL_DEFAULT;
  938. ch = styler.SafeGetCharAt(i);
  939. //chNext = styler.SafeGetCharAt(i + 1);
  940. goto restartLexer;
  941. }
  942. }
  943. }
  944. }
  945. } else if (state == SCE_PL_SCALAR // variable names
  946. || state == SCE_PL_ARRAY
  947. || state == SCE_PL_HASH
  948. || state == SCE_PL_SYMBOLTABLE) {
  949. if (ch == ':' && chNext == ':') { // skip ::
  950. i++;
  951. ch = chNext;
  952. chNext = chNext2;
  953. }
  954. else if (isEndVar(ch)) {
  955. if (i == (styler.GetStartSegment() + 1)) {
  956. // Special variable: $(, $_ etc.
  957. styler.ColourTo(i, state);
  958. state = SCE_PL_DEFAULT;
  959. } else {
  960. styler.ColourTo(i - 1, state);
  961. state = SCE_PL_DEFAULT;
  962. goto restartLexer;
  963. }
  964. }
  965. } else if (state == SCE_PL_REGEX
  966. || state == SCE_PL_STRING_QR
  967. ) {
  968. if (!Quote.Up && !isspacechar(ch)) {
  969. Quote.Open(ch);
  970. } else if (ch == '\\' && Quote.Up != '\\') {
  971. // SG: Is it save to skip *every* escaped char?
  972. i++;
  973. ch = chNext;
  974. chNext = styler.SafeGetCharAt(i + 1);
  975. } else {
  976. if (ch == Quote.Down /*&& chPrev != '\\'*/) {
  977. Quote.Count--;
  978. if (Quote.Count == 0) {
  979. Quote.Rep--;
  980. if (Quote.Up == Quote.Down) {
  981. Quote.Count++;
  982. }
  983. }
  984. if (!isalpha(chNext)) {
  985. if (Quote.Rep <= 0) {
  986. styler.ColourTo(i, state);
  987. state = SCE_PL_DEFAULT;
  988. ch = ' ';
  989. }
  990. }
  991. } else if (ch == Quote.Up /*&& chPrev != '\\'*/) {
  992. Quote.Count++;
  993. } else if (!isascii(chNext) || !isalpha(chNext)) {
  994. if (Quote.Rep <= 0) {
  995. styler.ColourTo(i, state);
  996. state = SCE_PL_DEFAULT;
  997. ch = ' ';
  998. }
  999. }
  1000. }
  1001. } else if (state == SCE_PL_REGSUBST) {
  1002. if (!Quote.Up && !isspacechar(ch)) {
  1003. Quote.Open(ch);
  1004. } else if (ch == '\\' && Quote.Up != '\\') {
  1005. // SG: Is it save to skip *every* escaped char?
  1006. i++;
  1007. ch = chNext;
  1008. chNext = styler.SafeGetCharAt(i + 1);
  1009. } else {
  1010. if (Quote.Count == 0 && Quote.Rep == 1) {
  1011. /* We matched something like s(...) or tr{...}
  1012. * and are looking for the next matcher characters,
  1013. * which could be either bracketed ({...}) or non-bracketed
  1014. * (/.../).
  1015. *
  1016. * Number-signs are problematic. If they occur after
  1017. * the close of the first part, treat them like
  1018. * a Quote.Up char, even if they actually start comments.
  1019. *
  1020. * If we find an alnum, we end the regsubst, and punt.
  1021. *
  1022. * Eric Promislow ericp@activestate.com Aug 9,2000
  1023. */
  1024. if (isspacechar(ch)) {
  1025. // Keep going
  1026. }
  1027. else if (!isascii(ch) || isalnum(ch)) {
  1028. styler.ColourTo(i, state);
  1029. state = SCE_PL_DEFAULT;
  1030. ch = ' ';
  1031. } else {
  1032. Quote.Open(ch);
  1033. }
  1034. } else if (ch == Quote.Down /*&& chPrev != '\\'*/) {
  1035. Quote.Count--;
  1036. if (Quote.Count == 0) {
  1037. Quote.Rep--;
  1038. }
  1039. if (!isascii(chNext) || !isalpha(chNext)) {
  1040. if (Quote.Rep <= 0) {
  1041. styler.ColourTo(i, state);
  1042. state = SCE_PL_DEFAULT;
  1043. ch = ' ';
  1044. }
  1045. }
  1046. if (Quote.Up == Quote.Down) {
  1047. Quote.Count++;
  1048. }
  1049. } else if (ch == Quote.Up /*&& chPrev != '\\'*/) {
  1050. Quote.Count++;
  1051. } else if (!isascii(chNext) || !isalpha(chNext)) {
  1052. if (Quote.Rep <= 0) {
  1053. styler.ColourTo(i, state);
  1054. state = SCE_PL_DEFAULT;
  1055. ch = ' ';
  1056. }
  1057. }
  1058. }
  1059. } else if (state == SCE_PL_STRING_Q
  1060. || state == SCE_PL_STRING_QQ
  1061. || state == SCE_PL_STRING_QX
  1062. || state == SCE_PL_STRING_QW
  1063. || state == SCE_PL_STRING
  1064. || state == SCE_PL_CHARACTER
  1065. || state == SCE_PL_BACKTICKS
  1066. ) {
  1067. if (!Quote.Down && !isspacechar(ch)) {
  1068. Quote.Open(ch);
  1069. } else if (ch == '\\' && Quote.Up != '\\') {
  1070. i++;
  1071. ch = chNext;
  1072. chNext = styler.SafeGetCharAt(i + 1);
  1073. } else if (ch == Quote.Down) {
  1074. Quote.Count--;
  1075. if (Quote.Count == 0) {
  1076. Quote.Rep--;
  1077. if (Quote.Rep <= 0) {
  1078. styler.ColourTo(i, state);
  1079. state = SCE_PL_DEFAULT;
  1080. ch = ' ';
  1081. }
  1082. if (Quote.Up == Quote.Down) {
  1083. Quote.Count++;
  1084. }
  1085. }
  1086. } else if (ch == Quote.Up) {
  1087. Quote.Count++;
  1088. }
  1089. }
  1090. }
  1091. if (state == SCE_PL_ERROR) {
  1092. break;
  1093. }
  1094. chPrev = ch;
  1095. }
  1096. styler.ColourTo(lengthDoc - 1, state);
  1097. }
  1098. static bool IsCommentLine(int line, Accessor &styler) {
  1099. int pos = styler.LineStart(line);
  1100. int eol_pos = styler.LineStart(line + 1) - 1;
  1101. for (int i = pos; i < eol_pos; i++) {
  1102. char ch = styler[i];
  1103. int style = styler.StyleAt(i);
  1104. if (ch == '#' && style == SCE_PL_COMMENTLINE)
  1105. return true;
  1106. else if (ch != ' ' && ch != '\t')
  1107. return false;
  1108. }
  1109. return false;
  1110. }
  1111. static void FoldPerlDoc(unsigned int startPos, int length, int, WordList *[],
  1112. Accessor &styler) {
  1113. bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
  1114. bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
  1115. // Custom folding of POD and packages
  1116. bool foldPOD = styler.GetPropertyInt("fold.perl.pod", 1) != 0;
  1117. bool foldPackage = styler.GetPropertyInt("fold.perl.package", 1) != 0;
  1118. unsigned int endPos = startPos + length;
  1119. int visibleChars = 0;
  1120. int lineCurrent = styler.GetLine(startPos);
  1121. int levelPrev = SC_FOLDLEVELBASE;
  1122. if (lineCurrent > 0)
  1123. levelPrev = styler.LevelAt(lineCurrent - 1) >> 16;
  1124. int levelCurrent = levelPrev;
  1125. char chNext = styler[startPos];
  1126. char chPrev = styler.SafeGetCharAt(startPos - 1);
  1127. int styleNext = styler.StyleAt(startPos);
  1128. // Used at end of line to determine if the line was a package definition
  1129. bool isPackageLine = false;
  1130. bool isPodHeading = false;
  1131. for (unsigned int i = startPos; i < endPos; i++) {
  1132. char ch = chNext;
  1133. chNext = styler.SafeGetCharAt(i + 1);
  1134. int style = styleNext;
  1135. styleNext = styler.StyleAt(i + 1);
  1136. bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
  1137. bool atLineStart = isEOLChar(chPrev) || i == 0;
  1138. // Comment folding
  1139. if (foldComment && atEOL && IsCommentLine(lineCurrent, styler))
  1140. {
  1141. if (!IsCommentLine(lineCurrent - 1, styler)
  1142. && IsCommentLine(lineCurrent + 1, styler))
  1143. levelCurrent++;
  1144. else if (IsCommentLine(lineCurrent - 1, styler)
  1145. && !IsCommentLine(lineCurrent+1, styler))
  1146. levelCurrent--;
  1147. }
  1148. if (style == SCE_C_OPERATOR) {
  1149. if (ch == '{') {
  1150. levelCurrent++;
  1151. } else if (ch == '}') {
  1152. levelCurrent--;
  1153. }
  1154. }
  1155. // Custom POD folding
  1156. if (foldPOD && atLineStart) {
  1157. int stylePrevCh = (i) ? styler.StyleAt(i - 1):SCE_PL_DEFAULT;
  1158. if (style == SCE_PL_POD) {
  1159. if (stylePrevCh != SCE_PL_POD && stylePrevCh != SCE_PL_POD_VERB)
  1160. levelCurrent++;
  1161. else if (styler.Match(i, "=cut"))
  1162. levelCurrent--;
  1163. else if (styler.Match(i, "=head"))
  1164. isPodHeading = true;
  1165. } else if (style == SCE_PL_DATASECTION) {
  1166. if (ch == '=' && isalpha(chNext) && levelCurrent == SC_FOLDLEVELBASE)
  1167. levelCurrent++;
  1168. else if (styler.Match(i, "=cut") && levelCurrent > SC_FOLDLEVELBASE)
  1169. levelCurrent--;
  1170. else if (styler.Match(i, "=head"))
  1171. isPodHeading = true;
  1172. // if package used or unclosed brace, level > SC_FOLDLEVELBASE!
  1173. // reset needed as level test is vs. SC_FOLDLEVELBASE
  1174. else if (styler.Match(i, "__END__"))
  1175. levelCurrent = SC_FOLDLEVELBASE;
  1176. }
  1177. }
  1178. // Custom package folding
  1179. if (foldPackage && atLineStart) {
  1180. if (style == SCE_PL_WORD && styler.Match(i, "package")) {
  1181. isPackageLine = true;
  1182. }
  1183. }
  1184. if (atEOL) {
  1185. int lev = levelPrev;
  1186. if (isPodHeading) {
  1187. lev = levelPrev - 1;
  1188. lev |= SC_FOLDLEVELHEADERFLAG;
  1189. isPodHeading = false;
  1190. }
  1191. // Check if line was a package declaration
  1192. // because packages need "special" treatment
  1193. if (isPackageLine) {
  1194. lev = SC_FOLDLEVELBASE | SC_FOLDLEVELHEADERFLAG;
  1195. levelCurrent = SC_FOLDLEVELBASE + 1;
  1196. isPackageLine = false;
  1197. }
  1198. lev |= levelCurrent << 16;
  1199. if (visibleChars == 0 && foldCompact)
  1200. lev |= SC_FOLDLEVELWHITEFLAG;
  1201. if ((levelCurrent > levelPrev) && (visibleChars > 0))
  1202. lev |= SC_FOLDLEVELHEADERFLAG;
  1203. if (lev != styler.LevelAt(lineCurrent)) {
  1204. styler.SetLevel(lineCurrent, lev);
  1205. }
  1206. lineCurrent++;
  1207. levelPrev = levelCurrent;
  1208. visibleChars = 0;
  1209. }
  1210. if (!isspacechar(ch))
  1211. visibleChars++;
  1212. chPrev = ch;
  1213. }
  1214. // Fill in the real level of the next line, keeping the current flags as they will be filled in later
  1215. int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
  1216. styler.SetLevel(lineCurrent, levelPrev | flagsNext);
  1217. }
  1218. static const char * const perlWordListDesc[] = {
  1219. "Keywords",
  1220. 0
  1221. };
  1222. LexerModule lmPerl(SCLEX_PERL, ColourisePerlDoc, "perl", FoldPerlDoc, perlWordListDesc);