| 1 |
// Scintilla source code edit control |
| 2 |
/** @file LexNsis.cxx |
| 3 |
** Lexer for NSIS |
| 4 |
**/ |
| 5 |
// Copyright 2003 - 2005 by Angelo Mandato <angelo [at] spaceblue [dot] com> |
| 6 |
// Last Updated: 03/13/2005 |
| 7 |
// The License.txt file describes the conditions under which this software may be distributed. |
| 8 |
|
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <stdarg.h> |
| 13 |
#include <assert.h> |
| 14 |
#include <ctype.h> |
| 15 |
|
| 16 |
#include <string> |
| 17 |
#include <string_view> |
| 18 |
|
| 19 |
#include "ILexer.h" |
| 20 |
#include "Scintilla.h" |
| 21 |
#include "SciLexer.h" |
| 22 |
|
| 23 |
#include "WordList.h" |
| 24 |
#include "LexAccessor.h" |
| 25 |
#include "Accessor.h" |
| 26 |
#include "StyleContext.h" |
| 27 |
#include "CharacterSet.h" |
| 28 |
#include "LexerModule.h" |
| 29 |
|
| 30 |
using namespace Lexilla; |
| 31 |
|
| 32 |
/* |
| 33 |
// located in SciLexer.h |
| 34 |
#define SCLEX_NSIS 43 |
| 35 |
|
| 36 |
#define SCE_NSIS_DEFAULT 0 |
| 37 |
#define SCE_NSIS_COMMENT 1 |
| 38 |
#define SCE_NSIS_STRINGDQ 2 |
| 39 |
#define SCE_NSIS_STRINGLQ 3 |
| 40 |
#define SCE_NSIS_STRINGRQ 4 |
| 41 |
#define SCE_NSIS_FUNCTION 5 |
| 42 |
#define SCE_NSIS_VARIABLE 6 |
| 43 |
#define SCE_NSIS_LABEL 7 |
| 44 |
#define SCE_NSIS_USERDEFINED 8 |
| 45 |
#define SCE_NSIS_SECTIONDEF 9 |
| 46 |
#define SCE_NSIS_SUBSECTIONDEF 10 |
| 47 |
#define SCE_NSIS_IFDEFINEDEF 11 |
| 48 |
#define SCE_NSIS_MACRODEF 12 |
| 49 |
#define SCE_NSIS_STRINGVAR 13 |
| 50 |
#define SCE_NSIS_NUMBER 14 |
| 51 |
// ADDED for Scintilla v1.63 |
| 52 |
#define SCE_NSIS_SECTIONGROUP 15 |
| 53 |
#define SCE_NSIS_PAGEEX 16 |
| 54 |
#define SCE_NSIS_FUNCTIONDEF 17 |
| 55 |
#define SCE_NSIS_COMMENTBOX 18 |
| 56 |
*/ |
| 57 |
|
| 58 |
static bool isNsisNumber(char ch) |
| 59 |
{ |
| 60 |
return (ch >= '0' && ch <= '9'); |
| 61 |
} |
| 62 |
|
| 63 |
static bool isNsisChar(char ch) |
| 64 |
{ |
| 65 |
return (ch == '.' ) || (ch == '_' ) || isNsisNumber(ch) || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'); |
| 66 |
} |
| 67 |
|
| 68 |
static bool isNsisLetter(char ch) |
| 69 |
{ |
| 70 |
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'); |
| 71 |
} |
| 72 |
|
| 73 |
static bool NsisNextLineHasElse(Sci_PositionU start, Sci_PositionU end, Accessor &styler) |
| 74 |
{ |
| 75 |
Sci_Position nNextLine = -1; |
| 76 |
for( Sci_PositionU i = start; i < end; i++ ) |
| 77 |
{ |
| 78 |
char cNext = styler.SafeGetCharAt( i ); |
| 79 |
if( cNext == '\n' ) |
| 80 |
{ |
| 81 |
nNextLine = i+1; |
| 82 |
break; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if( nNextLine == -1 ) // We never found the next line... |
| 87 |
return false; |
| 88 |
|
| 89 |
for( Sci_PositionU firstChar = nNextLine; firstChar < end; firstChar++ ) |
| 90 |
{ |
| 91 |
char cNext = styler.SafeGetCharAt( firstChar ); |
| 92 |
if( cNext == ' ' ) |
| 93 |
continue; |
| 94 |
if( cNext == '\t' ) |
| 95 |
continue; |
| 96 |
if( cNext == '!' ) |
| 97 |
{ |
| 98 |
if( styler.Match(firstChar, "!else") ) |
| 99 |
return true; |
| 100 |
} |
| 101 |
break; |
| 102 |
} |
| 103 |
|
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
static int NsisCmp( const char *s1, const char *s2, bool bIgnoreCase ) |
| 108 |
{ |
| 109 |
if( bIgnoreCase ) |
| 110 |
return CompareCaseInsensitive( s1, s2); |
| 111 |
|
| 112 |
return strcmp( s1, s2 ); |
| 113 |
} |
| 114 |
|
| 115 |
static int calculateFoldNsis(Sci_PositionU start, Sci_PositionU end, int foldlevel, Accessor &styler, bool bElse, bool foldUtilityCmd ) |
| 116 |
{ |
| 117 |
int style = styler.StyleAt(end); |
| 118 |
|
| 119 |
// If the word is too long, it is not what we are looking for |
| 120 |
if( end - start > 20 ) |
| 121 |
return foldlevel; |
| 122 |
|
| 123 |
if( foldUtilityCmd ) |
| 124 |
{ |
| 125 |
// Check the style at this point, if it is not valid, then return zero |
| 126 |
if( style != SCE_NSIS_FUNCTIONDEF && style != SCE_NSIS_SECTIONDEF && |
| 127 |
style != SCE_NSIS_SUBSECTIONDEF && style != SCE_NSIS_IFDEFINEDEF && |
| 128 |
style != SCE_NSIS_MACRODEF && style != SCE_NSIS_SECTIONGROUP && |
| 129 |
style != SCE_NSIS_PAGEEX ) |
| 130 |
return foldlevel; |
| 131 |
} |
| 132 |
else |
| 133 |
{ |
| 134 |
if( style != SCE_NSIS_FUNCTIONDEF && style != SCE_NSIS_SECTIONDEF && |
| 135 |
style != SCE_NSIS_SUBSECTIONDEF && style != SCE_NSIS_SECTIONGROUP && |
| 136 |
style != SCE_NSIS_PAGEEX ) |
| 137 |
return foldlevel; |
| 138 |
} |
| 139 |
|
| 140 |
int newFoldlevel = foldlevel; |
| 141 |
bool bIgnoreCase = false; |
| 142 |
// property nsis.ignorecase |
| 143 |
// Set to 1 to ignore case for NSIS. |
| 144 |
if( styler.GetPropertyInt("nsis.ignorecase") == 1 ) |
| 145 |
bIgnoreCase = true; |
| 146 |
|
| 147 |
char s[20]; // The key word we are looking for has atmost 13 characters |
| 148 |
s[0] = '\0'; |
| 149 |
for (Sci_PositionU i = 0; i < end - start + 1 && i < 19; i++) |
| 150 |
{ |
| 151 |
s[i] = static_cast<char>( styler[ start + i ] ); |
| 152 |
s[i + 1] = '\0'; |
| 153 |
} |
| 154 |
|
| 155 |
if( s[0] == '!' ) |
| 156 |
{ |
| 157 |
if( NsisCmp(s, "!ifndef", bIgnoreCase) == 0 || NsisCmp(s, "!ifdef", bIgnoreCase ) == 0 || NsisCmp(s, "!ifmacrodef", bIgnoreCase ) == 0 || NsisCmp(s, "!ifmacrondef", bIgnoreCase ) == 0 || NsisCmp(s, "!if", bIgnoreCase ) == 0 || NsisCmp(s, "!macro", bIgnoreCase ) == 0 ) |
| 158 |
newFoldlevel++; |
| 159 |
else if( NsisCmp(s, "!endif", bIgnoreCase) == 0 || NsisCmp(s, "!macroend", bIgnoreCase ) == 0 ) |
| 160 |
newFoldlevel--; |
| 161 |
else if( bElse && NsisCmp(s, "!else", bIgnoreCase) == 0 ) |
| 162 |
newFoldlevel++; |
| 163 |
} |
| 164 |
else |
| 165 |
{ |
| 166 |
if( NsisCmp(s, "Section", bIgnoreCase ) == 0 || NsisCmp(s, "SectionGroup", bIgnoreCase ) == 0 || NsisCmp(s, "Function", bIgnoreCase) == 0 || NsisCmp(s, "SubSection", bIgnoreCase ) == 0 || NsisCmp(s, "PageEx", bIgnoreCase ) == 0 ) |
| 167 |
newFoldlevel++; |
| 168 |
else if( NsisCmp(s, "SectionGroupEnd", bIgnoreCase ) == 0 || NsisCmp(s, "SubSectionEnd", bIgnoreCase ) == 0 || NsisCmp(s, "FunctionEnd", bIgnoreCase) == 0 || NsisCmp(s, "SectionEnd", bIgnoreCase ) == 0 || NsisCmp(s, "PageExEnd", bIgnoreCase ) == 0 ) |
| 169 |
newFoldlevel--; |
| 170 |
} |
| 171 |
|
| 172 |
return newFoldlevel; |
| 173 |
} |
| 174 |
|
| 175 |
static int classifyWordNsis(Sci_PositionU start, Sci_PositionU end, WordList *keywordLists[], Accessor &styler ) |
| 176 |
{ |
| 177 |
bool bIgnoreCase = false; |
| 178 |
if( styler.GetPropertyInt("nsis.ignorecase") == 1 ) |
| 179 |
bIgnoreCase = true; |
| 180 |
|
| 181 |
bool bUserVars = false; |
| 182 |
// property nsis.uservars |
| 183 |
// Set to 1 to recognise user defined variables in NSIS. |
| 184 |
if( styler.GetPropertyInt("nsis.uservars") == 1 ) |
| 185 |
bUserVars = true; |
| 186 |
|
| 187 |
char s[100]; |
| 188 |
s[0] = '\0'; |
| 189 |
s[1] = '\0'; |
| 190 |
|
| 191 |
WordList &Functions = *keywordLists[0]; |
| 192 |
WordList &Variables = *keywordLists[1]; |
| 193 |
WordList &Lables = *keywordLists[2]; |
| 194 |
WordList &UserDefined = *keywordLists[3]; |
| 195 |
|
| 196 |
for (Sci_PositionU i = 0; i < end - start + 1 && i < 99; i++) |
| 197 |
{ |
| 198 |
if( bIgnoreCase ) |
| 199 |
s[i] = static_cast<char>( tolower(styler[ start + i ] ) ); |
| 200 |
else |
| 201 |
s[i] = static_cast<char>( styler[ start + i ] ); |
| 202 |
s[i + 1] = '\0'; |
| 203 |
} |
| 204 |
|
| 205 |
// Check for special words... |
| 206 |
if( NsisCmp(s, "!macro", bIgnoreCase ) == 0 || NsisCmp(s, "!macroend", bIgnoreCase) == 0 ) // Covers !macro and !macroend |
| 207 |
return SCE_NSIS_MACRODEF; |
| 208 |
|
| 209 |
if( NsisCmp(s, "!ifdef", bIgnoreCase ) == 0 || NsisCmp(s, "!ifndef", bIgnoreCase) == 0 || NsisCmp(s, "!endif", bIgnoreCase) == 0 ) // Covers !ifdef, !ifndef and !endif |
| 210 |
return SCE_NSIS_IFDEFINEDEF; |
| 211 |
|
| 212 |
if( NsisCmp(s, "!if", bIgnoreCase ) == 0 || NsisCmp(s, "!else", bIgnoreCase ) == 0 ) // Covers !if and else |
| 213 |
return SCE_NSIS_IFDEFINEDEF; |
| 214 |
|
| 215 |
if (NsisCmp(s, "!ifmacrodef", bIgnoreCase ) == 0 || NsisCmp(s, "!ifmacrondef", bIgnoreCase ) == 0 ) // Covers !ifmacrodef and !ifnmacrodef |
| 216 |
return SCE_NSIS_IFDEFINEDEF; |
| 217 |
|
| 218 |
if( NsisCmp(s, "SectionGroup", bIgnoreCase) == 0 || NsisCmp(s, "SectionGroupEnd", bIgnoreCase) == 0 ) // Covers SectionGroup and SectionGroupEnd |
| 219 |
return SCE_NSIS_SECTIONGROUP; |
| 220 |
|
| 221 |
if( NsisCmp(s, "Section", bIgnoreCase ) == 0 || NsisCmp(s, "SectionEnd", bIgnoreCase) == 0 ) // Covers Section and SectionEnd |
| 222 |
return SCE_NSIS_SECTIONDEF; |
| 223 |
|
| 224 |
if( NsisCmp(s, "SubSection", bIgnoreCase) == 0 || NsisCmp(s, "SubSectionEnd", bIgnoreCase) == 0 ) // Covers SubSection and SubSectionEnd |
| 225 |
return SCE_NSIS_SUBSECTIONDEF; |
| 226 |
|
| 227 |
if( NsisCmp(s, "PageEx", bIgnoreCase) == 0 || NsisCmp(s, "PageExEnd", bIgnoreCase) == 0 ) // Covers PageEx and PageExEnd |
| 228 |
return SCE_NSIS_PAGEEX; |
| 229 |
|
| 230 |
if( NsisCmp(s, "Function", bIgnoreCase) == 0 || NsisCmp(s, "FunctionEnd", bIgnoreCase) == 0 ) // Covers Function and FunctionEnd |
| 231 |
return SCE_NSIS_FUNCTIONDEF; |
| 232 |
|
| 233 |
if ( Functions.InList(s) ) |
| 234 |
return SCE_NSIS_FUNCTION; |
| 235 |
|
| 236 |
if ( Variables.InList(s) ) |
| 237 |
return SCE_NSIS_VARIABLE; |
| 238 |
|
| 239 |
if ( Lables.InList(s) ) |
| 240 |
return SCE_NSIS_LABEL; |
| 241 |
|
| 242 |
if( UserDefined.InList(s) ) |
| 243 |
return SCE_NSIS_USERDEFINED; |
| 244 |
|
| 245 |
if( strlen(s) > 3 ) |
| 246 |
{ |
| 247 |
if( s[1] == '{' && s[strlen(s)-1] == '}' ) |
| 248 |
return SCE_NSIS_VARIABLE; |
| 249 |
} |
| 250 |
|
| 251 |
// See if the variable is a user defined variable |
| 252 |
if( s[0] == '$' && bUserVars ) |
| 253 |
{ |
| 254 |
bool bHasSimpleNsisChars = true; |
| 255 |
for (Sci_PositionU j = 1; j < end - start + 1 && j < 99; j++) |
| 256 |
{ |
| 257 |
if( !isNsisChar( s[j] ) ) |
| 258 |
{ |
| 259 |
bHasSimpleNsisChars = false; |
| 260 |
break; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
if( bHasSimpleNsisChars ) |
| 265 |
return SCE_NSIS_VARIABLE; |
| 266 |
} |
| 267 |
|
| 268 |
// To check for numbers |
| 269 |
if( isNsisNumber( s[0] ) ) |
| 270 |
{ |
| 271 |
bool bHasSimpleNsisNumber = true; |
| 272 |
for (Sci_PositionU j = 1; j < end - start + 1 && j < 99; j++) |
| 273 |
{ |
| 274 |
if( !isNsisNumber( s[j] ) ) |
| 275 |
{ |
| 276 |
bHasSimpleNsisNumber = false; |
| 277 |
break; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
if( bHasSimpleNsisNumber ) |
| 282 |
return SCE_NSIS_NUMBER; |
| 283 |
} |
| 284 |
|
| 285 |
return SCE_NSIS_DEFAULT; |
| 286 |
} |
| 287 |
|
| 288 |
static void ColouriseNsisDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *keywordLists[], Accessor &styler) |
| 289 |
{ |
| 290 |
int state = SCE_NSIS_DEFAULT; |
| 291 |
if( startPos > 0 ) |
| 292 |
state = styler.StyleAt(startPos-1); // Use the style from the previous line, usually default, but could be commentbox |
| 293 |
|
| 294 |
styler.StartAt( startPos ); |
| 295 |
styler.GetLine( startPos ); |
| 296 |
|
| 297 |
Sci_PositionU nLengthDoc = startPos + length; |
| 298 |
styler.StartSegment( startPos ); |
| 299 |
|
| 300 |
char cCurrChar; |
| 301 |
bool bVarInString = false; |
| 302 |
bool bClassicVarInString = false; |
| 303 |
|
| 304 |
Sci_PositionU i; |
| 305 |
for( i = startPos; i < nLengthDoc; i++ ) |
| 306 |
{ |
| 307 |
cCurrChar = styler.SafeGetCharAt( i ); |
| 308 |
char cNextChar = styler.SafeGetCharAt(i+1); |
| 309 |
|
| 310 |
switch(state) |
| 311 |
{ |
| 312 |
case SCE_NSIS_DEFAULT: |
| 313 |
if( cCurrChar == ';' || cCurrChar == '#' ) // we have a comment line |
| 314 |
{ |
| 315 |
styler.ColourTo(i-1, state ); |
| 316 |
state = SCE_NSIS_COMMENT; |
| 317 |
break; |
| 318 |
} |
| 319 |
if( cCurrChar == '"' ) |
| 320 |
{ |
| 321 |
styler.ColourTo(i-1, state ); |
| 322 |
state = SCE_NSIS_STRINGDQ; |
| 323 |
bVarInString = false; |
| 324 |
bClassicVarInString = false; |
| 325 |
break; |
| 326 |
} |
| 327 |
if( cCurrChar == '\'' ) |
| 328 |
{ |
| 329 |
styler.ColourTo(i-1, state ); |
| 330 |
state = SCE_NSIS_STRINGRQ; |
| 331 |
bVarInString = false; |
| 332 |
bClassicVarInString = false; |
| 333 |
break; |
| 334 |
} |
| 335 |
if( cCurrChar == '`' ) |
| 336 |
{ |
| 337 |
styler.ColourTo(i-1, state ); |
| 338 |
state = SCE_NSIS_STRINGLQ; |
| 339 |
bVarInString = false; |
| 340 |
bClassicVarInString = false; |
| 341 |
break; |
| 342 |
} |
| 343 |
|
| 344 |
// NSIS KeyWord,Function, Variable, UserDefined: |
| 345 |
if( cCurrChar == '$' || isNsisChar(cCurrChar) || cCurrChar == '!' ) |
| 346 |
{ |
| 347 |
styler.ColourTo(i-1,state); |
| 348 |
state = SCE_NSIS_FUNCTION; |
| 349 |
|
| 350 |
// If it is a number, we must check and set style here first... |
| 351 |
if( isNsisNumber(cCurrChar) && (cNextChar == '\t' || cNextChar == ' ' || cNextChar == '\r' || cNextChar == '\n' ) ) |
| 352 |
styler.ColourTo( i, SCE_NSIS_NUMBER); |
| 353 |
|
| 354 |
break; |
| 355 |
} |
| 356 |
|
| 357 |
if( cCurrChar == '/' && cNextChar == '*' ) |
| 358 |
{ |
| 359 |
styler.ColourTo(i-1,state); |
| 360 |
state = SCE_NSIS_COMMENTBOX; |
| 361 |
break; |
| 362 |
} |
| 363 |
|
| 364 |
break; |
| 365 |
case SCE_NSIS_COMMENT: |
| 366 |
if( cNextChar == '\n' || cNextChar == '\r' ) |
| 367 |
{ |
| 368 |
// Special case: |
| 369 |
if( cCurrChar == '\\' ) |
| 370 |
{ |
| 371 |
styler.ColourTo(i-2,state); |
| 372 |
styler.ColourTo(i,SCE_NSIS_DEFAULT); |
| 373 |
} |
| 374 |
else |
| 375 |
{ |
| 376 |
styler.ColourTo(i,state); |
| 377 |
state = SCE_NSIS_DEFAULT; |
| 378 |
} |
| 379 |
} |
| 380 |
break; |
| 381 |
case SCE_NSIS_STRINGDQ: |
| 382 |
case SCE_NSIS_STRINGLQ: |
| 383 |
case SCE_NSIS_STRINGRQ: |
| 384 |
|
| 385 |
if( styler.SafeGetCharAt(i-1) == '\\' && styler.SafeGetCharAt(i-2) == '$' ) |
| 386 |
break; // Ignore the next character, even if it is a quote of some sort |
| 387 |
|
| 388 |
if( cCurrChar == '"' && state == SCE_NSIS_STRINGDQ ) |
| 389 |
{ |
| 390 |
styler.ColourTo(i,state); |
| 391 |
state = SCE_NSIS_DEFAULT; |
| 392 |
break; |
| 393 |
} |
| 394 |
|
| 395 |
if( cCurrChar == '`' && state == SCE_NSIS_STRINGLQ ) |
| 396 |
{ |
| 397 |
styler.ColourTo(i,state); |
| 398 |
state = SCE_NSIS_DEFAULT; |
| 399 |
break; |
| 400 |
} |
| 401 |
|
| 402 |
if( cCurrChar == '\'' && state == SCE_NSIS_STRINGRQ ) |
| 403 |
{ |
| 404 |
styler.ColourTo(i,state); |
| 405 |
state = SCE_NSIS_DEFAULT; |
| 406 |
break; |
| 407 |
} |
| 408 |
|
| 409 |
if( cNextChar == '\r' || cNextChar == '\n' ) |
| 410 |
{ |
| 411 |
Sci_Position nCurLine = styler.GetLine(i+1); |
| 412 |
Sci_Position nBack = i; |
| 413 |
// We need to check if the previous line has a \ in it... |
| 414 |
bool bNextLine = false; |
| 415 |
|
| 416 |
while( nBack > 0 ) |
| 417 |
{ |
| 418 |
if( styler.GetLine(nBack) != nCurLine ) |
| 419 |
break; |
| 420 |
|
| 421 |
char cTemp = styler.SafeGetCharAt(nBack, 'a'); // Letter 'a' is safe here |
| 422 |
|
| 423 |
if( cTemp == '\\' ) |
| 424 |
{ |
| 425 |
bNextLine = true; |
| 426 |
break; |
| 427 |
} |
| 428 |
if( cTemp != '\r' && cTemp != '\n' && cTemp != '\t' && cTemp != ' ' ) |
| 429 |
break; |
| 430 |
|
| 431 |
nBack--; |
| 432 |
} |
| 433 |
|
| 434 |
if( bNextLine ) |
| 435 |
{ |
| 436 |
styler.ColourTo(i+1,state); |
| 437 |
} |
| 438 |
if( bNextLine == false ) |
| 439 |
{ |
| 440 |
styler.ColourTo(i,state); |
| 441 |
state = SCE_NSIS_DEFAULT; |
| 442 |
} |
| 443 |
} |
| 444 |
break; |
| 445 |
|
| 446 |
case SCE_NSIS_FUNCTION: |
| 447 |
|
| 448 |
// NSIS KeyWord: |
| 449 |
if( cCurrChar == '$' ) |
| 450 |
state = SCE_NSIS_DEFAULT; |
| 451 |
else if( cCurrChar == '\\' && (cNextChar == 'n' || cNextChar == 'r' || cNextChar == 't' ) ) |
| 452 |
state = SCE_NSIS_DEFAULT; |
| 453 |
else if( (isNsisChar(cCurrChar) && !isNsisChar( cNextChar) && cNextChar != '}') || cCurrChar == '}' ) |
| 454 |
{ |
| 455 |
state = classifyWordNsis( styler.GetStartSegment(), i, keywordLists, styler ); |
| 456 |
styler.ColourTo( i, state); |
| 457 |
state = SCE_NSIS_DEFAULT; |
| 458 |
} |
| 459 |
else if( !isNsisChar( cCurrChar ) && cCurrChar != '{' && cCurrChar != '}' ) |
| 460 |
{ |
| 461 |
if( classifyWordNsis( styler.GetStartSegment(), i-1, keywordLists, styler) == SCE_NSIS_NUMBER ) |
| 462 |
styler.ColourTo( i-1, SCE_NSIS_NUMBER ); |
| 463 |
|
| 464 |
state = SCE_NSIS_DEFAULT; |
| 465 |
|
| 466 |
if( cCurrChar == '"' ) |
| 467 |
{ |
| 468 |
state = SCE_NSIS_STRINGDQ; |
| 469 |
bVarInString = false; |
| 470 |
bClassicVarInString = false; |
| 471 |
} |
| 472 |
else if( cCurrChar == '`' ) |
| 473 |
{ |
| 474 |
state = SCE_NSIS_STRINGLQ; |
| 475 |
bVarInString = false; |
| 476 |
bClassicVarInString = false; |
| 477 |
} |
| 478 |
else if( cCurrChar == '\'' ) |
| 479 |
{ |
| 480 |
state = SCE_NSIS_STRINGRQ; |
| 481 |
bVarInString = false; |
| 482 |
bClassicVarInString = false; |
| 483 |
} |
| 484 |
else if( cCurrChar == '#' || cCurrChar == ';' ) |
| 485 |
{ |
| 486 |
state = SCE_NSIS_COMMENT; |
| 487 |
} |
| 488 |
} |
| 489 |
break; |
| 490 |
case SCE_NSIS_COMMENTBOX: |
| 491 |
|
| 492 |
if( styler.SafeGetCharAt(i-1) == '*' && cCurrChar == '/' ) |
| 493 |
{ |
| 494 |
styler.ColourTo(i,state); |
| 495 |
state = SCE_NSIS_DEFAULT; |
| 496 |
} |
| 497 |
break; |
| 498 |
} |
| 499 |
|
| 500 |
if( state == SCE_NSIS_COMMENT || state == SCE_NSIS_COMMENTBOX ) |
| 501 |
{ |
| 502 |
styler.ColourTo(i,state); |
| 503 |
} |
| 504 |
else if( state == SCE_NSIS_STRINGDQ || state == SCE_NSIS_STRINGLQ || state == SCE_NSIS_STRINGRQ ) |
| 505 |
{ |
| 506 |
bool bIngoreNextDollarSign = false; |
| 507 |
bool bUserVars = false; |
| 508 |
if( styler.GetPropertyInt("nsis.uservars") == 1 ) |
| 509 |
bUserVars = true; |
| 510 |
|
| 511 |
if( bVarInString && cCurrChar == '$' ) |
| 512 |
{ |
| 513 |
bVarInString = false; |
| 514 |
bIngoreNextDollarSign = true; |
| 515 |
} |
| 516 |
else if( bVarInString && cCurrChar == '\\' && (cNextChar == 'n' || cNextChar == 'r' || cNextChar == 't' || cNextChar == '"' || cNextChar == '`' || cNextChar == '\'' ) ) |
| 517 |
{ |
| 518 |
styler.ColourTo( i+1, SCE_NSIS_STRINGVAR); |
| 519 |
bVarInString = false; |
| 520 |
bIngoreNextDollarSign = false; |
| 521 |
} |
| 522 |
|
| 523 |
// Covers "$INSTDIR and user vars like $MYVAR" |
| 524 |
else if( bVarInString && !isNsisChar(cNextChar) ) |
| 525 |
{ |
| 526 |
int nWordState = classifyWordNsis( styler.GetStartSegment(), i, keywordLists, styler); |
| 527 |
if( nWordState == SCE_NSIS_VARIABLE ) |
| 528 |
styler.ColourTo( i, SCE_NSIS_STRINGVAR); |
| 529 |
else if( bUserVars ) |
| 530 |
styler.ColourTo( i, SCE_NSIS_STRINGVAR); |
| 531 |
bVarInString = false; |
| 532 |
} |
| 533 |
// Covers "${TEST}..." |
| 534 |
else if( bClassicVarInString && cNextChar == '}' ) |
| 535 |
{ |
| 536 |
styler.ColourTo( i+1, SCE_NSIS_STRINGVAR); |
| 537 |
bClassicVarInString = false; |
| 538 |
} |
| 539 |
|
| 540 |
// Start of var in string |
| 541 |
if( !bIngoreNextDollarSign && cCurrChar == '$' && cNextChar == '{' ) |
| 542 |
{ |
| 543 |
styler.ColourTo( i-1, state); |
| 544 |
bClassicVarInString = true; |
| 545 |
bVarInString = false; |
| 546 |
} |
| 547 |
else if( !bIngoreNextDollarSign && cCurrChar == '$' ) |
| 548 |
{ |
| 549 |
styler.ColourTo( i-1, state); |
| 550 |
bVarInString = true; |
| 551 |
bClassicVarInString = false; |
| 552 |
} |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
// Colourise remaining document |
| 557 |
styler.ColourTo(nLengthDoc-1,state); |
| 558 |
} |
| 559 |
|
| 560 |
static void FoldNsisDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler) |
| 561 |
{ |
| 562 |
// No folding enabled, no reason to continue... |
| 563 |
if( styler.GetPropertyInt("fold") == 0 ) |
| 564 |
return; |
| 565 |
|
| 566 |
bool foldAtElse = styler.GetPropertyInt("fold.at.else", 0) == 1; |
| 567 |
bool foldUtilityCmd = styler.GetPropertyInt("nsis.foldutilcmd", 1) == 1; |
| 568 |
bool blockComment = false; |
| 569 |
|
| 570 |
Sci_Position lineCurrent = styler.GetLine(startPos); |
| 571 |
Sci_PositionU safeStartPos = styler.LineStart( lineCurrent ); |
| 572 |
|
| 573 |
bool bArg1 = true; |
| 574 |
Sci_Position nWordStart = -1; |
| 575 |
|
| 576 |
int levelCurrent = SC_FOLDLEVELBASE; |
| 577 |
if (lineCurrent > 0) |
| 578 |
levelCurrent = styler.LevelAt(lineCurrent-1) >> 16; |
| 579 |
int levelNext = levelCurrent; |
| 580 |
int style = styler.StyleAt(safeStartPos); |
| 581 |
if( style == SCE_NSIS_COMMENTBOX ) |
| 582 |
{ |
| 583 |
if( styler.SafeGetCharAt(safeStartPos) == '/' && styler.SafeGetCharAt(safeStartPos+1) == '*' ) |
| 584 |
levelNext++; |
| 585 |
blockComment = true; |
| 586 |
} |
| 587 |
|
| 588 |
for (Sci_PositionU i = safeStartPos; i < startPos + length; i++) |
| 589 |
{ |
| 590 |
char chCurr = styler.SafeGetCharAt(i); |
| 591 |
style = styler.StyleAt(i); |
| 592 |
if( blockComment && style != SCE_NSIS_COMMENTBOX ) |
| 593 |
{ |
| 594 |
levelNext--; |
| 595 |
blockComment = false; |
| 596 |
} |
| 597 |
else if( !blockComment && style == SCE_NSIS_COMMENTBOX ) |
| 598 |
{ |
| 599 |
levelNext++; |
| 600 |
blockComment = true; |
| 601 |
} |
| 602 |
|
| 603 |
if( bArg1 && !blockComment) |
| 604 |
{ |
| 605 |
if( nWordStart == -1 && (isNsisLetter(chCurr) || chCurr == '!') ) |
| 606 |
{ |
| 607 |
nWordStart = i; |
| 608 |
} |
| 609 |
else if( isNsisLetter(chCurr) == false && nWordStart > -1 ) |
| 610 |
{ |
| 611 |
int newLevel = calculateFoldNsis( nWordStart, i-1, levelNext, styler, foldAtElse, foldUtilityCmd ); |
| 612 |
|
| 613 |
if( newLevel == levelNext ) |
| 614 |
{ |
| 615 |
if( foldAtElse && foldUtilityCmd ) |
| 616 |
{ |
| 617 |
if( NsisNextLineHasElse(i, startPos + length, styler) ) |
| 618 |
levelNext--; |
| 619 |
} |
| 620 |
} |
| 621 |
else |
| 622 |
levelNext = newLevel; |
| 623 |
bArg1 = false; |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
if( chCurr == '\n' ) |
| 628 |
{ |
| 629 |
if( bArg1 && foldAtElse && foldUtilityCmd && !blockComment ) |
| 630 |
{ |
| 631 |
if( NsisNextLineHasElse(i, startPos + length, styler) ) |
| 632 |
levelNext--; |
| 633 |
} |
| 634 |
|
| 635 |
// If we are on a new line... |
| 636 |
int levelUse = levelCurrent; |
| 637 |
int lev = levelUse | levelNext << 16; |
| 638 |
if (levelUse < levelNext ) |
| 639 |
lev |= SC_FOLDLEVELHEADERFLAG; |
| 640 |
if (lev != styler.LevelAt(lineCurrent)) |
| 641 |
styler.SetLevel(lineCurrent, lev); |
| 642 |
|
| 643 |
lineCurrent++; |
| 644 |
levelCurrent = levelNext; |
| 645 |
bArg1 = true; // New line, lets look at first argument again |
| 646 |
nWordStart = -1; |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
int levelUse = levelCurrent; |
| 651 |
int lev = levelUse | levelNext << 16; |
| 652 |
if (levelUse < levelNext) |
| 653 |
lev |= SC_FOLDLEVELHEADERFLAG; |
| 654 |
if (lev != styler.LevelAt(lineCurrent)) |
| 655 |
styler.SetLevel(lineCurrent, lev); |
| 656 |
} |
| 657 |
|
| 658 |
static const char * const nsisWordLists[] = { |
| 659 |
"Functions", |
| 660 |
"Variables", |
| 661 |
"Lables", |
| 662 |
"UserDefined", |
| 663 |
0, }; |
| 664 |
|
| 665 |
|
| 666 |
extern const LexerModule lmNsis(SCLEX_NSIS, ColouriseNsisDoc, "nsis", FoldNsisDoc, nsisWordLists); |
| 667 |
|