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

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

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