📄 lexbash.cxx
字号:
// Scintilla source code edit control/** @file LexBash.cxx ** Lexer for Bash. **/// Copyright 2004-2005 by Neil Hodgson <neilh@scintilla.org>// Adapted from LexPerl by Kein-Hong Man <mkh@pl.jaring.my> 2004// The License.txt file describes the conditions under which this software may be distributed.#include <stdlib.h>#include <string.h>#include <ctype.h>#include <stdio.h>#include <stdarg.h>#include "Platform.h"#include "PropSet.h"#include "Accessor.h"#include "KeyWords.h"#include "Scintilla.h"#include "SciLexer.h"#define BASH_BASE_ERROR 65#define BASH_BASE_DECIMAL 66#define BASH_BASE_HEX 67#define BASH_BASE_OCTAL 68#define BASH_BASE_OCTAL_ERROR 69#define HERE_DELIM_MAX 256static inline int translateBashDigit(char ch) { 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;}static inline bool isEOLChar(char ch) { return (ch == '\r') || (ch == '\n');}static bool isSingleCharOp(char ch) { char strCharSet[2]; strCharSet[0] = ch; strCharSet[1] = '\0'; return (NULL != strstr("rwxoRWXOezsfdlpSbctugkTBMACahGLNn", strCharSet));}static inline bool isBashOperator(char ch) { if (ch == '^' || ch == '&' || ch == '\\' || ch == '%' || ch == '(' || ch == ')' || ch == '-' || ch == '+' || ch == '=' || ch == '|' || ch == '{' || ch == '}' || ch == '[' || ch == ']' || ch == ':' || ch == ';' || ch == '>' || ch == ',' || ch == '/' || ch == '<' || ch == '?' || ch == '!' || ch == '.' || ch == '~' || ch == '@') return true; return false;}static int classifyWordBash(unsigned int start, unsigned int end, WordList &keywords, Accessor &styler) { char s[100]; for (unsigned int i = 0; i < end - start + 1 && i < 30; i++) { s[i] = styler[start + i]; s[i + 1] = '\0'; } char chAttr = SCE_SH_IDENTIFIER; if (keywords.InList(s)) chAttr = SCE_SH_WORD; styler.ColourTo(end, chAttr); return chAttr;}static inline int getBashNumberBase(unsigned int start, unsigned int end, Accessor &styler) { int base = 0; for (unsigned int i = 0; i < end - start + 1 && i < 10; i++) { base = base * 10 + (styler[start + i] - '0'); } if (base > 64 || (end - start) > 1) { return BASH_BASE_ERROR; } return base;}static inline bool isEndVar(char ch) { return !isalnum(ch) && ch != '$' && ch != '_';}static inline bool isNonQuote(char ch) { return isalnum(ch) || ch == '_';}static bool isMatch(Accessor &styler, int lengthDoc, int pos, const char *val) { if ((pos + static_cast<int>(strlen(val))) >= lengthDoc) { return false; } while (*val) { if (*val != styler[pos++]) { return false; } val++; } return true;}static char opposite(char ch) { if (ch == '(') return ')'; if (ch == '[') return ']'; if (ch == '{') return '}'; if (ch == '<') return '>'; return ch;}static void ColouriseBashDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[], Accessor &styler) { // Lexer for bash often has to backtrack to start of current style to determine // which characters are being used as quotes, how deeply nested is the // start position and what the termination string is for here documents WordList &keywords = *keywordlists[0]; class HereDocCls { public: int State; // 0: '<<' encountered // 1: collect the delimiter // 2: here doc text (lines after the delimiter) char Quote; // the char after '<<' bool Quoted; // true if Quote in ('\'','"','`') bool Indent; // indented delimiter (for <<-) int DelimiterLength; // strlen(Delimiter) char *Delimiter; // the Delimiter, 256: sizeof PL_tokenbuf HereDocCls() { State = 0; Quote = 0; Quoted = false; Indent = 0; DelimiterLength = 0; Delimiter = new char[HERE_DELIM_MAX]; Delimiter[0] = '\0'; } ~HereDocCls() { delete []Delimiter; } }; HereDocCls HereDoc; class QuoteCls { public: int Rep; int Count; char Up; char Down; QuoteCls() { this->New(1); } void New(int r) { Rep = r; Count = 0; Up = '\0'; Down = '\0'; } void Open(char u) { Count++; Up = u; Down = opposite(Up); } }; QuoteCls Quote; int state = initStyle; int numBase = 0; unsigned int lengthDoc = startPos + length; // If in a long distance lexical state, seek to the beginning to find quote characters // Bash strings can be multi-line with embedded newlines, so backtrack. // Bash numbers have additional state during lexing, so backtrack too. if (state == SCE_SH_HERE_Q) { while ((startPos > 1) && (styler.StyleAt(startPos) != SCE_SH_HERE_DELIM)) { startPos--; } startPos = styler.LineStart(styler.GetLine(startPos)); state = styler.StyleAt(startPos - 1); } if (state == SCE_SH_STRING || state == SCE_SH_BACKTICKS || state == SCE_SH_CHARACTER || state == SCE_SH_NUMBER || state == SCE_SH_IDENTIFIER || state == SCE_SH_COMMENTLINE ) { while ((startPos > 1) && (styler.StyleAt(startPos - 1) == state)) { startPos--; } state = SCE_SH_DEFAULT; } styler.StartAt(startPos); char chPrev = styler.SafeGetCharAt(startPos - 1); if (startPos == 0) chPrev = '\n'; char chNext = styler[startPos]; styler.StartSegment(startPos); for (unsigned int i = startPos; i < lengthDoc; i++) { char ch = chNext; // if the current character is not consumed due to the completion of an // earlier style, lexing can be restarted via a simple goto restartLexer: chNext = styler.SafeGetCharAt(i + 1); char chNext2 = styler.SafeGetCharAt(i + 2); if (styler.IsLeadByte(ch)) { chNext = styler.SafeGetCharAt(i + 2); chPrev = ' '; i += 1; continue; } if ((chPrev == '\r' && ch == '\n')) { // skip on DOS/Windows styler.ColourTo(i, state); chPrev = ch; continue; } if (HereDoc.State == 1 && isEOLChar(ch)) { // 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 (state == SCE_SH_HERE_DELIM) { // Missing quote at end of string! We are stricter than bash. // Colour here-doc anyway while marking this bit as an error. state = SCE_SH_ERROR; } styler.ColourTo(i - 1, state); // HereDoc.Quote always == '\'' state = SCE_SH_HERE_Q; } else { styler.ColourTo(i - 1, state); // always switch state = SCE_SH_HERE_Q; } } if (state == SCE_SH_DEFAULT) { if (ch == '\\') { // escaped character if (i < lengthDoc - 1) i++; ch = chNext; chNext = chNext2; styler.ColourTo(i, SCE_SH_IDENTIFIER); } else if (isdigit(ch)) { state = SCE_SH_NUMBER; numBase = BASH_BASE_DECIMAL; if (ch == '0') { // hex,octal if (chNext == 'x' || chNext == 'X') { numBase = BASH_BASE_HEX; i++; ch = chNext; chNext = chNext2; } else if (isdigit(chNext)) { numBase = BASH_BASE_OCTAL; } } } else if (iswordstart(ch)) { state = SCE_SH_WORD; if (!iswordchar(chNext) && chNext != '+' && chNext != '-') { // We need that if length of word == 1! // This test is copied from the SCE_SH_WORD handler. classifyWordBash(styler.GetStartSegment(), i, keywords, styler); state = SCE_SH_DEFAULT; } } else if (ch == '#') { state = SCE_SH_COMMENTLINE; } else if (ch == '\"') { state = SCE_SH_STRING; Quote.New(1); Quote.Open(ch); } else if (ch == '\'') { state = SCE_SH_CHARACTER; Quote.New(1); Quote.Open(ch); } else if (ch == '`') { state = SCE_SH_BACKTICKS; Quote.New(1); Quote.Open(ch); } else if (ch == '$') { if (chNext == '{') { state = SCE_SH_PARAM; goto startQuote; } else if (chNext == '\'') { state = SCE_SH_CHARACTER; goto startQuote; } else if (chNext == '"') { state = SCE_SH_STRING; goto startQuote; } else if (chNext == '(' && chNext2 == '(') { styler.ColourTo(i, SCE_SH_OPERATOR); state = SCE_SH_DEFAULT; goto skipChar; } else if (chNext == '(' || chNext == '`') { state = SCE_SH_BACKTICKS; startQuote: Quote.New(1); Quote.Open(chNext); goto skipChar; } else { state = SCE_SH_SCALAR; skipChar: i++; ch = chNext; chNext = chNext2; } } else if (ch == '*') { if (chNext == '*') { // exponentiation i++; ch = chNext; chNext = chNext2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -