1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
// 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.
// Modified by Michael Heath.
// 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>
#include <assert.h>
#include <ctype.h>
#include <string>
#include <string_view>
#include "ILexer.h"
#include "Scintilla.h"
#include "SciLexer.h"
#include "WordList.h"
#include "LexAccessor.h"
#include "Accessor.h"
#include "StyleContext.h"
#include "CharacterSet.h"
#include "LexerModule.h"
using namespace Lexilla;
static bool innoIsBlank(int ch) {
return (ch == ' ') || (ch == '\t');
}
static bool innoNextNotBlankIs(Sci_Position i, Accessor &styler, char needle) {
char ch;
while (i < styler.Length()) {
ch = styler.SafeGetCharAt(i);
if (ch == needle)
return true;
if (!innoIsBlank(ch))
return false;
i++;
}
return false;
}
static void ColouriseInnoDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *keywordLists[], Accessor &styler) {
int state = SCE_INNO_DEFAULT;
char chPrev;
char ch = 0;
char chNext = styler[startPos];
Sci_Position lengthDoc = startPos + length;
char *buffer = new char[length + 1];
Sci_Position bufferCount = 0;
bool isBOL, isEOL, isWS, isBOLWS = 0;
// Save line state later with bitState and bitand with bit... to get line state
int bitState = 0;
int const bitCode = 1, bitMessages = 2, bitCommentCurly = 4, bitCommentRound = 8;
// Get keyword lists
WordList §ionKeywords = *keywordLists[0];
WordList &standardKeywords = *keywordLists[1];
WordList ¶meterKeywords = *keywordLists[2];
WordList &preprocessorKeywords = *keywordLists[3];
WordList &pascalKeywords = *keywordLists[4];
WordList &userKeywords = *keywordLists[5];
// Get line state
Sci_Position curLine = styler.GetLine(startPos);
int curLineState = curLine > 0 ? styler.GetLineState(curLine - 1) : 0;
bool isCode = (curLineState & bitCode);
bool isMessages = (curLineState & bitMessages);
bool isCommentCurly = (curLineState & bitCommentCurly);
bool isCommentRound = (curLineState & bitCommentRound);
bool isCommentSlash = false;
// Continue Pascal multline comment state
if (isCommentCurly || isCommentRound)
state = SCE_INNO_COMMENT_PASCAL;
// Go through all provided text segment
// using the hand-written state machine shown below
styler.StartAt(startPos);
styler.StartSegment(startPos);
for (Sci_Position i = startPos; i < lengthDoc; i++) {
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');
if ((ch == '\r' && chNext != '\n') || (ch == '\n')) {
// Remember the line state for future incremental lexing
curLine = styler.GetLine(i);
bitState = 0;
if (isCode)
bitState |= bitCode;
if (isMessages)
bitState |= bitMessages;
if (isCommentCurly)
bitState |= bitCommentCurly;
if (isCommentRound)
bitState |= bitCommentRound;
styler.SetLineState(curLine, bitState);
}
switch(state) {
case SCE_INNO_DEFAULT:
if (!isCode && ch == ';' && isBOLWS) {
// Start of a comment
state = SCE_INNO_COMMENT;
styler.ColourTo(i, SCE_INNO_COMMENT);
} else if (ch == '[' && isBOLWS) {
// Start of a section name
state = SCE_INNO_SECTION;
bufferCount = 0;
} else if (ch == '#' && isBOLWS) {
// Start of a preprocessor directive
state = SCE_INNO_PREPROC;
} else if (!isCode && ch == '{' && chNext != '{' && chPrev != '{') {
// Start of an inline expansion
state = SCE_INNO_INLINE_EXPANSION;
} else if (isCode && ch == '{') {
// Start of a Pascal comment
state = SCE_INNO_COMMENT_PASCAL;
isCommentCurly = true;
styler.ColourTo(i, SCE_INNO_COMMENT_PASCAL);
} else if (isCode && (ch == '(' && chNext == '*')) {
// Start of a Pascal comment
state = SCE_INNO_COMMENT_PASCAL;
isCommentRound = true;
styler.ColourTo(i + 1, SCE_INNO_COMMENT_PASCAL);
} else if (isCode && ch == '/' && chNext == '/') {
// Start of C-style comment
state = SCE_INNO_COMMENT_PASCAL;
isCommentSlash = true;
styler.ColourTo(i + 1, SCE_INNO_COMMENT_PASCAL);
} else if (!isMessages && ch == '"') {
// Start of a double-quote string
state = SCE_INNO_STRING_DOUBLE;
styler.ColourTo(i, SCE_INNO_STRING_DOUBLE);
} else if (!isMessages && ch == '\'') {
// Start of a single-quote string
state = SCE_INNO_STRING_SINGLE;
styler.ColourTo(i, SCE_INNO_STRING_SINGLE);
} else if (!isMessages && IsASCII(ch) && (isalpha(ch) || (ch == '_'))) {
// Start of an identifier
state = SCE_INNO_IDENTIFIER;
bufferCount = 0;
buffer[bufferCount++] = static_cast<char>(tolower(ch));
} 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 - 1, SCE_INNO_COMMENT);
styler.ColourTo(i, SCE_INNO_DEFAULT);
} else {
styler.ColourTo(i, SCE_INNO_COMMENT);
}
break;
case SCE_INNO_IDENTIFIER:
if (IsASCII(ch) && (isalnum(ch) || (ch == '_'))) {
buffer[bufferCount++] = static_cast<char>(tolower(ch));
} else {
state = SCE_INNO_DEFAULT;
buffer[bufferCount] = '\0';
// Check if the buffer contains a keyword
if (!isCode && standardKeywords.InList(buffer) && innoNextNotBlankIs(i, styler, '=')) {
styler.ColourTo(i - 1, SCE_INNO_KEYWORD);
} else if (!isCode && parameterKeywords.InList(buffer) && innoNextNotBlankIs(i, styler, ':')) {
styler.ColourTo(i - 1, SCE_INNO_PARAMETER);
} else if (isCode && pascalKeywords.InList(buffer)) {
styler.ColourTo(i - 1, SCE_INNO_KEYWORD_PASCAL);
} else if (!isCode && userKeywords.InList(buffer)) {
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);
isCode = !CompareCaseInsensitive(buffer, "code");
isMessages = isCode ? false : (
!CompareCaseInsensitive(buffer, "custommessages")
|| !CompareCaseInsensitive(buffer, "messages"));
} else {
styler.ColourTo(i, SCE_INNO_DEFAULT);
}
} else if (IsASCII(ch) && (isalnum(ch) || (ch == '_'))) {
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) {
if (IsASCII(chPrev) && isalpha(chPrev)) {
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;
}
} else if (IsASCII(ch) && isalpha(ch)) {
if (chPrev == '#' || chPrev == ' ' || chPrev == '\t')
bufferCount = 0;
buffer[bufferCount++] = static_cast<char>(tolower(ch));
}
break;
case SCE_INNO_STRING_DOUBLE:
if (ch == '"') {
state = SCE_INNO_DEFAULT;
styler.ColourTo(i, SCE_INNO_STRING_DOUBLE);
} else if (isEOL) {
state = SCE_INNO_DEFAULT;
styler.ColourTo(i - 1, SCE_INNO_STRING_DOUBLE);
styler.ColourTo(i, SCE_INNO_DEFAULT);
} else {
styler.ColourTo(i, SCE_INNO_STRING_DOUBLE);
}
break;
case SCE_INNO_STRING_SINGLE:
if (ch == '\'') {
state = SCE_INNO_DEFAULT;
styler.ColourTo(i, SCE_INNO_STRING_SINGLE);
} else if (isEOL) {
state = SCE_INNO_DEFAULT;
styler.ColourTo(i - 1, SCE_INNO_STRING_SINGLE);
styler.ColourTo(i, SCE_INNO_DEFAULT);
} else {
styler.ColourTo(i, SCE_INNO_STRING_SINGLE);
}
break;
case SCE_INNO_INLINE_EXPANSION:
if (ch == '}') {
state = SCE_INNO_DEFAULT;
styler.ColourTo(i, SCE_INNO_INLINE_EXPANSION);
} else if (isEOL) {
state = SCE_INNO_DEFAULT;
styler.ColourTo(i, SCE_INNO_DEFAULT);
}
break;
case SCE_INNO_COMMENT_PASCAL:
if (isCommentSlash) {
if (isEOL) {
state = SCE_INNO_DEFAULT;
isCommentSlash = false;
styler.ColourTo(i - 1, SCE_INNO_COMMENT_PASCAL);
styler.ColourTo(i, SCE_INNO_DEFAULT);
} else {
styler.ColourTo(i, SCE_INNO_COMMENT_PASCAL);
}
} else if (isCommentCurly) {
if (ch == '}') {
state = SCE_INNO_DEFAULT;
isCommentCurly = false;
}
styler.ColourTo(i, SCE_INNO_COMMENT_PASCAL);
} else if (isCommentRound) {
if (ch == ')' && chPrev == '*') {
state = SCE_INNO_DEFAULT;
isCommentRound = false;
}
styler.ColourTo(i, SCE_INNO_COMMENT_PASCAL);
}
break;
}
}
delete []buffer;
}
static const char * const innoWordListDesc[] = {
"Sections",
"Keywords",
"Parameters",
"Preprocessor directives",
"Pascal keywords",
"User defined keywords",
0
};
static void FoldInnoDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler) {
Sci_PositionU endPos = startPos + length;
char chNext = styler[startPos];
Sci_Position lineCurrent = styler.GetLine(startPos);
bool sectionFlag = false;
int levelPrev = lineCurrent > 0 ? styler.LevelAt(lineCurrent - 1) : SC_FOLDLEVELBASE;
int level;
for (Sci_PositionU i = startPos; i < endPos; i++) {
char ch = chNext;
chNext = styler[i + 1];
bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
int style = styler.StyleAt(i);
if (style == SCE_INNO_SECTION)
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++;
}
styler.SetLevel(lineCurrent, level);
levelPrev = level;
lineCurrent++;
sectionFlag = false;
}
}
}
LexerModule lmInno(SCLEX_INNOSETUP, ColouriseInnoDoc, "inno", FoldInnoDoc, innoWordListDesc);
|