1 // Scintilla source code edit control
5 // Copyright 2004-2012 by Neil Hodgson <neilh@scintilla.org>
6 // Adapted from LexPerl by Kein-Hong Man 2004
7 // The License.txt file describes the conditions under which this software may be distributed.
16 #include "Scintilla.h"
20 #include "LexAccessor.h"
22 #include "StyleContext.h"
23 #include "CharacterSet.h"
24 #include "LexerModule.h"
27 using namespace Scintilla
;
30 #define HERE_DELIM_MAX 256
32 // define this if you want 'invalid octals' to be marked as errors
33 // usually, this is not a good idea, permissive lexing is better
36 #define BASH_BASE_ERROR 65
37 #define BASH_BASE_DECIMAL 66
38 #define BASH_BASE_HEX 67
40 #define BASH_BASE_OCTAL 68
41 #define BASH_BASE_OCTAL_ERROR 69
44 // state constants for parts of a bash command segment
45 #define BASH_CMD_BODY 0
46 #define BASH_CMD_START 1
47 #define BASH_CMD_WORD 2
48 #define BASH_CMD_TEST 3
49 #define BASH_CMD_ARITH 4
50 #define BASH_CMD_DELIM 5
52 // state constants for nested delimiter pairs, used by
53 // SCE_SH_STRING and SCE_SH_BACKTICKS processing
54 #define BASH_DELIM_LITERAL 0
55 #define BASH_DELIM_STRING 1
56 #define BASH_DELIM_CSTRING 2
57 #define BASH_DELIM_LSTRING 3
58 #define BASH_DELIM_COMMAND 4
59 #define BASH_DELIM_BACKTICK 5
61 #define BASH_DELIM_STACK_MAX 7
63 static inline int translateBashDigit(int ch
) {
64 if (ch
>= '0' && ch
<= '9') {
66 } else if (ch
>= 'a' && ch
<= 'z') {
68 } else if (ch
>= 'A' && ch
<= 'Z') {
70 } else if (ch
== '@') {
72 } else if (ch
== '_') {
75 return BASH_BASE_ERROR
;
78 static inline int getBashNumberBase(char *s
) {
82 base
= base
* 10 + (*s
++ - '0');
85 if (base
> 64 || i
> 2) {
86 return BASH_BASE_ERROR
;
91 static int opposite(int ch
) {
92 if (ch
== '(') return ')';
93 if (ch
== '[') return ']';
94 if (ch
== '{') return '}';
95 if (ch
== '<') return '>';
99 static void ColouriseBashDoc(unsigned int startPos
, int length
, int initStyle
,
100 WordList
*keywordlists
[], Accessor
&styler
) {
102 WordList
&keywords
= *keywordlists
[0];
103 WordList cmdDelimiter
, bashStruct
, bashStruct_in
;
104 cmdDelimiter
.Set("| || |& & && ; ;; ( ) { }");
105 bashStruct
.Set("if elif fi while until else then do done esac eval");
106 bashStruct_in
.Set("for case select");
108 CharacterSet
setWordStart(CharacterSet::setAlpha
, "_");
109 // note that [+-] are often parts of identifiers in shell scripts
110 CharacterSet
setWord(CharacterSet::setAlphaNum
, "._+-");
111 CharacterSet
setMetaCharacter(CharacterSet::setNone
, "|&;()<> \t\r\n");
112 setMetaCharacter
.Add(0);
113 CharacterSet
setBashOperator(CharacterSet::setNone
, "^&%()-+=|{}[]:;>,*/<?!.~@");
114 CharacterSet
setSingleCharOp(CharacterSet::setNone
, "rwxoRWXOezsfdlpSbctugkTBMACahGLNn");
115 CharacterSet
setParam(CharacterSet::setAlphaNum
, "$_");
116 CharacterSet
setHereDoc(CharacterSet::setAlpha
, "_\\-+!");
117 CharacterSet
setHereDoc2(CharacterSet::setAlphaNum
, "_-+!");
118 CharacterSet
setLeftShift(CharacterSet::setDigits
, "=$");
120 class HereDocCls
{ // Class to manage HERE document elements
122 int State
; // 0: '<<' encountered
123 // 1: collect the delimiter
124 // 2: here doc text (lines after the delimiter)
125 int Quote
; // the char after '<<'
126 bool Quoted
; // true if Quote in ('\'','"','`')
127 bool Indent
; // indented delimiter (for <<-)
128 int DelimiterLength
; // strlen(Delimiter)
129 char *Delimiter
; // the Delimiter, 256: sizeof PL_tokenbuf
136 Delimiter
= new char[HERE_DELIM_MAX
];
139 void Append(int ch
) {
140 Delimiter
[DelimiterLength
++] = static_cast<char>(ch
);
141 Delimiter
[DelimiterLength
] = '\0';
149 class QuoteCls
{ // Class to manage quote pairs (simplified vs LexPerl)
170 class QuoteStackCls
{ // Class to manage quote pairs that nest
175 int Depth
; // levels pushed
185 CountStack
= new int[BASH_DELIM_STACK_MAX
];
186 UpStack
= new int[BASH_DELIM_STACK_MAX
];
187 StyleStack
= new int[BASH_DELIM_STACK_MAX
];
189 void Start(int u
, int s
) {
195 void Push(int u
, int s
) {
196 if (Depth
>= BASH_DELIM_STACK_MAX
)
198 CountStack
[Depth
] = Count
;
199 UpStack
[Depth
] = Up
;
200 StyleStack
[Depth
] = Style
;
211 Count
= CountStack
[Depth
];
212 Up
= UpStack
[Depth
];
213 Style
= StyleStack
[Depth
];
222 QuoteStackCls QuoteStack
;
226 unsigned int endPos
= startPos
+ length
;
227 int cmdState
= BASH_CMD_START
;
228 int testExprType
= 0;
230 // Always backtracks to the start of a line that is not a continuation
231 // of the previous line (i.e. start of a bash command segment)
232 int ln
= styler
.GetLine(startPos
);
233 if (ln
> 0 && startPos
== static_cast<unsigned int>(styler
.LineStart(ln
)))
236 startPos
= styler
.LineStart(ln
);
237 if (ln
== 0 || styler
.GetLineState(ln
) == BASH_CMD_START
)
241 initStyle
= SCE_SH_DEFAULT
;
243 StyleContext
sc(startPos
, endPos
- startPos
, initStyle
, styler
);
245 for (; sc
.More(); sc
.Forward()) {
247 // handle line continuation, updates per-line stored state
248 if (sc
.atLineStart
) {
249 ln
= styler
.GetLine(sc
.currentPos
);
250 if (sc
.state
== SCE_SH_STRING
251 || sc
.state
== SCE_SH_BACKTICKS
252 || sc
.state
== SCE_SH_CHARACTER
253 || sc
.state
== SCE_SH_HERE_Q
254 || sc
.state
== SCE_SH_COMMENTLINE
255 || sc
.state
== SCE_SH_PARAM
) {
256 // force backtrack while retaining cmdState
257 styler
.SetLineState(ln
, BASH_CMD_BODY
);
260 if ((sc
.GetRelative(-3) == '\\' && sc
.GetRelative(-2) == '\r' && sc
.chPrev
== '\n')
261 || sc
.GetRelative(-2) == '\\') { // handle '\' line continuation
262 // retain last line's state
264 cmdState
= BASH_CMD_START
;
266 styler
.SetLineState(ln
, cmdState
);
270 // controls change of cmdState at the end of a non-whitespace element
271 // states BODY|TEST|ARITH persist until the end of a command segment
272 // state WORD persist, but ends with 'in' or 'do' construct keywords
273 int cmdStateNew
= BASH_CMD_BODY
;
274 if (cmdState
== BASH_CMD_TEST
|| cmdState
== BASH_CMD_ARITH
|| cmdState
== BASH_CMD_WORD
)
275 cmdStateNew
= cmdState
;
276 int stylePrev
= sc
.state
;
278 // Determine if the current state should terminate.
280 case SCE_SH_OPERATOR
:
281 sc
.SetState(SCE_SH_DEFAULT
);
282 if (cmdState
== BASH_CMD_DELIM
) // if command delimiter, start new command
283 cmdStateNew
= BASH_CMD_START
;
284 else if (sc
.chPrev
== '\\') // propagate command state if line continued
285 cmdStateNew
= cmdState
;
288 // "." never used in Bash variable names but used in file names
289 if (!setWord
.Contains(sc
.ch
)) {
292 sc
.GetCurrent(s
, sizeof(s
));
293 // allow keywords ending in a whitespace or command delimiter
294 s2
[0] = static_cast<char>(sc
.ch
);
296 bool keywordEnds
= IsASpace(sc
.ch
) || cmdDelimiter
.InList(s2
);
297 // 'in' or 'do' may be construct keywords
298 if (cmdState
== BASH_CMD_WORD
) {
299 if (strcmp(s
, "in") == 0 && keywordEnds
)
300 cmdStateNew
= BASH_CMD_BODY
;
301 else if (strcmp(s
, "do") == 0 && keywordEnds
)
302 cmdStateNew
= BASH_CMD_START
;
304 sc
.ChangeState(SCE_SH_IDENTIFIER
);
305 sc
.SetState(SCE_SH_DEFAULT
);
308 // a 'test' keyword starts a test expression
309 if (strcmp(s
, "test") == 0) {
310 if (cmdState
== BASH_CMD_START
&& keywordEnds
) {
311 cmdStateNew
= BASH_CMD_TEST
;
314 sc
.ChangeState(SCE_SH_IDENTIFIER
);
316 // detect bash construct keywords
317 else if (bashStruct
.InList(s
)) {
318 if (cmdState
== BASH_CMD_START
&& keywordEnds
)
319 cmdStateNew
= BASH_CMD_START
;
321 sc
.ChangeState(SCE_SH_IDENTIFIER
);
323 // 'for'|'case'|'select' needs 'in'|'do' to be highlighted later
324 else if (bashStruct_in
.InList(s
)) {
325 if (cmdState
== BASH_CMD_START
&& keywordEnds
)
326 cmdStateNew
= BASH_CMD_WORD
;
328 sc
.ChangeState(SCE_SH_IDENTIFIER
);
330 // disambiguate option items and file test operators
331 else if (s
[0] == '-') {
332 if (cmdState
!= BASH_CMD_TEST
)
333 sc
.ChangeState(SCE_SH_IDENTIFIER
);
335 // disambiguate keywords and identifiers
336 else if (cmdState
!= BASH_CMD_START
337 || !(keywords
.InList(s
) && keywordEnds
)) {
338 sc
.ChangeState(SCE_SH_IDENTIFIER
);
340 sc
.SetState(SCE_SH_DEFAULT
);
343 case SCE_SH_IDENTIFIER
:
344 if (sc
.chPrev
== '\\') { // for escaped chars
345 sc
.ForwardSetState(SCE_SH_DEFAULT
);
346 } else if (!setWord
.Contains(sc
.ch
)) {
347 sc
.SetState(SCE_SH_DEFAULT
);
351 digit
= translateBashDigit(sc
.ch
);
352 if (numBase
== BASH_BASE_DECIMAL
) {
355 sc
.GetCurrent(s
, sizeof(s
));
356 numBase
= getBashNumberBase(s
);
357 if (numBase
!= BASH_BASE_ERROR
)
359 } else if (IsADigit(sc
.ch
))
361 } else if (numBase
== BASH_BASE_HEX
) {
362 if (IsADigit(sc
.ch
, 16))
364 #ifdef PEDANTIC_OCTAL
365 } else if (numBase
== BASH_BASE_OCTAL
||
366 numBase
== BASH_BASE_OCTAL_ERROR
) {
370 numBase
= BASH_BASE_OCTAL_ERROR
;
374 } else if (numBase
== BASH_BASE_ERROR
) {
377 } else { // DD#DDDD number style handling
378 if (digit
!= BASH_BASE_ERROR
) {
380 // case-insensitive if base<=36
381 if (digit
>= 36) digit
-= 26;
386 numBase
= BASH_BASE_ERROR
;
391 // fallthrough when number is at an end or error
392 if (numBase
== BASH_BASE_ERROR
393 #ifdef PEDANTIC_OCTAL
394 || numBase
== BASH_BASE_OCTAL_ERROR
397 sc
.ChangeState(SCE_SH_ERROR
);
399 sc
.SetState(SCE_SH_DEFAULT
);
401 case SCE_SH_COMMENTLINE
:
402 if (sc
.atLineEnd
&& sc
.chPrev
!= '\\') {
403 sc
.SetState(SCE_SH_DEFAULT
);
406 case SCE_SH_HERE_DELIM
:
409 // Specifier format is: <<[-]WORD
410 // Optional '-' is for removal of leading tabs from here-doc.
411 // Whitespace acceptable after <<[-] operator
413 if (HereDoc
.State
== 0) { // '<<' encountered
414 HereDoc
.Quote
= sc
.chNext
;
415 HereDoc
.Quoted
= false;
416 HereDoc
.DelimiterLength
= 0;
417 HereDoc
.Delimiter
[HereDoc
.DelimiterLength
] = '\0';
418 if (sc
.chNext
== '\'' || sc
.chNext
== '\"') { // a quoted here-doc delimiter (' or ")
420 HereDoc
.Quoted
= true;
422 } else if (!HereDoc
.Indent
&& sc
.chNext
== '-') { // <<- indent case
423 HereDoc
.Indent
= true;
424 } else if (setHereDoc
.Contains(sc
.chNext
)) {
425 // an unquoted here-doc delimiter, no special handling
426 // TODO check what exactly bash considers part of the delim
428 } else if (sc
.chNext
== '<') { // HERE string <<<
430 sc
.ForwardSetState(SCE_SH_DEFAULT
);
431 } else if (IsASpace(sc
.chNext
)) {
433 } else if (setLeftShift
.Contains(sc
.chNext
)) {
434 // left shift << or <<= operator cases
435 sc
.ChangeState(SCE_SH_OPERATOR
);
436 sc
.ForwardSetState(SCE_SH_DEFAULT
);
438 // symbols terminates; deprecated zero-length delimiter
441 } else if (HereDoc
.State
== 1) { // collect the delimiter
442 // * if single quoted, there's no escape
443 // * if double quoted, there are \\ and \" escapes
444 if ((HereDoc
.Quote
== '\'' && sc
.ch
!= HereDoc
.Quote
) ||
445 (HereDoc
.Quoted
&& sc
.ch
!= HereDoc
.Quote
&& sc
.ch
!= '\\') ||
446 (HereDoc
.Quote
!= '\'' && sc
.chPrev
== '\\') ||
447 (setHereDoc2
.Contains(sc
.ch
))) {
448 HereDoc
.Append(sc
.ch
);
449 } else if (HereDoc
.Quoted
&& sc
.ch
== HereDoc
.Quote
) { // closing quote => end of delimiter
450 sc
.ForwardSetState(SCE_SH_DEFAULT
);
451 } else if (sc
.ch
== '\\') {
452 if (HereDoc
.Quoted
&& sc
.chNext
!= HereDoc
.Quote
&& sc
.chNext
!= '\\') {
453 // in quoted prefixes only \ and the quote eat the escape
454 HereDoc
.Append(sc
.ch
);
456 // skip escape prefix
458 } else if (!HereDoc
.Quoted
) {
459 sc
.SetState(SCE_SH_DEFAULT
);
461 if (HereDoc
.DelimiterLength
>= HERE_DELIM_MAX
- 1) { // force blowup
462 sc
.SetState(SCE_SH_ERROR
);
468 // HereDoc.State == 2
469 if (sc
.atLineStart
) {
470 sc
.SetState(SCE_SH_HERE_Q
);
472 while (IsASpace(sc
.ch
) && !sc
.atLineEnd
) { // whitespace prefix
477 sc
.SetState(SCE_SH_HERE_Q
);
478 while (!sc
.atLineEnd
) {
481 char s
[HERE_DELIM_MAX
];
482 sc
.GetCurrent(s
, sizeof(s
));
483 if (sc
.LengthCurrent() == 0) { // '' or "" delimiters
484 if (prefixws
== 0 && HereDoc
.Quoted
&& HereDoc
.DelimiterLength
== 0)
485 sc
.SetState(SCE_SH_DEFAULT
);
488 if (s
[strlen(s
) - 1] == '\r')
489 s
[strlen(s
) - 1] = '\0';
490 if (strcmp(HereDoc
.Delimiter
, s
) == 0) {
491 if ((prefixws
== 0) || // indentation rule
492 (prefixws
> 0 && HereDoc
.Indent
)) {
493 sc
.SetState(SCE_SH_DEFAULT
);
499 case SCE_SH_SCALAR
: // variable names
500 if (!setParam
.Contains(sc
.ch
)) {
501 if (sc
.LengthCurrent() == 1) {
502 // Special variable: $(, $_ etc.
503 sc
.ForwardSetState(SCE_SH_DEFAULT
);
505 sc
.SetState(SCE_SH_DEFAULT
);
509 case SCE_SH_STRING
: // delimited styles, can nest
510 case SCE_SH_BACKTICKS
:
511 if (sc
.ch
== '\\' && QuoteStack
.Up
!= '\\') {
512 if (QuoteStack
.Style
!= BASH_DELIM_LITERAL
)
514 } else if (sc
.ch
== QuoteStack
.Down
) {
516 if (QuoteStack
.Count
== 0) {
517 if (QuoteStack
.Depth
> 0) {
520 sc
.ForwardSetState(SCE_SH_DEFAULT
);
522 } else if (sc
.ch
== QuoteStack
.Up
) {
525 if (QuoteStack
.Style
== BASH_DELIM_STRING
||
526 QuoteStack
.Style
== BASH_DELIM_LSTRING
527 ) { // do nesting for "string", $"locale-string"
529 QuoteStack
.Push(sc
.ch
, BASH_DELIM_BACKTICK
);
530 } else if (sc
.ch
== '$' && sc
.chNext
== '(') {
532 QuoteStack
.Push(sc
.ch
, BASH_DELIM_COMMAND
);
534 } else if (QuoteStack
.Style
== BASH_DELIM_COMMAND
||
535 QuoteStack
.Style
== BASH_DELIM_BACKTICK
536 ) { // do nesting for $(command), `command`
538 QuoteStack
.Push(sc
.ch
, BASH_DELIM_LITERAL
);
539 } else if (sc
.ch
== '\"') {
540 QuoteStack
.Push(sc
.ch
, BASH_DELIM_STRING
);
541 } else if (sc
.ch
== '`') {
542 QuoteStack
.Push(sc
.ch
, BASH_DELIM_BACKTICK
);
543 } else if (sc
.ch
== '$') {
544 if (sc
.chNext
== '\'') {
546 QuoteStack
.Push(sc
.ch
, BASH_DELIM_CSTRING
);
547 } else if (sc
.chNext
== '\"') {
549 QuoteStack
.Push(sc
.ch
, BASH_DELIM_LSTRING
);
550 } else if (sc
.chNext
== '(') {
552 QuoteStack
.Push(sc
.ch
, BASH_DELIM_COMMAND
);
558 case SCE_SH_PARAM
: // ${parameter}
559 if (sc
.ch
== '\\' && Quote
.Up
!= '\\') {
561 } else if (sc
.ch
== Quote
.Down
) {
563 if (Quote
.Count
== 0) {
564 sc
.ForwardSetState(SCE_SH_DEFAULT
);
566 } else if (sc
.ch
== Quote
.Up
) {
570 case SCE_SH_CHARACTER
: // singly-quoted strings
571 if (sc
.ch
== Quote
.Down
) {
573 if (Quote
.Count
== 0) {
574 sc
.ForwardSetState(SCE_SH_DEFAULT
);
580 // Must check end of HereDoc state 1 before default state is handled
581 if (HereDoc
.State
== 1 && sc
.atLineEnd
) {
582 // Begin of here-doc (the line after the here-doc delimiter):
583 // Lexically, the here-doc starts from the next line after the >>, but the
584 // first line of here-doc seem to follow the style of the last EOL sequence
586 if (HereDoc
.Quoted
) {
587 if (sc
.state
== SCE_SH_HERE_DELIM
) {
588 // Missing quote at end of string! We are stricter than bash.
589 // Colour here-doc anyway while marking this bit as an error.
590 sc
.ChangeState(SCE_SH_ERROR
);
592 // HereDoc.Quote always == '\''
593 sc
.SetState(SCE_SH_HERE_Q
);
594 } else if (HereDoc
.DelimiterLength
== 0) {
595 // no delimiter, illegal (but '' and "" are legal)
596 sc
.ChangeState(SCE_SH_ERROR
);
597 sc
.SetState(SCE_SH_DEFAULT
);
599 sc
.SetState(SCE_SH_HERE_Q
);
603 // update cmdState about the current command segment
604 if (stylePrev
!= SCE_SH_DEFAULT
&& sc
.state
== SCE_SH_DEFAULT
) {
605 cmdState
= cmdStateNew
;
607 // Determine if a new state should be entered.
608 if (sc
.state
== SCE_SH_DEFAULT
) {
610 // Bash can escape any non-newline as a literal
611 sc
.SetState(SCE_SH_IDENTIFIER
);
612 if (sc
.chNext
== '\r' || sc
.chNext
== '\n')
613 sc
.SetState(SCE_SH_OPERATOR
);
614 } else if (IsADigit(sc
.ch
)) {
615 sc
.SetState(SCE_SH_NUMBER
);
616 numBase
= BASH_BASE_DECIMAL
;
617 if (sc
.ch
== '0') { // hex,octal
618 if (sc
.chNext
== 'x' || sc
.chNext
== 'X') {
619 numBase
= BASH_BASE_HEX
;
621 } else if (IsADigit(sc
.chNext
)) {
622 #ifdef PEDANTIC_OCTAL
623 numBase
= BASH_BASE_OCTAL
;
625 numBase
= BASH_BASE_HEX
;
629 } else if (setWordStart
.Contains(sc
.ch
)) {
630 sc
.SetState(SCE_SH_WORD
);
631 } else if (sc
.ch
== '#') {
632 if (stylePrev
!= SCE_SH_WORD
&& stylePrev
!= SCE_SH_IDENTIFIER
&&
633 (sc
.currentPos
== 0 || setMetaCharacter
.Contains(sc
.chPrev
))) {
634 sc
.SetState(SCE_SH_COMMENTLINE
);
636 sc
.SetState(SCE_SH_WORD
);
638 } else if (sc
.ch
== '\"') {
639 sc
.SetState(SCE_SH_STRING
);
640 QuoteStack
.Start(sc
.ch
, BASH_DELIM_STRING
);
641 } else if (sc
.ch
== '\'') {
642 sc
.SetState(SCE_SH_CHARACTER
);
644 } else if (sc
.ch
== '`') {
645 sc
.SetState(SCE_SH_BACKTICKS
);
646 QuoteStack
.Start(sc
.ch
, BASH_DELIM_BACKTICK
);
647 } else if (sc
.ch
== '$') {
648 if (sc
.Match("$((")) {
649 sc
.SetState(SCE_SH_OPERATOR
); // handle '((' later
652 sc
.SetState(SCE_SH_SCALAR
);
655 sc
.ChangeState(SCE_SH_PARAM
);
657 } else if (sc
.ch
== '\'') {
658 sc
.ChangeState(SCE_SH_STRING
);
659 QuoteStack
.Start(sc
.ch
, BASH_DELIM_CSTRING
);
660 } else if (sc
.ch
== '"') {
661 sc
.ChangeState(SCE_SH_STRING
);
662 QuoteStack
.Start(sc
.ch
, BASH_DELIM_LSTRING
);
663 } else if (sc
.ch
== '(') {
664 sc
.ChangeState(SCE_SH_BACKTICKS
);
665 QuoteStack
.Start(sc
.ch
, BASH_DELIM_COMMAND
);
666 } else if (sc
.ch
== '`') { // $` seen in a configure script, valid?
667 sc
.ChangeState(SCE_SH_BACKTICKS
);
668 QuoteStack
.Start(sc
.ch
, BASH_DELIM_BACKTICK
);
670 continue; // scalar has no delimiter pair
672 } else if (sc
.Match('<', '<')) {
673 sc
.SetState(SCE_SH_HERE_DELIM
);
675 HereDoc
.Indent
= false;
676 } else if (sc
.ch
== '-' && // one-char file test operators
677 setSingleCharOp
.Contains(sc
.chNext
) &&
678 !setWord
.Contains(sc
.GetRelative(2)) &&
679 IsASpace(sc
.chPrev
)) {
680 sc
.SetState(SCE_SH_WORD
);
682 } else if (setBashOperator
.Contains(sc
.ch
)) {
684 bool isCmdDelim
= false;
685 sc
.SetState(SCE_SH_OPERATOR
);
686 // handle opening delimiters for test/arithmetic expressions - ((,[[,[
687 if (cmdState
== BASH_CMD_START
688 || cmdState
== BASH_CMD_BODY
) {
689 if (sc
.Match('(', '(')) {
690 cmdState
= BASH_CMD_ARITH
;
692 } else if (sc
.Match('[', '[') && IsASpace(sc
.GetRelative(2))) {
693 cmdState
= BASH_CMD_TEST
;
696 } else if (sc
.ch
== '[' && IsASpace(sc
.chNext
)) {
697 cmdState
= BASH_CMD_TEST
;
701 // special state -- for ((x;y;z)) in ... looping
702 if (cmdState
== BASH_CMD_WORD
&& sc
.Match('(', '(')) {
703 cmdState
= BASH_CMD_ARITH
;
707 // handle command delimiters in command START|BODY|WORD state, also TEST if 'test'
708 if (cmdState
== BASH_CMD_START
709 || cmdState
== BASH_CMD_BODY
710 || cmdState
== BASH_CMD_WORD
711 || (cmdState
== BASH_CMD_TEST
&& testExprType
== 0)) {
712 s
[0] = static_cast<char>(sc
.ch
);
713 if (setBashOperator
.Contains(sc
.chNext
)) {
714 s
[1] = static_cast<char>(sc
.chNext
);
716 isCmdDelim
= cmdDelimiter
.InList(s
);
722 isCmdDelim
= cmdDelimiter
.InList(s
);
725 cmdState
= BASH_CMD_DELIM
;
729 // handle closing delimiters for test/arithmetic expressions - )),]],]
730 if (cmdState
== BASH_CMD_ARITH
&& sc
.Match(')', ')')) {
731 cmdState
= BASH_CMD_BODY
;
733 } else if (cmdState
== BASH_CMD_TEST
&& IsASpace(sc
.chPrev
)) {
734 if (sc
.Match(']', ']') && testExprType
== 1) {
736 cmdState
= BASH_CMD_BODY
;
737 } else if (sc
.ch
== ']' && testExprType
== 2) {
738 cmdState
= BASH_CMD_BODY
;
745 if (sc
.state
== SCE_SH_HERE_Q
) {
746 styler
.ChangeLexerState(sc
.currentPos
, styler
.Length());
751 static bool IsCommentLine(int line
, Accessor
&styler
) {
752 int pos
= styler
.LineStart(line
);
753 int eol_pos
= styler
.LineStart(line
+ 1) - 1;
754 for (int i
= pos
; i
< eol_pos
; i
++) {
758 else if (ch
!= ' ' && ch
!= '\t')
764 static void FoldBashDoc(unsigned int startPos
, int length
, int, WordList
*[],
766 bool foldComment
= styler
.GetPropertyInt("fold.comment") != 0;
767 bool foldCompact
= styler
.GetPropertyInt("fold.compact", 1) != 0;
768 unsigned int endPos
= startPos
+ length
;
769 int visibleChars
= 0;
771 int lineCurrent
= styler
.GetLine(startPos
);
772 int levelPrev
= styler
.LevelAt(lineCurrent
) & SC_FOLDLEVELNUMBERMASK
;
773 int levelCurrent
= levelPrev
;
774 char chNext
= styler
[startPos
];
775 int styleNext
= styler
.StyleAt(startPos
);
776 for (unsigned int i
= startPos
; i
< endPos
; i
++) {
778 chNext
= styler
.SafeGetCharAt(i
+ 1);
779 int style
= styleNext
;
780 styleNext
= styler
.StyleAt(i
+ 1);
781 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
783 if (foldComment
&& atEOL
&& IsCommentLine(lineCurrent
, styler
))
785 if (!IsCommentLine(lineCurrent
- 1, styler
)
786 && IsCommentLine(lineCurrent
+ 1, styler
))
788 else if (IsCommentLine(lineCurrent
- 1, styler
)
789 && !IsCommentLine(lineCurrent
+ 1, styler
))
792 if (style
== SCE_SH_OPERATOR
) {
795 } else if (ch
== '}') {
799 // Here Document folding
800 if (style
== SCE_SH_HERE_DELIM
) {
801 if (ch
== '<' && chNext
== '<') {
802 if (styler
.SafeGetCharAt(i
+ 2) == '<') {
805 if (skipHereCh
== 0) {
812 } else if (style
== SCE_SH_HERE_Q
&& styler
.StyleAt(i
+1) == SCE_SH_DEFAULT
) {
817 if (visibleChars
== 0 && foldCompact
)
818 lev
|= SC_FOLDLEVELWHITEFLAG
;
819 if ((levelCurrent
> levelPrev
) && (visibleChars
> 0))
820 lev
|= SC_FOLDLEVELHEADERFLAG
;
821 if (lev
!= styler
.LevelAt(lineCurrent
)) {
822 styler
.SetLevel(lineCurrent
, lev
);
825 levelPrev
= levelCurrent
;
828 if (!isspacechar(ch
))
831 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
832 int flagsNext
= styler
.LevelAt(lineCurrent
) & ~SC_FOLDLEVELNUMBERMASK
;
833 styler
.SetLevel(lineCurrent
, levelPrev
| flagsNext
);
836 static const char * const bashWordListDesc
[] = {
841 LexerModule
lmBash(SCLEX_BASH
, ColouriseBashDoc
, "bash", FoldBashDoc
, bashWordListDesc
);