Update Scintilla to version 3.4.4
[TortoiseGit.git] / ext / scintilla / lexers / LexBash.cxx
blobcfc214a9099f312be2ca5355d887425dbc404b48
1 // Scintilla source code edit control
2 /** @file LexBash.cxx
3 ** Lexer for Bash.
4 **/
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.
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdarg.h>
13 #include <assert.h>
15 #include "ILexer.h"
16 #include "Scintilla.h"
17 #include "SciLexer.h"
19 #include "WordList.h"
20 #include "LexAccessor.h"
21 #include "Accessor.h"
22 #include "StyleContext.h"
23 #include "CharacterSet.h"
24 #include "LexerModule.h"
26 #ifdef SCI_NAMESPACE
27 using namespace Scintilla;
28 #endif
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
34 #undef PEDANTIC_OCTAL
36 #define BASH_BASE_ERROR 65
37 #define BASH_BASE_DECIMAL 66
38 #define BASH_BASE_HEX 67
39 #ifdef PEDANTIC_OCTAL
40 #define BASH_BASE_OCTAL 68
41 #define BASH_BASE_OCTAL_ERROR 69
42 #endif
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') {
65 return ch - '0';
66 } else if (ch >= 'a' && ch <= 'z') {
67 return ch - 'a' + 10;
68 } else if (ch >= 'A' && ch <= 'Z') {
69 return ch - 'A' + 36;
70 } else if (ch == '@') {
71 return 62;
72 } else if (ch == '_') {
73 return 63;
75 return BASH_BASE_ERROR;
78 static inline int getBashNumberBase(char *s) {
79 int i = 0;
80 int base = 0;
81 while (*s) {
82 base = base * 10 + (*s++ - '0');
83 i++;
85 if (base > 64 || i > 2) {
86 return BASH_BASE_ERROR;
88 return base;
91 static int opposite(int ch) {
92 if (ch == '(') return ')';
93 if (ch == '[') return ']';
94 if (ch == '{') return '}';
95 if (ch == '<') return '>';
96 return ch;
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
121 public:
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
130 HereDocCls() {
131 State = 0;
132 Quote = 0;
133 Quoted = false;
134 Indent = 0;
135 DelimiterLength = 0;
136 Delimiter = new char[HERE_DELIM_MAX];
137 Delimiter[0] = '\0';
139 void Append(int ch) {
140 Delimiter[DelimiterLength++] = static_cast<char>(ch);
141 Delimiter[DelimiterLength] = '\0';
143 ~HereDocCls() {
144 delete []Delimiter;
147 HereDocCls HereDoc;
149 class QuoteCls { // Class to manage quote pairs (simplified vs LexPerl)
150 public:
151 int Count;
152 int Up, Down;
153 QuoteCls() {
154 Count = 0;
155 Up = '\0';
156 Down = '\0';
158 void Open(int u) {
159 Count++;
160 Up = u;
161 Down = opposite(Up);
163 void Start(int u) {
164 Count = 0;
165 Open(u);
168 QuoteCls Quote;
170 class QuoteStackCls { // Class to manage quote pairs that nest
171 public:
172 int Count;
173 int Up, Down;
174 int Style;
175 int Depth; // levels pushed
176 int *CountStack;
177 int *UpStack;
178 int *StyleStack;
179 QuoteStackCls() {
180 Count = 0;
181 Up = '\0';
182 Down = '\0';
183 Style = 0;
184 Depth = 0;
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) {
190 Count = 1;
191 Up = u;
192 Down = opposite(Up);
193 Style = s;
195 void Push(int u, int s) {
196 if (Depth >= BASH_DELIM_STACK_MAX)
197 return;
198 CountStack[Depth] = Count;
199 UpStack [Depth] = Up;
200 StyleStack[Depth] = Style;
201 Depth++;
202 Count = 1;
203 Up = u;
204 Down = opposite(Up);
205 Style = s;
207 void Pop(void) {
208 if (Depth <= 0)
209 return;
210 Depth--;
211 Count = CountStack[Depth];
212 Up = UpStack [Depth];
213 Style = StyleStack[Depth];
214 Down = opposite(Up);
216 ~QuoteStackCls() {
217 delete []CountStack;
218 delete []UpStack;
219 delete []StyleStack;
222 QuoteStackCls QuoteStack;
224 int numBase = 0;
225 int digit;
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)))
234 ln--;
235 for (;;) {
236 startPos = styler.LineStart(ln);
237 if (ln == 0 || styler.GetLineState(ln) == BASH_CMD_START)
238 break;
239 ln--;
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);
258 } else {
259 if (ln > 0) {
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
263 } else
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.
279 switch (sc.state) {
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;
286 break;
287 case SCE_SH_WORD:
288 // "." never used in Bash variable names but used in file names
289 if (!setWord.Contains(sc.ch)) {
290 char s[500];
291 char s2[10];
292 sc.GetCurrent(s, sizeof(s));
293 // allow keywords ending in a whitespace or command delimiter
294 s2[0] = static_cast<char>(sc.ch);
295 s2[1] = '\0';
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;
303 else
304 sc.ChangeState(SCE_SH_IDENTIFIER);
305 sc.SetState(SCE_SH_DEFAULT);
306 break;
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;
312 testExprType = 0;
313 } else
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;
320 else
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;
327 else
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);
342 break;
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);
349 break;
350 case SCE_SH_NUMBER:
351 digit = translateBashDigit(sc.ch);
352 if (numBase == BASH_BASE_DECIMAL) {
353 if (sc.ch == '#') {
354 char s[10];
355 sc.GetCurrent(s, sizeof(s));
356 numBase = getBashNumberBase(s);
357 if (numBase != BASH_BASE_ERROR)
358 break;
359 } else if (IsADigit(sc.ch))
360 break;
361 } else if (numBase == BASH_BASE_HEX) {
362 if (IsADigit(sc.ch, 16))
363 break;
364 #ifdef PEDANTIC_OCTAL
365 } else if (numBase == BASH_BASE_OCTAL ||
366 numBase == BASH_BASE_OCTAL_ERROR) {
367 if (digit <= 7)
368 break;
369 if (digit <= 9) {
370 numBase = BASH_BASE_OCTAL_ERROR;
371 break;
373 #endif
374 } else if (numBase == BASH_BASE_ERROR) {
375 if (digit <= 9)
376 break;
377 } else { // DD#DDDD number style handling
378 if (digit != BASH_BASE_ERROR) {
379 if (numBase <= 36) {
380 // case-insensitive if base<=36
381 if (digit >= 36) digit -= 26;
383 if (digit < numBase)
384 break;
385 if (digit <= 9) {
386 numBase = BASH_BASE_ERROR;
387 break;
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
395 #endif
397 sc.ChangeState(SCE_SH_ERROR);
399 sc.SetState(SCE_SH_DEFAULT);
400 break;
401 case SCE_SH_COMMENTLINE:
402 if (sc.atLineEnd && sc.chPrev != '\\') {
403 sc.SetState(SCE_SH_DEFAULT);
405 break;
406 case SCE_SH_HERE_DELIM:
407 // From Bash info:
408 // ---------------
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 ")
419 sc.Forward();
420 HereDoc.Quoted = true;
421 HereDoc.State = 1;
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
427 HereDoc.State = 1;
428 } else if (sc.chNext == '<') { // HERE string <<<
429 sc.Forward();
430 sc.ForwardSetState(SCE_SH_DEFAULT);
431 } else if (IsASpace(sc.chNext)) {
432 // eat whitespace
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);
437 } else {
438 // symbols terminates; deprecated zero-length delimiter
439 HereDoc.State = 1;
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);
455 } else {
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);
463 HereDoc.State = 0;
466 break;
467 case SCE_SH_HERE_Q:
468 // HereDoc.State == 2
469 if (sc.atLineStart) {
470 sc.SetState(SCE_SH_HERE_Q);
471 int prefixws = 0;
472 while (IsASpace(sc.ch) && !sc.atLineEnd) { // whitespace prefix
473 sc.Forward();
474 prefixws++;
476 if (prefixws > 0)
477 sc.SetState(SCE_SH_HERE_Q);
478 while (!sc.atLineEnd) {
479 sc.Forward();
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);
486 break;
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);
494 break;
498 break;
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);
504 } else {
505 sc.SetState(SCE_SH_DEFAULT);
508 break;
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)
513 sc.Forward();
514 } else if (sc.ch == QuoteStack.Down) {
515 QuoteStack.Count--;
516 if (QuoteStack.Count == 0) {
517 if (QuoteStack.Depth > 0) {
518 QuoteStack.Pop();
519 } else
520 sc.ForwardSetState(SCE_SH_DEFAULT);
522 } else if (sc.ch == QuoteStack.Up) {
523 QuoteStack.Count++;
524 } else {
525 if (QuoteStack.Style == BASH_DELIM_STRING ||
526 QuoteStack.Style == BASH_DELIM_LSTRING
527 ) { // do nesting for "string", $"locale-string"
528 if (sc.ch == '`') {
529 QuoteStack.Push(sc.ch, BASH_DELIM_BACKTICK);
530 } else if (sc.ch == '$' && sc.chNext == '(') {
531 sc.Forward();
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`
537 if (sc.ch == '\'') {
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 == '\'') {
545 sc.Forward();
546 QuoteStack.Push(sc.ch, BASH_DELIM_CSTRING);
547 } else if (sc.chNext == '\"') {
548 sc.Forward();
549 QuoteStack.Push(sc.ch, BASH_DELIM_LSTRING);
550 } else if (sc.chNext == '(') {
551 sc.Forward();
552 QuoteStack.Push(sc.ch, BASH_DELIM_COMMAND);
557 break;
558 case SCE_SH_PARAM: // ${parameter}
559 if (sc.ch == '\\' && Quote.Up != '\\') {
560 sc.Forward();
561 } else if (sc.ch == Quote.Down) {
562 Quote.Count--;
563 if (Quote.Count == 0) {
564 sc.ForwardSetState(SCE_SH_DEFAULT);
566 } else if (sc.ch == Quote.Up) {
567 Quote.Count++;
569 break;
570 case SCE_SH_CHARACTER: // singly-quoted strings
571 if (sc.ch == Quote.Down) {
572 Quote.Count--;
573 if (Quote.Count == 0) {
574 sc.ForwardSetState(SCE_SH_DEFAULT);
577 break;
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
585 HereDoc.State = 2;
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);
598 } else {
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) {
609 if (sc.ch == '\\') {
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;
620 sc.Forward();
621 } else if (IsADigit(sc.chNext)) {
622 #ifdef PEDANTIC_OCTAL
623 numBase = BASH_BASE_OCTAL;
624 #else
625 numBase = BASH_BASE_HEX;
626 #endif
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);
635 } else {
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);
643 Quote.Start(sc.ch);
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
650 continue;
652 sc.SetState(SCE_SH_SCALAR);
653 sc.Forward();
654 if (sc.ch == '{') {
655 sc.ChangeState(SCE_SH_PARAM);
656 Quote.Start(sc.ch);
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);
669 } else {
670 continue; // scalar has no delimiter pair
672 } else if (sc.Match('<', '<')) {
673 sc.SetState(SCE_SH_HERE_DELIM);
674 HereDoc.State = 0;
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);
681 sc.Forward();
682 } else if (setBashOperator.Contains(sc.ch)) {
683 char s[10];
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;
691 sc.Forward();
692 } else if (sc.Match('[', '[') && IsASpace(sc.GetRelative(2))) {
693 cmdState = BASH_CMD_TEST;
694 testExprType = 1;
695 sc.Forward();
696 } else if (sc.ch == '[' && IsASpace(sc.chNext)) {
697 cmdState = BASH_CMD_TEST;
698 testExprType = 2;
701 // special state -- for ((x;y;z)) in ... looping
702 if (cmdState == BASH_CMD_WORD && sc.Match('(', '(')) {
703 cmdState = BASH_CMD_ARITH;
704 sc.Forward();
705 continue;
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);
715 s[2] = '\0';
716 isCmdDelim = cmdDelimiter.InList(s);
717 if (isCmdDelim)
718 sc.Forward();
720 if (!isCmdDelim) {
721 s[1] = '\0';
722 isCmdDelim = cmdDelimiter.InList(s);
724 if (isCmdDelim) {
725 cmdState = BASH_CMD_DELIM;
726 continue;
729 // handle closing delimiters for test/arithmetic expressions - )),]],]
730 if (cmdState == BASH_CMD_ARITH && sc.Match(')', ')')) {
731 cmdState = BASH_CMD_BODY;
732 sc.Forward();
733 } else if (cmdState == BASH_CMD_TEST && IsASpace(sc.chPrev)) {
734 if (sc.Match(']', ']') && testExprType == 1) {
735 sc.Forward();
736 cmdState = BASH_CMD_BODY;
737 } else if (sc.ch == ']' && testExprType == 2) {
738 cmdState = BASH_CMD_BODY;
742 }// sc.state
744 sc.Complete();
745 if (sc.state == SCE_SH_HERE_Q) {
746 styler.ChangeLexerState(sc.currentPos, styler.Length());
748 sc.Complete();
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++) {
755 char ch = styler[i];
756 if (ch == '#')
757 return true;
758 else if (ch != ' ' && ch != '\t')
759 return false;
761 return false;
764 static void FoldBashDoc(unsigned int startPos, int length, int, WordList *[],
765 Accessor &styler) {
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;
770 int skipHereCh = 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++) {
777 char ch = chNext;
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');
782 // Comment folding
783 if (foldComment && atEOL && IsCommentLine(lineCurrent, styler))
785 if (!IsCommentLine(lineCurrent - 1, styler)
786 && IsCommentLine(lineCurrent + 1, styler))
787 levelCurrent++;
788 else if (IsCommentLine(lineCurrent - 1, styler)
789 && !IsCommentLine(lineCurrent + 1, styler))
790 levelCurrent--;
792 if (style == SCE_SH_OPERATOR) {
793 if (ch == '{') {
794 levelCurrent++;
795 } else if (ch == '}') {
796 levelCurrent--;
799 // Here Document folding
800 if (style == SCE_SH_HERE_DELIM) {
801 if (ch == '<' && chNext == '<') {
802 if (styler.SafeGetCharAt(i + 2) == '<') {
803 skipHereCh = 1;
804 } else {
805 if (skipHereCh == 0) {
806 levelCurrent++;
807 } else {
808 skipHereCh = 0;
812 } else if (style == SCE_SH_HERE_Q && styler.StyleAt(i+1) == SCE_SH_DEFAULT) {
813 levelCurrent--;
815 if (atEOL) {
816 int lev = levelPrev;
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);
824 lineCurrent++;
825 levelPrev = levelCurrent;
826 visibleChars = 0;
828 if (!isspacechar(ch))
829 visibleChars++;
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[] = {
837 "Keywords",
841 LexerModule lmBash(SCLEX_BASH, ColouriseBashDoc, "bash", FoldBashDoc, bashWordListDesc);