]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexNsis.cxx
1 // Scintilla source code edit control
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.
19 #include "Scintilla.h"
23 // located in SciLexer.h
26 #define SCE_NSIS_DEFAULT 0
27 #define SCE_NSIS_COMMENT 1
28 #define SCE_NSIS_STRINGDQ 2
29 #define SCE_NSIS_STRINGLQ 3
30 #define SCE_NSIS_STRINGRQ 4
31 #define SCE_NSIS_FUNCTION 5
32 #define SCE_NSIS_VARIABLE 6
33 #define SCE_NSIS_LABEL 7
34 #define SCE_NSIS_USERDEFINED 8
35 #define SCE_NSIS_SECTIONDEF 9
36 #define SCE_NSIS_SUBSECTIONDEF 10
37 #define SCE_NSIS_IFDEFINEDEF 11
38 #define SCE_NSIS_MACRODEF 12
39 #define SCE_NSIS_STRINGVAR 13
40 #define SCE_NSIS_NUMBER 14
41 // ADDED for Scintilla v1.63
42 #define SCE_NSIS_SECTIONGROUP 15
43 #define SCE_NSIS_PAGEEX 16
44 #define SCE_NSIS_FUNCTIONDEF 17
45 #define SCE_NSIS_COMMENTBOX 18
48 static bool isNsisNumber(char ch
)
50 return (ch
>= '0' && ch
<= '9');
53 static bool isNsisChar(char ch
)
55 return (ch
== '.' ) || (ch
== '_' ) || isNsisNumber(ch
) || (ch
>= 'A' && ch
<= 'Z') || (ch
>= 'a' && ch
<= 'z');
58 static bool isNsisLetter(char ch
)
60 return (ch
>= 'A' && ch
<= 'Z') || (ch
>= 'a' && ch
<= 'z');
63 static bool NsisNextLineHasElse(unsigned int start
, unsigned int end
, Accessor
&styler
)
66 for( unsigned int i
= start
; i
< end
; i
++ )
68 char cNext
= styler
.SafeGetCharAt( i
);
76 if( nNextLine
== -1 ) // We never foudn the next line...
79 for( unsigned int firstChar
= nNextLine
; firstChar
< end
; firstChar
++ )
81 char cNext
= styler
.SafeGetCharAt( firstChar
);
88 if( styler
.Match(firstChar
, "!else") )
97 static int NsisCmp( const char *s1
, const char *s2
, bool bIgnoreCase
)
100 return CompareCaseInsensitive( s1
, s2
);
102 return strcmp( s1
, s2
);
105 static int calculateFoldNsis(unsigned int start
, unsigned int end
, int foldlevel
, Accessor
&styler
, bool bElse
, bool foldUtilityCmd
)
107 int style
= styler
.StyleAt(end
);
109 // If the word is too long, it is not what we are looking for
110 if( end
- start
> 20 )
115 // Check the style at this point, if it is not valid, then return zero
116 if( style
!= SCE_NSIS_FUNCTIONDEF
&& style
!= SCE_NSIS_SECTIONDEF
&&
117 style
!= SCE_NSIS_SUBSECTIONDEF
&& style
!= SCE_NSIS_IFDEFINEDEF
&&
118 style
!= SCE_NSIS_MACRODEF
&& style
!= SCE_NSIS_SECTIONGROUP
&&
119 style
!= SCE_NSIS_PAGEEX
)
124 if( style
!= SCE_NSIS_FUNCTIONDEF
&& style
!= SCE_NSIS_SECTIONDEF
&&
125 style
!= SCE_NSIS_SUBSECTIONDEF
&& style
!= SCE_NSIS_SECTIONGROUP
&&
126 style
!= SCE_NSIS_PAGEEX
)
130 int newFoldlevel
= foldlevel
;
131 bool bIgnoreCase
= false;
132 if( styler
.GetPropertyInt("nsis.ignorecase") == 1 )
135 char s
[20]; // The key word we are looking for has atmost 13 characters
136 for (unsigned int i
= 0; i
< end
- start
+ 1 && i
< 19; i
++)
138 s
[i
] = static_cast<char>( styler
[ start
+ i
] );
144 if( NsisCmp(s
, "!ifndef", bIgnoreCase
) == 0 || NsisCmp(s
, "!ifdef", bIgnoreCase
) == 0 || NsisCmp(s
, "!macro", bIgnoreCase
) == 0 )
146 else if( NsisCmp(s
, "!endif", bIgnoreCase
) == 0 || NsisCmp(s
, "!macroend", bIgnoreCase
) == 0 )
148 else if( bElse
&& NsisCmp(s
, "!else", bIgnoreCase
) == 0 )
153 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 )
155 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 )
162 static int classifyWordNsis(unsigned int start
, unsigned int end
, WordList
*keywordLists
[], Accessor
&styler
)
164 bool bIgnoreCase
= false;
165 if( styler
.GetPropertyInt("nsis.ignorecase") == 1 )
168 bool bUserVars
= false;
169 if( styler
.GetPropertyInt("nsis.uservars") == 1 )
174 WordList
&Functions
= *keywordLists
[0];
175 WordList
&Variables
= *keywordLists
[1];
176 WordList
&Lables
= *keywordLists
[2];
177 WordList
&UserDefined
= *keywordLists
[3];
179 for (unsigned int i
= 0; i
< end
- start
+ 1 && i
< 99; i
++)
182 s
[i
] = static_cast<char>( tolower(styler
[ start
+ i
] ) );
184 s
[i
] = static_cast<char>( styler
[ start
+ i
] );
188 // Check for special words...
189 if( NsisCmp(s
, "!macro", bIgnoreCase
) == 0 || NsisCmp(s
, "!macroend", bIgnoreCase
) == 0 ) // Covers !micro and !microend
190 return SCE_NSIS_MACRODEF
;
192 if( NsisCmp(s
, "!ifdef", bIgnoreCase
) == 0 || NsisCmp(s
, "!ifndef", bIgnoreCase
) == 0 || NsisCmp(s
, "!endif", bIgnoreCase
) == 0 )
193 return SCE_NSIS_IFDEFINEDEF
;
195 if( NsisCmp(s
, "!else", bIgnoreCase
) == 0 ) // || NsisCmp(s, "!ifndef", bIgnoreCase) == 0 || NsisCmp(s, "!endif", bIgnoreCase) == 0 )
196 return SCE_NSIS_IFDEFINEDEF
;
198 if( NsisCmp(s
, "SectionGroup", bIgnoreCase
) == 0 || NsisCmp(s
, "SectionGroupEnd", bIgnoreCase
) == 0 ) // Covers SectionGroup and SectionGroupEnd
199 return SCE_NSIS_SECTIONGROUP
;
201 if( NsisCmp(s
, "Section", bIgnoreCase
) == 0 || NsisCmp(s
, "SectionEnd", bIgnoreCase
) == 0 ) // Covers Section and SectionEnd
202 return SCE_NSIS_SECTIONDEF
;
204 if( NsisCmp(s
, "SubSection", bIgnoreCase
) == 0 || NsisCmp(s
, "SubSectionEnd", bIgnoreCase
) == 0 ) // Covers SubSection and SubSectionEnd
205 return SCE_NSIS_SUBSECTIONDEF
;
207 if( NsisCmp(s
, "PageEx", bIgnoreCase
) == 0 || NsisCmp(s
, "PageExEnd", bIgnoreCase
) == 0 ) // Covers PageEx and PageExEnd
208 return SCE_NSIS_PAGEEX
;
210 if( NsisCmp(s
, "Function", bIgnoreCase
) == 0 || NsisCmp(s
, "FunctionEnd", bIgnoreCase
) == 0 ) // Covers Function and FunctionEnd
211 return SCE_NSIS_FUNCTIONDEF
;
213 if ( Functions
.InList(s
) )
214 return SCE_NSIS_FUNCTION
;
216 if ( Variables
.InList(s
) )
217 return SCE_NSIS_VARIABLE
;
219 if ( Lables
.InList(s
) )
220 return SCE_NSIS_LABEL
;
222 if( UserDefined
.InList(s
) )
223 return SCE_NSIS_USERDEFINED
;
227 if( s
[1] == '{' && s
[strlen(s
)-1] == '}' )
228 return SCE_NSIS_VARIABLE
;
231 // See if the variable is a user defined variable
232 if( s
[0] == '$' && bUserVars
)
234 bool bHasSimpleNsisChars
= true;
235 for (unsigned int j
= 1; j
< end
- start
+ 1 && j
< 99; j
++)
237 if( !isNsisChar( s
[j
] ) )
239 bHasSimpleNsisChars
= false;
244 if( bHasSimpleNsisChars
)
245 return SCE_NSIS_VARIABLE
;
248 // To check for numbers
249 if( isNsisNumber( s
[0] ) )
251 bool bHasSimpleNsisNumber
= true;
252 for (unsigned int j
= 1; j
< end
- start
+ 1 && j
< 99; j
++)
254 if( !isNsisNumber( s
[j
] ) )
256 bHasSimpleNsisNumber
= false;
261 if( bHasSimpleNsisNumber
)
262 return SCE_NSIS_NUMBER
;
265 return SCE_NSIS_DEFAULT
;
268 static void ColouriseNsisDoc(unsigned int startPos
, int length
, int, WordList
*keywordLists
[], Accessor
&styler
)
270 int state
= SCE_NSIS_DEFAULT
;
272 state
= styler
.StyleAt(startPos
-1); // Use the style from the previous line, usually default, but could be commentbox
274 styler
.StartAt( startPos
);
275 styler
.GetLine( startPos
);
277 unsigned int nLengthDoc
= startPos
+ length
;
278 styler
.StartSegment( startPos
);
281 bool bVarInString
= false;
282 bool bClassicVarInString
= false;
285 for( i
= startPos
; i
< nLengthDoc
; i
++ )
287 cCurrChar
= styler
.SafeGetCharAt( i
);
288 char cNextChar
= styler
.SafeGetCharAt(i
+1);
292 case SCE_NSIS_DEFAULT
:
293 if( cCurrChar
== ';' || cCurrChar
== '#' ) // we have a comment line
295 styler
.ColourTo(i
-1, state
);
296 state
= SCE_NSIS_COMMENT
;
299 if( cCurrChar
== '"' )
301 styler
.ColourTo(i
-1, state
);
302 state
= SCE_NSIS_STRINGDQ
;
303 bVarInString
= false;
304 bClassicVarInString
= false;
307 if( cCurrChar
== '\'' )
309 styler
.ColourTo(i
-1, state
);
310 state
= SCE_NSIS_STRINGRQ
;
311 bVarInString
= false;
312 bClassicVarInString
= false;
315 if( cCurrChar
== '`' )
317 styler
.ColourTo(i
-1, state
);
318 state
= SCE_NSIS_STRINGLQ
;
319 bVarInString
= false;
320 bClassicVarInString
= false;
324 // NSIS KeyWord,Function, Variable, UserDefined:
325 if( cCurrChar
== '$' || isNsisChar(cCurrChar
) || cCurrChar
== '!' )
327 styler
.ColourTo(i
-1,state
);
328 state
= SCE_NSIS_FUNCTION
;
330 // If it is a number, we must check and set style here first...
331 if( isNsisNumber(cCurrChar
) && (cNextChar
== '\t' || cNextChar
== ' ' || cNextChar
== '\r' || cNextChar
== '\n' ) )
332 styler
.ColourTo( i
, SCE_NSIS_NUMBER
);
337 if( cCurrChar
== '/' && cNextChar
== '*' )
339 styler
.ColourTo(i
-1,state
);
340 state
= SCE_NSIS_COMMENTBOX
;
345 case SCE_NSIS_COMMENT
:
346 if( cNextChar
== '\n' || cNextChar
== '\r' )
349 if( cCurrChar
== '\\' )
351 styler
.ColourTo(i
-2,state
);
352 styler
.ColourTo(i
,SCE_NSIS_DEFAULT
);
356 styler
.ColourTo(i
,state
);
357 state
= SCE_NSIS_DEFAULT
;
361 case SCE_NSIS_STRINGDQ
:
362 case SCE_NSIS_STRINGLQ
:
363 case SCE_NSIS_STRINGRQ
:
365 if( styler
.SafeGetCharAt(i
-1) == '\\' && styler
.SafeGetCharAt(i
-2) == '$' )
366 break; // Ignore the next character, even if it is a quote of some sort
368 if( cCurrChar
== '"' && state
== SCE_NSIS_STRINGDQ
)
370 styler
.ColourTo(i
,state
);
371 state
= SCE_NSIS_DEFAULT
;
375 if( cCurrChar
== '`' && state
== SCE_NSIS_STRINGLQ
)
377 styler
.ColourTo(i
,state
);
378 state
= SCE_NSIS_DEFAULT
;
382 if( cCurrChar
== '\'' && state
== SCE_NSIS_STRINGRQ
)
384 styler
.ColourTo(i
,state
);
385 state
= SCE_NSIS_DEFAULT
;
389 if( cNextChar
== '\r' || cNextChar
== '\n' )
391 int nCurLine
= styler
.GetLine(i
+1);
393 // We need to check if the previous line has a \ in it...
394 bool bNextLine
= false;
398 if( styler
.GetLine(nBack
) != nCurLine
)
401 char cTemp
= styler
.SafeGetCharAt(nBack
, 'a'); // Letter 'a' is safe here
408 if( cTemp
!= '\r' && cTemp
!= '\n' && cTemp
!= '\t' && cTemp
!= ' ' )
416 styler
.ColourTo(i
+1,state
);
418 if( bNextLine
== false )
420 styler
.ColourTo(i
,state
);
421 state
= SCE_NSIS_DEFAULT
;
426 case SCE_NSIS_FUNCTION
:
429 if( cCurrChar
== '$' )
430 state
= SCE_NSIS_DEFAULT
;
431 else if( cCurrChar
== '\\' && (cNextChar
== 'n' || cNextChar
== 'r' || cNextChar
== 't' ) )
432 state
= SCE_NSIS_DEFAULT
;
433 else if( (isNsisChar(cCurrChar
) && !isNsisChar( cNextChar
) && cNextChar
!= '}') || cCurrChar
== '}' )
435 state
= classifyWordNsis( styler
.GetStartSegment(), i
, keywordLists
, styler
);
436 styler
.ColourTo( i
, state
);
437 state
= SCE_NSIS_DEFAULT
;
439 else if( !isNsisChar( cCurrChar
) && cCurrChar
!= '{' && cCurrChar
!= '}' )
441 if( classifyWordNsis( styler
.GetStartSegment(), i
-1, keywordLists
, styler
) == SCE_NSIS_NUMBER
)
442 styler
.ColourTo( i
-1, SCE_NSIS_NUMBER
);
444 state
= SCE_NSIS_DEFAULT
;
446 if( cCurrChar
== '"' )
448 state
= SCE_NSIS_STRINGDQ
;
449 bVarInString
= false;
450 bClassicVarInString
= false;
452 else if( cCurrChar
== '`' )
454 state
= SCE_NSIS_STRINGLQ
;
455 bVarInString
= false;
456 bClassicVarInString
= false;
458 else if( cCurrChar
== '\'' )
460 state
= SCE_NSIS_STRINGRQ
;
461 bVarInString
= false;
462 bClassicVarInString
= false;
464 else if( cCurrChar
== '#' || cCurrChar
== ';' )
466 state
= SCE_NSIS_COMMENT
;
470 case SCE_NSIS_COMMENTBOX
:
472 if( styler
.SafeGetCharAt(i
-1) == '*' && cCurrChar
== '/' )
474 styler
.ColourTo(i
,state
);
475 state
= SCE_NSIS_DEFAULT
;
480 if( state
== SCE_NSIS_COMMENT
|| state
== SCE_NSIS_COMMENTBOX
)
482 styler
.ColourTo(i
,state
);
484 else if( state
== SCE_NSIS_STRINGDQ
|| state
== SCE_NSIS_STRINGLQ
|| state
== SCE_NSIS_STRINGRQ
)
486 bool bIngoreNextDollarSign
= false;
487 bool bUserVars
= false;
488 if( styler
.GetPropertyInt("nsis.uservars") == 1 )
491 if( bVarInString
&& cCurrChar
== '$' )
493 bVarInString
= false;
494 bIngoreNextDollarSign
= true;
496 else if( bVarInString
&& cCurrChar
== '\\' && (cNextChar
== 'n' || cNextChar
== 'r' || cNextChar
== 't' || cNextChar
== '"' || cNextChar
== '`' || cNextChar
== '\'' ) )
498 styler
.ColourTo( i
+1, SCE_NSIS_STRINGVAR
);
499 bVarInString
= false;
500 bIngoreNextDollarSign
= false;
503 // Covers "$INSTDIR and user vars like $MYVAR"
504 else if( bVarInString
&& !isNsisChar(cNextChar
) )
506 int nWordState
= classifyWordNsis( styler
.GetStartSegment(), i
, keywordLists
, styler
);
507 if( nWordState
== SCE_NSIS_VARIABLE
)
508 styler
.ColourTo( i
, SCE_NSIS_STRINGVAR
);
510 styler
.ColourTo( i
, SCE_NSIS_STRINGVAR
);
511 bVarInString
= false;
513 // Covers "${TEST}..."
514 else if( bClassicVarInString
&& cNextChar
== '}' )
516 styler
.ColourTo( i
+1, SCE_NSIS_STRINGVAR
);
517 bClassicVarInString
= false;
520 // Start of var in string
521 if( !bIngoreNextDollarSign
&& cCurrChar
== '$' && cNextChar
== '{' )
523 styler
.ColourTo( i
-1, state
);
524 bClassicVarInString
= true;
525 bVarInString
= false;
527 else if( !bIngoreNextDollarSign
&& cCurrChar
== '$' )
529 styler
.ColourTo( i
-1, state
);
531 bClassicVarInString
= false;
536 // Colourise remaining document
537 styler
.ColourTo(nLengthDoc
-1,state
);
540 static void FoldNsisDoc(unsigned int startPos
, int length
, int, WordList
*[], Accessor
&styler
)
542 // No folding enabled, no reason to continue...
543 if( styler
.GetPropertyInt("fold") == 0 )
546 bool foldAtElse
= styler
.GetPropertyInt("fold.at.else", 0) == 1;
547 bool foldUtilityCmd
= styler
.GetPropertyInt("nsis.foldutilcmd", 1) == 1;
548 bool blockComment
= false;
550 int lineCurrent
= styler
.GetLine(startPos
);
551 unsigned int safeStartPos
= styler
.LineStart( lineCurrent
);
556 int levelCurrent
= SC_FOLDLEVELBASE
;
558 levelCurrent
= styler
.LevelAt(lineCurrent
-1) >> 16;
559 int levelNext
= levelCurrent
;
560 int style
= styler
.StyleAt(safeStartPos
);
561 if( style
== SCE_NSIS_COMMENTBOX
)
563 if( styler
.SafeGetCharAt(safeStartPos
) == '/' && styler
.SafeGetCharAt(safeStartPos
+1) == '*' )
568 for (unsigned int i
= safeStartPos
; i
< startPos
+ length
; i
++)
570 char chCurr
= styler
.SafeGetCharAt(i
);
571 style
= styler
.StyleAt(i
);
572 if( blockComment
&& style
!= SCE_NSIS_COMMENTBOX
)
575 blockComment
= false;
577 else if( !blockComment
&& style
== SCE_NSIS_COMMENTBOX
)
583 if( bArg1
&& !blockComment
)
585 if( nWordStart
== -1 && (isNsisLetter(chCurr
) || chCurr
== '!') )
589 else if( isNsisLetter(chCurr
) == false && nWordStart
> -1 )
591 int newLevel
= calculateFoldNsis( nWordStart
, i
-1, levelNext
, styler
, foldAtElse
, foldUtilityCmd
);
593 if( newLevel
== levelNext
)
595 if( foldAtElse
&& foldUtilityCmd
)
597 if( NsisNextLineHasElse(i
, startPos
+ length
, styler
) )
602 levelNext
= newLevel
;
609 if( bArg1
&& foldAtElse
&& foldUtilityCmd
&& !blockComment
)
611 if( NsisNextLineHasElse(i
, startPos
+ length
, styler
) )
615 // If we are on a new line...
616 int levelUse
= levelCurrent
;
617 int lev
= levelUse
| levelNext
<< 16;
618 if (levelUse
< levelNext
)
619 lev
|= SC_FOLDLEVELHEADERFLAG
;
620 if (lev
!= styler
.LevelAt(lineCurrent
))
621 styler
.SetLevel(lineCurrent
, lev
);
624 levelCurrent
= levelNext
;
625 bArg1
= true; // New line, lets look at first argument again
630 int levelUse
= levelCurrent
;
631 int lev
= levelUse
| levelNext
<< 16;
632 if (levelUse
< levelNext
)
633 lev
|= SC_FOLDLEVELHEADERFLAG
;
634 if (lev
!= styler
.LevelAt(lineCurrent
))
635 styler
.SetLevel(lineCurrent
, lev
);
638 static const char * const nsisWordLists
[] = {
646 LexerModule
lmNsis(SCLEX_NSIS
, ColouriseNsisDoc
, "nsis", FoldNsisDoc
, nsisWordLists
);