PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wxWidgets/src/stc/scintilla/src/LexPerl.cxx

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