Debian Codegraph @

scintilla/lexilla/lexers/LexPerl.cxx

raw
1 // Scintilla source code edit control
2 /** @file LexPerl.cxx
3 ** Lexer for Perl.
4 ** Converted to lexer object by "Udo Lechner" <dlchnr(at)gmx(dot)net>
5 **/
6 // Copyright 1998-2008 by Neil Hodgson <[email protected]>
7 // Lexical analysis fixes by Kein-Hong Man <[email protected]>
8 // The License.txt file describes the conditions under which this software may be distributed.
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <stdarg.h>
14 #include <assert.h>
15 #include <ctype.h>
16
17 #include <string>
18 #include <string_view>
19 #include <map>
20 #include <functional>
21
22 #include "ILexer.h"
23 #include "Scintilla.h"
24 #include "SciLexer.h"
25
26 #include "WordList.h"
27 #include "LexAccessor.h"
28 #include "StyleContext.h"
29 #include "CharacterSet.h"
30 #include "LexerModule.h"
31 #include "OptionSet.h"
32 #include "DefaultLexer.h"
33
34 using namespace Scintilla;
35 using namespace Lexilla;
36
37 namespace {
38
39 // Info for HERE document handling from perldata.pod (reformatted):
40 // ----------------------------------------------------------------
41 // A line-oriented form of quoting is based on the shell ``here-doc'' syntax.
42 // Following a << you specify a string to terminate the quoted material, and
43 // all lines following the current line down to the terminating string are
44 // the value of the item.
45 // Prefixing the terminating string with a "~" specifies that you want to
46 // use "Indented Here-docs" (see below).
47 // * The terminating string may be either an identifier (a word), or some
48 // quoted text.
49 // * If quoted, the type of quotes you use determines the treatment of the
50 // text, just as in regular quoting.
51 // * An unquoted identifier works like double quotes.
52 // * There must be no space between the << and the identifier.
53 // (If you put a space it will be treated as a null identifier,
54 // which is valid, and matches the first empty line.)
55 // (This is deprecated, -w warns of this syntax)
56 // * The terminating string must appear by itself (unquoted and
57 // with no surrounding whitespace) on the terminating line.
58 //
59 // Indented Here-docs
60 // ------------------
61 // The here-doc modifier "~" allows you to indent your here-docs to
62 // make the code more readable.
63 // The delimiter is used to determine the exact whitespace to remove
64 // from the beginning of each line. All lines must have at least the
65 // same starting whitespace (except lines only containing a newline)
66 // or perl will croak. Tabs and spaces can be mixed, but are matched
67 // exactly. One tab will not be equal to 8 spaces!
68 // Additional beginning whitespace (beyond what preceded the
69 // delimiter) will be preserved.
70
71 #define HERE_DELIM_MAX 256 // maximum length of HERE doc delimiter
72
73 #define PERLNUM_BINARY 1 // order is significant: 1-3 cannot have a dot
74 #define PERLNUM_OCTAL 2
75 #define PERLNUM_FLOAT_EXP 3 // exponent part only
76 #define PERLNUM_HEX 4 // may be a hex float
77 #define PERLNUM_DECIMAL 5 // 1-5 are numbers; 6-7 are strings
78 #define PERLNUM_VECTOR 6
79 #define PERLNUM_V_VECTOR 7
80 #define PERLNUM_BAD 8
81
82 #define BACK_NONE 0 // lookback state for bareword disambiguation:
83 #define BACK_OPERATOR 1 // whitespace/comments are insignificant
84 #define BACK_KEYWORD 2 // operators/keywords are needed for disambiguation
85
86 #define SUB_BEGIN 0 // states for subroutine prototype scan:
87 #define SUB_HAS_PROTO 1 // only 'prototype' attribute allows prototypes
88 #define SUB_HAS_ATTRIB 2 // other attributes can exist leftward
89 #define SUB_HAS_MODULE 3 // sub name can have a ::identifier part
90 #define SUB_HAS_SUB 4 // 'sub' keyword
91
92 // all interpolated styles are different from their parent styles by a constant difference
93 // we also assume SCE_PL_STRING_VAR is the interpolated style with the smallest value
94 #define INTERPOLATE_SHIFT (SCE_PL_STRING_VAR - SCE_PL_STRING)
95
96 bool isPerlKeyword(Sci_PositionU start, Sci_PositionU end, WordList &keywords, LexAccessor &styler) {
97 // old-style keyword matcher; needed because GetCurrent() needs
98 // current segment to be committed, but we may abandon early...
99 char s[100];
100 Sci_PositionU i, len = end - start;
101 if (len > 30) { len = 30; }
102 for (i = 0; i < len; i++, start++) s[i] = styler[start];
103 s[i] = '\0';
104 return keywords.InList(s);
105 }
106
107 int disambiguateBareword(LexAccessor &styler, Sci_PositionU bk, Sci_PositionU fw,
108 int backFlag, Sci_PositionU backPos, Sci_PositionU endPos) {
109 // identifiers are recognized by Perl as barewords under some
110 // conditions, the following attempts to do the disambiguation
111 // by looking backward and forward; result in 2 LSB
112 int result = 0;
113 bool moreback = false; // true if passed newline/comments
114 bool brace = false; // true if opening brace found
115 // if BACK_NONE, neither operator nor keyword, so skip test
116 if (backFlag == BACK_NONE)
117 return result;
118 // first look backwards past whitespace/comments to set EOL flag
119 // (some disambiguation patterns must be on a single line)
120 if (backPos <= static_cast<Sci_PositionU>(styler.LineStart(styler.GetLine(bk))))
121 moreback = true;
122 // look backwards at last significant lexed item for disambiguation
123 bk = backPos - 1;
124 int ch = static_cast<unsigned char>(styler.SafeGetCharAt(bk));
125 if (ch == '{' && !moreback) {
126 // {bareword: possible variable spec
127 brace = true;
128 } else if ((ch == '&' && styler.SafeGetCharAt(bk - 1) != '&')
129 // &bareword: subroutine call
130 || styler.Match(bk - 1, "->")
131 // ->bareword: part of variable spec
132 || styler.Match(bk - 1, "::")
133 // ::bareword: part of module spec
134 || styler.Match(bk - 2, "sub")) {
135 // sub bareword: subroutine declaration
136 // (implied BACK_KEYWORD, no keywords end in 'sub'!)
137 result |= 1;
138 }
139 // next, scan forward after word past tab/spaces only;
140 // if ch isn't one of '[{(,' we can skip the test
141 if ((ch == '{' || ch == '(' || ch == '['|| ch == ',')
142 && fw < endPos) {
143 while (IsASpaceOrTab(ch = static_cast<unsigned char>(styler.SafeGetCharAt(fw)))
144 && fw < endPos) {
145 fw++;
146 }
147 if ((ch == '}' && brace)
148 // {bareword}: variable spec
149 || styler.Match(fw, "=>")) {
150 // [{(, bareword=>: hash literal
151 result |= 2;
152 }
153 }
154 return result;
155 }
156
157 void skipWhitespaceComment(LexAccessor &styler, Sci_PositionU &p) {
158 // when backtracking, we need to skip whitespace and comments
159 while (p > 0) {
160 const int style = styler.StyleAt(p);
161 if (style != SCE_PL_DEFAULT && style != SCE_PL_COMMENTLINE)
162 break;
163 p--;
164 }
165 }
166
167 int findPrevLexeme(LexAccessor &styler, Sci_PositionU &bk, int &style) {
168 // scan backward past whitespace and comments to find a lexeme
169 skipWhitespaceComment(styler, bk);
170 if (bk == 0)
171 return 0;
172 int sz = 1;
173 style = styler.StyleAt(bk);
174 while (bk > 0) { // find extent of lexeme
175 if (styler.StyleAt(bk - 1) == style) {
176 bk--; sz++;
177 } else
178 break;
179 }
180 return sz;
181 }
182
183 int styleBeforeBracePair(LexAccessor &styler, Sci_PositionU bk) {
184 // backtrack to find open '{' corresponding to a '}', balanced
185 // return significant style to be tested for '/' disambiguation
186 int braceCount = 1;
187 if (bk == 0)
188 return SCE_PL_DEFAULT;
189 while (--bk > 0) {
190 if (styler.StyleAt(bk) == SCE_PL_OPERATOR) {
191 int bkch = static_cast<unsigned char>(styler.SafeGetCharAt(bk));
192 if (bkch == ';') { // early out
193 break;
194 } else if (bkch == '}') {
195 braceCount++;
196 } else if (bkch == '{') {
197 if (--braceCount == 0) break;
198 }
199 }
200 }
201 if (bk > 0 && braceCount == 0) {
202 // balanced { found, bk > 0, skip more whitespace/comments
203 bk--;
204 skipWhitespaceComment(styler, bk);
205 return styler.StyleAt(bk);
206 }
207 return SCE_PL_DEFAULT;
208 }
209
210 int styleCheckIdentifier(LexAccessor &styler, Sci_PositionU bk) {
211 // backtrack to classify sub-styles of identifier under test
212 // return sub-style to be tested for '/' disambiguation
213 if (styler.SafeGetCharAt(bk) == '>') // inputsymbol, like <foo>
214 return 1;
215 // backtrack to check for possible "->" or "::" before identifier
216 while (bk > 0 && styler.StyleAt(bk) == SCE_PL_IDENTIFIER) {
217 bk--;
218 }
219 while (bk > 0) {
220 int bkstyle = styler.StyleAt(bk);
221 if (bkstyle == SCE_PL_DEFAULT
222 || bkstyle == SCE_PL_COMMENTLINE) {
223 // skip whitespace, comments
224 } else if (bkstyle == SCE_PL_OPERATOR) {
225 // test for "->" and "::"
226 if (styler.Match(bk - 1, "->") || styler.Match(bk - 1, "::"))
227 return 2;
228 } else
229 return 3; // bare identifier
230 bk--;
231 }
232 return 0;
233 }
234
235 int podLineScan(LexAccessor &styler, Sci_PositionU &pos, Sci_PositionU endPos) {
236 // forward scan the current line to classify line for POD style
237 int state = -1;
238 while (pos < endPos) {
239 int ch = static_cast<unsigned char>(styler.SafeGetCharAt(pos));
240 if (ch == '\n' || ch == '\r') {
241 if (ch == '\r' && styler.SafeGetCharAt(pos + 1) == '\n') pos++;
242 break;
243 }
244 if (IsASpaceOrTab(ch)) { // whitespace, take note
245 if (state == -1)
246 state = SCE_PL_DEFAULT;
247 } else if (state == SCE_PL_DEFAULT) { // verbatim POD line
248 state = SCE_PL_POD_VERB;
249 } else if (state != SCE_PL_POD_VERB) { // regular POD line
250 state = SCE_PL_POD;
251 }
252 pos++;
253 }
254 if (state == -1)
255 state = SCE_PL_DEFAULT;
256 return state;
257 }
258
259 bool styleCheckSubPrototype(LexAccessor &styler, Sci_PositionU bk) {
260 // backtrack to identify if we're starting a subroutine prototype
261 // we also need to ignore whitespace/comments, format is like:
262 // sub abc::pqr :const :prototype(...)
263 // lexemes are tested in pairs, e.g. '::'+'pqr', ':'+'const', etc.
264 // and a state machine generates legal subroutine syntax matches
265 styler.Flush();
266 int state = SUB_BEGIN;
267 do {
268 // find two lexemes, lexeme 2 follows lexeme 1
269 int style2 = SCE_PL_DEFAULT;
270 Sci_PositionU pos2 = bk;
271 int len2 = findPrevLexeme(styler, pos2, style2);
272 int style1 = SCE_PL_DEFAULT;
273 Sci_PositionU pos1 = pos2;
274 if (pos1 > 0) pos1--;
275 int len1 = findPrevLexeme(styler, pos1, style1);
276 if (len1 == 0 || len2 == 0) // lexeme pair must exist
277 break;
278
279 // match parts of syntax, if invalid subroutine syntax, break off
280 if (style1 == SCE_PL_OPERATOR && len1 == 1 &&
281 styler.SafeGetCharAt(pos1) == ':') { // ':'
282 if (style2 == SCE_PL_IDENTIFIER || style2 == SCE_PL_WORD) {
283 if (len2 == 9 && styler.Match(pos2, "prototype")) { // ':' 'prototype'
284 if (state == SUB_BEGIN) {
285 state = SUB_HAS_PROTO;
286 } else
287 break;
288 } else { // ':' <attribute>
289 if (state == SUB_HAS_PROTO || state == SUB_HAS_ATTRIB) {
290 state = SUB_HAS_ATTRIB;
291 } else
292 break;
293 }
294 } else
295 break;
296 } else if (style1 == SCE_PL_OPERATOR && len1 == 2 &&
297 styler.Match(pos1, "::")) { // '::'
298 if (style2 == SCE_PL_IDENTIFIER) { // '::' <identifier>
299 state = SUB_HAS_MODULE;
300 } else
301 break;
302 } else if (style1 == SCE_PL_WORD && len1 == 3 &&
303 styler.Match(pos1, "sub")) { // 'sub'
304 if (style2 == SCE_PL_IDENTIFIER) { // 'sub' <identifier>
305 state = SUB_HAS_SUB;
306 } else
307 break;
308 } else
309 break;
310 bk = pos1; // set position for finding next lexeme pair
311 if (bk > 0) bk--;
312 } while (state != SUB_HAS_SUB);
313 return (state == SUB_HAS_SUB);
314 }
315
316 int actualNumStyle(int numberStyle) {
317 if (numberStyle == PERLNUM_VECTOR || numberStyle == PERLNUM_V_VECTOR) {
318 return SCE_PL_STRING;
319 } else if (numberStyle == PERLNUM_BAD) {
320 return SCE_PL_ERROR;
321 }
322 return SCE_PL_NUMBER;
323 }
324
325 int opposite(int ch) {
326 if (ch == '(') return ')';
327 if (ch == '[') return ']';
328 if (ch == '{') return '}';
329 if (ch == '<') return '>';
330 return ch;
331 }
332
333 bool IsCommentLine(Sci_Position line, LexAccessor &styler) {
334 Sci_Position pos = styler.LineStart(line);
335 Sci_Position eol_pos = styler.LineStart(line + 1) - 1;
336 for (Sci_Position i = pos; i < eol_pos; i++) {
337 char ch = styler[i];
338 int style = styler.StyleAt(i);
339 if (ch == '#' && style == SCE_PL_COMMENTLINE)
340 return true;
341 else if (!IsASpaceOrTab(ch))
342 return false;
343 }
344 return false;
345 }
346
347 bool IsPackageLine(Sci_Position line, LexAccessor &styler) {
348 Sci_Position pos = styler.LineStart(line);
349 int style = styler.StyleAt(pos);
350 if (style == SCE_PL_WORD && styler.Match(pos, "package")) {
351 return true;
352 }
353 return false;
354 }
355
356 int PodHeadingLevel(Sci_Position pos, LexAccessor &styler) {
357 int lvl = static_cast<unsigned char>(styler.SafeGetCharAt(pos + 5));
358 if (lvl >= '1' && lvl <= '4') {
359 return lvl - '0';
360 }
361 return 0;
362 }
363
364 // An individual named option for use in an OptionSet
365
366 // Options used for LexerPerl
367 struct OptionsPerl {
368 bool fold;
369 bool foldComment;
370 bool foldCompact;
371 // Custom folding of POD and packages
372 bool foldPOD; // fold.perl.pod
373 // Enable folding Pod blocks when using the Perl lexer.
374 bool foldPackage; // fold.perl.package
375 // Enable folding packages when using the Perl lexer.
376
377 bool foldCommentExplicit;
378
379 bool foldAtElse;
380
381 OptionsPerl() {
382 fold = false;
383 foldComment = false;
384 foldCompact = true;
385 foldPOD = true;
386 foldPackage = true;
387 foldCommentExplicit = true;
388 foldAtElse = false;
389 }
390 };
391
392 const char *const perlWordListDesc[] = {
393 "Keywords",
394 0
395 };
396
397 struct OptionSetPerl : public OptionSet<OptionsPerl> {
398 OptionSetPerl() {
399 DefineProperty("fold", &OptionsPerl::fold);
400
401 DefineProperty("fold.comment", &OptionsPerl::foldComment);
402
403 DefineProperty("fold.compact", &OptionsPerl::foldCompact);
404
405 DefineProperty("fold.perl.pod", &OptionsPerl::foldPOD,
406 "Set to 0 to disable folding Pod blocks when using the Perl lexer.");
407
408 DefineProperty("fold.perl.package", &OptionsPerl::foldPackage,
409 "Set to 0 to disable folding packages when using the Perl lexer.");
410
411 DefineProperty("fold.perl.comment.explicit", &OptionsPerl::foldCommentExplicit,
412 "Set to 0 to disable explicit folding.");
413
414 DefineProperty("fold.perl.at.else", &OptionsPerl::foldAtElse,
415 "This option enables Perl folding on a \"} else {\" line of an if statement.");
416
417 DefineWordListSets(perlWordListDesc);
418 }
419 };
420
421 const LexicalClass lexicalClasses[] = {
422 // Lexer perl SCLEX_PERL SCE_PL_:
423 0, "SCE_PL_DEFAULT", "default", "white space",
424 1, "SCE_PL_ERROR", "error", "error",
425 2, "SCE_PL_COMMENTLINE", "comment line", "comment",
426 3, "SCE_PL_POD", "data", "pod: = at beginning of line",
427 4, "SCE_PL_NUMBER", "literal numeric", "number",
428 5, "SCE_PL_WORD", "keyword", "keyword",
429 6, "SCE_PL_STRING", "literal string interpolated", "double quoted string",
430 7, "SCE_PL_CHARACTER", "literal string", "single quoted string",
431 8, "SCE_PL_PUNCTUATION", "operator", "symbols / punctuation. currently not used",
432 9, "SCE_PL_PREPROCESSOR", "preprocessor unused", "preprocessor. currently not used",
433 10, "SCE_PL_OPERATOR", "operator", "operators",
434 11, "SCE_PL_IDENTIFIER", "identifier", "identifiers (functions, etc.)",
435 12, "SCE_PL_SCALAR", "identifier", "scalars: $var",
436 13, "SCE_PL_ARRAY", "identifier", "array: @var",
437 14, "SCE_PL_HASH", "identifier", "hash: %var",
438 15, "SCE_PL_SYMBOLTABLE", "identifier", "symbol table: *var",
439 16, "SCE_PL_VARIABLE_INDEXER", "identifier unused", "sce_pl_variable_indexer allocated but unused",
440 17, "SCE_PL_REGEX", "literal regex", "regex: /re/ or m{re}",
441 18, "SCE_PL_REGSUBST", "literal regex", "substitution: s/re/ore/",
442 19, "SCE_PL_LONGQUOTE", "literal string", "long quote (qq, qr, qw, qx) -- obsolete: replaced by qq, qx, qr, qw",
443 20, "SCE_PL_BACKTICKS", "literal string interpolated", "back ticks",
444 21, "SCE_PL_DATASECTION", "data", "data section: __data__ or __end__ at beginning of line",
445 22, "SCE_PL_HERE_DELIM", "here-doc literal string", "here-doc (delimiter)",
446 23, "SCE_PL_HERE_Q", "here-doc literal string", "here-doc (single quoted, q)",
447 24, "SCE_PL_HERE_QQ", "here-doc literal string interpolated", "here-doc (double quoted, qq)",
448 25, "SCE_PL_HERE_QX", "here-doc literal interpolated", "here-doc (back ticks, qx)",
449 26, "SCE_PL_STRING_Q", "literal string", "single quoted string, generic",
450 27, "SCE_PL_STRING_QQ", "literal string interpolated", "qq = double quoted string",
451 28, "SCE_PL_STRING_QX", "literal string interpolated", "qx = back ticks",
452 29, "SCE_PL_STRING_QR", "literal regex", "qr = regex",
453 30, "SCE_PL_STRING_QW", "literal string interpolated", "qw = array",
454 31, "SCE_PL_POD_VERB", "data", "pod: verbatim paragraphs",
455 40, "SCE_PL_SUB_PROTOTYPE", "identifier", "subroutine prototype",
456 41, "SCE_PL_FORMAT_IDENT", "identifier", "format identifier",
457 42, "SCE_PL_FORMAT", "literal string", "format body",
458 43, "SCE_PL_STRING_VAR", "identifier interpolated", "double quoted string (interpolated variable)",
459 44, "SCE_PL_XLAT", "literal string", "translation: tr{}{} y{}{}",
460 54, "SCE_PL_REGEX_VAR", "identifier interpolated", "regex: /re/ or m{re} (interpolated variable)",
461 55, "SCE_PL_REGSUBST_VAR", "identifier interpolated", "substitution: s/re/ore/ (interpolated variable)",
462 57, "SCE_PL_BACKTICKS_VAR", "identifier interpolated", "back ticks (interpolated variable)",
463 61, "SCE_PL_HERE_QQ_VAR", "identifier interpolated", "here-doc (double quoted, qq) (interpolated variable)",
464 62, "SCE_PL_HERE_QX_VAR", "identifier interpolated", "here-doc (back ticks, qx) (interpolated variable)",
465 64, "SCE_PL_STRING_QQ_VAR", "identifier interpolated", "qq = double quoted string (interpolated variable)",
466 65, "SCE_PL_STRING_QX_VAR", "identifier interpolated", "qx = back ticks (interpolated variable)",
467 66, "SCE_PL_STRING_QR_VAR", "identifier interpolated", "qr = regex (interpolated variable)",
468 };
469
470 class LexerPerl : public DefaultLexer {
471 CharacterSet setWordStart;
472 CharacterSet setWord;
473 CharacterSet setSpecialVar;
474 CharacterSet setControlVar;
475 WordList keywords;
476 OptionsPerl options;
477 OptionSetPerl osPerl;
478 public:
479 LexerPerl() :
480 DefaultLexer("perl", SCLEX_PERL, lexicalClasses, std::size(lexicalClasses)),
481 setWordStart(CharacterSet::setAlpha, "_", 0x80, true),
482 setWord(CharacterSet::setAlphaNum, "_", 0x80, true),
483 setSpecialVar(CharacterSet::setNone, "\"$;<>&`'+,./\\%:=~!?@[]"),
484 setControlVar(CharacterSet::setNone, "ACDEFHILMNOPRSTVWX") {
485 }
486 virtual ~LexerPerl() {
487 }
488 void SCI_METHOD Release() override {
489 delete this;
490 }
491 int SCI_METHOD Version() const override {
492 return lvRelease5;
493 }
494 const char *SCI_METHOD PropertyNames() override {
495 return osPerl.PropertyNames();
496 }
497 int SCI_METHOD PropertyType(const char *name) override {
498 return osPerl.PropertyType(name);
499 }
500 const char *SCI_METHOD DescribeProperty(const char *name) override {
501 return osPerl.DescribeProperty(name);
502 }
503 Sci_Position SCI_METHOD PropertySet(const char *key, const char *val) override;
504 const char * SCI_METHOD PropertyGet(const char *key) override {
505 return osPerl.PropertyGet(key);
506 }
507 const char *SCI_METHOD DescribeWordListSets() override {
508 return osPerl.DescribeWordListSets();
509 }
510 Sci_Position SCI_METHOD WordListSet(int n, const char *wl) override;
511 void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
512 void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
513
514 void *SCI_METHOD PrivateCall(int, void *) override {
515 return 0;
516 }
517
518 static ILexer5 *LexerFactoryPerl() {
519 return new LexerPerl();
520 }
521 int InputSymbolScan(StyleContext &sc);
522 void InterpolateSegment(StyleContext &sc, int maxSeg, bool isPattern=false);
523 };
524
525 Sci_Position SCI_METHOD LexerPerl::PropertySet(const char *key, const char *val) {
526 if (osPerl.PropertySet(&options, key, val)) {
527 return 0;
528 }
529 return -1;
530 }
531
532 Sci_Position SCI_METHOD LexerPerl::WordListSet(int n, const char *wl) {
533 WordList *wordListN = 0;
534 switch (n) {
535 case 0:
536 wordListN = &keywords;
537 break;
538 }
539 Sci_Position firstModification = -1;
540 if (wordListN) {
541 if (wordListN->Set(wl)) {
542 firstModification = 0;
543 }
544 }
545 return firstModification;
546 }
547
548 int LexerPerl::InputSymbolScan(StyleContext &sc) {
549 // forward scan for matching > on same line; file handles
550 int c, sLen = 0;
551 while ((c = sc.GetRelativeCharacter(++sLen)) != 0) {
552 if (c == '\r' || c == '\n') {
553 return 0;
554 } else if (c == '>') {
555 if (sc.Match("<=>")) // '<=>' case
556 return 0;
557 return sLen;
558 }
559 }
560 return 0;
561 }
562
563 void LexerPerl::InterpolateSegment(StyleContext &sc, int maxSeg, bool isPattern) {
564 // interpolate a segment (with no active backslashes or delimiters within)
565 // switch in or out of an interpolation style or continue current style
566 // commit variable patterns if found, trim segment, repeat until done
567 while (maxSeg > 0) {
568 bool isVar = false;
569 int sLen = 0;
570 if ((maxSeg > 1) && (sc.ch == '$' || sc.ch == '@')) {
571 // $#[$]*word [$@][$]*word (where word or {word} is always present)
572 bool braces = false;
573 sLen = 1;
574 if (sc.ch == '$' && sc.chNext == '#') { // starts with $#
575 sLen++;
576 }
577 while ((maxSeg > sLen) && (sc.GetRelativeCharacter(sLen) == '$')) // >0 $ dereference within
578 sLen++;
579 if ((maxSeg > sLen) && (sc.GetRelativeCharacter(sLen) == '{')) { // { start for {word}
580 sLen++;
581 braces = true;
582 }
583 if (maxSeg > sLen) {
584 int c = sc.GetRelativeCharacter(sLen);
585 if (setWordStart.Contains(c)) { // word (various)
586 sLen++;
587 isVar = true;
588 while (maxSeg > sLen) {
589 if (!setWord.Contains(sc.GetRelativeCharacter(sLen)))
590 break;
591 sLen++;
592 }
593 } else if (braces && IsADigit(c) && (sLen == 2)) { // digit for ${digit}
594 sLen++;
595 isVar = true;
596 }
597 }
598 if (braces) {
599 if ((maxSeg > sLen) && (sc.GetRelativeCharacter(sLen) == '}')) { // } end for {word}
600 sLen++;
601 } else
602 isVar = false;
603 }
604 }
605 if (!isVar && (maxSeg > 1)) { // $- or @-specific variable patterns
606 int c = sc.chNext;
607 if (sc.ch == '$') {
608 sLen = 1;
609 if (IsADigit(c)) { // $[0-9] and slurp trailing digits
610 sLen++;
611 isVar = true;
612 while ((maxSeg > sLen) && IsADigit(sc.GetRelativeCharacter(sLen)))
613 sLen++;
614 } else if (setSpecialVar.Contains(c)) { // $ special variables
615 sLen++;
616 isVar = true;
617 } else if (!isPattern && ((c == '(') || (c == ')') || (c == '|'))) { // $ additional
618 sLen++;
619 isVar = true;
620 } else if (c == '^') { // $^A control-char style
621 sLen++;
622 if ((maxSeg > sLen) && setControlVar.Contains(sc.GetRelativeCharacter(sLen))) {
623 sLen++;
624 isVar = true;
625 }
626 }
627 } else if (sc.ch == '@') {
628 sLen = 1;
629 if (!isPattern && ((c == '+') || (c == '-'))) { // @ specials non-pattern
630 sLen++;
631 isVar = true;
632 }
633 }
634 }
635 if (isVar) { // commit as interpolated variable or normal character
636 if (sc.state < SCE_PL_STRING_VAR)
637 sc.SetState(sc.state + INTERPOLATE_SHIFT);
638 sc.Forward(sLen);
639 maxSeg -= sLen;
640 } else {
641 if (sc.state >= SCE_PL_STRING_VAR)
642 sc.SetState(sc.state - INTERPOLATE_SHIFT);
643 sc.Forward();
644 maxSeg--;
645 }
646 }
647 if (sc.state >= SCE_PL_STRING_VAR)
648 sc.SetState(sc.state - INTERPOLATE_SHIFT);
649 }
650
651 void SCI_METHOD LexerPerl::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) {
652 LexAccessor styler(pAccess);
653
654 // keywords that forces /PATTERN/ at all times; should track vim's behaviour
655 WordList reWords;
656 reWords.Set("elsif if split while");
657
658 // charset classes
659 CharacterSet setSingleCharOp(CharacterSet::setNone, "rwxoRWXOezsfdlpSbctugkTBMAC");
660 // lexing of "%*</" operators is non-trivial; these are missing in the set below
661 CharacterSet setPerlOperator(CharacterSet::setNone, "^&\\()-+=|{}[]:;>,?!.~");
662 CharacterSet setQDelim(CharacterSet::setNone, "qrwx");
663 CharacterSet setModifiers(CharacterSet::setAlpha);
664 CharacterSet setPreferRE(CharacterSet::setNone, "*/<%");
665 // setArray and setHash also accepts chars for special vars like $_,
666 // which are then truncated when the next char does not match setVar
667 CharacterSet setVar(CharacterSet::setAlphaNum, "#$_'", 0x80, true);
668 CharacterSet setArray(CharacterSet::setAlpha, "#$_+-", 0x80, true);
669 CharacterSet setHash(CharacterSet::setAlpha, "#$_!^+-", 0x80, true);
670 CharacterSet &setPOD = setModifiers;
671 CharacterSet setNonHereDoc(CharacterSet::setDigits, "=$@");
672 CharacterSet setHereDocDelim(CharacterSet::setAlphaNum, "_");
673 CharacterSet setSubPrototype(CharacterSet::setNone, "\\[$@%&*+];_ \t");
674 CharacterSet setRepetition(CharacterSet::setDigits, ")\"'");
675 // for format identifiers
676 CharacterSet setFormatStart(CharacterSet::setAlpha, "_=");
677 CharacterSet &setFormat = setHereDocDelim;
678
679 // Lexer for perl often has to backtrack to start of current style to determine
680 // which characters are being used as quotes, how deeply nested is the
681 // start position and what the termination string is for HERE documents.
682
683 class HereDocCls { // Class to manage HERE doc sequence
684 public:
685 int State;
686 // 0: '<<' encountered
687 // 1: collect the delimiter
688 // 2: here doc text (lines after the delimiter)
689 int Quote; // the char after '<<'
690 bool Quoted; // true if Quote in ('\'','"','`')
691 bool StripIndent; // true if '<<~' requested to strip leading whitespace
692 int DelimiterLength; // strlen(Delimiter)
693 char Delimiter[HERE_DELIM_MAX]; // the Delimiter
694 HereDocCls() {
695 State = 0;
696 Quote = 0;
697 Quoted = false;
698 StripIndent = false;
699 DelimiterLength = 0;
700 Delimiter[0] = '\0';
701 }
702 void Append(int ch) {
703 Delimiter[DelimiterLength++] = static_cast<char>(ch);
704 Delimiter[DelimiterLength] = '\0';
705 }
706 ~HereDocCls() {
707 }
708 };
709 HereDocCls HereDoc; // TODO: FIFO for stacked here-docs
710
711 class QuoteCls { // Class to manage quote pairs
712 public:
713 int Rep;
714 int Count;
715 int Up, Down;
716 QuoteCls() {
717 New(1);
718 }
719 void New(int r = 1) {
720 Rep = r;
721 Count = 0;
722 Up = '\0';
723 Down = '\0';
724 }
725 void Open(int u) {
726 Count++;
727 Up = u;
728 Down = opposite(Up);
729 }
730 };
731 QuoteCls Quote;
732
733 // additional state for number lexing
734 int numState = PERLNUM_DECIMAL;
735 int dotCount = 0;
736
737 Sci_PositionU endPos = startPos + length;
738
739 // Backtrack to beginning of style if required...
740 // If in a long distance lexical state, backtrack to find quote characters.
741 // Includes strings (may be multi-line), numbers (additional state), format
742 // bodies, as well as POD sections.
743 if (initStyle == SCE_PL_HERE_Q
744 || initStyle == SCE_PL_HERE_QQ
745 || initStyle == SCE_PL_HERE_QX
746 || initStyle == SCE_PL_FORMAT
747 || initStyle == SCE_PL_HERE_QQ_VAR
748 || initStyle == SCE_PL_HERE_QX_VAR
749 ) {
750 // backtrack through multiple styles to reach the delimiter start
751 int delim = (initStyle == SCE_PL_FORMAT) ? SCE_PL_FORMAT_IDENT:SCE_PL_HERE_DELIM;
752 while ((startPos > 1) && (styler.StyleAt(startPos) != delim)) {
753 startPos--;
754 }
755 startPos = styler.LineStart(styler.GetLine(startPos));
756 initStyle = styler.StyleAt(startPos - 1);
757 }
758 if (initStyle == SCE_PL_STRING
759 || initStyle == SCE_PL_STRING_QQ
760 || initStyle == SCE_PL_BACKTICKS
761 || initStyle == SCE_PL_STRING_QX
762 || initStyle == SCE_PL_REGEX
763 || initStyle == SCE_PL_STRING_QR
764 || initStyle == SCE_PL_REGSUBST
765 || initStyle == SCE_PL_STRING_VAR
766 || initStyle == SCE_PL_STRING_QQ_VAR
767 || initStyle == SCE_PL_BACKTICKS_VAR
768 || initStyle == SCE_PL_STRING_QX_VAR
769 || initStyle == SCE_PL_REGEX_VAR
770 || initStyle == SCE_PL_STRING_QR_VAR
771 || initStyle == SCE_PL_REGSUBST_VAR
772 ) {
773 // for interpolation, must backtrack through a mix of two different styles
774 int otherStyle = (initStyle >= SCE_PL_STRING_VAR) ?
775 initStyle - INTERPOLATE_SHIFT : initStyle + INTERPOLATE_SHIFT;
776 while (startPos > 1) {
777 int st = styler.StyleAt(startPos - 1);
778 if ((st != initStyle) && (st != otherStyle))
779 break;
780 startPos--;
781 }
782 initStyle = SCE_PL_DEFAULT;
783 } else if (initStyle == SCE_PL_STRING_Q
784 || initStyle == SCE_PL_STRING_QW
785 || initStyle == SCE_PL_XLAT
786 || initStyle == SCE_PL_CHARACTER
787 || initStyle == SCE_PL_NUMBER
788 || initStyle == SCE_PL_IDENTIFIER
789 || initStyle == SCE_PL_ERROR
790 || initStyle == SCE_PL_SUB_PROTOTYPE
791 ) {
792 while ((startPos > 1) && (styler.StyleAt(startPos - 1) == initStyle)) {
793 startPos--;
794 }
795 initStyle = SCE_PL_DEFAULT;
796 } else if (initStyle == SCE_PL_POD
797 || initStyle == SCE_PL_POD_VERB
798 ) {
799 // POD backtracking finds preceding blank lines and goes back past them
800 Sci_Position ln = styler.GetLine(startPos);
801 if (ln > 0) {
802 initStyle = styler.StyleAt(styler.LineStart(--ln));
803 if (initStyle == SCE_PL_POD || initStyle == SCE_PL_POD_VERB) {
804 while (ln > 0 && styler.GetLineState(ln) == SCE_PL_DEFAULT)
805 ln--;
806 }
807 startPos = styler.LineStart(++ln);
808 initStyle = styler.StyleAt(startPos - 1);
809 } else {
810 startPos = 0;
811 initStyle = SCE_PL_DEFAULT;
812 }
813 }
814
815 // backFlag, backPos are additional state to aid identifier corner cases.
816 // Look backwards past whitespace and comments in order to detect either
817 // operator or keyword. Later updated as we go along.
818 int backFlag = BACK_NONE;
819 Sci_PositionU backPos = startPos;
820 if (backPos > 0) {
821 backPos--;
822 skipWhitespaceComment(styler, backPos);
823 if (styler.StyleAt(backPos) == SCE_PL_OPERATOR)
824 backFlag = BACK_OPERATOR;
825 else if (styler.StyleAt(backPos) == SCE_PL_WORD)
826 backFlag = BACK_KEYWORD;
827 backPos++;
828 }
829
830 StyleContext sc(startPos, endPos - startPos, initStyle, styler);
831
832 for (; sc.More(); sc.Forward()) {
833
834 // Determine if the current state should terminate.
835 switch (sc.state) {
836 case SCE_PL_OPERATOR:
837 sc.SetState(SCE_PL_DEFAULT);
838 backFlag = BACK_OPERATOR;
839 backPos = sc.currentPos;
840 break;
841 case SCE_PL_IDENTIFIER: // identifier, bareword, inputsymbol
842 if ((!setWord.Contains(sc.ch) && sc.ch != '\'')
843 || sc.Match('.', '.')
844 || sc.chPrev == '>') { // end of inputsymbol
845 sc.SetState(SCE_PL_DEFAULT);
846 }
847 break;
848 case SCE_PL_WORD: // keyword, plus special cases
849 if (!setWord.Contains(sc.ch)) {
850 char s[100];
851 sc.GetCurrent(s, sizeof(s));
852 if ((strcmp(s, "__DATA__") == 0) || (strcmp(s, "__END__") == 0)) {
853 sc.ChangeState(SCE_PL_DATASECTION);
854 } else {
855 if ((strcmp(s, "format") == 0)) {
856 sc.SetState(SCE_PL_FORMAT_IDENT);
857 HereDoc.State = 0;
858 } else {
859 sc.SetState(SCE_PL_DEFAULT);
860 }
861 backFlag = BACK_KEYWORD;
862 backPos = sc.currentPos;
863 }
864 }
865 break;
866 case SCE_PL_SCALAR:
867 case SCE_PL_ARRAY:
868 case SCE_PL_HASH:
869 case SCE_PL_SYMBOLTABLE:
870 if (sc.Match(':', ':')) { // skip ::
871 sc.Forward();
872 } else if (!setVar.Contains(sc.ch)) {
873 if (sc.LengthCurrent() == 1) {
874 // Special variable: $(, $_ etc.
875 sc.Forward();
876 }
877 sc.SetState(SCE_PL_DEFAULT);
878 }
879 break;
880 case SCE_PL_NUMBER:
881 // if no early break, number style is terminated at "(go through)"
882 if (sc.ch == '.') {
883 if (sc.chNext == '.') {
884 // double dot is always an operator (go through)
885 } else if (numState <= PERLNUM_FLOAT_EXP) {
886 // non-decimal number or float exponent, consume next dot
887 sc.SetState(SCE_PL_OPERATOR);
888 break;
889 } else { // decimal or vectors allows dots
890 dotCount++;
891 if (numState == PERLNUM_DECIMAL) {
892 if (dotCount <= 1) // number with one dot in it
893 break;
894 if (IsADigit(sc.chNext)) { // really a vector
895 numState = PERLNUM_VECTOR;
896 break;
897 }
898 // number then dot (go through)
899 } else if (numState == PERLNUM_HEX) {
900 if (dotCount <= 1 && IsADigit(sc.chNext, 16)) {
901 break; // hex with one dot is a hex float
902 } else {
903 sc.SetState(SCE_PL_OPERATOR);
904 break;
905 }
906 // hex then dot (go through)
907 } else if (IsADigit(sc.chNext)) // vectors
908 break;
909 // vector then dot (go through)
910 }
911 } else if (sc.ch == '_') {
912 // permissive underscoring for number and vector literals
913 break;
914 } else if (numState == PERLNUM_DECIMAL) {
915 if (sc.ch == 'E' || sc.ch == 'e') { // exponent, sign
916 numState = PERLNUM_FLOAT_EXP;
917 if (sc.chNext == '+' || sc.chNext == '-') {
918 sc.Forward();
919 }
920 break;
921 } else if (IsADigit(sc.ch))
922 break;
923 // number then word (go through)
924 } else if (numState == PERLNUM_HEX) {
925 if (sc.ch == 'P' || sc.ch == 'p') { // hex float exponent, sign
926 numState = PERLNUM_FLOAT_EXP;
927 if (sc.chNext == '+' || sc.chNext == '-') {
928 sc.Forward();
929 }
930 break;
931 } else if (IsADigit(sc.ch, 16))
932 break;
933 // hex or hex float then word (go through)
934 } else if (numState == PERLNUM_VECTOR || numState == PERLNUM_V_VECTOR) {
935 if (IsADigit(sc.ch)) // vector
936 break;
937 if (setWord.Contains(sc.ch) && dotCount == 0) { // change to word
938 sc.ChangeState(SCE_PL_IDENTIFIER);
939 break;
940 }
941 // vector then word (go through)
942 } else if (IsADigit(sc.ch)) {
943 if (numState == PERLNUM_FLOAT_EXP) {
944 break;
945 } else if (numState == PERLNUM_OCTAL) {
946 if (sc.ch <= '7') break;
947 } else if (numState == PERLNUM_BINARY) {
948 if (sc.ch <= '1') break;
949 }
950 // mark invalid octal, binary numbers (go through)
951 numState = PERLNUM_BAD;
952 break;
953 }
954 // complete current number or vector
955 sc.ChangeState(actualNumStyle(numState));
956 sc.SetState(SCE_PL_DEFAULT);
957 break;
958 case SCE_PL_COMMENTLINE:
959 if (sc.atLineStart) {
960 sc.SetState(SCE_PL_DEFAULT);
961 }
962 break;
963 case SCE_PL_HERE_DELIM:
964 if (HereDoc.State == 0) { // '<<' encountered
965 int delim_ch = sc.chNext;
966 Sci_Position ws_skip = 0;
967 HereDoc.State = 1; // pre-init HERE doc class
968 HereDoc.Quote = sc.chNext;
969 HereDoc.Quoted = false;
970 HereDoc.StripIndent = false;
971 HereDoc.DelimiterLength = 0;
972 HereDoc.Delimiter[HereDoc.DelimiterLength] = '\0';
973 if (delim_ch == '~') { // was actually '<<~'
974 sc.Forward();
975 HereDoc.StripIndent = true;
976 HereDoc.Quote = delim_ch = sc.chNext;
977 }
978 if (IsASpaceOrTab(delim_ch)) {
979 // skip whitespace; legal only for quoted delimiters
980 Sci_PositionU i = sc.currentPos + 1;
981 while ((i < endPos) && IsASpaceOrTab(delim_ch)) {
982 i++;
983 delim_ch = static_cast<unsigned char>(styler.SafeGetCharAt(i));
984 }
985 ws_skip = i - sc.currentPos - 1;
986 }
987 if (delim_ch == '\'' || delim_ch == '"' || delim_ch == '`') {
988 // a quoted here-doc delimiter; skip any whitespace
989 sc.Forward(ws_skip + 1);
990 HereDoc.Quote = delim_ch;
991 HereDoc.Quoted = true;
992 } else if ((ws_skip == 0 && setNonHereDoc.Contains(sc.chNext))
993 || ws_skip > 0) {
994 // left shift << or <<= operator cases
995 // restore position if operator
996 sc.ChangeState(SCE_PL_OPERATOR);
997 sc.ForwardSetState(SCE_PL_DEFAULT);
998 backFlag = BACK_OPERATOR;
999 backPos = sc.currentPos;
1000 HereDoc.State = 0;
1001 } else {
1002 // specially handle initial '\' for identifier
1003 if (ws_skip == 0 && HereDoc.Quote == '\\')
1004 sc.Forward();
1005 // an unquoted here-doc delimiter, no special handling
1006 // (cannot be prefixed by spaces/tabs), or
1007 // symbols terminates; deprecated zero-length delimiter
1008 }
1009 } else if (HereDoc.State == 1) { // collect the delimiter
1010 backFlag = BACK_NONE;
1011 if (HereDoc.Quoted) { // a quoted here-doc delimiter
1012 if (sc.ch == HereDoc.Quote) { // closing quote => end of delimiter
1013 sc.ForwardSetState(SCE_PL_DEFAULT);
1014 } else if (!sc.atLineEnd) {
1015 if (sc.Match('\\', static_cast<char>(HereDoc.Quote))) { // escaped quote
1016 sc.Forward();
1017 }
1018 if (sc.ch != '\r') { // skip CR if CRLF
1019 int i = 0; // else append char, possibly an extended char
1020 while (i < sc.width) {
1021 HereDoc.Append(static_cast<unsigned char>(styler.SafeGetCharAt(sc.currentPos + i)));
1022 i++;
1023 }
1024 }
1025 }
1026 } else { // an unquoted here-doc delimiter, no extended charsets
1027 if (setHereDocDelim.Contains(sc.ch)) {
1028 HereDoc.Append(sc.ch);
1029 } else {
1030 sc.SetState(SCE_PL_DEFAULT);
1031 }
1032 }
1033 if (HereDoc.DelimiterLength >= HERE_DELIM_MAX - 1) {
1034 sc.SetState(SCE_PL_ERROR);
1035 HereDoc.State = 0;
1036 }
1037 }
1038 break;
1039 case SCE_PL_HERE_Q:
1040 case SCE_PL_HERE_QQ:
1041 case SCE_PL_HERE_QX:
1042 // also implies HereDoc.State == 2
1043 sc.Complete();
1044 if (HereDoc.StripIndent) {
1045 // skip whitespace
1046 while (IsASpaceOrTab(sc.ch) && !sc.atLineEnd)
1047 sc.Forward();
1048 }
1049 if (HereDoc.DelimiterLength == 0 || sc.Match(HereDoc.Delimiter)) {
1050 int c = sc.GetRelative(HereDoc.DelimiterLength);
1051 if (c == '\r' || c == '\n') { // peek first, do not consume match
1052 sc.ForwardBytes(HereDoc.DelimiterLength);
1053 sc.SetState(SCE_PL_DEFAULT);
1054 backFlag = BACK_NONE;
1055 HereDoc.State = 0;
1056 if (!sc.atLineEnd)
1057 sc.Forward();
1058 break;
1059 }
1060 }
1061 if (sc.state == SCE_PL_HERE_Q) { // \EOF and 'EOF' non-interpolated
1062 while (!sc.atLineEnd)
1063 sc.Forward();
1064 break;
1065 }
1066 while (!sc.atLineEnd) { // "EOF" and `EOF` interpolated
1067 int c, sLen = 0, endType = 0;
1068 while ((c = sc.GetRelativeCharacter(sLen)) != 0) {
1069 // scan to break string into segments
1070 if (c == '\\') {
1071 endType = 1; break;
1072 } else if (c == '\r' || c == '\n') {
1073 endType = 2; break;
1074 }
1075 sLen++;
1076 }
1077 if (sLen > 0) // process non-empty segments
1078 InterpolateSegment(sc, sLen);
1079 if (endType == 1) {
1080 sc.Forward();
1081 // \ at end-of-line does not appear to have any effect, skip
1082 if (sc.ch != '\r' && sc.ch != '\n')
1083 sc.Forward();
1084 } else if (endType == 2) {
1085 if (!sc.atLineEnd)
1086 sc.Forward();
1087 }
1088 }
1089 break;
1090 case SCE_PL_POD:
1091 case SCE_PL_POD_VERB: {
1092 Sci_PositionU fw = sc.currentPos;
1093 Sci_Position ln = styler.GetLine(fw);
1094 if (sc.atLineStart && sc.Match("=cut")) { // end of POD
1095 sc.SetState(SCE_PL_POD);
1096 sc.Forward(4);
1097 sc.SetState(SCE_PL_DEFAULT);
1098 styler.SetLineState(ln, SCE_PL_POD);
1099 break;
1100 }
1101 int pod = podLineScan(styler, fw, endPos); // classify POD line
1102 styler.SetLineState(ln, pod);
1103 if (pod == SCE_PL_DEFAULT) {
1104 if (sc.state == SCE_PL_POD_VERB) {
1105 Sci_PositionU fw2 = fw;
1106 while (fw2 < (endPos - 1) && pod == SCE_PL_DEFAULT) {
1107 fw = fw2++; // penultimate line (last blank line)
1108 pod = podLineScan(styler, fw2, endPos);
1109 styler.SetLineState(styler.GetLine(fw2), pod);
1110 }
1111 if (pod == SCE_PL_POD) { // truncate verbatim POD early
1112 sc.SetState(SCE_PL_POD);
1113 } else
1114 fw = fw2;
1115 }
1116 } else {
1117 if (pod == SCE_PL_POD_VERB // still part of current paragraph
1118 && (styler.GetLineState(ln - 1) == SCE_PL_POD)) {
1119 pod = SCE_PL_POD;
1120 styler.SetLineState(ln, pod);
1121 } else if (pod == SCE_PL_POD
1122 && (styler.GetLineState(ln - 1) == SCE_PL_POD_VERB)) {
1123 pod = SCE_PL_POD_VERB;
1124 styler.SetLineState(ln, pod);
1125 }
1126 sc.SetState(pod);
1127 }
1128 sc.ForwardBytes(fw - sc.currentPos); // commit style
1129 }
1130 break;
1131 case SCE_PL_REGEX:
1132 case SCE_PL_STRING_QR:
1133 if (Quote.Rep <= 0) {
1134 if (!setModifiers.Contains(sc.ch))
1135 sc.SetState(SCE_PL_DEFAULT);
1136 } else if (!Quote.Up && !IsASpace(sc.ch)) {
1137 Quote.Open(sc.ch);
1138 } else {
1139 int c, sLen = 0, endType = 0;
1140 while ((c = sc.GetRelativeCharacter(sLen)) != 0) {
1141 // scan to break string into segments
1142 if (IsASpace(c)) {
1143 break;
1144 } else if (c == '\\' && Quote.Up != '\\') {
1145 endType = 1; break;
1146 } else if (c == Quote.Down) {
1147 Quote.Count--;
1148 if (Quote.Count == 0) {
1149 Quote.Rep--;
1150 break;
1151 }
1152 } else if (c == Quote.Up)
1153 Quote.Count++;
1154 sLen++;
1155 }
1156 if (sLen > 0) { // process non-empty segments
1157 if (Quote.Up != '\'') {
1158 InterpolateSegment(sc, sLen, true);
1159 } else // non-interpolated path
1160 sc.Forward(sLen);
1161 }
1162 if (endType == 1)
1163 sc.Forward();
1164 }
1165 break;
1166 case SCE_PL_REGSUBST:
1167 case SCE_PL_XLAT:
1168 if (Quote.Rep <= 0) {
1169 if (!setModifiers.Contains(sc.ch))
1170 sc.SetState(SCE_PL_DEFAULT);
1171 } else if (!Quote.Up && !IsASpace(sc.ch)) {
1172 Quote.Open(sc.ch);
1173 } else {
1174 int c, sLen = 0, endType = 0;
1175 bool isPattern = (Quote.Rep == 2);
1176 while ((c = sc.GetRelativeCharacter(sLen)) != 0) {
1177 // scan to break string into segments
1178 if (c == '\\' && Quote.Up != '\\') {
1179 endType = 2; break;
1180 } else if (Quote.Count == 0 && Quote.Rep == 1) {
1181 // We matched something like s(...) or tr{...}, Perl 5.10
1182 // appears to allow almost any character for use as the
1183 // next delimiters. Whitespace and comments are accepted in
1184 // between, but we'll limit to whitespace here.
1185 // For '#', if no whitespace in between, it's a delimiter.
1186 if (IsASpace(c)) {
1187 // Keep going
1188 } else if (c == '#' && IsASpaceOrTab(sc.GetRelativeCharacter(sLen - 1))) {
1189 endType = 3;
1190 } else
1191 Quote.Open(c);
1192 break;
1193 } else if (c == Quote.Down) {
1194 Quote.Count--;
1195 if (Quote.Count == 0) {
1196 Quote.Rep--;
1197 endType = 1;
1198 }
1199 if (Quote.Up == Quote.Down)
1200 Quote.Count++;
1201 if (endType == 1)
1202 break;
1203 } else if (c == Quote.Up) {
1204 Quote.Count++;
1205 } else if (IsASpace(c))
1206 break;
1207 sLen++;
1208 }
1209 if (sLen > 0) { // process non-empty segments
1210 if (sc.state == SCE_PL_REGSUBST && Quote.Up != '\'') {
1211 InterpolateSegment(sc, sLen, isPattern);
1212 } else // non-interpolated path
1213 sc.Forward(sLen);
1214 }
1215 if (endType == 2) {
1216 sc.Forward();
1217 } else if (endType == 3)
1218 sc.SetState(SCE_PL_DEFAULT);
1219 }
1220 break;
1221 case SCE_PL_STRING_Q:
1222 case SCE_PL_STRING_QQ:
1223 case SCE_PL_STRING_QX:
1224 case SCE_PL_STRING_QW:
1225 case SCE_PL_STRING:
1226 case SCE_PL_CHARACTER:
1227 case SCE_PL_BACKTICKS:
1228 if (!Quote.Down && !IsASpace(sc.ch)) {
1229 Quote.Open(sc.ch);
1230 } else {
1231 int c, sLen = 0, endType = 0;
1232 while ((c = sc.GetRelativeCharacter(sLen)) != 0) {
1233 // scan to break string into segments
1234 if (IsASpace(c)) {
1235 break;
1236 } else if (c == '\\' && Quote.Up != '\\') {
1237 endType = 2; break;
1238 } else if (c == Quote.Down) {
1239 Quote.Count--;
1240 if (Quote.Count == 0) {
1241 endType = 3; break;
1242 }
1243 } else if (c == Quote.Up)
1244 Quote.Count++;
1245 sLen++;
1246 }
1247 if (sLen > 0) { // process non-empty segments
1248 switch (sc.state) {
1249 case SCE_PL_STRING:
1250 case SCE_PL_STRING_QQ:
1251 case SCE_PL_BACKTICKS:
1252 InterpolateSegment(sc, sLen);
1253 break;
1254 case SCE_PL_STRING_QX:
1255 if (Quote.Up != '\'') {
1256 InterpolateSegment(sc, sLen);
1257 break;
1258 }
1259 // (continued for ' delim)
1260 // Falls through.
1261 default: // non-interpolated path
1262 sc.Forward(sLen);
1263 }
1264 }
1265 if (endType == 2) {
1266 sc.Forward();
1267 } else if (endType == 3)
1268 sc.ForwardSetState(SCE_PL_DEFAULT);
1269 }
1270 break;
1271 case SCE_PL_SUB_PROTOTYPE: {
1272 int i = 0;
1273 // forward scan; must all be valid proto characters
1274 while (setSubPrototype.Contains(sc.GetRelative(i)))
1275 i++;
1276 if (sc.GetRelative(i) == ')') { // valid sub prototype
1277 sc.ForwardBytes(i);
1278 sc.ForwardSetState(SCE_PL_DEFAULT);
1279 } else {
1280 // abandon prototype, restart from '('
1281 sc.ChangeState(SCE_PL_OPERATOR);
1282 sc.SetState(SCE_PL_DEFAULT);
1283 }
1284 }
1285 break;
1286 case SCE_PL_FORMAT: {
1287 sc.Complete();
1288 if (sc.Match('.')) {
1289 sc.Forward();
1290 if (sc.atLineEnd || ((sc.ch == '\r' && sc.chNext == '\n')))
1291 sc.SetState(SCE_PL_DEFAULT);
1292 }
1293 while (!sc.atLineEnd)
1294 sc.Forward();
1295 }
1296 break;
1297 case SCE_PL_ERROR:
1298 break;
1299 }
1300 // Needed for specific continuation styles (one follows the other)
1301 switch (sc.state) {
1302 // continued from SCE_PL_WORD
1303 case SCE_PL_FORMAT_IDENT:
1304 // occupies HereDoc state 3 to avoid clashing with HERE docs
1305 if (IsASpaceOrTab(sc.ch)) { // skip whitespace
1306 sc.ChangeState(SCE_PL_DEFAULT);
1307 while (IsASpaceOrTab(sc.ch) && !sc.atLineEnd)
1308 sc.Forward();
1309 sc.SetState(SCE_PL_FORMAT_IDENT);
1310 }
1311 if (setFormatStart.Contains(sc.ch)) { // identifier or '='
1312 if (sc.ch != '=') {
1313 do {
1314 sc.Forward();
1315 } while (setFormat.Contains(sc.ch));
1316 }
1317 while (IsASpaceOrTab(sc.ch) && !sc.atLineEnd)
1318 sc.Forward();
1319 if (sc.ch == '=') {
1320 sc.ForwardSetState(SCE_PL_DEFAULT);
1321 HereDoc.State = 3;
1322 } else {
1323 // invalid identifier; inexact fallback, but hey
1324 sc.ChangeState(SCE_PL_IDENTIFIER);
1325 sc.SetState(SCE_PL_DEFAULT);
1326 }
1327 } else {
1328 sc.ChangeState(SCE_PL_DEFAULT); // invalid identifier
1329 }
1330 backFlag = BACK_NONE;
1331 break;
1332 }
1333
1334 // Must check end of HereDoc states here before default state is handled
1335 if (HereDoc.State == 1 && sc.atLineEnd) {
1336 // Begin of here-doc (the line after the here-doc delimiter):
1337 // Lexically, the here-doc starts from the next line after the >>, but the
1338 // first line of here-doc seem to follow the style of the last EOL sequence
1339 int st_new = SCE_PL_HERE_QQ;
1340 HereDoc.State = 2;
1341 if (HereDoc.Quoted) {
1342 if (sc.state == SCE_PL_HERE_DELIM) {
1343 // Missing quote at end of string! We are stricter than perl.
1344 // Colour here-doc anyway while marking this bit as an error.
1345 sc.ChangeState(SCE_PL_ERROR);
1346 }
1347 switch (HereDoc.Quote) {
1348 case '\'':
1349 st_new = SCE_PL_HERE_Q;
1350 break;
1351 case '"' :
1352 st_new = SCE_PL_HERE_QQ;
1353 break;
1354 case '`' :
1355 st_new = SCE_PL_HERE_QX;
1356 break;
1357 }
1358 } else {
1359 if (HereDoc.Quote == '\\')
1360 st_new = SCE_PL_HERE_Q;
1361 }
1362 sc.SetState(st_new);
1363 }
1364 if (HereDoc.State == 3 && sc.atLineEnd) {
1365 // Start of format body.
1366 HereDoc.State = 0;
1367 sc.SetState(SCE_PL_FORMAT);
1368 }
1369
1370 // Determine if a new state should be entered.
1371 if (sc.state == SCE_PL_DEFAULT) {
1372 if (IsADigit(sc.ch) ||
1373 (IsADigit(sc.chNext) && (sc.ch == '.' || sc.ch == 'v'))) {
1374 sc.SetState(SCE_PL_NUMBER);
1375 backFlag = BACK_NONE;
1376 numState = PERLNUM_DECIMAL;
1377 dotCount = 0;
1378 if (sc.ch == '0') { // hex,bin,octal
1379 if (sc.chNext == 'x' || sc.chNext == 'X') {
1380 numState = PERLNUM_HEX;
1381 } else if (sc.chNext == 'b' || sc.chNext == 'B') {
1382 numState = PERLNUM_BINARY;
1383 } else if (IsADigit(sc.chNext)) {
1384 numState = PERLNUM_OCTAL;
1385 }
1386 if (numState != PERLNUM_DECIMAL) {
1387 sc.Forward();
1388 }
1389 } else if (sc.ch == 'v') { // vector
1390 numState = PERLNUM_V_VECTOR;
1391 }
1392 } else if (setWord.Contains(sc.ch)) {
1393 // if immediately prefixed by '::', always a bareword
1394 sc.SetState(SCE_PL_WORD);
1395 if (sc.chPrev == ':' && sc.GetRelative(-2) == ':') {
1396 sc.ChangeState(SCE_PL_IDENTIFIER);
1397 }
1398 Sci_PositionU bk = sc.currentPos;
1399 Sci_PositionU fw = sc.currentPos + 1;
1400 // first check for possible quote-like delimiter
1401 if (sc.ch == 's' && !setWord.Contains(sc.chNext)) {
1402 sc.ChangeState(SCE_PL_REGSUBST);
1403 Quote.New(2);
1404 } else if (sc.ch == 'm' && !setWord.Contains(sc.chNext)) {
1405 sc.ChangeState(SCE_PL_REGEX);
1406 Quote.New();
1407 } else if (sc.ch == 'q' && !setWord.Contains(sc.chNext)) {
1408 sc.ChangeState(SCE_PL_STRING_Q);
1409 Quote.New();
1410 } else if (sc.ch == 'y' && !setWord.Contains(sc.chNext)) {
1411 sc.ChangeState(SCE_PL_XLAT);
1412 Quote.New(2);
1413 } else if (sc.Match('t', 'r') && !setWord.Contains(sc.GetRelative(2))) {
1414 sc.ChangeState(SCE_PL_XLAT);
1415 Quote.New(2);
1416 sc.Forward();
1417 fw++;
1418 } else if (sc.ch == 'q' && setQDelim.Contains(sc.chNext)
1419 && !setWord.Contains(sc.GetRelative(2))) {
1420 if (sc.chNext == 'q') sc.ChangeState(SCE_PL_STRING_QQ);
1421 else if (sc.chNext == 'x') sc.ChangeState(SCE_PL_STRING_QX);
1422 else if (sc.chNext == 'r') sc.ChangeState(SCE_PL_STRING_QR);
1423 else sc.ChangeState(SCE_PL_STRING_QW); // sc.chNext == 'w'
1424 Quote.New();
1425 sc.Forward();
1426 fw++;
1427 } else if (sc.ch == 'x' && (sc.chNext == '=' || // repetition
1428 !setWord.Contains(sc.chNext) ||
1429 (setRepetition.Contains(sc.chPrev) && IsADigit(sc.chNext)))) {
1430 sc.ChangeState(SCE_PL_OPERATOR);
1431 }
1432 // if potentially a keyword, scan forward and grab word, then check
1433 // if it's really one; if yes, disambiguation test is performed
1434 // otherwise it is always a bareword and we skip a lot of scanning
1435 if (sc.state == SCE_PL_WORD) {
1436 while (setWord.Contains(static_cast<unsigned char>(styler.SafeGetCharAt(fw))))
1437 fw++;
1438 if (!isPerlKeyword(styler.GetStartSegment(), fw, keywords, styler)) {
1439 sc.ChangeState(SCE_PL_IDENTIFIER);
1440 }
1441 }
1442 // if already SCE_PL_IDENTIFIER, then no ambiguity, skip this
1443 // for quote-like delimiters/keywords, attempt to disambiguate
1444 // to select for bareword, change state -> SCE_PL_IDENTIFIER
1445 if (sc.state != SCE_PL_IDENTIFIER && bk > 0) {
1446 if (disambiguateBareword(styler, bk, fw, backFlag, backPos, endPos))
1447 sc.ChangeState(SCE_PL_IDENTIFIER);
1448 }
1449 backFlag = BACK_NONE;
1450 } else if (sc.ch == '#') {
1451 sc.SetState(SCE_PL_COMMENTLINE);
1452 } else if (sc.ch == '\"') {
1453 sc.SetState(SCE_PL_STRING);
1454 Quote.New();
1455 Quote.Open(sc.ch);
1456 backFlag = BACK_NONE;
1457 } else if (sc.ch == '\'') {
1458 if (sc.chPrev == '&' && setWordStart.Contains(sc.chNext)) {
1459 // Archaic call
1460 sc.SetState(SCE_PL_IDENTIFIER);
1461 } else {
1462 sc.SetState(SCE_PL_CHARACTER);
1463 Quote.New();
1464 Quote.Open(sc.ch);
1465 }
1466 backFlag = BACK_NONE;
1467 } else if (sc.ch == '`') {
1468 sc.SetState(SCE_PL_BACKTICKS);
1469 Quote.New();
1470 Quote.Open(sc.ch);
1471 backFlag = BACK_NONE;
1472 } else if (sc.ch == '$') {
1473 sc.SetState(SCE_PL_SCALAR);
1474 if (sc.chNext == '{') {
1475 sc.ForwardSetState(SCE_PL_OPERATOR);
1476 } else if (IsASpace(sc.chNext)) {
1477 sc.ForwardSetState(SCE_PL_DEFAULT);
1478 } else {
1479 sc.Forward();
1480 if (sc.Match('`', '`') || sc.Match(':', ':')) {
1481 sc.Forward();
1482 }
1483 }
1484 backFlag = BACK_NONE;
1485 } else if (sc.ch == '@') {
1486 sc.SetState(SCE_PL_ARRAY);
1487 if (setArray.Contains(sc.chNext)) {
1488 // no special treatment
1489 } else if (sc.chNext == ':' && sc.GetRelative(2) == ':') {
1490 sc.ForwardBytes(2);
1491 } else if (sc.chNext == '{' || sc.chNext == '[') {
1492 sc.ForwardSetState(SCE_PL_OPERATOR);
1493 } else {
1494 sc.ChangeState(SCE_PL_OPERATOR);
1495 }
1496 backFlag = BACK_NONE;
1497 } else if (setPreferRE.Contains(sc.ch)) {
1498 // Explicit backward peeking to set a consistent preferRE for
1499 // any slash found, so no longer need to track preferRE state.
1500 // Find first previous significant lexed element and interpret.
1501 // A few symbols shares this code for disambiguation.
1502 bool preferRE = false;
1503 bool isHereDoc = sc.Match('<', '<');
1504 bool hereDocSpace = false; // for: SCALAR [whitespace] '<<'
1505 Sci_PositionU bk = (sc.currentPos > 0) ? sc.currentPos - 1: 0;
1506 sc.Complete();
1507 styler.Flush();
1508 if (styler.StyleAt(bk) == SCE_PL_DEFAULT)
1509 hereDocSpace = true;
1510 skipWhitespaceComment(styler, bk);
1511 if (bk == 0) {
1512 // avoid backward scanning breakage
1513 preferRE = true;
1514 } else {
1515 int bkstyle = styler.StyleAt(bk);
1516 int bkch = static_cast<unsigned char>(styler.SafeGetCharAt(bk));
1517 switch (bkstyle) {
1518 case SCE_PL_OPERATOR:
1519 preferRE = true;
1520 if (bkch == ')' || bkch == ']') {
1521 preferRE = false;
1522 } else if (bkch == '}') {
1523 // backtrack by counting balanced brace pairs
1524 // needed to test for variables like ${}, @{} etc.
1525 bkstyle = styleBeforeBracePair(styler, bk);
1526 if (bkstyle == SCE_PL_SCALAR
1527 || bkstyle == SCE_PL_ARRAY
1528 || bkstyle == SCE_PL_HASH
1529 || bkstyle == SCE_PL_SYMBOLTABLE
1530 || bkstyle == SCE_PL_OPERATOR) {
1531 preferRE = false;
1532 }
1533 } else if (bkch == '+' || bkch == '-') {
1534 if (bkch == static_cast<unsigned char>(styler.SafeGetCharAt(bk - 1))
1535 && bkch != static_cast<unsigned char>(styler.SafeGetCharAt(bk - 2)))
1536 // exceptions for operators: unary suffixes ++, --
1537 preferRE = false;
1538 }
1539 break;
1540 case SCE_PL_IDENTIFIER:
1541 preferRE = true;
1542 bkstyle = styleCheckIdentifier(styler, bk);
1543 if ((bkstyle == 1) || (bkstyle == 2)) {
1544 // inputsymbol or var with "->" or "::" before identifier
1545 preferRE = false;
1546 } else if (bkstyle == 3) {
1547 // bare identifier, test cases follows:
1548 if (sc.ch == '/') {
1549 // if '/', /PATTERN/ unless digit/space immediately after '/'
1550 // if '//', always expect defined-or operator to follow identifier
1551 if (IsASpace(sc.chNext) || IsADigit(sc.chNext) || sc.chNext == '/')
1552 preferRE = false;
1553 } else if (sc.ch == '*' || sc.ch == '%') {
1554 if (IsASpace(sc.chNext) || IsADigit(sc.chNext) || sc.Match('*', '*'))
1555 preferRE = false;
1556 } else if (sc.ch == '<') {
1557 if (IsASpace(sc.chNext) || sc.chNext == '=')
1558 preferRE = false;
1559 }
1560 }
1561 break;
1562 case SCE_PL_SCALAR: // for $var<< case:
1563 if (isHereDoc && hereDocSpace) // if SCALAR whitespace '<<', *always* a HERE doc
1564 preferRE = true;
1565 break;
1566 case SCE_PL_WORD:
1567 preferRE = true;
1568 // for HERE docs, always true
1569 if (sc.ch == '/') {
1570 // adopt heuristics similar to vim-style rules:
1571 // keywords always forced as /PATTERN/: split, if, elsif, while
1572 // everything else /PATTERN/ unless digit/space immediately after '/'
1573 // for '//', defined-or favoured unless special keywords
1574 Sci_PositionU bkend = bk + 1;
1575 while (bk > 0 && styler.StyleAt(bk - 1) == SCE_PL_WORD) {
1576 bk--;
1577 }
1578 if (isPerlKeyword(bk, bkend, reWords, styler))
1579 break;
1580 if (IsASpace(sc.chNext) || IsADigit(sc.chNext) || sc.chNext == '/')
1581 preferRE = false;
1582 } else if (sc.ch == '*' || sc.ch == '%') {
1583 if (IsASpace(sc.chNext) || IsADigit(sc.chNext) || sc.Match('*', '*'))
1584 preferRE = false;
1585 } else if (sc.ch == '<') {
1586 if (IsASpace(sc.chNext) || sc.chNext == '=')
1587 preferRE = false;
1588 }
1589 break;
1590
1591 // other styles uses the default, preferRE=false
1592 case SCE_PL_POD:
1593 case SCE_PL_HERE_Q:
1594 case SCE_PL_HERE_QQ:
1595 case SCE_PL_HERE_QX:
1596 preferRE = true;
1597 break;
1598 }
1599 }
1600 backFlag = BACK_NONE;
1601 if (isHereDoc) { // handle '<<', HERE doc
1602 if (sc.Match("<<>>")) { // double-diamond operator (5.22)
1603 sc.SetState(SCE_PL_OPERATOR);
1604 sc.Forward(3);
1605 } else if (preferRE) {
1606 sc.SetState(SCE_PL_HERE_DELIM);
1607 HereDoc.State = 0;
1608 } else { // << operator
1609 sc.SetState(SCE_PL_OPERATOR);
1610 sc.Forward();
1611 }
1612 } else if (sc.ch == '*') { // handle '*', typeglob
1613 if (preferRE) {
1614 sc.SetState(SCE_PL_SYMBOLTABLE);
1615 if (sc.chNext == ':' && sc.GetRelative(2) == ':') {
1616 sc.ForwardBytes(2);
1617 } else if (sc.chNext == '{') {
1618 sc.ForwardSetState(SCE_PL_OPERATOR);
1619 } else {
1620 sc.Forward();
1621 }
1622 } else {
1623 sc.SetState(SCE_PL_OPERATOR);
1624 if (sc.chNext == '*') // exponentiation
1625 sc.Forward();
1626 }
1627 } else if (sc.ch == '%') { // handle '%', hash
1628 if (preferRE) {
1629 sc.SetState(SCE_PL_HASH);
1630 if (setHash.Contains(sc.chNext)) {
1631 sc.Forward();
1632 } else if (sc.chNext == ':' && sc.GetRelative(2) == ':') {
1633 sc.ForwardBytes(2);
1634 } else if (sc.chNext == '{') {
1635 sc.ForwardSetState(SCE_PL_OPERATOR);
1636 } else {
1637 sc.ChangeState(SCE_PL_OPERATOR);
1638 }
1639 } else {
1640 sc.SetState(SCE_PL_OPERATOR);
1641 }
1642 } else if (sc.ch == '<') { // handle '<', inputsymbol
1643 if (preferRE) {
1644 // forward scan
1645 int i = InputSymbolScan(sc);
1646 if (i > 0) {
1647 sc.SetState(SCE_PL_IDENTIFIER);
1648 sc.Forward(i);
1649 } else {
1650 sc.SetState(SCE_PL_OPERATOR);
1651 }
1652 } else {
1653 sc.SetState(SCE_PL_OPERATOR);
1654 }
1655 } else { // handle '/', regexp
1656 if (preferRE) {
1657 sc.SetState(SCE_PL_REGEX);
1658 Quote.New();
1659 Quote.Open(sc.ch);
1660 } else { // / and // operators
1661 sc.SetState(SCE_PL_OPERATOR);
1662 if (sc.chNext == '/') {
1663 sc.Forward();
1664 }
1665 }
1666 }
1667 } else if (sc.ch == '=' // POD
1668 && setPOD.Contains(sc.chNext)
1669 && sc.atLineStart) {
1670 sc.SetState(SCE_PL_POD);
1671 backFlag = BACK_NONE;
1672 } else if (sc.ch == '-' && setWordStart.Contains(sc.chNext)) { // extended '-' cases
1673 Sci_PositionU bk = sc.currentPos;
1674 Sci_PositionU fw = 2;
1675 if (setSingleCharOp.Contains(sc.chNext) && // file test operators
1676 !setWord.Contains(sc.GetRelative(2))) {
1677 sc.SetState(SCE_PL_WORD);
1678 } else {
1679 // nominally a minus and bareword; find extent of bareword
1680 while (setWord.Contains(sc.GetRelative(fw)))
1681 fw++;
1682 sc.SetState(SCE_PL_OPERATOR);
1683 }
1684 // force to bareword for hash key => or {variable literal} cases
1685 if (disambiguateBareword(styler, bk, bk + fw, backFlag, backPos, endPos) & 2) {
1686 sc.ChangeState(SCE_PL_IDENTIFIER);
1687 }
1688 backFlag = BACK_NONE;
1689 } else if (sc.ch == '(' && sc.currentPos > 0) { // '(' or subroutine prototype
1690 sc.Complete();
1691 if (styleCheckSubPrototype(styler, sc.currentPos - 1)) {
1692 sc.SetState(SCE_PL_SUB_PROTOTYPE);
1693 backFlag = BACK_NONE;
1694 } else {
1695 sc.SetState(SCE_PL_OPERATOR);
1696 }
1697 } else if (setPerlOperator.Contains(sc.ch)) { // operators
1698 sc.SetState(SCE_PL_OPERATOR);
1699 if (sc.Match('.', '.')) { // .. and ...
1700 sc.Forward();
1701 if (sc.chNext == '.') sc.Forward();
1702 }
1703 } else if (sc.ch == 4 || sc.ch == 26) { // ^D and ^Z ends valid perl source
1704 sc.SetState(SCE_PL_DATASECTION);
1705 } else {
1706 // keep colouring defaults
1707 sc.Complete();
1708 }
1709 }
1710 }
1711 sc.Complete();
1712 if (sc.state == SCE_PL_HERE_Q
1713 || sc.state == SCE_PL_HERE_QQ
1714 || sc.state == SCE_PL_HERE_QX
1715 || sc.state == SCE_PL_FORMAT) {
1716 styler.ChangeLexerState(sc.currentPos, styler.Length());
1717 }
1718 sc.Complete();
1719 }
1720
1721 #define PERL_HEADFOLD_SHIFT 4
1722 #define PERL_HEADFOLD_MASK 0xF0
1723
1724 void SCI_METHOD LexerPerl::Fold(Sci_PositionU startPos, Sci_Position length, int /* initStyle */, IDocument *pAccess) {
1725
1726 if (!options.fold)
1727 return;
1728
1729 LexAccessor styler(pAccess);
1730
1731 Sci_PositionU endPos = startPos + length;
1732 int visibleChars = 0;
1733 Sci_Position lineCurrent = styler.GetLine(startPos);
1734
1735 // Backtrack to previous line in case need to fix its fold status
1736 if (startPos > 0) {
1737 if (lineCurrent > 0) {
1738 lineCurrent--;
1739 startPos = styler.LineStart(lineCurrent);
1740 }
1741 }
1742
1743 int levelPrev = SC_FOLDLEVELBASE;
1744 if (lineCurrent > 0)
1745 levelPrev = styler.LevelAt(lineCurrent - 1) >> 16;
1746 int levelCurrent = levelPrev;
1747 char chNext = styler[startPos];
1748 char chPrev = styler.SafeGetCharAt(startPos - 1);
1749 int styleNext = styler.StyleAt(startPos);
1750 // Used at end of line to determine if the line was a package definition
1751 bool isPackageLine = false;
1752 int podHeading = 0;
1753 for (Sci_PositionU i = startPos; i < endPos; i++) {
1754 char ch = chNext;
1755 chNext = styler.SafeGetCharAt(i + 1);
1756 int style = styleNext;
1757 styleNext = styler.StyleAt(i + 1);
1758 int stylePrevCh = (i) ? styler.StyleAt(i - 1):SCE_PL_DEFAULT;
1759 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
1760 bool atLineStart = ((chPrev == '\r') || (chPrev == '\n')) || i == 0;
1761 // Comment folding
1762 if (options.foldComment && atEOL && IsCommentLine(lineCurrent, styler)) {
1763 if (!IsCommentLine(lineCurrent - 1, styler)
1764 && IsCommentLine(lineCurrent + 1, styler))
1765 levelCurrent++;
1766 else if (IsCommentLine(lineCurrent - 1, styler)
1767 && !IsCommentLine(lineCurrent + 1, styler))
1768 levelCurrent--;
1769 }
1770 // {} [] block folding
1771 if (style == SCE_PL_OPERATOR) {
1772 if (ch == '{') {
1773 if (options.foldAtElse && levelCurrent < levelPrev)
1774 --levelPrev;
1775 levelCurrent++;
1776 } else if (ch == '}') {
1777 levelCurrent--;
1778 }
1779 if (ch == '[') {
1780 if (options.foldAtElse && levelCurrent < levelPrev)
1781 --levelPrev;
1782 levelCurrent++;
1783 } else if (ch == ']') {
1784 levelCurrent--;
1785 }
1786 } else if (style == SCE_PL_STRING_QW) {
1787 // qw
1788 if (stylePrevCh != style)
1789 levelCurrent++;
1790 else if (styleNext != style)
1791 levelCurrent--;
1792 }
1793 // POD folding
1794 if (options.foldPOD && atLineStart) {
1795 if (style == SCE_PL_POD) {
1796 if (stylePrevCh != SCE_PL_POD && stylePrevCh != SCE_PL_POD_VERB)
1797 levelCurrent++;
1798 else if (styler.Match(i, "=cut"))
1799 levelCurrent = (levelCurrent & ~PERL_HEADFOLD_MASK) - 1;
1800 else if (styler.Match(i, "=head"))
1801 podHeading = PodHeadingLevel(i, styler);
1802 } else if (style == SCE_PL_DATASECTION) {
1803 if (ch == '=' && IsASCII(chNext) && isalpha(chNext) && levelCurrent == SC_FOLDLEVELBASE)
1804 levelCurrent++;
1805 else if (styler.Match(i, "=cut") && levelCurrent > SC_FOLDLEVELBASE)
1806 levelCurrent = (levelCurrent & ~PERL_HEADFOLD_MASK) - 1;
1807 else if (styler.Match(i, "=head"))
1808 podHeading = PodHeadingLevel(i, styler);
1809 // if package used or unclosed brace, level > SC_FOLDLEVELBASE!
1810 // reset needed as level test is vs. SC_FOLDLEVELBASE
1811 else if (stylePrevCh != SCE_PL_DATASECTION)
1812 levelCurrent = SC_FOLDLEVELBASE;
1813 }
1814 }
1815 // package folding
1816 if (options.foldPackage && atLineStart) {
1817 if (IsPackageLine(lineCurrent, styler)
1818 && !IsPackageLine(lineCurrent + 1, styler))
1819 isPackageLine = true;
1820 }
1821
1822 //heredoc folding
1823 switch (style) {
1824 case SCE_PL_HERE_QQ :
1825 case SCE_PL_HERE_Q :
1826 case SCE_PL_HERE_QX :
1827 switch (stylePrevCh) {
1828 case SCE_PL_HERE_QQ :
1829 case SCE_PL_HERE_Q :
1830 case SCE_PL_HERE_QX :
1831 //do nothing;
1832 break;
1833 default :
1834 levelCurrent++;
1835 break;
1836 }
1837 break;
1838 default:
1839 switch (stylePrevCh) {
1840 case SCE_PL_HERE_QQ :
1841 case SCE_PL_HERE_Q :
1842 case SCE_PL_HERE_QX :
1843 levelCurrent--;
1844 break;
1845 default :
1846 //do nothing;
1847 break;
1848 }
1849 break;
1850 }
1851
1852 //explicit folding
1853 if (options.foldCommentExplicit && style == SCE_PL_COMMENTLINE && ch == '#') {
1854 if (chNext == '{') {
1855 levelCurrent++;
1856 } else if (levelCurrent > SC_FOLDLEVELBASE && chNext == '}') {
1857 levelCurrent--;
1858 }
1859 }
1860
1861 if (atEOL) {
1862 int lev = levelPrev;
1863 // POD headings occupy bits 7-4, leaving some breathing room for
1864 // non-standard practice -- POD sections stuck in blocks, etc.
1865 if (podHeading > 0) {
1866 levelCurrent = (lev & ~PERL_HEADFOLD_MASK) | (podHeading << PERL_HEADFOLD_SHIFT);
1867 lev = levelCurrent - 1;
1868 lev |= SC_FOLDLEVELHEADERFLAG;
1869 podHeading = 0;
1870 }
1871 // Check if line was a package declaration
1872 // because packages need "special" treatment
1873 if (isPackageLine) {
1874 lev = SC_FOLDLEVELBASE | SC_FOLDLEVELHEADERFLAG;
1875 levelCurrent = SC_FOLDLEVELBASE + 1;
1876 isPackageLine = false;
1877 }
1878 lev |= levelCurrent << 16;
1879 if (visibleChars == 0 && options.foldCompact)
1880 lev |= SC_FOLDLEVELWHITEFLAG;
1881 if ((levelCurrent > levelPrev) && (visibleChars > 0))
1882 lev |= SC_FOLDLEVELHEADERFLAG;
1883 if (lev != styler.LevelAt(lineCurrent)) {
1884 styler.SetLevel(lineCurrent, lev);
1885 }
1886 lineCurrent++;
1887 levelPrev = levelCurrent;
1888 visibleChars = 0;
1889 }
1890 if (!isspacechar(ch))
1891 visibleChars++;
1892 chPrev = ch;
1893 }
1894 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
1895 int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
1896 styler.SetLevel(lineCurrent, levelPrev | flagsNext);
1897 }
1898
1899 }
1900
1901 extern const LexerModule lmPerl(SCLEX_PERL, LexerPerl::LexerFactoryPerl, "perl", perlWordListDesc);