2009-04-24 23:35:41 +00:00
|
|
|
// Scintilla source code edit control
|
|
|
|
/** @file LexBash.cxx
|
|
|
|
** Lexer for Bash.
|
|
|
|
**/
|
2013-08-28 00:44:27 +00:00
|
|
|
// Copyright 2004-2012 by Neil Hodgson <neilh@scintilla.org>
|
2009-04-25 23:38:15 +00:00
|
|
|
// Adapted from LexPerl by Kein-Hong Man 2004
|
2009-04-24 23:35:41 +00:00
|
|
|
// The License.txt file describes the conditions under which this software may be distributed.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2010-08-21 23:59:56 +00:00
|
|
|
#include <assert.h>
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
#include "ILexer.h"
|
|
|
|
#include "Scintilla.h"
|
|
|
|
#include "SciLexer.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
#include "StringCopy.h"
|
2010-08-21 23:59:56 +00:00
|
|
|
#include "WordList.h"
|
|
|
|
#include "LexAccessor.h"
|
2009-04-25 23:38:15 +00:00
|
|
|
#include "StyleContext.h"
|
|
|
|
#include "CharacterSet.h"
|
2010-08-21 23:59:56 +00:00
|
|
|
#include "LexerModule.h"
|
2019-05-04 18:14:48 +00:00
|
|
|
#include "OptionSet.h"
|
|
|
|
#include "SubStyles.h"
|
|
|
|
#include "DefaultLexer.h"
|
2009-04-25 23:38:15 +00:00
|
|
|
|
|
|
|
using namespace Scintilla;
|
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
#define HERE_DELIM_MAX 256
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
// define this if you want 'invalid octals' to be marked as errors
|
|
|
|
// usually, this is not a good idea, permissive lexing is better
|
|
|
|
#undef PEDANTIC_OCTAL
|
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
#define BASH_BASE_ERROR 65
|
|
|
|
#define BASH_BASE_DECIMAL 66
|
|
|
|
#define BASH_BASE_HEX 67
|
2009-04-24 23:35:41 +00:00
|
|
|
#ifdef PEDANTIC_OCTAL
|
2010-08-21 23:59:56 +00:00
|
|
|
#define BASH_BASE_OCTAL 68
|
|
|
|
#define BASH_BASE_OCTAL_ERROR 69
|
2009-04-24 23:35:41 +00:00
|
|
|
#endif
|
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
// state constants for parts of a bash command segment
|
|
|
|
#define BASH_CMD_BODY 0
|
|
|
|
#define BASH_CMD_START 1
|
|
|
|
#define BASH_CMD_WORD 2
|
|
|
|
#define BASH_CMD_TEST 3
|
|
|
|
#define BASH_CMD_ARITH 4
|
|
|
|
#define BASH_CMD_DELIM 5
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
// state constants for nested delimiter pairs, used by
|
|
|
|
// SCE_SH_STRING and SCE_SH_BACKTICKS processing
|
|
|
|
#define BASH_DELIM_LITERAL 0
|
|
|
|
#define BASH_DELIM_STRING 1
|
|
|
|
#define BASH_DELIM_CSTRING 2
|
|
|
|
#define BASH_DELIM_LSTRING 3
|
|
|
|
#define BASH_DELIM_COMMAND 4
|
|
|
|
#define BASH_DELIM_BACKTICK 5
|
|
|
|
|
|
|
|
#define BASH_DELIM_STACK_MAX 7
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
inline int translateBashDigit(int ch) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (ch >= '0' && ch <= '9') {
|
|
|
|
return ch - '0';
|
|
|
|
} else if (ch >= 'a' && ch <= 'z') {
|
|
|
|
return ch - 'a' + 10;
|
|
|
|
} else if (ch >= 'A' && ch <= 'Z') {
|
|
|
|
return ch - 'A' + 36;
|
|
|
|
} else if (ch == '@') {
|
|
|
|
return 62;
|
|
|
|
} else if (ch == '_') {
|
|
|
|
return 63;
|
|
|
|
}
|
|
|
|
return BASH_BASE_ERROR;
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
inline int getBashNumberBase(char *s) {
|
2009-04-25 23:38:15 +00:00
|
|
|
int i = 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
int base = 0;
|
2009-04-25 23:38:15 +00:00
|
|
|
while (*s) {
|
|
|
|
base = base * 10 + (*s++ - '0');
|
|
|
|
i++;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
if (base > 64 || i > 2) {
|
2009-04-24 23:35:41 +00:00
|
|
|
return BASH_BASE_ERROR;
|
|
|
|
}
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
int opposite(int ch) {
|
2009-04-25 23:38:15 +00:00
|
|
|
if (ch == '(') return ')';
|
|
|
|
if (ch == '[') return ']';
|
|
|
|
if (ch == '{') return '}';
|
|
|
|
if (ch == '<') return '>';
|
2009-04-24 23:35:41 +00:00
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
int GlobScan(StyleContext &sc) {
|
|
|
|
// forward scan for zsh globs, disambiguate versus bash arrays
|
|
|
|
// complex expressions may still fail, e.g. unbalanced () '' "" etc
|
|
|
|
int c, sLen = 0;
|
|
|
|
int pCount = 0;
|
|
|
|
int hash = 0;
|
|
|
|
while ((c = sc.GetRelativeCharacter(++sLen)) != 0) {
|
|
|
|
if (IsASpace(c)) {
|
|
|
|
return 0;
|
|
|
|
} else if (c == '\'' || c == '\"') {
|
|
|
|
if (hash != 2) return 0;
|
|
|
|
} else if (c == '#' && hash == 0) {
|
|
|
|
hash = (sLen == 1) ? 2:1;
|
|
|
|
} else if (c == '(') {
|
|
|
|
pCount++;
|
|
|
|
} else if (c == ')') {
|
|
|
|
if (pCount == 0) {
|
|
|
|
if (hash) return sLen;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
pCount--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsCommentLine(Sci_Position line, LexAccessor &styler) {
|
|
|
|
Sci_Position pos = styler.LineStart(line);
|
|
|
|
Sci_Position eol_pos = styler.LineStart(line + 1) - 1;
|
|
|
|
for (Sci_Position i = pos; i < eol_pos; i++) {
|
|
|
|
char ch = styler[i];
|
|
|
|
if (ch == '#')
|
|
|
|
return true;
|
|
|
|
else if (ch != ' ' && ch != '\t')
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct OptionsBash {
|
|
|
|
bool fold;
|
|
|
|
bool foldComment;
|
|
|
|
bool foldCompact;
|
|
|
|
|
|
|
|
OptionsBash() {
|
|
|
|
fold = false;
|
|
|
|
foldComment = false;
|
|
|
|
foldCompact = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const char * const bashWordListDesc[] = {
|
|
|
|
"Keywords",
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
struct OptionSetBash : public OptionSet<OptionsBash> {
|
|
|
|
OptionSetBash() {
|
|
|
|
DefineProperty("fold", &OptionsBash::fold);
|
|
|
|
|
|
|
|
DefineProperty("fold.comment", &OptionsBash::foldComment);
|
|
|
|
|
|
|
|
DefineProperty("fold.compact", &OptionsBash::foldCompact);
|
|
|
|
|
|
|
|
DefineWordListSets(bashWordListDesc);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const char styleSubable[] = { SCE_SH_IDENTIFIER, SCE_SH_SCALAR, 0 };
|
|
|
|
|
|
|
|
LexicalClass lexicalClasses[] = {
|
|
|
|
// Lexer Bash SCLEX_BASH SCE_SH_:
|
|
|
|
0, "SCE_SH_DEFAULT", "default", "White space",
|
|
|
|
1, "SCE_SH_ERROR", "error", "Error",
|
|
|
|
2, "SCE_SH_COMMENTLINE", "comment line", "Line comment: #",
|
|
|
|
3, "SCE_SH_NUMBER", "literal numeric", "Number",
|
|
|
|
4, "SCE_SH_WORD", "keyword", "Keyword",
|
|
|
|
5, "SCE_SH_STRING", "literal string", "String",
|
|
|
|
6, "SCE_SH_CHARACTER", "literal string", "Single quoted string",
|
|
|
|
7, "SCE_SH_OPERATOR", "operator", "Operators",
|
|
|
|
8, "SCE_SH_IDENTIFIER", "identifier", "Identifiers",
|
|
|
|
9, "SCE_SH_SCALAR", "identifier", "Scalar variable",
|
|
|
|
10, "SCE_SH_PARAM", "identifier", "Parameter",
|
|
|
|
11, "SCE_SH_BACKTICKS", "literal string", "Backtick quoted command",
|
|
|
|
12, "SCE_SH_HERE_DELIM", "operator", "Heredoc delimiter",
|
|
|
|
13, "SCE_SH_HERE_Q", "literal string", "Heredoc quoted string",
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class LexerBash : public DefaultLexer {
|
|
|
|
WordList keywords;
|
|
|
|
OptionsBash options;
|
|
|
|
OptionSetBash osBash;
|
|
|
|
enum { ssIdentifier, ssScalar };
|
|
|
|
SubStyles subStyles;
|
|
|
|
public:
|
|
|
|
LexerBash() :
|
|
|
|
DefaultLexer(lexicalClasses, ELEMENTS(lexicalClasses)),
|
|
|
|
subStyles(styleSubable, 0x80, 0x40, 0) {
|
|
|
|
}
|
|
|
|
virtual ~LexerBash() {
|
|
|
|
}
|
|
|
|
void SCI_METHOD Release() override {
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
int SCI_METHOD Version() const override {
|
|
|
|
return lvRelease4;
|
|
|
|
}
|
|
|
|
const char * SCI_METHOD PropertyNames() override {
|
|
|
|
return osBash.PropertyNames();
|
|
|
|
}
|
|
|
|
int SCI_METHOD PropertyType(const char* name) override {
|
|
|
|
return osBash.PropertyType(name);
|
|
|
|
}
|
|
|
|
const char * SCI_METHOD DescribeProperty(const char *name) override {
|
|
|
|
return osBash.DescribeProperty(name);
|
|
|
|
}
|
|
|
|
Sci_Position SCI_METHOD PropertySet(const char *key, const char *val) override;
|
|
|
|
const char * SCI_METHOD DescribeWordListSets() override {
|
|
|
|
return osBash.DescribeWordListSets();
|
|
|
|
}
|
|
|
|
Sci_Position SCI_METHOD WordListSet(int n, const char *wl) override;
|
|
|
|
void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
|
|
|
|
void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
void * SCI_METHOD PrivateCall(int, void *) override {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SCI_METHOD AllocateSubStyles(int styleBase, int numberStyles) override {
|
|
|
|
return subStyles.Allocate(styleBase, numberStyles);
|
|
|
|
}
|
|
|
|
int SCI_METHOD SubStylesStart(int styleBase) override {
|
|
|
|
return subStyles.Start(styleBase);
|
|
|
|
}
|
|
|
|
int SCI_METHOD SubStylesLength(int styleBase) override {
|
|
|
|
return subStyles.Length(styleBase);
|
|
|
|
}
|
|
|
|
int SCI_METHOD StyleFromSubStyle(int subStyle) override {
|
|
|
|
const int styleBase = subStyles.BaseStyle(subStyle);
|
|
|
|
return styleBase;
|
|
|
|
}
|
|
|
|
int SCI_METHOD PrimaryStyleFromStyle(int style) override {
|
|
|
|
return style;
|
|
|
|
}
|
|
|
|
void SCI_METHOD FreeSubStyles() override {
|
|
|
|
subStyles.Free();
|
|
|
|
}
|
|
|
|
void SCI_METHOD SetIdentifiers(int style, const char *identifiers) override {
|
|
|
|
subStyles.SetIdentifiers(style, identifiers);
|
|
|
|
}
|
|
|
|
int SCI_METHOD DistanceToSecondaryStyles() override {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const char *SCI_METHOD GetSubStyleBases() override {
|
|
|
|
return styleSubable;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ILexer4 *LexerFactoryBash() {
|
|
|
|
return new LexerBash();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Sci_Position SCI_METHOD LexerBash::PropertySet(const char *key, const char *val) {
|
|
|
|
if (osBash.PropertySet(&options, key, val)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sci_Position SCI_METHOD LexerBash::WordListSet(int n, const char *wl) {
|
|
|
|
WordList *wordListN = 0;
|
|
|
|
switch (n) {
|
|
|
|
case 0:
|
|
|
|
wordListN = &keywords;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Sci_Position firstModification = -1;
|
|
|
|
if (wordListN) {
|
|
|
|
WordList wlNew;
|
|
|
|
wlNew.Set(wl);
|
|
|
|
if (*wordListN != wlNew) {
|
|
|
|
wordListN->Set(wl);
|
|
|
|
firstModification = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return firstModification;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SCI_METHOD LexerBash::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) {
|
2010-08-21 23:59:56 +00:00
|
|
|
WordList cmdDelimiter, bashStruct, bashStruct_in;
|
|
|
|
cmdDelimiter.Set("| || |& & && ; ;; ( ) { }");
|
|
|
|
bashStruct.Set("if elif fi while until else then do done esac eval");
|
|
|
|
bashStruct_in.Set("for case select");
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
CharacterSet setWordStart(CharacterSet::setAlpha, "_");
|
|
|
|
// note that [+-] are often parts of identifiers in shell scripts
|
|
|
|
CharacterSet setWord(CharacterSet::setAlphaNum, "._+-");
|
2015-06-07 21:19:26 +00:00
|
|
|
CharacterSet setMetaCharacter(CharacterSet::setNone, "|&;()<> \t\r\n");
|
|
|
|
setMetaCharacter.Add(0);
|
2010-08-21 23:59:56 +00:00
|
|
|
CharacterSet setBashOperator(CharacterSet::setNone, "^&%()-+=|{}[]:;>,*/<?!.~@");
|
2009-04-25 23:38:15 +00:00
|
|
|
CharacterSet setSingleCharOp(CharacterSet::setNone, "rwxoRWXOezsfdlpSbctugkTBMACahGLNn");
|
|
|
|
CharacterSet setParam(CharacterSet::setAlphaNum, "$_");
|
2019-05-04 18:14:48 +00:00
|
|
|
CharacterSet setHereDoc(CharacterSet::setAlpha, "_\\-+!%*,./:?@[]^`{}~");
|
|
|
|
CharacterSet setHereDoc2(CharacterSet::setAlphaNum, "_-+!%*,./:=?@[]^`{}~");
|
|
|
|
CharacterSet setLeftShift(CharacterSet::setDigits, "$");
|
2009-04-25 23:38:15 +00:00
|
|
|
|
|
|
|
class HereDocCls { // Class to manage HERE document elements
|
2009-04-24 23:35:41 +00:00
|
|
|
public:
|
|
|
|
int State; // 0: '<<' encountered
|
|
|
|
// 1: collect the delimiter
|
|
|
|
// 2: here doc text (lines after the delimiter)
|
2009-04-25 23:38:15 +00:00
|
|
|
int Quote; // the char after '<<'
|
2009-04-24 23:35:41 +00:00
|
|
|
bool Quoted; // true if Quote in ('\'','"','`')
|
|
|
|
bool Indent; // indented delimiter (for <<-)
|
|
|
|
int DelimiterLength; // strlen(Delimiter)
|
2019-05-04 18:14:48 +00:00
|
|
|
char Delimiter[HERE_DELIM_MAX]; // the Delimiter
|
2009-04-24 23:35:41 +00:00
|
|
|
HereDocCls() {
|
|
|
|
State = 0;
|
2009-04-25 23:38:15 +00:00
|
|
|
Quote = 0;
|
|
|
|
Quoted = false;
|
|
|
|
Indent = 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
DelimiterLength = 0;
|
|
|
|
Delimiter[0] = '\0';
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
void Append(int ch) {
|
|
|
|
Delimiter[DelimiterLength++] = static_cast<char>(ch);
|
|
|
|
Delimiter[DelimiterLength] = '\0';
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
~HereDocCls() {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
HereDocCls HereDoc;
|
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
class QuoteCls { // Class to manage quote pairs (simplified vs LexPerl)
|
2009-04-24 23:35:41 +00:00
|
|
|
public:
|
2009-04-25 23:38:15 +00:00
|
|
|
int Count;
|
|
|
|
int Up, Down;
|
2009-04-24 23:35:41 +00:00
|
|
|
QuoteCls() {
|
|
|
|
Count = 0;
|
|
|
|
Up = '\0';
|
|
|
|
Down = '\0';
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
void Open(int u) {
|
2009-04-24 23:35:41 +00:00
|
|
|
Count++;
|
|
|
|
Up = u;
|
|
|
|
Down = opposite(Up);
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
void Start(int u) {
|
|
|
|
Count = 0;
|
|
|
|
Open(u);
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
};
|
|
|
|
QuoteCls Quote;
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
class QuoteStackCls { // Class to manage quote pairs that nest
|
|
|
|
public:
|
|
|
|
int Count;
|
|
|
|
int Up, Down;
|
|
|
|
int Style;
|
|
|
|
int Depth; // levels pushed
|
2019-05-04 18:14:48 +00:00
|
|
|
int CountStack[BASH_DELIM_STACK_MAX];
|
|
|
|
int UpStack [BASH_DELIM_STACK_MAX];
|
|
|
|
int StyleStack[BASH_DELIM_STACK_MAX];
|
2013-08-28 00:44:27 +00:00
|
|
|
QuoteStackCls() {
|
|
|
|
Count = 0;
|
|
|
|
Up = '\0';
|
|
|
|
Down = '\0';
|
|
|
|
Style = 0;
|
|
|
|
Depth = 0;
|
|
|
|
}
|
|
|
|
void Start(int u, int s) {
|
|
|
|
Count = 1;
|
|
|
|
Up = u;
|
|
|
|
Down = opposite(Up);
|
|
|
|
Style = s;
|
|
|
|
}
|
|
|
|
void Push(int u, int s) {
|
|
|
|
if (Depth >= BASH_DELIM_STACK_MAX)
|
|
|
|
return;
|
|
|
|
CountStack[Depth] = Count;
|
|
|
|
UpStack [Depth] = Up;
|
|
|
|
StyleStack[Depth] = Style;
|
|
|
|
Depth++;
|
|
|
|
Count = 1;
|
|
|
|
Up = u;
|
|
|
|
Down = opposite(Up);
|
|
|
|
Style = s;
|
|
|
|
}
|
|
|
|
void Pop(void) {
|
|
|
|
if (Depth <= 0)
|
|
|
|
return;
|
|
|
|
Depth--;
|
|
|
|
Count = CountStack[Depth];
|
|
|
|
Up = UpStack [Depth];
|
|
|
|
Style = StyleStack[Depth];
|
|
|
|
Down = opposite(Up);
|
|
|
|
}
|
|
|
|
~QuoteStackCls() {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
QuoteStackCls QuoteStack;
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
const WordClassifier &classifierIdentifiers = subStyles.Classifier(SCE_SH_IDENTIFIER);
|
|
|
|
const WordClassifier &classifierScalars = subStyles.Classifier(SCE_SH_SCALAR);
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
int numBase = 0;
|
2009-04-25 23:38:15 +00:00
|
|
|
int digit;
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU endPos = startPos + length;
|
2010-08-21 23:59:56 +00:00
|
|
|
int cmdState = BASH_CMD_START;
|
|
|
|
int testExprType = 0;
|
2019-05-04 18:14:48 +00:00
|
|
|
LexAccessor styler(pAccess);
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
// Always backtracks to the start of a line that is not a continuation
|
|
|
|
// of the previous line (i.e. start of a bash command segment)
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position ln = styler.GetLine(startPos);
|
|
|
|
if (ln > 0 && startPos == static_cast<Sci_PositionU>(styler.LineStart(ln)))
|
2013-08-28 00:44:27 +00:00
|
|
|
ln--;
|
2010-08-21 23:59:56 +00:00
|
|
|
for (;;) {
|
|
|
|
startPos = styler.LineStart(ln);
|
|
|
|
if (ln == 0 || styler.GetLineState(ln) == BASH_CMD_START)
|
|
|
|
break;
|
|
|
|
ln--;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2010-08-21 23:59:56 +00:00
|
|
|
initStyle = SCE_SH_DEFAULT;
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
StyleContext sc(startPos, endPos - startPos, initStyle, styler);
|
|
|
|
|
|
|
|
for (; sc.More(); sc.Forward()) {
|
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
// handle line continuation, updates per-line stored state
|
|
|
|
if (sc.atLineStart) {
|
|
|
|
ln = styler.GetLine(sc.currentPos);
|
|
|
|
if (sc.state == SCE_SH_STRING
|
|
|
|
|| sc.state == SCE_SH_BACKTICKS
|
|
|
|
|| sc.state == SCE_SH_CHARACTER
|
|
|
|
|| sc.state == SCE_SH_HERE_Q
|
|
|
|
|| sc.state == SCE_SH_COMMENTLINE
|
|
|
|
|| sc.state == SCE_SH_PARAM) {
|
|
|
|
// force backtrack while retaining cmdState
|
|
|
|
styler.SetLineState(ln, BASH_CMD_BODY);
|
|
|
|
} else {
|
|
|
|
if (ln > 0) {
|
|
|
|
if ((sc.GetRelative(-3) == '\\' && sc.GetRelative(-2) == '\r' && sc.chPrev == '\n')
|
|
|
|
|| sc.GetRelative(-2) == '\\') { // handle '\' line continuation
|
|
|
|
// retain last line's state
|
|
|
|
} else
|
|
|
|
cmdState = BASH_CMD_START;
|
|
|
|
}
|
|
|
|
styler.SetLineState(ln, cmdState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// controls change of cmdState at the end of a non-whitespace element
|
|
|
|
// states BODY|TEST|ARITH persist until the end of a command segment
|
|
|
|
// state WORD persist, but ends with 'in' or 'do' construct keywords
|
|
|
|
int cmdStateNew = BASH_CMD_BODY;
|
|
|
|
if (cmdState == BASH_CMD_TEST || cmdState == BASH_CMD_ARITH || cmdState == BASH_CMD_WORD)
|
|
|
|
cmdStateNew = cmdState;
|
|
|
|
int stylePrev = sc.state;
|
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
// Determine if the current state should terminate.
|
|
|
|
switch (sc.state) {
|
|
|
|
case SCE_SH_OPERATOR:
|
|
|
|
sc.SetState(SCE_SH_DEFAULT);
|
2010-08-21 23:59:56 +00:00
|
|
|
if (cmdState == BASH_CMD_DELIM) // if command delimiter, start new command
|
|
|
|
cmdStateNew = BASH_CMD_START;
|
|
|
|
else if (sc.chPrev == '\\') // propagate command state if line continued
|
|
|
|
cmdStateNew = cmdState;
|
2009-04-25 23:38:15 +00:00
|
|
|
break;
|
|
|
|
case SCE_SH_WORD:
|
|
|
|
// "." never used in Bash variable names but used in file names
|
|
|
|
if (!setWord.Contains(sc.ch)) {
|
2010-08-21 23:59:56 +00:00
|
|
|
char s[500];
|
|
|
|
char s2[10];
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.GetCurrent(s, sizeof(s));
|
2019-05-04 18:14:48 +00:00
|
|
|
int identifierStyle = SCE_SH_IDENTIFIER;
|
|
|
|
int subStyle = classifierIdentifiers.ValueFor(s);
|
|
|
|
if (subStyle >= 0) {
|
|
|
|
identifierStyle = subStyle;
|
|
|
|
}
|
2010-08-21 23:59:56 +00:00
|
|
|
// allow keywords ending in a whitespace or command delimiter
|
|
|
|
s2[0] = static_cast<char>(sc.ch);
|
|
|
|
s2[1] = '\0';
|
|
|
|
bool keywordEnds = IsASpace(sc.ch) || cmdDelimiter.InList(s2);
|
|
|
|
// 'in' or 'do' may be construct keywords
|
|
|
|
if (cmdState == BASH_CMD_WORD) {
|
|
|
|
if (strcmp(s, "in") == 0 && keywordEnds)
|
|
|
|
cmdStateNew = BASH_CMD_BODY;
|
|
|
|
else if (strcmp(s, "do") == 0 && keywordEnds)
|
|
|
|
cmdStateNew = BASH_CMD_START;
|
|
|
|
else
|
2019-05-04 18:14:48 +00:00
|
|
|
sc.ChangeState(identifierStyle);
|
2010-08-21 23:59:56 +00:00
|
|
|
sc.SetState(SCE_SH_DEFAULT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// a 'test' keyword starts a test expression
|
|
|
|
if (strcmp(s, "test") == 0) {
|
|
|
|
if (cmdState == BASH_CMD_START && keywordEnds) {
|
|
|
|
cmdStateNew = BASH_CMD_TEST;
|
|
|
|
testExprType = 0;
|
|
|
|
} else
|
2019-05-04 18:14:48 +00:00
|
|
|
sc.ChangeState(identifierStyle);
|
2010-08-21 23:59:56 +00:00
|
|
|
}
|
|
|
|
// detect bash construct keywords
|
|
|
|
else if (bashStruct.InList(s)) {
|
|
|
|
if (cmdState == BASH_CMD_START && keywordEnds)
|
|
|
|
cmdStateNew = BASH_CMD_START;
|
|
|
|
else
|
2019-05-04 18:14:48 +00:00
|
|
|
sc.ChangeState(identifierStyle);
|
2010-08-21 23:59:56 +00:00
|
|
|
}
|
|
|
|
// 'for'|'case'|'select' needs 'in'|'do' to be highlighted later
|
|
|
|
else if (bashStruct_in.InList(s)) {
|
|
|
|
if (cmdState == BASH_CMD_START && keywordEnds)
|
|
|
|
cmdStateNew = BASH_CMD_WORD;
|
|
|
|
else
|
2019-05-04 18:14:48 +00:00
|
|
|
sc.ChangeState(identifierStyle);
|
2010-08-21 23:59:56 +00:00
|
|
|
}
|
|
|
|
// disambiguate option items and file test operators
|
|
|
|
else if (s[0] == '-') {
|
|
|
|
if (cmdState != BASH_CMD_TEST)
|
2019-05-04 18:14:48 +00:00
|
|
|
sc.ChangeState(identifierStyle);
|
2010-08-21 23:59:56 +00:00
|
|
|
}
|
|
|
|
// disambiguate keywords and identifiers
|
|
|
|
else if (cmdState != BASH_CMD_START
|
|
|
|
|| !(keywords.InList(s) && keywordEnds)) {
|
2019-05-04 18:14:48 +00:00
|
|
|
sc.ChangeState(identifierStyle);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_SH_DEFAULT);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
break;
|
|
|
|
case SCE_SH_IDENTIFIER:
|
2019-05-04 18:14:48 +00:00
|
|
|
if (sc.chPrev == '\\' || !setWord.Contains(sc.ch) ||
|
|
|
|
(cmdState == BASH_CMD_ARITH && !setWordStart.Contains(sc.ch))) {
|
|
|
|
char s[500];
|
|
|
|
sc.GetCurrent(s, sizeof(s));
|
|
|
|
int subStyle = classifierIdentifiers.ValueFor(s);
|
|
|
|
if (subStyle >= 0) {
|
|
|
|
sc.ChangeState(subStyle);
|
|
|
|
}
|
|
|
|
if (sc.chPrev == '\\') { // for escaped chars
|
|
|
|
sc.ForwardSetState(SCE_SH_DEFAULT);
|
|
|
|
} else {
|
|
|
|
sc.SetState(SCE_SH_DEFAULT);
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
break;
|
|
|
|
case SCE_SH_NUMBER:
|
|
|
|
digit = translateBashDigit(sc.ch);
|
|
|
|
if (numBase == BASH_BASE_DECIMAL) {
|
|
|
|
if (sc.ch == '#') {
|
|
|
|
char s[10];
|
|
|
|
sc.GetCurrent(s, sizeof(s));
|
|
|
|
numBase = getBashNumberBase(s);
|
|
|
|
if (numBase != BASH_BASE_ERROR)
|
|
|
|
break;
|
|
|
|
} else if (IsADigit(sc.ch))
|
|
|
|
break;
|
|
|
|
} else if (numBase == BASH_BASE_HEX) {
|
|
|
|
if (IsADigit(sc.ch, 16))
|
|
|
|
break;
|
2009-04-24 23:35:41 +00:00
|
|
|
#ifdef PEDANTIC_OCTAL
|
2009-04-25 23:38:15 +00:00
|
|
|
} else if (numBase == BASH_BASE_OCTAL ||
|
|
|
|
numBase == BASH_BASE_OCTAL_ERROR) {
|
|
|
|
if (digit <= 7)
|
|
|
|
break;
|
2009-04-24 23:35:41 +00:00
|
|
|
if (digit <= 9) {
|
2009-04-25 23:38:15 +00:00
|
|
|
numBase = BASH_BASE_OCTAL_ERROR;
|
|
|
|
break;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
#endif
|
|
|
|
} else if (numBase == BASH_BASE_ERROR) {
|
|
|
|
if (digit <= 9)
|
|
|
|
break;
|
|
|
|
} else { // DD#DDDD number style handling
|
|
|
|
if (digit != BASH_BASE_ERROR) {
|
|
|
|
if (numBase <= 36) {
|
|
|
|
// case-insensitive if base<=36
|
|
|
|
if (digit >= 36) digit -= 26;
|
|
|
|
}
|
|
|
|
if (digit < numBase)
|
|
|
|
break;
|
2009-04-24 23:35:41 +00:00
|
|
|
if (digit <= 9) {
|
|
|
|
numBase = BASH_BASE_ERROR;
|
2009-04-25 23:38:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
}
|
|
|
|
// fallthrough when number is at an end or error
|
|
|
|
if (numBase == BASH_BASE_ERROR
|
2009-04-24 23:35:41 +00:00
|
|
|
#ifdef PEDANTIC_OCTAL
|
2009-04-25 23:38:15 +00:00
|
|
|
|| numBase == BASH_BASE_OCTAL_ERROR
|
2009-04-24 23:35:41 +00:00
|
|
|
#endif
|
2009-04-25 23:38:15 +00:00
|
|
|
) {
|
|
|
|
sc.ChangeState(SCE_SH_ERROR);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_SH_DEFAULT);
|
|
|
|
break;
|
|
|
|
case SCE_SH_COMMENTLINE:
|
2010-07-12 22:19:51 +00:00
|
|
|
if (sc.atLineEnd && sc.chPrev != '\\') {
|
|
|
|
sc.SetState(SCE_SH_DEFAULT);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
break;
|
|
|
|
case SCE_SH_HERE_DELIM:
|
2009-04-24 23:35:41 +00:00
|
|
|
// From Bash info:
|
|
|
|
// ---------------
|
|
|
|
// Specifier format is: <<[-]WORD
|
|
|
|
// Optional '-' is for removal of leading tabs from here-doc.
|
|
|
|
// Whitespace acceptable after <<[-] operator
|
|
|
|
//
|
|
|
|
if (HereDoc.State == 0) { // '<<' encountered
|
2009-04-25 23:38:15 +00:00
|
|
|
HereDoc.Quote = sc.chNext;
|
2009-04-24 23:35:41 +00:00
|
|
|
HereDoc.Quoted = false;
|
|
|
|
HereDoc.DelimiterLength = 0;
|
|
|
|
HereDoc.Delimiter[HereDoc.DelimiterLength] = '\0';
|
2009-04-25 23:38:15 +00:00
|
|
|
if (sc.chNext == '\'' || sc.chNext == '\"') { // a quoted here-doc delimiter (' or ")
|
|
|
|
sc.Forward();
|
2009-04-24 23:35:41 +00:00
|
|
|
HereDoc.Quoted = true;
|
2009-04-25 23:38:15 +00:00
|
|
|
HereDoc.State = 1;
|
2019-05-04 18:14:48 +00:00
|
|
|
} else if (setHereDoc.Contains(sc.chNext) ||
|
|
|
|
(sc.chNext == '=' && cmdState != BASH_CMD_ARITH)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
// an unquoted here-doc delimiter, no special handling
|
2009-04-25 23:38:15 +00:00
|
|
|
HereDoc.State = 1;
|
|
|
|
} else if (sc.chNext == '<') { // HERE string <<<
|
|
|
|
sc.Forward();
|
|
|
|
sc.ForwardSetState(SCE_SH_DEFAULT);
|
|
|
|
} else if (IsASpace(sc.chNext)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
// eat whitespace
|
2019-05-04 18:14:48 +00:00
|
|
|
} else if (setLeftShift.Contains(sc.chNext) ||
|
|
|
|
(sc.chNext == '=' && cmdState == BASH_CMD_ARITH)) {
|
|
|
|
// left shift <<$var or <<= cases
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.ChangeState(SCE_SH_OPERATOR);
|
|
|
|
sc.ForwardSetState(SCE_SH_DEFAULT);
|
2009-04-24 23:35:41 +00:00
|
|
|
} else {
|
|
|
|
// symbols terminates; deprecated zero-length delimiter
|
2009-04-25 23:38:15 +00:00
|
|
|
HereDoc.State = 1;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
} else if (HereDoc.State == 1) { // collect the delimiter
|
2015-06-07 21:19:26 +00:00
|
|
|
// * if single quoted, there's no escape
|
|
|
|
// * if double quoted, there are \\ and \" escapes
|
|
|
|
if ((HereDoc.Quote == '\'' && sc.ch != HereDoc.Quote) ||
|
|
|
|
(HereDoc.Quoted && sc.ch != HereDoc.Quote && sc.ch != '\\') ||
|
|
|
|
(HereDoc.Quote != '\'' && sc.chPrev == '\\') ||
|
|
|
|
(setHereDoc2.Contains(sc.ch))) {
|
2010-07-12 22:19:51 +00:00
|
|
|
HereDoc.Append(sc.ch);
|
|
|
|
} else if (HereDoc.Quoted && sc.ch == HereDoc.Quote) { // closing quote => end of delimiter
|
|
|
|
sc.ForwardSetState(SCE_SH_DEFAULT);
|
|
|
|
} else if (sc.ch == '\\') {
|
2015-06-07 21:19:26 +00:00
|
|
|
if (HereDoc.Quoted && sc.chNext != HereDoc.Quote && sc.chNext != '\\') {
|
|
|
|
// in quoted prefixes only \ and the quote eat the escape
|
|
|
|
HereDoc.Append(sc.ch);
|
|
|
|
} else {
|
|
|
|
// skip escape prefix
|
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
} else if (!HereDoc.Quoted) {
|
2010-07-12 22:19:51 +00:00
|
|
|
sc.SetState(SCE_SH_DEFAULT);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
if (HereDoc.DelimiterLength >= HERE_DELIM_MAX - 1) { // force blowup
|
|
|
|
sc.SetState(SCE_SH_ERROR);
|
|
|
|
HereDoc.State = 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
break;
|
|
|
|
case SCE_SH_HERE_Q:
|
|
|
|
// HereDoc.State == 2
|
|
|
|
if (sc.atLineStart) {
|
|
|
|
sc.SetState(SCE_SH_HERE_Q);
|
|
|
|
int prefixws = 0;
|
2015-06-07 21:19:26 +00:00
|
|
|
while (sc.ch == '\t' && !sc.atLineEnd) { // tabulation prefix
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.Forward();
|
|
|
|
prefixws++;
|
|
|
|
}
|
|
|
|
if (prefixws > 0)
|
|
|
|
sc.SetState(SCE_SH_HERE_Q);
|
|
|
|
while (!sc.atLineEnd) {
|
|
|
|
sc.Forward();
|
|
|
|
}
|
|
|
|
char s[HERE_DELIM_MAX];
|
|
|
|
sc.GetCurrent(s, sizeof(s));
|
2013-08-28 00:44:27 +00:00
|
|
|
if (sc.LengthCurrent() == 0) { // '' or "" delimiters
|
2015-06-07 21:19:26 +00:00
|
|
|
if ((prefixws == 0 || HereDoc.Indent) &&
|
|
|
|
HereDoc.Quoted && HereDoc.DelimiterLength == 0)
|
2013-08-28 00:44:27 +00:00
|
|
|
sc.SetState(SCE_SH_DEFAULT);
|
2009-06-24 19:09:31 +00:00
|
|
|
break;
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
2009-06-24 19:09:31 +00:00
|
|
|
if (s[strlen(s) - 1] == '\r')
|
|
|
|
s[strlen(s) - 1] = '\0';
|
2009-04-25 23:38:15 +00:00
|
|
|
if (strcmp(HereDoc.Delimiter, s) == 0) {
|
2011-03-22 00:16:49 +00:00
|
|
|
if ((prefixws == 0) || // indentation rule
|
|
|
|
(prefixws > 0 && HereDoc.Indent)) {
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_SH_DEFAULT);
|
|
|
|
break;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
break;
|
|
|
|
case SCE_SH_SCALAR: // variable names
|
|
|
|
if (!setParam.Contains(sc.ch)) {
|
2019-05-04 18:14:48 +00:00
|
|
|
char s[500];
|
|
|
|
sc.GetCurrent(s, sizeof(s));
|
|
|
|
int subStyle = classifierScalars.ValueFor(&s[1]); // skip the $
|
|
|
|
if (subStyle >= 0) {
|
|
|
|
sc.ChangeState(subStyle);
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
if (sc.LengthCurrent() == 1) {
|
2009-04-24 23:35:41 +00:00
|
|
|
// Special variable: $(, $_ etc.
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.ForwardSetState(SCE_SH_DEFAULT);
|
2009-04-24 23:35:41 +00:00
|
|
|
} else {
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_SH_DEFAULT);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
break;
|
2013-08-28 00:44:27 +00:00
|
|
|
case SCE_SH_STRING: // delimited styles, can nest
|
2009-04-25 23:38:15 +00:00
|
|
|
case SCE_SH_BACKTICKS:
|
2013-08-28 00:44:27 +00:00
|
|
|
if (sc.ch == '\\' && QuoteStack.Up != '\\') {
|
|
|
|
if (QuoteStack.Style != BASH_DELIM_LITERAL)
|
|
|
|
sc.Forward();
|
|
|
|
} else if (sc.ch == QuoteStack.Down) {
|
|
|
|
QuoteStack.Count--;
|
|
|
|
if (QuoteStack.Count == 0) {
|
|
|
|
if (QuoteStack.Depth > 0) {
|
|
|
|
QuoteStack.Pop();
|
|
|
|
} else
|
|
|
|
sc.ForwardSetState(SCE_SH_DEFAULT);
|
|
|
|
}
|
|
|
|
} else if (sc.ch == QuoteStack.Up) {
|
|
|
|
QuoteStack.Count++;
|
|
|
|
} else {
|
|
|
|
if (QuoteStack.Style == BASH_DELIM_STRING ||
|
|
|
|
QuoteStack.Style == BASH_DELIM_LSTRING
|
|
|
|
) { // do nesting for "string", $"locale-string"
|
|
|
|
if (sc.ch == '`') {
|
|
|
|
QuoteStack.Push(sc.ch, BASH_DELIM_BACKTICK);
|
|
|
|
} else if (sc.ch == '$' && sc.chNext == '(') {
|
|
|
|
sc.Forward();
|
|
|
|
QuoteStack.Push(sc.ch, BASH_DELIM_COMMAND);
|
|
|
|
}
|
|
|
|
} else if (QuoteStack.Style == BASH_DELIM_COMMAND ||
|
|
|
|
QuoteStack.Style == BASH_DELIM_BACKTICK
|
|
|
|
) { // do nesting for $(command), `command`
|
|
|
|
if (sc.ch == '\'') {
|
|
|
|
QuoteStack.Push(sc.ch, BASH_DELIM_LITERAL);
|
|
|
|
} else if (sc.ch == '\"') {
|
|
|
|
QuoteStack.Push(sc.ch, BASH_DELIM_STRING);
|
|
|
|
} else if (sc.ch == '`') {
|
|
|
|
QuoteStack.Push(sc.ch, BASH_DELIM_BACKTICK);
|
|
|
|
} else if (sc.ch == '$') {
|
|
|
|
if (sc.chNext == '\'') {
|
|
|
|
sc.Forward();
|
|
|
|
QuoteStack.Push(sc.ch, BASH_DELIM_CSTRING);
|
|
|
|
} else if (sc.chNext == '\"') {
|
|
|
|
sc.Forward();
|
|
|
|
QuoteStack.Push(sc.ch, BASH_DELIM_LSTRING);
|
|
|
|
} else if (sc.chNext == '(') {
|
|
|
|
sc.Forward();
|
|
|
|
QuoteStack.Push(sc.ch, BASH_DELIM_COMMAND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SCE_SH_PARAM: // ${parameter}
|
2009-04-25 23:38:15 +00:00
|
|
|
if (sc.ch == '\\' && Quote.Up != '\\') {
|
|
|
|
sc.Forward();
|
|
|
|
} else if (sc.ch == Quote.Down) {
|
2009-04-24 23:35:41 +00:00
|
|
|
Quote.Count--;
|
|
|
|
if (Quote.Count == 0) {
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.ForwardSetState(SCE_SH_DEFAULT);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
} else if (sc.ch == Quote.Up) {
|
2009-04-24 23:35:41 +00:00
|
|
|
Quote.Count++;
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
break;
|
2013-08-28 00:44:27 +00:00
|
|
|
case SCE_SH_CHARACTER: // singly-quoted strings
|
|
|
|
if (sc.ch == Quote.Down) {
|
|
|
|
Quote.Count--;
|
|
|
|
if (Quote.Count == 0) {
|
|
|
|
sc.ForwardSetState(SCE_SH_DEFAULT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2009-04-25 23:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Must check end of HereDoc state 1 before default state is handled
|
|
|
|
if (HereDoc.State == 1 && sc.atLineEnd) {
|
|
|
|
// Begin of here-doc (the line after the here-doc delimiter):
|
|
|
|
// Lexically, the here-doc starts from the next line after the >>, but the
|
|
|
|
// first line of here-doc seem to follow the style of the last EOL sequence
|
|
|
|
HereDoc.State = 2;
|
|
|
|
if (HereDoc.Quoted) {
|
|
|
|
if (sc.state == SCE_SH_HERE_DELIM) {
|
2019-05-04 18:14:48 +00:00
|
|
|
// Missing quote at end of string! Syntax error in bash 4.3
|
|
|
|
// Mark this bit as an error, do not colour any here-doc
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.ChangeState(SCE_SH_ERROR);
|
2019-05-04 18:14:48 +00:00
|
|
|
sc.SetState(SCE_SH_DEFAULT);
|
|
|
|
} else {
|
|
|
|
// HereDoc.Quote always == '\''
|
|
|
|
sc.SetState(SCE_SH_HERE_Q);
|
2009-04-25 23:38:15 +00:00
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
} else if (HereDoc.DelimiterLength == 0) {
|
|
|
|
// no delimiter, illegal (but '' and "" are legal)
|
|
|
|
sc.ChangeState(SCE_SH_ERROR);
|
|
|
|
sc.SetState(SCE_SH_DEFAULT);
|
|
|
|
} else {
|
|
|
|
sc.SetState(SCE_SH_HERE_Q);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
// update cmdState about the current command segment
|
|
|
|
if (stylePrev != SCE_SH_DEFAULT && sc.state == SCE_SH_DEFAULT) {
|
|
|
|
cmdState = cmdStateNew;
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
// Determine if a new state should be entered.
|
|
|
|
if (sc.state == SCE_SH_DEFAULT) {
|
2010-08-21 23:59:56 +00:00
|
|
|
if (sc.ch == '\\') {
|
|
|
|
// Bash can escape any non-newline as a literal
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_SH_IDENTIFIER);
|
2010-08-21 23:59:56 +00:00
|
|
|
if (sc.chNext == '\r' || sc.chNext == '\n')
|
|
|
|
sc.SetState(SCE_SH_OPERATOR);
|
2009-04-25 23:38:15 +00:00
|
|
|
} else if (IsADigit(sc.ch)) {
|
|
|
|
sc.SetState(SCE_SH_NUMBER);
|
|
|
|
numBase = BASH_BASE_DECIMAL;
|
|
|
|
if (sc.ch == '0') { // hex,octal
|
|
|
|
if (sc.chNext == 'x' || sc.chNext == 'X') {
|
|
|
|
numBase = BASH_BASE_HEX;
|
|
|
|
sc.Forward();
|
|
|
|
} else if (IsADigit(sc.chNext)) {
|
|
|
|
#ifdef PEDANTIC_OCTAL
|
|
|
|
numBase = BASH_BASE_OCTAL;
|
|
|
|
#else
|
|
|
|
numBase = BASH_BASE_HEX;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (setWordStart.Contains(sc.ch)) {
|
|
|
|
sc.SetState(SCE_SH_WORD);
|
|
|
|
} else if (sc.ch == '#') {
|
2015-06-07 21:19:26 +00:00
|
|
|
if (stylePrev != SCE_SH_WORD && stylePrev != SCE_SH_IDENTIFIER &&
|
|
|
|
(sc.currentPos == 0 || setMetaCharacter.Contains(sc.chPrev))) {
|
|
|
|
sc.SetState(SCE_SH_COMMENTLINE);
|
|
|
|
} else {
|
|
|
|
sc.SetState(SCE_SH_WORD);
|
|
|
|
}
|
2019-05-04 18:14:48 +00:00
|
|
|
// handle some zsh features within arithmetic expressions only
|
|
|
|
if (cmdState == BASH_CMD_ARITH) {
|
|
|
|
if (sc.chPrev == '[') { // [#8] [##8] output digit setting
|
|
|
|
sc.SetState(SCE_SH_WORD);
|
|
|
|
if (sc.chNext == '#') {
|
|
|
|
sc.Forward();
|
|
|
|
}
|
|
|
|
} else if (sc.Match("##^") && IsUpperCase(sc.GetRelative(3))) { // ##^A
|
|
|
|
sc.SetState(SCE_SH_IDENTIFIER);
|
|
|
|
sc.Forward(3);
|
|
|
|
} else if (sc.chNext == '#' && !IsASpace(sc.GetRelative(2))) { // ##a
|
|
|
|
sc.SetState(SCE_SH_IDENTIFIER);
|
|
|
|
sc.Forward(2);
|
|
|
|
} else if (setWordStart.Contains(sc.chNext)) { // #name
|
|
|
|
sc.SetState(SCE_SH_IDENTIFIER);
|
|
|
|
}
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
} else if (sc.ch == '\"') {
|
|
|
|
sc.SetState(SCE_SH_STRING);
|
2013-08-28 00:44:27 +00:00
|
|
|
QuoteStack.Start(sc.ch, BASH_DELIM_STRING);
|
2009-04-25 23:38:15 +00:00
|
|
|
} else if (sc.ch == '\'') {
|
|
|
|
sc.SetState(SCE_SH_CHARACTER);
|
|
|
|
Quote.Start(sc.ch);
|
|
|
|
} else if (sc.ch == '`') {
|
|
|
|
sc.SetState(SCE_SH_BACKTICKS);
|
2013-08-28 00:44:27 +00:00
|
|
|
QuoteStack.Start(sc.ch, BASH_DELIM_BACKTICK);
|
2009-04-25 23:38:15 +00:00
|
|
|
} else if (sc.ch == '$') {
|
2010-08-21 23:59:56 +00:00
|
|
|
if (sc.Match("$((")) {
|
|
|
|
sc.SetState(SCE_SH_OPERATOR); // handle '((' later
|
|
|
|
continue;
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_SH_SCALAR);
|
|
|
|
sc.Forward();
|
|
|
|
if (sc.ch == '{') {
|
|
|
|
sc.ChangeState(SCE_SH_PARAM);
|
2013-08-28 00:44:27 +00:00
|
|
|
Quote.Start(sc.ch);
|
2009-04-25 23:38:15 +00:00
|
|
|
} else if (sc.ch == '\'') {
|
2013-08-28 00:44:27 +00:00
|
|
|
sc.ChangeState(SCE_SH_STRING);
|
|
|
|
QuoteStack.Start(sc.ch, BASH_DELIM_CSTRING);
|
2009-04-25 23:38:15 +00:00
|
|
|
} else if (sc.ch == '"') {
|
|
|
|
sc.ChangeState(SCE_SH_STRING);
|
2013-08-28 00:44:27 +00:00
|
|
|
QuoteStack.Start(sc.ch, BASH_DELIM_LSTRING);
|
|
|
|
} else if (sc.ch == '(') {
|
|
|
|
sc.ChangeState(SCE_SH_BACKTICKS);
|
|
|
|
QuoteStack.Start(sc.ch, BASH_DELIM_COMMAND);
|
|
|
|
} else if (sc.ch == '`') { // $` seen in a configure script, valid?
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.ChangeState(SCE_SH_BACKTICKS);
|
2013-08-28 00:44:27 +00:00
|
|
|
QuoteStack.Start(sc.ch, BASH_DELIM_BACKTICK);
|
2009-04-25 23:38:15 +00:00
|
|
|
} else {
|
|
|
|
continue; // scalar has no delimiter pair
|
|
|
|
}
|
|
|
|
} else if (sc.Match('<', '<')) {
|
|
|
|
sc.SetState(SCE_SH_HERE_DELIM);
|
|
|
|
HereDoc.State = 0;
|
2015-06-07 21:19:26 +00:00
|
|
|
if (sc.GetRelative(2) == '-') { // <<- indent case
|
|
|
|
HereDoc.Indent = true;
|
|
|
|
sc.Forward();
|
|
|
|
} else {
|
|
|
|
HereDoc.Indent = false;
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
} else if (sc.ch == '-' && // one-char file test operators
|
|
|
|
setSingleCharOp.Contains(sc.chNext) &&
|
|
|
|
!setWord.Contains(sc.GetRelative(2)) &&
|
|
|
|
IsASpace(sc.chPrev)) {
|
|
|
|
sc.SetState(SCE_SH_WORD);
|
|
|
|
sc.Forward();
|
|
|
|
} else if (setBashOperator.Contains(sc.ch)) {
|
2010-08-21 23:59:56 +00:00
|
|
|
char s[10];
|
|
|
|
bool isCmdDelim = false;
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_SH_OPERATOR);
|
2019-05-04 18:14:48 +00:00
|
|
|
// globs have no whitespace, do not appear in arithmetic expressions
|
|
|
|
if (cmdState != BASH_CMD_ARITH && sc.ch == '(' && sc.chNext != '(') {
|
|
|
|
int i = GlobScan(sc);
|
|
|
|
if (i > 1) {
|
|
|
|
sc.SetState(SCE_SH_IDENTIFIER);
|
|
|
|
sc.Forward(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2010-08-21 23:59:56 +00:00
|
|
|
// handle opening delimiters for test/arithmetic expressions - ((,[[,[
|
|
|
|
if (cmdState == BASH_CMD_START
|
|
|
|
|| cmdState == BASH_CMD_BODY) {
|
|
|
|
if (sc.Match('(', '(')) {
|
|
|
|
cmdState = BASH_CMD_ARITH;
|
|
|
|
sc.Forward();
|
|
|
|
} else if (sc.Match('[', '[') && IsASpace(sc.GetRelative(2))) {
|
|
|
|
cmdState = BASH_CMD_TEST;
|
|
|
|
testExprType = 1;
|
|
|
|
sc.Forward();
|
|
|
|
} else if (sc.ch == '[' && IsASpace(sc.chNext)) {
|
|
|
|
cmdState = BASH_CMD_TEST;
|
|
|
|
testExprType = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// special state -- for ((x;y;z)) in ... looping
|
|
|
|
if (cmdState == BASH_CMD_WORD && sc.Match('(', '(')) {
|
|
|
|
cmdState = BASH_CMD_ARITH;
|
|
|
|
sc.Forward();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// handle command delimiters in command START|BODY|WORD state, also TEST if 'test'
|
|
|
|
if (cmdState == BASH_CMD_START
|
|
|
|
|| cmdState == BASH_CMD_BODY
|
|
|
|
|| cmdState == BASH_CMD_WORD
|
|
|
|
|| (cmdState == BASH_CMD_TEST && testExprType == 0)) {
|
|
|
|
s[0] = static_cast<char>(sc.ch);
|
|
|
|
if (setBashOperator.Contains(sc.chNext)) {
|
|
|
|
s[1] = static_cast<char>(sc.chNext);
|
|
|
|
s[2] = '\0';
|
|
|
|
isCmdDelim = cmdDelimiter.InList(s);
|
|
|
|
if (isCmdDelim)
|
|
|
|
sc.Forward();
|
|
|
|
}
|
|
|
|
if (!isCmdDelim) {
|
|
|
|
s[1] = '\0';
|
|
|
|
isCmdDelim = cmdDelimiter.InList(s);
|
|
|
|
}
|
|
|
|
if (isCmdDelim) {
|
|
|
|
cmdState = BASH_CMD_DELIM;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// handle closing delimiters for test/arithmetic expressions - )),]],]
|
|
|
|
if (cmdState == BASH_CMD_ARITH && sc.Match(')', ')')) {
|
|
|
|
cmdState = BASH_CMD_BODY;
|
|
|
|
sc.Forward();
|
|
|
|
} else if (cmdState == BASH_CMD_TEST && IsASpace(sc.chPrev)) {
|
|
|
|
if (sc.Match(']', ']') && testExprType == 1) {
|
|
|
|
sc.Forward();
|
|
|
|
cmdState = BASH_CMD_BODY;
|
|
|
|
} else if (sc.ch == ']' && testExprType == 2) {
|
|
|
|
cmdState = BASH_CMD_BODY;
|
|
|
|
}
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
}
|
2010-08-21 23:59:56 +00:00
|
|
|
}// sc.state
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.Complete();
|
2013-08-28 00:44:27 +00:00
|
|
|
if (sc.state == SCE_SH_HERE_Q) {
|
|
|
|
styler.ChangeLexerState(sc.currentPos, styler.Length());
|
|
|
|
}
|
|
|
|
sc.Complete();
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
void SCI_METHOD LexerBash::Fold(Sci_PositionU startPos, Sci_Position length, int, IDocument *pAccess) {
|
|
|
|
if(!options.fold)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LexAccessor styler(pAccess);
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU endPos = startPos + length;
|
2009-04-24 23:35:41 +00:00
|
|
|
int visibleChars = 0;
|
2015-06-07 21:19:26 +00:00
|
|
|
int skipHereCh = 0;
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position lineCurrent = styler.GetLine(startPos);
|
2009-04-24 23:35:41 +00:00
|
|
|
int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
|
|
|
|
int levelCurrent = levelPrev;
|
|
|
|
char chNext = styler[startPos];
|
|
|
|
int styleNext = styler.StyleAt(startPos);
|
2019-05-04 18:14:48 +00:00
|
|
|
char word[8] = { '\0' }; // we're not interested in long words anyway
|
|
|
|
unsigned int wordlen = 0;
|
|
|
|
for (Sci_PositionU i = startPos; i < endPos; i++) {
|
2009-04-24 23:35:41 +00:00
|
|
|
char ch = chNext;
|
|
|
|
chNext = styler.SafeGetCharAt(i + 1);
|
|
|
|
int style = styleNext;
|
|
|
|
styleNext = styler.StyleAt(i + 1);
|
|
|
|
bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
|
2009-04-25 23:38:15 +00:00
|
|
|
// Comment folding
|
2019-05-04 18:14:48 +00:00
|
|
|
if (options.foldComment && atEOL && IsCommentLine(lineCurrent, styler))
|
2009-04-25 23:38:15 +00:00
|
|
|
{
|
|
|
|
if (!IsCommentLine(lineCurrent - 1, styler)
|
|
|
|
&& IsCommentLine(lineCurrent + 1, styler))
|
|
|
|
levelCurrent++;
|
|
|
|
else if (IsCommentLine(lineCurrent - 1, styler)
|
|
|
|
&& !IsCommentLine(lineCurrent + 1, styler))
|
|
|
|
levelCurrent--;
|
|
|
|
}
|
2019-05-04 18:14:48 +00:00
|
|
|
if (style == SCE_SH_WORD) {
|
|
|
|
if ((wordlen + 1) < sizeof(word))
|
|
|
|
word[wordlen++] = ch;
|
|
|
|
if (styleNext != style) {
|
|
|
|
word[wordlen] = '\0';
|
|
|
|
wordlen = 0;
|
|
|
|
if (strcmp(word, "if") == 0 || strcmp(word, "case") == 0 || strcmp(word, "do") == 0) {
|
|
|
|
levelCurrent++;
|
|
|
|
} else if (strcmp(word, "fi") == 0 || strcmp(word, "esac") == 0 || strcmp(word, "done") == 0) {
|
|
|
|
levelCurrent--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
if (style == SCE_SH_OPERATOR) {
|
|
|
|
if (ch == '{') {
|
|
|
|
levelCurrent++;
|
|
|
|
} else if (ch == '}') {
|
|
|
|
levelCurrent--;
|
|
|
|
}
|
|
|
|
}
|
2011-03-22 00:16:49 +00:00
|
|
|
// Here Document folding
|
|
|
|
if (style == SCE_SH_HERE_DELIM) {
|
|
|
|
if (ch == '<' && chNext == '<') {
|
2015-06-07 21:19:26 +00:00
|
|
|
if (styler.SafeGetCharAt(i + 2) == '<') {
|
|
|
|
skipHereCh = 1;
|
|
|
|
} else {
|
|
|
|
if (skipHereCh == 0) {
|
|
|
|
levelCurrent++;
|
|
|
|
} else {
|
|
|
|
skipHereCh = 0;
|
|
|
|
}
|
|
|
|
}
|
2011-03-22 00:16:49 +00:00
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
} else if (style == SCE_SH_HERE_Q && styler.StyleAt(i+1) == SCE_SH_DEFAULT) {
|
2011-03-22 00:16:49 +00:00
|
|
|
levelCurrent--;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
if (atEOL) {
|
|
|
|
int lev = levelPrev;
|
2019-05-04 18:14:48 +00:00
|
|
|
if (visibleChars == 0 && options.foldCompact)
|
2009-04-24 23:35:41 +00:00
|
|
|
lev |= SC_FOLDLEVELWHITEFLAG;
|
|
|
|
if ((levelCurrent > levelPrev) && (visibleChars > 0))
|
|
|
|
lev |= SC_FOLDLEVELHEADERFLAG;
|
|
|
|
if (lev != styler.LevelAt(lineCurrent)) {
|
|
|
|
styler.SetLevel(lineCurrent, lev);
|
|
|
|
}
|
|
|
|
lineCurrent++;
|
|
|
|
levelPrev = levelCurrent;
|
|
|
|
visibleChars = 0;
|
|
|
|
}
|
|
|
|
if (!isspacechar(ch))
|
|
|
|
visibleChars++;
|
|
|
|
}
|
|
|
|
// Fill in the real level of the next line, keeping the current flags as they will be filled in later
|
|
|
|
int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
|
|
|
|
styler.SetLevel(lineCurrent, levelPrev | flagsNext);
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
LexerModule lmBash(SCLEX_BASH, LexerBash::LexerFactoryBash, "bash", bashWordListDesc);
|