2009-04-24 23:35:41 +00:00
|
|
|
// Scintilla source code edit control
|
|
|
|
/** @file LexInno.cxx
|
|
|
|
** Lexer for Inno Setup scripts.
|
|
|
|
**/
|
|
|
|
// Written by Friedrich Vedder <fvedd@t-online.de>, using code from LexOthers.cxx.
|
|
|
|
// 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>
|
|
|
|
#include <ctype.h>
|
2009-04-24 23:35:41 +00:00
|
|
|
|
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
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
#include "WordList.h"
|
|
|
|
#include "LexAccessor.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
#include "Accessor.h"
|
|
|
|
#include "StyleContext.h"
|
2010-08-21 23:59:56 +00:00
|
|
|
#include "CharacterSet.h"
|
|
|
|
#include "LexerModule.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
using namespace Scintilla;
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static void ColouriseInnoDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *keywordLists[], Accessor &styler) {
|
2009-04-24 23:35:41 +00:00
|
|
|
int state = SCE_INNO_DEFAULT;
|
|
|
|
char chPrev;
|
|
|
|
char ch = 0;
|
|
|
|
char chNext = styler[startPos];
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position lengthDoc = startPos + length;
|
|
|
|
char *buffer = new char[length+1];
|
|
|
|
Sci_Position bufferCount = 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
bool isBOL, isEOL, isWS, isBOLWS = 0;
|
2009-06-24 19:09:31 +00:00
|
|
|
bool isCStyleComment = false;
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
WordList §ionKeywords = *keywordLists[0];
|
|
|
|
WordList &standardKeywords = *keywordLists[1];
|
|
|
|
WordList ¶meterKeywords = *keywordLists[2];
|
|
|
|
WordList &preprocessorKeywords = *keywordLists[3];
|
|
|
|
WordList &pascalKeywords = *keywordLists[4];
|
|
|
|
WordList &userKeywords = *keywordLists[5];
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position curLine = styler.GetLine(startPos);
|
2011-07-17 22:30:49 +00:00
|
|
|
int curLineState = curLine > 0 ? styler.GetLineState(curLine - 1) : 0;
|
|
|
|
bool isCode = (curLineState == 1);
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
// Go through all provided text segment
|
|
|
|
// using the hand-written state machine shown below
|
|
|
|
styler.StartAt(startPos);
|
|
|
|
styler.StartSegment(startPos);
|
2019-05-04 18:14:48 +00:00
|
|
|
for (Sci_Position i = startPos; i < lengthDoc; i++) {
|
2009-04-24 23:35:41 +00:00
|
|
|
chPrev = ch;
|
|
|
|
ch = chNext;
|
|
|
|
chNext = styler.SafeGetCharAt(i + 1);
|
|
|
|
|
|
|
|
if (styler.IsLeadByte(ch)) {
|
|
|
|
chNext = styler.SafeGetCharAt(i + 2);
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
isBOL = (chPrev == 0) || (chPrev == '\n') || (chPrev == '\r' && ch != '\n');
|
|
|
|
isBOLWS = (isBOL) ? 1 : (isBOLWS && (chPrev == ' ' || chPrev == '\t'));
|
|
|
|
isEOL = (ch == '\n' || ch == '\r');
|
|
|
|
isWS = (ch == ' ' || ch == '\t');
|
|
|
|
|
2011-07-17 22:30:49 +00:00
|
|
|
if ((ch == '\r' && chNext != '\n') || (ch == '\n')) {
|
|
|
|
// Remember the line state for future incremental lexing
|
|
|
|
curLine = styler.GetLine(i);
|
|
|
|
styler.SetLineState(curLine, (isCode ? 1 : 0));
|
|
|
|
}
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
switch(state) {
|
|
|
|
case SCE_INNO_DEFAULT:
|
2009-06-24 19:09:31 +00:00
|
|
|
if (!isCode && ch == ';' && isBOLWS) {
|
2009-04-24 23:35:41 +00:00
|
|
|
// Start of a comment
|
|
|
|
state = SCE_INNO_COMMENT;
|
|
|
|
} else if (ch == '[' && isBOLWS) {
|
|
|
|
// Start of a section name
|
|
|
|
bufferCount = 0;
|
|
|
|
state = SCE_INNO_SECTION;
|
|
|
|
} else if (ch == '#' && isBOLWS) {
|
|
|
|
// Start of a preprocessor directive
|
|
|
|
state = SCE_INNO_PREPROC;
|
2009-06-24 19:09:31 +00:00
|
|
|
} else if (!isCode && ch == '{' && chNext != '{' && chPrev != '{') {
|
|
|
|
// Start of an inline expansion
|
|
|
|
state = SCE_INNO_INLINE_EXPANSION;
|
|
|
|
} else if (isCode && (ch == '{' || (ch == '(' && chNext == '*'))) {
|
2009-04-24 23:35:41 +00:00
|
|
|
// Start of a Pascal comment
|
|
|
|
state = SCE_INNO_COMMENT_PASCAL;
|
2009-06-24 19:09:31 +00:00
|
|
|
isCStyleComment = false;
|
|
|
|
} else if (isCode && ch == '/' && chNext == '/') {
|
|
|
|
// Apparently, C-style comments are legal, too
|
|
|
|
state = SCE_INNO_COMMENT_PASCAL;
|
|
|
|
isCStyleComment = true;
|
2009-04-24 23:35:41 +00:00
|
|
|
} else if (ch == '"') {
|
|
|
|
// Start of a double-quote string
|
|
|
|
state = SCE_INNO_STRING_DOUBLE;
|
|
|
|
} else if (ch == '\'') {
|
|
|
|
// Start of a single-quote string
|
|
|
|
state = SCE_INNO_STRING_SINGLE;
|
2015-06-07 21:19:26 +00:00
|
|
|
} else if (IsASCII(ch) && (isalpha(ch) || (ch == '_'))) {
|
2009-04-24 23:35:41 +00:00
|
|
|
// Start of an identifier
|
|
|
|
bufferCount = 0;
|
|
|
|
buffer[bufferCount++] = static_cast<char>(tolower(ch));
|
|
|
|
state = SCE_INNO_IDENTIFIER;
|
|
|
|
} else {
|
|
|
|
// Style it the default style
|
|
|
|
styler.ColourTo(i,SCE_INNO_DEFAULT);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCE_INNO_COMMENT:
|
|
|
|
if (isEOL) {
|
|
|
|
state = SCE_INNO_DEFAULT;
|
|
|
|
styler.ColourTo(i,SCE_INNO_COMMENT);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCE_INNO_IDENTIFIER:
|
2015-06-07 21:19:26 +00:00
|
|
|
if (IsASCII(ch) && (isalnum(ch) || (ch == '_'))) {
|
2009-04-24 23:35:41 +00:00
|
|
|
buffer[bufferCount++] = static_cast<char>(tolower(ch));
|
|
|
|
} else {
|
|
|
|
state = SCE_INNO_DEFAULT;
|
|
|
|
buffer[bufferCount] = '\0';
|
|
|
|
|
|
|
|
// Check if the buffer contains a keyword
|
2009-06-24 19:09:31 +00:00
|
|
|
if (!isCode && standardKeywords.InList(buffer)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
styler.ColourTo(i-1,SCE_INNO_KEYWORD);
|
2009-06-24 19:09:31 +00:00
|
|
|
} else if (!isCode && parameterKeywords.InList(buffer)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
styler.ColourTo(i-1,SCE_INNO_PARAMETER);
|
2009-06-24 19:09:31 +00:00
|
|
|
} else if (isCode && pascalKeywords.InList(buffer)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
styler.ColourTo(i-1,SCE_INNO_KEYWORD_PASCAL);
|
2009-06-24 19:09:31 +00:00
|
|
|
} else if (!isCode && userKeywords.InList(buffer)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
styler.ColourTo(i-1,SCE_INNO_KEYWORD_USER);
|
|
|
|
} else {
|
|
|
|
styler.ColourTo(i-1,SCE_INNO_DEFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push back the faulty character
|
|
|
|
chNext = styler[i--];
|
|
|
|
ch = chPrev;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCE_INNO_SECTION:
|
|
|
|
if (ch == ']') {
|
|
|
|
state = SCE_INNO_DEFAULT;
|
|
|
|
buffer[bufferCount] = '\0';
|
|
|
|
|
|
|
|
// Check if the buffer contains a section name
|
|
|
|
if (sectionKeywords.InList(buffer)) {
|
|
|
|
styler.ColourTo(i,SCE_INNO_SECTION);
|
2009-06-24 19:09:31 +00:00
|
|
|
isCode = !CompareCaseInsensitive(buffer, "code");
|
2009-04-24 23:35:41 +00:00
|
|
|
} else {
|
|
|
|
styler.ColourTo(i,SCE_INNO_DEFAULT);
|
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
} else if (IsASCII(ch) && (isalnum(ch) || (ch == '_'))) {
|
2009-04-24 23:35:41 +00:00
|
|
|
buffer[bufferCount++] = static_cast<char>(tolower(ch));
|
|
|
|
} else {
|
|
|
|
state = SCE_INNO_DEFAULT;
|
|
|
|
styler.ColourTo(i,SCE_INNO_DEFAULT);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCE_INNO_PREPROC:
|
|
|
|
if (isWS || isEOL) {
|
2015-06-07 21:19:26 +00:00
|
|
|
if (IsASCII(chPrev) && isalpha(chPrev)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
state = SCE_INNO_DEFAULT;
|
|
|
|
buffer[bufferCount] = '\0';
|
|
|
|
|
|
|
|
// Check if the buffer contains a preprocessor directive
|
|
|
|
if (preprocessorKeywords.InList(buffer)) {
|
|
|
|
styler.ColourTo(i-1,SCE_INNO_PREPROC);
|
|
|
|
} else {
|
|
|
|
styler.ColourTo(i-1,SCE_INNO_DEFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push back the faulty character
|
|
|
|
chNext = styler[i--];
|
|
|
|
ch = chPrev;
|
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
} else if (IsASCII(ch) && isalpha(ch)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (chPrev == '#' || chPrev == ' ' || chPrev == '\t')
|
|
|
|
bufferCount = 0;
|
|
|
|
buffer[bufferCount++] = static_cast<char>(tolower(ch));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCE_INNO_STRING_DOUBLE:
|
|
|
|
if (ch == '"' || isEOL) {
|
|
|
|
state = SCE_INNO_DEFAULT;
|
|
|
|
styler.ColourTo(i,SCE_INNO_STRING_DOUBLE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCE_INNO_STRING_SINGLE:
|
|
|
|
if (ch == '\'' || isEOL) {
|
|
|
|
state = SCE_INNO_DEFAULT;
|
|
|
|
styler.ColourTo(i,SCE_INNO_STRING_SINGLE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-06-24 19:09:31 +00:00
|
|
|
case SCE_INNO_INLINE_EXPANSION:
|
2009-04-24 23:35:41 +00:00
|
|
|
if (ch == '}') {
|
|
|
|
state = SCE_INNO_DEFAULT;
|
2009-06-24 19:09:31 +00:00
|
|
|
styler.ColourTo(i,SCE_INNO_INLINE_EXPANSION);
|
2009-04-24 23:35:41 +00:00
|
|
|
} else if (isEOL) {
|
|
|
|
state = SCE_INNO_DEFAULT;
|
|
|
|
styler.ColourTo(i,SCE_INNO_DEFAULT);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCE_INNO_COMMENT_PASCAL:
|
2009-06-24 19:09:31 +00:00
|
|
|
if (isCStyleComment) {
|
|
|
|
if (isEOL) {
|
|
|
|
state = SCE_INNO_DEFAULT;
|
|
|
|
styler.ColourTo(i,SCE_INNO_COMMENT_PASCAL);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ch == '}' || (ch == ')' && chPrev == '*')) {
|
|
|
|
state = SCE_INNO_DEFAULT;
|
|
|
|
styler.ColourTo(i,SCE_INNO_COMMENT_PASCAL);
|
|
|
|
} else if (isEOL) {
|
|
|
|
state = SCE_INNO_DEFAULT;
|
|
|
|
styler.ColourTo(i,SCE_INNO_DEFAULT);
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete []buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char * const innoWordListDesc[] = {
|
|
|
|
"Sections",
|
|
|
|
"Keywords",
|
|
|
|
"Parameters",
|
|
|
|
"Preprocessor directives",
|
|
|
|
"Pascal keywords",
|
|
|
|
"User defined keywords",
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static void FoldInnoDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler) {
|
|
|
|
Sci_PositionU endPos = startPos + length;
|
2009-04-25 23:38:15 +00:00
|
|
|
char chNext = styler[startPos];
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position lineCurrent = styler.GetLine(startPos);
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
bool sectionFlag = false;
|
|
|
|
int levelPrev = lineCurrent > 0 ? styler.LevelAt(lineCurrent - 1) : SC_FOLDLEVELBASE;
|
|
|
|
int level;
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
for (Sci_PositionU i = startPos; i < endPos; i++) {
|
2009-04-24 23:35:41 +00:00
|
|
|
char ch = chNext;
|
|
|
|
chNext = styler[i+1];
|
|
|
|
bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
|
2009-04-25 23:38:15 +00:00
|
|
|
int style = styler.StyleAt(i);
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
if (style == SCE_INNO_SECTION)
|
2009-04-25 23:38:15 +00:00
|
|
|
sectionFlag = true;
|
|
|
|
|
|
|
|
if (atEOL || i == endPos - 1) {
|
|
|
|
if (sectionFlag) {
|
|
|
|
level = SC_FOLDLEVELBASE | SC_FOLDLEVELHEADERFLAG;
|
|
|
|
if (level == levelPrev)
|
|
|
|
styler.SetLevel(lineCurrent - 1, levelPrev & ~SC_FOLDLEVELHEADERFLAG);
|
|
|
|
} else {
|
|
|
|
level = levelPrev & SC_FOLDLEVELNUMBERMASK;
|
|
|
|
if (levelPrev & SC_FOLDLEVELHEADERFLAG)
|
|
|
|
level++;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
styler.SetLevel(lineCurrent, level);
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
levelPrev = level;
|
2009-04-24 23:35:41 +00:00
|
|
|
lineCurrent++;
|
2009-04-25 23:38:15 +00:00
|
|
|
sectionFlag = false;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LexerModule lmInno(SCLEX_INNOSETUP, ColouriseInnoDoc, "inno", FoldInnoDoc, innoWordListDesc);
|