Debian Codegraph @

scintilla/lexilla/lexers/LexBash.cxx

raw
1 // Scintilla source code edit control
2 /** @file LexBash.cxx
3 ** Lexer for Bash.
4 **/
5 // Copyright 2004-2012 by Neil Hodgson <[email protected]>
6 // Adapted from LexPerl by Kein-Hong Man 2004
7 // The License.txt file describes the conditions under which this software may be distributed.
8
9 #include <cstdlib>
10 #include <cassert>
11 #include <cstring>
12 #include <cstdio>
13 #include <cstdarg>
14
15 #include <string>
16 #include <string_view>
17 #include <vector>
18 #include <map>
19 #include <initializer_list>
20 #include <functional>
21
22 #include "ILexer.h"
23 #include "Scintilla.h"
24 #include "SciLexer.h"
25
26 #include "StringCopy.h"
27 #include "InList.h"
28 #include "WordList.h"
29 #include "LexAccessor.h"
30 #include "StyleContext.h"
31 #include "CharacterSet.h"
32 #include "LexerModule.h"
33 #include "OptionSet.h"
34 #include "SubStyles.h"
35 #include "DefaultLexer.h"
36
37 using namespace Scintilla;
38 using namespace Lexilla;
39
40 namespace {
41
42 #define HERE_DELIM_MAX 256
43
44 // define this if you want 'invalid octals' to be marked as errors
45 // usually, this is not a good idea, permissive lexing is better
46 #undef PEDANTIC_OCTAL
47
48 #define BASH_BASE_ERROR 65
49 #define BASH_BASE_DECIMAL 66
50 #define BASH_BASE_HEX 67
51 #ifdef PEDANTIC_OCTAL
52 #define BASH_BASE_OCTAL 68
53 #define BASH_BASE_OCTAL_ERROR 69
54 #endif
55
56 // state constants for parts of a bash command segment
57 enum class CmdState {
58 Body,
59 Start,
60 Word,
61 Test, // test
62 SingleBracket, // []
63 DoubleBracket, // [[]]
64 Arithmetic,
65 Delimiter,
66 };
67
68 enum class CommandSubstitution : int {
69 Backtick,
70 Inside,
71 InsideTrack,
72 };
73
74 // state constants for nested delimiter pairs, used by
75 // SCE_SH_STRING, SCE_SH_PARAM and SCE_SH_BACKTICKS processing
76 enum class QuoteStyle {
77 Literal, // ''
78 CString, // $''
79 String, // ""
80 LString, // $""
81 HereDoc, // here document
82 Backtick, // ``
83 Parameter, // ${}
84 Command, // $()
85 CommandInside, // $() with styling inside
86 Arithmetic, // $(()), $[]
87 };
88
89 #define BASH_QUOTE_STACK_MAX 7
90 #define BASH_SPECIAL_PARAMETER "*@#?-$!"
91
92 constexpr int commandSubstitutionFlag = 0x40;
93 constexpr int MaskCommand(int state) noexcept {
94 return state & ~commandSubstitutionFlag;
95 }
96
97 constexpr int translateBashDigit(int ch) noexcept {
98 if (ch >= '0' && ch <= '9') {
99 return ch - '0';
100 } else if (ch >= 'a' && ch <= 'z') {
101 return ch - 'a' + 10;
102 } else if (ch >= 'A' && ch <= 'Z') {
103 return ch - 'A' + 36;
104 } else if (ch == '@') {
105 return 62;
106 } else if (ch == '_') {
107 return 63;
108 }
109 return BASH_BASE_ERROR;
110 }
111
112 int getBashNumberBase(char *s) noexcept {
113 int i = 0;
114 int base = 0;
115 while (*s) {
116 base = base * 10 + (*s++ - '0');
117 i++;
118 }
119 if (base > 64 || i > 2) {
120 return BASH_BASE_ERROR;
121 }
122 return base;
123 }
124
125 constexpr int opposite(int ch) noexcept {
126 if (ch == '(') return ')';
127 if (ch == '[') return ']';
128 if (ch == '{') return '}';
129 if (ch == '<') return '>';
130 return ch;
131 }
132
133 int GlobScan(StyleContext &sc) {
134 // forward scan for zsh globs, disambiguate versus bash arrays
135 // complex expressions may still fail, e.g. unbalanced () '' "" etc
136 int c = 0;
137 int sLen = 0;
138 int pCount = 0;
139 int hash = 0;
140 while ((c = sc.GetRelativeCharacter(++sLen)) != 0) {
141 if (IsASpace(c)) {
142 return 0;
143 } else if (c == '\'' || c == '\"') {
144 if (hash != 2) return 0;
145 } else if (c == '#' && hash == 0) {
146 hash = (sLen == 1) ? 2:1;
147 } else if (c == '(') {
148 pCount++;
149 } else if (c == ')') {
150 if (pCount == 0) {
151 if (hash) return sLen;
152 return 0;
153 }
154 pCount--;
155 }
156 }
157 return 0;
158 }
159
160 bool IsCommentLine(Sci_Position line, LexAccessor &styler) {
161 const Sci_Position pos = styler.LineStart(line);
162 const Sci_Position eol_pos = styler.LineStart(line + 1) - 1;
163 for (Sci_Position i = pos; i < eol_pos; i++) {
164 const char ch = styler[i];
165 if (ch == '#')
166 return true;
167 else if (ch != ' ' && ch != '\t')
168 return false;
169 }
170 return false;
171 }
172
173 constexpr bool StyleForceBacktrack(int state) noexcept {
174 return AnyOf(state, SCE_SH_CHARACTER, SCE_SH_STRING, SCE_SH_BACKTICKS, SCE_SH_HERE_Q, SCE_SH_PARAM);
175 }
176
177 struct OptionsBash {
178 bool fold = false;
179 bool foldComment = false;
180 bool foldCompact = true;
181 bool stylingInsideString = false;
182 bool stylingInsideBackticks = false;
183 bool stylingInsideParameter = false;
184 bool stylingInsideHeredoc = false;
185 bool nestedBackticks = true;
186 CommandSubstitution commandSubstitution = CommandSubstitution::Backtick;
187 std::string specialParameter = BASH_SPECIAL_PARAMETER;
188
189 [[nodiscard]] bool stylingInside(int state) const noexcept {
190 switch (state) {
191 case SCE_SH_STRING:
192 return stylingInsideString;
193 case SCE_SH_BACKTICKS:
194 return stylingInsideBackticks;
195 case SCE_SH_PARAM:
196 return stylingInsideParameter;
197 case SCE_SH_HERE_Q:
198 return stylingInsideHeredoc;
199 default:
200 return false;
201 }
202 }
203 };
204
205 const char * const bashWordListDesc[] = {
206 "Keywords",
207 nullptr
208 };
209
210 struct OptionSetBash : public OptionSet<OptionsBash> {
211 OptionSetBash() {
212 DefineProperty("fold", &OptionsBash::fold);
213
214 DefineProperty("fold.comment", &OptionsBash::foldComment);
215
216 DefineProperty("fold.compact", &OptionsBash::foldCompact);
217
218 DefineProperty("lexer.bash.styling.inside.string", &OptionsBash::stylingInsideString,
219 "Set this property to 1 to highlight shell expansions inside string.");
220
221 DefineProperty("lexer.bash.styling.inside.backticks", &OptionsBash::stylingInsideBackticks,
222 "Set this property to 1 to highlight shell expansions inside backticks.");
223
224 DefineProperty("lexer.bash.styling.inside.parameter", &OptionsBash::stylingInsideParameter,
225 "Set this property to 1 to highlight shell expansions inside ${} parameter expansion.");
226
227 DefineProperty("lexer.bash.styling.inside.heredoc", &OptionsBash::stylingInsideHeredoc,
228 "Set this property to 1 to highlight shell expansions inside here document.");
229
230 DefineProperty("lexer.bash.command.substitution", &OptionsBash::commandSubstitution,
231 "Set how to highlight $() command substitution. "
232 "0 (the default) highlighted as backticks. "
233 "1 highlighted inside. "
234 "2 highlighted inside with extra scope tracking.");
235
236 DefineProperty("lexer.bash.nested.backticks", &OptionsBash::nestedBackticks,
237 "Set this property to 0 to disable nested backquoted command substitution.");
238
239 DefineProperty("lexer.bash.special.parameter", &OptionsBash::specialParameter,
240 "Set shell (default is Bash) special parameters.");
241
242 DefineWordListSets(bashWordListDesc);
243 }
244 };
245
246 class QuoteCls { // Class to manage quote pairs (simplified vs LexPerl)
247 public:
248 int Count = 0;
249 int Up = '\0';
250 int Down = '\0';
251 QuoteStyle Style = QuoteStyle::Literal;
252 int Outer = SCE_SH_DEFAULT;
253 CmdState State = CmdState::Body;
254 void Clear() noexcept {
255 Count = 0;
256 Up = '\0';
257 Down = '\0';
258 Style = QuoteStyle::Literal;
259 Outer = SCE_SH_DEFAULT;
260 State = CmdState::Body;
261 }
262 void Start(int u, QuoteStyle s, int outer, CmdState state) noexcept {
263 Count = 1;
264 Up = u;
265 Down = opposite(Up);
266 Style = s;
267 Outer = outer;
268 State = state;
269 }
270 };
271
272 class QuoteStackCls { // Class to manage quote pairs that nest
273 public:
274 int Depth = 0;
275 int State = SCE_SH_DEFAULT;
276 bool lineContinuation = false;
277 bool nestedBackticks = false;
278 CommandSubstitution commandSubstitution = CommandSubstitution::Backtick;
279 int insideCommand = 0;
280 unsigned backtickLevel = 0;
281 QuoteCls Current;
282 QuoteCls Stack[BASH_QUOTE_STACK_MAX];
283 const CharacterSet &setParamStart;
284 QuoteStackCls(const CharacterSet &setParamStart_) noexcept : setParamStart{setParamStart_} {}
285 [[nodiscard]] bool Empty() const noexcept {
286 return Current.Up == '\0';
287 }
288 void Start(int u, QuoteStyle s, int outer, CmdState state) noexcept {
289 if (Empty()) {
290 Current.Start(u, s, outer, state);
291 if (s == QuoteStyle::Backtick) {
292 ++backtickLevel;
293 }
294 } else {
295 Push(u, s, outer, state);
296 }
297 }
298 void Push(int u, QuoteStyle s, int outer, CmdState state) noexcept {
299 if (Depth >= BASH_QUOTE_STACK_MAX) {
300 return;
301 }
302 Stack[Depth] = Current;
303 Depth++;
304 Current.Start(u, s, outer, state);
305 if (s == QuoteStyle::Backtick) {
306 ++backtickLevel;
307 }
308 }
309 void Pop() noexcept {
310 if (Depth == 0) {
311 Clear();
312 return;
313 }
314 if (backtickLevel != 0 && Current.Style == QuoteStyle::Backtick) {
315 --backtickLevel;
316 }
317 if (insideCommand != 0 && Current.Style == QuoteStyle::CommandInside) {
318 insideCommand = 0;
319 for (int i = 0; i < Depth; i++) {
320 if (Stack[i].Style == QuoteStyle::CommandInside) {
321 insideCommand = commandSubstitutionFlag;
322 break;
323 }
324 }
325 }
326 Depth--;
327 Current = Stack[Depth];
328 }
329 void Clear() noexcept {
330 Depth = 0;
331 State = SCE_SH_DEFAULT;
332 insideCommand = 0;
333 backtickLevel = 0;
334 Current.Clear();
335 }
336 bool CountDown(StyleContext &sc, CmdState &cmdState) {
337 Current.Count--;
338 while (Current.Count > 0 && sc.chNext == Current.Down) {
339 Current.Count--;
340 sc.Forward();
341 }
342 if (Current.Count == 0) {
343 cmdState = Current.State;
344 const int outer = Current.Outer;
345 Pop();
346 sc.ForwardSetState(outer | insideCommand);
347 return true;
348 }
349 return false;
350 }
351 void Expand(StyleContext &sc, CmdState &cmdState, bool stylingInside) {
352 const CmdState current = cmdState;
353 const int state = sc.state;
354 QuoteStyle style = QuoteStyle::Literal;
355 State = state;
356 sc.SetState(SCE_SH_SCALAR);
357 sc.Forward();
358 if (sc.ch == '{') {
359 style = QuoteStyle::Parameter;
360 sc.ChangeState(SCE_SH_PARAM);
361 } else if (sc.ch == '\'') {
362 style = QuoteStyle::CString;
363 sc.ChangeState(SCE_SH_STRING);
364 } else if (sc.ch == '"') {
365 style = QuoteStyle::LString;
366 sc.ChangeState(SCE_SH_STRING);
367 } else if (sc.ch == '(' || sc.ch == '[') {
368 if (sc.ch == '[' || sc.chNext == '(') {
369 style = QuoteStyle::Arithmetic;
370 cmdState = CmdState::Arithmetic;
371 sc.ChangeState(SCE_SH_OPERATOR);
372 } else {
373 if (stylingInside && commandSubstitution >= CommandSubstitution::Inside) {
374 style = QuoteStyle::CommandInside;
375 cmdState = CmdState::Delimiter;
376 sc.ChangeState(SCE_SH_OPERATOR);
377 if (commandSubstitution == CommandSubstitution::InsideTrack) {
378 insideCommand = commandSubstitutionFlag;
379 }
380 } else {
381 style = QuoteStyle::Command;
382 sc.ChangeState(SCE_SH_BACKTICKS);
383 }
384 }
385 } else {
386 // scalar has no delimiter pair
387 if (!setParamStart.Contains(sc.ch)) {
388 stylingInside = false; // not scalar
389 }
390 }
391 if (!stylingInside) {
392 sc.ChangeState(state);
393 } else {
394 sc.ChangeState(sc.state | insideCommand);
395 }
396 if (style != QuoteStyle::Literal) {
397 Start(sc.ch, style, state, current);
398 sc.Forward();
399 }
400 }
401 void Escape(StyleContext &sc) {
402 unsigned count = 1;
403 while (sc.chNext == '\\') {
404 ++count;
405 sc.Forward();
406 }
407 bool escaped = count & 1U; // odd backslash escape next character
408 if (escaped && (sc.chNext == '\r' || sc.chNext == '\n')) {
409 lineContinuation = true;
410 if (sc.state == SCE_SH_IDENTIFIER) {
411 sc.SetState(SCE_SH_OPERATOR | insideCommand);
412 }
413 return;
414 }
415 if (backtickLevel > 0 && nestedBackticks) {
416 /*
417 for $k$ level substitution with $N$ backslashes:
418 * when $N/2^k$ is odd, following dollar is escaped.
419 * when $(N - 1)/2^k$ is even, following quote is escaped.
420 * when $N = n\times 2^{k + 1} - 1$, following backtick is escaped.
421 * when $N = n\times 2^{k + 1} + 2^k - 1$, following backtick starts inner substitution.
422 * when $N = m\times 2^k + 2^{k - 1} - 1$ and $k > 1$, following backtick ends current substitution.
423 */
424 if (sc.chNext == '$') {
425 escaped = (count >> backtickLevel) & 1U;
426 } else if (sc.chNext == '\"' || sc.chNext == '\'') {
427 escaped = (((count - 1) >> backtickLevel) & 1U) == 0;
428 } else if (sc.chNext == '`' && escaped) {
429 unsigned mask = 1U << (backtickLevel + 1);
430 count += 1;
431 escaped = (count & (mask - 1)) == 0;
432 if (!escaped) {
433 unsigned remain = count - (mask >> 1U);
434 if (static_cast<int>(remain) >= 0 && (remain & (mask - 1)) == 0) {
435 escaped = true;
436 ++backtickLevel;
437 } else if (backtickLevel > 1) {
438 mask >>= 1U;
439 remain = count - (mask >> 1U);
440 if (static_cast<int>(remain) >= 0 && (remain & (mask - 1)) == 0) {
441 escaped = true;
442 --backtickLevel;
443 }
444 }
445 }
446 }
447 }
448 if (escaped) {
449 sc.Forward();
450 }
451 }
452 };
453
454 const char styleSubable[] = { SCE_SH_IDENTIFIER, SCE_SH_SCALAR, 0 };
455
456 const LexicalClass lexicalClasses[] = {
457 // Lexer Bash SCLEX_BASH SCE_SH_:
458 0, "SCE_SH_DEFAULT", "default", "White space",
459 1, "SCE_SH_ERROR", "error", "Error",
460 2, "SCE_SH_COMMENTLINE", "comment line", "Line comment: #",
461 3, "SCE_SH_NUMBER", "literal numeric", "Number",
462 4, "SCE_SH_WORD", "keyword", "Keyword",
463 5, "SCE_SH_STRING", "literal string", "String",
464 6, "SCE_SH_CHARACTER", "literal string", "Single quoted string",
465 7, "SCE_SH_OPERATOR", "operator", "Operators",
466 8, "SCE_SH_IDENTIFIER", "identifier", "Identifiers",
467 9, "SCE_SH_SCALAR", "identifier", "Scalar variable",
468 10, "SCE_SH_PARAM", "identifier", "Parameter",
469 11, "SCE_SH_BACKTICKS", "literal string", "Backtick quoted command",
470 12, "SCE_SH_HERE_DELIM", "operator", "Heredoc delimiter",
471 13, "SCE_SH_HERE_Q", "here-doc literal string", "Heredoc quoted string",
472 };
473
474 }
475
476 class LexerBash final : public DefaultLexer {
477 WordList keywords;
478 WordList cmdDelimiter;
479 WordList bashStruct;
480 WordList bashStruct_in;
481 WordList testOperator;
482 OptionsBash options;
483 OptionSetBash osBash;
484 CharacterSet setParamStart;
485 enum { ssIdentifier, ssScalar };
486 SubStyles subStyles{styleSubable};
487 public:
488 LexerBash() :
489 DefaultLexer("bash", SCLEX_BASH, lexicalClasses, std::size(lexicalClasses)),
490 setParamStart(CharacterSet::setAlphaNum, "_" BASH_SPECIAL_PARAMETER) {
491 cmdDelimiter.Set("| || |& & && ; ;; ( ) { }");
492 bashStruct.Set("if elif fi while until else then do done esac eval");
493 bashStruct_in.Set("for case select");
494 testOperator.Set("eq ge gt le lt ne ef nt ot");
495 }
496 void SCI_METHOD Release() override {
497 delete this;
498 }
499 int SCI_METHOD Version() const override {
500 return lvRelease5;
501 }
502 const char * SCI_METHOD PropertyNames() override {
503 return osBash.PropertyNames();
504 }
505 int SCI_METHOD PropertyType(const char* name) override {
506 return osBash.PropertyType(name);
507 }
508 const char * SCI_METHOD DescribeProperty(const char *name) override {
509 return osBash.DescribeProperty(name);
510 }
511 Sci_Position SCI_METHOD PropertySet(const char *key, const char *val) override;
512 const char * SCI_METHOD PropertyGet(const char* key) override {
513 return osBash.PropertyGet(key);
514 }
515 const char * SCI_METHOD DescribeWordListSets() override {
516 return osBash.DescribeWordListSets();
517 }
518 Sci_Position SCI_METHOD WordListSet(int n, const char *wl) override;
519 void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
520 void SCI_METHOD Fold(Sci_PositionU startPos_, Sci_Position length, int initStyle, IDocument *pAccess) override;
521
522 void * SCI_METHOD PrivateCall(int, void *) override {
523 return nullptr;
524 }
525
526 int SCI_METHOD AllocateSubStyles(int styleBase, int numberStyles) override {
527 return subStyles.Allocate(styleBase, numberStyles);
528 }
529 int SCI_METHOD SubStylesStart(int styleBase) override {
530 return subStyles.Start(styleBase);
531 }
532 int SCI_METHOD SubStylesLength(int styleBase) override {
533 return subStyles.Length(styleBase);
534 }
535 int SCI_METHOD StyleFromSubStyle(int subStyle) override {
536 const int styleBase = subStyles.BaseStyle(subStyle);
537 return styleBase;
538 }
539 int SCI_METHOD PrimaryStyleFromStyle(int style) override {
540 return style;
541 }
542 void SCI_METHOD FreeSubStyles() override {
543 subStyles.Free();
544 }
545 void SCI_METHOD SetIdentifiers(int style, const char *identifiers) override {
546 subStyles.SetIdentifiers(style, identifiers);
547 }
548 int SCI_METHOD DistanceToSecondaryStyles() override {
549 return 0;
550 }
551 const char *SCI_METHOD GetSubStyleBases() override {
552 return styleSubable;
553 }
554
555 bool IsTestOperator(const char *s, const CharacterSet &setSingleCharOp) const noexcept {
556 return (s[2] == '\0' && setSingleCharOp.Contains(s[1]))
557 || testOperator.InList(s + 1);
558 }
559
560 static ILexer5 *LexerFactoryBash() {
561 return new LexerBash();
562 }
563 };
564
565 Sci_Position SCI_METHOD LexerBash::PropertySet(const char *key, const char *val) {
566 if (osBash.PropertySet(&options, key, val)) {
567 if (strcmp(key, "lexer.bash.special.parameter") == 0) {
568 setParamStart = CharacterSet(CharacterSet::setAlphaNum, "_");
569 setParamStart.AddString(options.specialParameter.empty() ? BASH_SPECIAL_PARAMETER : options.specialParameter.c_str());
570 }
571 return 0;
572 }
573 return -1;
574 }
575
576 Sci_Position SCI_METHOD LexerBash::WordListSet(int n, const char *wl) {
577 WordList *wordListN = nullptr;
578 switch (n) {
579 case 0:
580 wordListN = &keywords;
581 break;
582 }
583 Sci_Position firstModification = -1;
584 if (wordListN) {
585 if (wordListN->Set(wl)) {
586 firstModification = 0;
587 }
588 }
589 return firstModification;
590 }
591
592 void SCI_METHOD LexerBash::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) {
593 const CharacterSet setWordStart(CharacterSet::setAlpha, "_");
594 // note that [+-] are often parts of identifiers in shell scripts
595 const CharacterSet setWord(CharacterSet::setAlphaNum, "._+-");
596 CharacterSet setMetaCharacter(CharacterSet::setNone, "|&;()<> \t\r\n");
597 setMetaCharacter.Add(0);
598 const CharacterSet setBashOperator(CharacterSet::setNone, "^&%()-+=|{}[]:;>,*/<?!.~@");
599 const CharacterSet setSingleCharOp(CharacterSet::setNone, "rwxoRWXOezsfdlpSbctugkTBMACahGLNn");
600 const CharacterSet setParam(CharacterSet::setAlphaNum, "_");
601 const CharacterSet setHereDoc(CharacterSet::setAlpha, "_\\-+!%*,./:?@[]^`{}~");
602 const CharacterSet setHereDoc2(CharacterSet::setAlphaNum, "_-+!%*,./:=?@[]^`{}~");
603 const CharacterSet setLeftShift(CharacterSet::setDigits, "$");
604
605 class HereDocCls { // Class to manage HERE document elements
606 public:
607 int State = 0; // 0: '<<' encountered
608 // 1: collect the delimiter
609 // 2: here doc text (lines after the delimiter)
610 int Quote = '\0'; // the char after '<<'
611 bool Quoted = false; // true if Quote in ('\'','"','`')
612 bool Escaped = false; // backslash in delimiter, common in configure script
613 bool Indent = false; // indented delimiter (for <<-)
614 int BackslashCount = 0;
615 int DelimiterLength = 0; // strlen(Delimiter)
616 char Delimiter[HERE_DELIM_MAX]{}; // the Delimiter
617 HereDocCls() noexcept = default;
618 void Append(int ch) {
619 Delimiter[DelimiterLength++] = static_cast<char>(ch);
620 Delimiter[DelimiterLength] = '\0';
621 }
622 };
623 HereDocCls HereDoc;
624
625 QuoteStackCls QuoteStack(setParamStart);
626 QuoteStack.nestedBackticks = options.nestedBackticks;
627 QuoteStack.commandSubstitution = options.commandSubstitution;
628
629 const WordClassifier &classifierIdentifiers = subStyles.Classifier(SCE_SH_IDENTIFIER);
630 const WordClassifier &classifierScalars = subStyles.Classifier(SCE_SH_SCALAR);
631
632 int numBase = 0;
633 int digit = 0;
634 const Sci_PositionU endPos = startPos + length;
635 CmdState cmdState = CmdState::Start;
636 LexAccessor styler(pAccess);
637
638 // Always backtracks to the start of a line that is not a continuation
639 // of the previous line (i.e. start of a bash command segment)
640 Sci_Position ln = styler.GetLine(startPos);
641 if (ln > 0 && startPos == static_cast<Sci_PositionU>(styler.LineStart(ln)))
642 ln--;
643 for (;;) {
644 startPos = styler.LineStart(ln);
645 if (ln == 0 || styler.GetLineState(ln) == static_cast<int>(CmdState::Start))
646 break;
647 ln--;
648 }
649 initStyle = SCE_SH_DEFAULT;
650
651 StyleContext sc(startPos, endPos - startPos, initStyle, styler);
652
653 while (sc.More()) {
654
655 // handle line continuation, updates per-line stored state
656 if (sc.atLineStart) {
657 CmdState state = CmdState::Body; // force backtrack while retaining cmdState
658 if (!StyleForceBacktrack(MaskCommand(sc.state))) {
659 // retain last line's state
660 // arithmetic expression and double bracket test can span multiline without line continuation
661 if (!QuoteStack.lineContinuation && !AnyOf(cmdState, CmdState::DoubleBracket, CmdState::Arithmetic)) {
662 cmdState = CmdState::Start;
663 }
664 if (QuoteStack.Empty()) { // force backtrack when nesting
665 state = cmdState;
666 }
667 }
668 QuoteStack.lineContinuation = false;
669 styler.SetLineState(sc.currentLine, static_cast<int>(state));
670 }
671
672 // controls change of cmdState at the end of a non-whitespace element
673 // states Body|Test|Arithmetic persist until the end of a command segment
674 // state Word persist, but ends with 'in' or 'do' construct keywords
675 CmdState cmdStateNew = CmdState::Body;
676 if (cmdState >= CmdState::Word && cmdState <= CmdState::Arithmetic)
677 cmdStateNew = cmdState;
678 const int stylePrev = MaskCommand(sc.state);
679 const int insideCommand = QuoteStack.insideCommand;
680
681 // Determine if the current state should terminate.
682 switch (MaskCommand(sc.state)) {
683 case SCE_SH_OPERATOR:
684 sc.SetState(SCE_SH_DEFAULT | insideCommand);
685 if (cmdState == CmdState::Delimiter) // if command delimiter, start new command
686 cmdStateNew = CmdState::Start;
687 else if (sc.chPrev == '\\') // propagate command state if line continued
688 cmdStateNew = cmdState;
689 break;
690 case SCE_SH_WORD:
691 // "." never used in Bash variable names but used in file names
692 if (!setWord.Contains(sc.ch) || sc.Match('+', '=') || sc.Match('.', '.')) {
693 char s[500];
694 sc.GetCurrent(s, sizeof(s));
695 int identifierStyle = SCE_SH_IDENTIFIER | insideCommand;
696 const int subStyle = classifierIdentifiers.ValueFor(s);
697 if (subStyle >= 0) {
698 identifierStyle = subStyle | insideCommand;
699 }
700 // allow keywords ending in a whitespace, meta character or command delimiter
701 char s2[10];
702 s2[0] = static_cast<char>(sc.ch);
703 s2[1] = '\0';
704 const bool keywordEnds = IsASpace(sc.ch) || setMetaCharacter.Contains(sc.ch) || cmdDelimiter.InList(s2);
705 // 'in' or 'do' may be construct keywords
706 if (cmdState == CmdState::Word) {
707 if (strcmp(s, "in") == 0 && keywordEnds)
708 cmdStateNew = CmdState::Body;
709 else if (strcmp(s, "do") == 0 && keywordEnds)
710 cmdStateNew = CmdState::Start;
711 else
712 sc.ChangeState(identifierStyle);
713 sc.SetState(SCE_SH_DEFAULT | insideCommand);
714 break;
715 }
716 // a 'test' keyword starts a test expression
717 if (strcmp(s, "test") == 0) {
718 if (cmdState == CmdState::Start && keywordEnds) {
719 cmdStateNew = CmdState::Test;
720 } else
721 sc.ChangeState(identifierStyle);
722 }
723 // detect bash construct keywords
724 else if (bashStruct.InList(s)) {
725 if (cmdState == CmdState::Start && keywordEnds)
726 cmdStateNew = CmdState::Start;
727 else
728 sc.ChangeState(identifierStyle);
729 }
730 // 'for'|'case'|'select' needs 'in'|'do' to be highlighted later
731 else if (bashStruct_in.InList(s)) {
732 if (cmdState == CmdState::Start && keywordEnds)
733 cmdStateNew = CmdState::Word;
734 else
735 sc.ChangeState(identifierStyle);
736 }
737 // disambiguate option items and file test operators
738 else if (s[0] == '-') {
739 if (!AnyOf(cmdState, CmdState::Test, CmdState::SingleBracket, CmdState::DoubleBracket)
740 || !keywordEnds || !IsTestOperator(s, setSingleCharOp))
741 sc.ChangeState(identifierStyle);
742 }
743 // disambiguate keywords and identifiers
744 else if (cmdState != CmdState::Start
745 || !(keywords.InList(s) && keywordEnds)) {
746 sc.ChangeState(identifierStyle);
747 }
748 sc.SetState(SCE_SH_DEFAULT | insideCommand);
749 }
750 break;
751 case SCE_SH_IDENTIFIER:
752 if (!setWord.Contains(sc.ch) ||
753 (cmdState == CmdState::Arithmetic && !setWordStart.Contains(sc.ch))) {
754 char s[500];
755 sc.GetCurrent(s, sizeof(s));
756 const int subStyle = classifierIdentifiers.ValueFor(s);
757 if (subStyle >= 0) {
758 sc.ChangeState(subStyle | insideCommand);
759 }
760 sc.SetState(SCE_SH_DEFAULT | insideCommand);
761 }
762 break;
763 case SCE_SH_NUMBER:
764 digit = translateBashDigit(sc.ch);
765 if (numBase == BASH_BASE_DECIMAL) {
766 if (sc.ch == '#') {
767 char s[10];
768 sc.GetCurrent(s, sizeof(s));
769 numBase = getBashNumberBase(s);
770 if (numBase != BASH_BASE_ERROR)
771 break;
772 } else if (IsADigit(sc.ch))
773 break;
774 } else if (numBase == BASH_BASE_HEX) {
775 if (IsADigit(sc.ch, 16))
776 break;
777 #ifdef PEDANTIC_OCTAL
778 } else if (numBase == BASH_BASE_OCTAL ||
779 numBase == BASH_BASE_OCTAL_ERROR) {
780 if (digit <= 7)
781 break;
782 if (digit <= 9) {
783 numBase = BASH_BASE_OCTAL_ERROR;
784 break;
785 }
786 #endif
787 } else if (numBase == BASH_BASE_ERROR) {
788 if (digit <= 9)
789 break;
790 } else { // DD#DDDD number style handling
791 if (digit != BASH_BASE_ERROR) {
792 if (numBase <= 36) {
793 // case-insensitive if base<=36
794 if (digit >= 36) digit -= 26;
795 }
796 if (digit < numBase)
797 break;
798 if (digit <= 9) {
799 numBase = BASH_BASE_ERROR;
800 break;
801 }
802 }
803 }
804 // fallthrough when number is at an end or error
805 if (numBase == BASH_BASE_ERROR
806 #ifdef PEDANTIC_OCTAL
807 || numBase == BASH_BASE_OCTAL_ERROR
808 #endif
809 ) {
810 sc.ChangeState(SCE_SH_ERROR | insideCommand);
811 } else if (digit < 62 || digit == 63 || (cmdState != CmdState::Arithmetic &&
812 (sc.ch == '-' || (sc.ch == '.' && sc.chNext != '.')))) {
813 // current character is alpha numeric, underscore, hyphen or dot
814 sc.ChangeState(SCE_SH_IDENTIFIER | insideCommand);
815 break;
816 }
817 sc.SetState(SCE_SH_DEFAULT | insideCommand);
818 break;
819 case SCE_SH_COMMENTLINE:
820 if (sc.MatchLineEnd()) {
821 sc.SetState(SCE_SH_DEFAULT | insideCommand);
822 }
823 break;
824 case SCE_SH_HERE_DELIM:
825 // From Bash info:
826 // ---------------
827 // Specifier format is: <<[-]WORD
828 // Optional '-' is for removal of leading tabs from here-doc.
829 // Whitespace acceptable after <<[-] operator
830 //
831 if (HereDoc.State == 0) { // '<<' encountered
832 HereDoc.Quote = sc.chNext;
833 HereDoc.Quoted = false;
834 HereDoc.Escaped = false;
835 HereDoc.BackslashCount = 0;
836 HereDoc.DelimiterLength = 0;
837 HereDoc.Delimiter[HereDoc.DelimiterLength] = '\0';
838 if (sc.chNext == '\'' || sc.chNext == '\"') { // a quoted here-doc delimiter (' or ")
839 sc.Forward();
840 HereDoc.Quoted = true;
841 HereDoc.State = 1;
842 } else if (setHereDoc.Contains(sc.chNext) ||
843 (sc.chNext == '=' && cmdState != CmdState::Arithmetic)) {
844 // an unquoted here-doc delimiter, no special handling
845 HereDoc.State = 1;
846 } else if (sc.chNext == '<') { // HERE string <<<
847 sc.Forward();
848 sc.ForwardSetState(SCE_SH_DEFAULT | insideCommand);
849 } else if (IsASpace(sc.chNext)) {
850 // eat whitespace
851 } else if (setLeftShift.Contains(sc.chNext) ||
852 (sc.chNext == '=' && cmdState == CmdState::Arithmetic)) {
853 // left shift <<$var or <<= cases
854 sc.ChangeState(SCE_SH_OPERATOR | insideCommand);
855 sc.ForwardSetState(SCE_SH_DEFAULT | insideCommand);
856 } else {
857 // symbols terminates; deprecated zero-length delimiter
858 HereDoc.State = 1;
859 }
860 } else if (HereDoc.State == 1) { // collect the delimiter
861 // * if single quoted, there's no escape
862 // * if double quoted, there are \\ and \" escapes
863 if (HereDoc.Quoted && sc.ch == HereDoc.Quote && (HereDoc.BackslashCount & 1) == 0) { // closing quote => end of delimiter
864 sc.ForwardSetState(SCE_SH_DEFAULT | insideCommand);
865 } else if (sc.ch == '\\' && HereDoc.Quote != '\'') {
866 HereDoc.Escaped = true;
867 HereDoc.BackslashCount += 1;
868 if ((HereDoc.BackslashCount & 1) == 0 || (HereDoc.Quoted && !AnyOf(sc.chNext, '\"', '\\'))) {
869 // in quoted prefixes only \ and the quote eat the escape
870 HereDoc.Append(sc.ch);
871 } else {
872 // skip escape prefix
873 }
874 } else if (HereDoc.Quoted || setHereDoc2.Contains(sc.ch) || (sc.ch > 32 && sc.ch < 127 && (HereDoc.BackslashCount & 1) != 0)) {
875 HereDoc.BackslashCount = 0;
876 HereDoc.Append(sc.ch);
877 } else {
878 sc.SetState(SCE_SH_DEFAULT | insideCommand);
879 }
880 if (HereDoc.DelimiterLength >= HERE_DELIM_MAX - 1) { // force blowup
881 sc.SetState(SCE_SH_ERROR | insideCommand);
882 HereDoc.State = 0;
883 }
884 }
885 break;
886 case SCE_SH_SCALAR: // variable names
887 if (!setParam.Contains(sc.ch)) {
888 char s[500];
889 sc.GetCurrent(s, sizeof(s));
890 const int subStyle = classifierScalars.ValueFor(&s[1]); // skip the $
891 if (subStyle >= 0) {
892 sc.ChangeState(subStyle | insideCommand);
893 }
894 if (sc.LengthCurrent() == 1) {
895 // Special variable
896 sc.Forward();
897 }
898 sc.SetState(QuoteStack.State | insideCommand);
899 continue;
900 }
901 break;
902 case SCE_SH_HERE_Q:
903 // HereDoc.State == 2
904 if (sc.atLineStart && QuoteStack.Current.Style == QuoteStyle::HereDoc) {
905 sc.SetState(SCE_SH_HERE_Q | insideCommand);
906 if (HereDoc.Indent) { // tabulation prefix
907 while (sc.ch == '\t') {
908 sc.Forward();
909 }
910 }
911 if ((static_cast<Sci_Position>(sc.currentPos + HereDoc.DelimiterLength) == sc.lineEnd) &&
912 (HereDoc.DelimiterLength == 0 || sc.Match(HereDoc.Delimiter))) {
913 if (HereDoc.DelimiterLength != 0) {
914 sc.SetState(SCE_SH_HERE_DELIM | insideCommand);
915 while (!sc.MatchLineEnd()) {
916 sc.Forward();
917 }
918 }
919 QuoteStack.Pop();
920 sc.SetState(SCE_SH_DEFAULT | QuoteStack.insideCommand);
921 break;
922 }
923 }
924 if (HereDoc.Quoted || HereDoc.Escaped) {
925 break;
926 }
927 // fall through to handle nested shell expansions
928 [[fallthrough]];
929 case SCE_SH_STRING: // delimited styles, can nest
930 case SCE_SH_PARAM: // ${parameter}
931 case SCE_SH_BACKTICKS:
932 if (sc.ch == '\\') {
933 if (QuoteStack.Current.Style != QuoteStyle::Literal)
934 QuoteStack.Escape(sc);
935 } else if (sc.ch == QuoteStack.Current.Down) {
936 if (QuoteStack.CountDown(sc, cmdState)) {
937 continue;
938 }
939 } else if (sc.ch == QuoteStack.Current.Up) {
940 if (QuoteStack.Current.Style != QuoteStyle::Parameter) {
941 QuoteStack.Current.Count++;
942 }
943 } else {
944 if (QuoteStack.Current.Style == QuoteStyle::String ||
945 QuoteStack.Current.Style == QuoteStyle::HereDoc ||
946 QuoteStack.Current.Style == QuoteStyle::LString
947 ) { // do nesting for "string", $"locale-string", heredoc
948 const bool stylingInside = options.stylingInside(MaskCommand(sc.state));
949 if (sc.ch == '`') {
950 QuoteStack.Push(sc.ch, QuoteStyle::Backtick, sc.state, cmdState);
951 if (stylingInside) {
952 sc.SetState(SCE_SH_BACKTICKS | insideCommand);
953 }
954 } else if (sc.ch == '$' && !AnyOf(sc.chNext, '\"', '\'')) {
955 QuoteStack.Expand(sc, cmdState, stylingInside);
956 continue;
957 }
958 } else if (QuoteStack.Current.Style == QuoteStyle::Command ||
959 QuoteStack.Current.Style == QuoteStyle::Parameter ||
960 QuoteStack.Current.Style == QuoteStyle::Backtick
961 ) { // do nesting for $(command), `command`, ${parameter}
962 const bool stylingInside = options.stylingInside(MaskCommand(sc.state));
963 if (sc.ch == '\'') {
964 if (stylingInside) {
965 QuoteStack.State = sc.state;
966 sc.SetState(SCE_SH_CHARACTER | insideCommand);
967 } else {
968 QuoteStack.Push(sc.ch, QuoteStyle::Literal, sc.state, cmdState);
969 }
970 } else if (sc.ch == '\"') {
971 QuoteStack.Push(sc.ch, QuoteStyle::String, sc.state, cmdState);
972 if (stylingInside) {
973 sc.SetState(SCE_SH_STRING | insideCommand);
974 }
975 } else if (sc.ch == '`') {
976 QuoteStack.Push(sc.ch, QuoteStyle::Backtick, sc.state, cmdState);
977 if (stylingInside) {
978 sc.SetState(SCE_SH_BACKTICKS | insideCommand);
979 }
980 } else if (sc.ch == '$') {
981 QuoteStack.Expand(sc, cmdState, stylingInside);
982 continue;
983 }
984 }
985 }
986 break;
987 case SCE_SH_CHARACTER: // singly-quoted strings
988 if (sc.ch == '\'') {
989 sc.ForwardSetState(QuoteStack.State | insideCommand);
990 continue;
991 }
992 break;
993 }
994
995 // Must check end of HereDoc state 1 before default state is handled
996 if (HereDoc.State == 1 && sc.MatchLineEnd()) {
997 // Begin of here-doc (the line after the here-doc delimiter):
998 // Lexically, the here-doc starts from the next line after the >>, but the
999 // first line of here-doc seem to follow the style of the last EOL sequence
1000 HereDoc.State = 2;
1001 if (HereDoc.Quoted) {
1002 if (MaskCommand(sc.state) == SCE_SH_HERE_DELIM) {
1003 // Missing quote at end of string! Syntax error in bash 4.3
1004 // Mark this bit as an error, do not colour any here-doc
1005 sc.ChangeState(SCE_SH_ERROR | insideCommand);
1006 sc.SetState(SCE_SH_DEFAULT | insideCommand);
1007 } else {
1008 // HereDoc.Quote always == '\''
1009 sc.SetState(SCE_SH_HERE_Q | insideCommand);
1010 QuoteStack.Start(-1, QuoteStyle::HereDoc, SCE_SH_DEFAULT, cmdState);
1011 }
1012 } else if (HereDoc.DelimiterLength == 0) {
1013 // no delimiter, illegal (but '' and "" are legal)
1014 sc.ChangeState(SCE_SH_ERROR | insideCommand);
1015 sc.SetState(SCE_SH_DEFAULT | insideCommand);
1016 } else {
1017 sc.SetState(SCE_SH_HERE_Q | insideCommand);
1018 QuoteStack.Start(-1, QuoteStyle::HereDoc, SCE_SH_DEFAULT, cmdState);
1019 }
1020 }
1021
1022 // update cmdState about the current command segment
1023 if (stylePrev != SCE_SH_DEFAULT && MaskCommand(sc.state) == SCE_SH_DEFAULT) {
1024 cmdState = cmdStateNew;
1025 }
1026 // Determine if a new state should be entered.
1027 if (MaskCommand(sc.state) == SCE_SH_DEFAULT) {
1028 if (sc.ch == '\\') {
1029 // Bash can escape any non-newline as a literal
1030 sc.SetState(SCE_SH_IDENTIFIER | insideCommand);
1031 QuoteStack.Escape(sc);
1032 } else if (IsADigit(sc.ch)) {
1033 sc.SetState(SCE_SH_NUMBER | insideCommand);
1034 numBase = BASH_BASE_DECIMAL;
1035 if (sc.ch == '0') { // hex,octal
1036 if (sc.chNext == 'x' || sc.chNext == 'X') {
1037 numBase = BASH_BASE_HEX;
1038 sc.Forward();
1039 } else if (IsADigit(sc.chNext)) {
1040 #ifdef PEDANTIC_OCTAL
1041 numBase = BASH_BASE_OCTAL;
1042 #endif
1043 }
1044 }
1045 } else if (setWordStart.Contains(sc.ch)) {
1046 sc.SetState(((cmdState == CmdState::Arithmetic)? SCE_SH_IDENTIFIER : SCE_SH_WORD) | insideCommand);
1047 } else if (sc.ch == '#') {
1048 if (stylePrev != SCE_SH_WORD && stylePrev != SCE_SH_IDENTIFIER &&
1049 (sc.currentPos == 0 || setMetaCharacter.Contains(sc.chPrev))) {
1050 sc.SetState(SCE_SH_COMMENTLINE | insideCommand);
1051 } else {
1052 sc.SetState(SCE_SH_WORD | insideCommand);
1053 }
1054 // handle some zsh features within arithmetic expressions only
1055 if (cmdState == CmdState::Arithmetic) {
1056 if (sc.chPrev == '[') { // [#8] [##8] output digit setting
1057 sc.SetState(SCE_SH_WORD | insideCommand);
1058 if (sc.chNext == '#') {
1059 sc.Forward();
1060 }
1061 } else if (sc.Match("##^") && IsUpperCase(sc.GetRelative(3))) { // ##^A
1062 sc.SetState(SCE_SH_IDENTIFIER | insideCommand);
1063 sc.Forward(3);
1064 } else if (sc.chNext == '#' && !IsASpace(sc.GetRelative(2))) { // ##a
1065 sc.SetState(SCE_SH_IDENTIFIER | insideCommand);
1066 sc.Forward(2);
1067 } else if (setWordStart.Contains(sc.chNext)) { // #name
1068 sc.SetState(SCE_SH_IDENTIFIER | insideCommand);
1069 }
1070 }
1071 } else if (sc.ch == '\"') {
1072 sc.SetState(SCE_SH_STRING | insideCommand);
1073 QuoteStack.Start(sc.ch, QuoteStyle::String, SCE_SH_DEFAULT, cmdState);
1074 } else if (sc.ch == '\'') {
1075 QuoteStack.State = SCE_SH_DEFAULT;
1076 sc.SetState(SCE_SH_CHARACTER | insideCommand);
1077 } else if (sc.ch == '`') {
1078 sc.SetState(SCE_SH_BACKTICKS | insideCommand);
1079 QuoteStack.Start(sc.ch, QuoteStyle::Backtick, SCE_SH_DEFAULT, cmdState);
1080 } else if (sc.ch == '$') {
1081 QuoteStack.Expand(sc, cmdState, true);
1082 continue;
1083 } else if (cmdState != CmdState::Arithmetic && sc.Match('<', '<')) {
1084 sc.SetState(SCE_SH_HERE_DELIM | insideCommand);
1085 HereDoc.State = 0;
1086 if (sc.GetRelative(2) == '-') { // <<- indent case
1087 HereDoc.Indent = true;
1088 sc.Forward();
1089 } else {
1090 HereDoc.Indent = false;
1091 }
1092 } else if (sc.ch == '-' && // test operator or short and long option
1093 cmdState != CmdState::Arithmetic &&
1094 sc.chPrev != '~' && !IsADigit(sc.chNext)) {
1095 if (IsASpace(sc.chPrev) || setMetaCharacter.Contains(sc.chPrev)) {
1096 sc.SetState(SCE_SH_WORD | insideCommand);
1097 } else {
1098 sc.SetState(SCE_SH_IDENTIFIER | insideCommand);
1099 }
1100 } else if (setBashOperator.Contains(sc.ch)) {
1101 bool isCmdDelim = false;
1102 sc.SetState(SCE_SH_OPERATOR | insideCommand);
1103 // arithmetic expansion and command substitution
1104 if (QuoteStack.Current.Style == QuoteStyle::Arithmetic || QuoteStack.Current.Style == QuoteStyle::CommandInside) {
1105 if (sc.ch == QuoteStack.Current.Down) {
1106 if (QuoteStack.CountDown(sc, cmdState)) {
1107 continue;
1108 }
1109 } else if (sc.ch == QuoteStack.Current.Up) {
1110 QuoteStack.Current.Count++;
1111 }
1112 }
1113 // globs have no whitespace, do not appear in arithmetic expressions
1114 if (cmdState != CmdState::Arithmetic && sc.ch == '(' && sc.chNext != '(') {
1115 const int i = GlobScan(sc);
1116 if (i > 1) {
1117 sc.SetState(SCE_SH_IDENTIFIER | insideCommand);
1118 sc.Forward(i + 1);
1119 continue;
1120 }
1121 }
1122 // handle opening delimiters for test/arithmetic expressions - ((,[[,[
1123 if (cmdState == CmdState::Start
1124 || cmdState == CmdState::Body) {
1125 if (sc.Match('(', '(')) {
1126 cmdState = CmdState::Arithmetic;
1127 sc.Forward();
1128 } else if (sc.Match('[', '[') && IsASpace(sc.GetRelative(2))) {
1129 cmdState = CmdState::DoubleBracket;
1130 sc.Forward();
1131 } else if (sc.ch == '[' && IsASpace(sc.chNext)) {
1132 cmdState = CmdState::SingleBracket;
1133 }
1134 }
1135 // special state -- for ((x;y;z)) in ... looping
1136 if (cmdState == CmdState::Word && sc.Match('(', '(')) {
1137 cmdState = CmdState::Arithmetic;
1138 sc.Forward(2);
1139 continue;
1140 }
1141 // handle command delimiters in command Start|Body|Word state, also Test if 'test' or '[]'
1142 if (cmdState < CmdState::DoubleBracket) {
1143 char s[10];
1144 s[0] = static_cast<char>(sc.ch);
1145 if (setBashOperator.Contains(sc.chNext)) {
1146 s[1] = static_cast<char>(sc.chNext);
1147 s[2] = '\0';
1148 isCmdDelim = cmdDelimiter.InList(s);
1149 if (isCmdDelim)
1150 sc.Forward();
1151 }
1152 if (!isCmdDelim) {
1153 s[1] = '\0';
1154 isCmdDelim = cmdDelimiter.InList(s);
1155 }
1156 if (isCmdDelim) {
1157 cmdState = CmdState::Delimiter;
1158 sc.Forward();
1159 continue;
1160 }
1161 }
1162 // handle closing delimiters for test/arithmetic expressions - )),]],]
1163 if (cmdState == CmdState::Arithmetic && sc.Match(')', ')')) {
1164 cmdState = CmdState::Body;
1165 sc.Forward();
1166 } else if (sc.ch == ']' && IsASpace(sc.chPrev)) {
1167 if (cmdState == CmdState::SingleBracket) {
1168 cmdState = CmdState::Body;
1169 } else if (cmdState == CmdState::DoubleBracket && sc.chNext == ']') {
1170 cmdState = CmdState::Body;
1171 sc.Forward();
1172 }
1173 }
1174 }
1175 }// sc.state
1176
1177 sc.Forward();
1178 }
1179 sc.Complete();
1180 if (MaskCommand(sc.state) == SCE_SH_HERE_Q) {
1181 styler.ChangeLexerState(sc.currentPos, styler.Length());
1182 }
1183 sc.Complete();
1184 }
1185
1186 void SCI_METHOD LexerBash::Fold(Sci_PositionU startPos_, Sci_Position length, int initStyle, IDocument *pAccess) {
1187 if(!options.fold)
1188 return;
1189
1190 LexAccessor styler(pAccess);
1191
1192 Sci_Position startPos = startPos_;
1193 const Sci_Position endPos = startPos + length;
1194 int visibleChars = 0;
1195 Sci_Position lineCurrent = styler.GetLine(startPos);
1196 // Backtrack to previous line in case need to fix its fold status
1197 if (lineCurrent > 0) {
1198 lineCurrent--;
1199 startPos = styler.LineStart(lineCurrent);
1200 initStyle = (startPos > 0) ? styler.StyleIndexAt(startPos - 1) : 0;
1201 }
1202
1203 int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
1204 int levelCurrent = levelPrev;
1205 char chNext = styler[startPos];
1206 int styleNext = MaskCommand(styler.StyleIndexAt(startPos));
1207 int style = MaskCommand(initStyle);
1208 char word[8] = { '\0' }; // we're not interested in long words anyway
1209 size_t wordlen = 0;
1210 for (Sci_Position i = startPos; i < endPos; i++) {
1211 const char ch = chNext;
1212 chNext = styler.SafeGetCharAt(i + 1);
1213 const int stylePrev = style;
1214 style = styleNext;
1215 styleNext = MaskCommand(styler.StyleIndexAt(i + 1));
1216 const bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
1217 // Comment folding
1218 if (options.foldComment && atEOL && IsCommentLine(lineCurrent, styler))
1219 {
1220 if (!IsCommentLine(lineCurrent - 1, styler)
1221 && IsCommentLine(lineCurrent + 1, styler))
1222 levelCurrent++;
1223 else if (IsCommentLine(lineCurrent - 1, styler)
1224 && !IsCommentLine(lineCurrent + 1, styler))
1225 levelCurrent--;
1226 }
1227
1228 switch (style) {
1229 case SCE_SH_WORD:
1230 if ((wordlen + 1) < sizeof(word))
1231 word[wordlen++] = ch;
1232 if (styleNext != style) {
1233 word[wordlen] = '\0';
1234 wordlen = 0;
1235 if (InList(word, {"if", "case", "do"})) {
1236 levelCurrent++;
1237 } else if (InList(word, {"fi", "esac", "done"})) {
1238 levelCurrent--;
1239 }
1240 }
1241 break;
1242
1243 case SCE_SH_OPERATOR:
1244 if (ch == '{') {
1245 levelCurrent++;
1246 } else if (ch == '}') {
1247 levelCurrent--;
1248 }
1249 break;
1250
1251 // Here Document folding
1252 case SCE_SH_HERE_DELIM:
1253 if (stylePrev == SCE_SH_HERE_Q) {
1254 levelCurrent--;
1255 } else if (stylePrev != SCE_SH_HERE_DELIM) {
1256 if (ch == '<' && chNext == '<') {
1257 if (styler.SafeGetCharAt(i + 2) != '<') {
1258 levelCurrent++;
1259 }
1260 }
1261 }
1262 break;
1263 case SCE_SH_HERE_Q:
1264 if (styleNext == SCE_SH_DEFAULT) {
1265 levelCurrent--;
1266 }
1267 break;
1268 }
1269
1270 if (atEOL) {
1271 int lev = levelPrev;
1272 if (visibleChars == 0 && options.foldCompact)
1273 lev |= SC_FOLDLEVELWHITEFLAG;
1274 if ((levelCurrent > levelPrev) && (visibleChars > 0))
1275 lev |= SC_FOLDLEVELHEADERFLAG;
1276 if (lev != styler.LevelAt(lineCurrent)) {
1277 styler.SetLevel(lineCurrent, lev);
1278 }
1279 lineCurrent++;
1280 levelPrev = levelCurrent;
1281 visibleChars = 0;
1282 }
1283 if (!isspacechar(ch))
1284 visibleChars++;
1285 }
1286 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
1287 const int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
1288 styler.SetLevel(lineCurrent, levelPrev | flagsNext);
1289 }
1290
1291 extern const LexerModule lmBash(SCLEX_BASH, LexerBash::LexerFactoryBash, "bash", bashWordListDesc);