]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexNsis.cxx
Added 'Back' to accelerator possibilities
[wxWidgets.git] / src / stc / scintilla / src / LexNsis.cxx
1 // Scintilla source code edit control
2 /** @file LexNsis.cxx
3 ** Lexer for NSIS
4 **/
5 // Copyright 2003 by Angelo Mandato <angelo@spaceblue.com>
6 // The License.txt file describes the conditions under which this software may be distributed.
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <stdio.h>
12 #include <stdarg.h>
13
14 #include "Platform.h"
15
16 #include "PropSet.h"
17 #include "Accessor.h"
18 #include "KeyWords.h"
19 #include "Scintilla.h"
20 #include "SciLexer.h"
21
22 /*
23 // Put in SciLexer.h
24 #define SCLEX_NSIS 34
25
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 */
41
42 static int classifyWordNsis(unsigned int start, unsigned int end, WordList *keywordLists[], Accessor &styler)
43 {
44 char s[100];
45
46 WordList &Functions = *keywordLists[0];
47 WordList &Variables = *keywordLists[1];
48 WordList &Lables = *keywordLists[2];
49 WordList &UserDefined = *keywordLists[3];
50
51 for (unsigned int i = 0; i < end - start + 1 && i < 30; i++)
52 {
53 s[i] = static_cast<char>( styler[ start + i ] );
54 s[i + 1] = '\0';
55 }
56
57 // Check for special words...
58
59 if( strcmp(s, "!macro") == 0 || strcmp(s, "!macroend") == 0 ) // Covers !micro and !microend
60 return SCE_NSIS_MACRODEF;
61
62 if( strcmp(s, "!ifdef") == 0 || strcmp(s, "!ifndef") == 0 || strcmp(s, "!endif") == 0 )
63 return SCE_NSIS_IFDEFINEDEF;
64
65 if( strcmp(s, "Section") == 0 || strcmp(s, "SectionEnd") == 0 ) // Covers Section and SectionEnd
66 return SCE_NSIS_SECTIONDEF;
67
68 if( strcmp(s, "SubSection") == 0 || strcmp(s, "SubSectionEnd") == 0 ) // Covers SubSection and SubSectionEnd
69 return SCE_NSIS_SUBSECTIONDEF;
70
71 if( strcmp(s, "Function") == 0 || strcmp(s, "FunctionEnd") == 0 ) // Covers SubSection and SubSectionEnd
72 return SCE_NSIS_FUNCTION;
73
74 if ( Functions.InList(s) )
75 return SCE_NSIS_FUNCTION;
76
77 if ( Variables.InList(s) )
78 return SCE_NSIS_VARIABLE;
79
80 if ( Lables.InList(s) )
81 return SCE_NSIS_LABEL;
82
83 if( UserDefined.InList(s) )
84 return SCE_NSIS_USERDEFINED;
85
86 if( strlen(s) > 2 )
87 {
88 if( s[1] == '{' && s[strlen(s)-1] == '}' )
89 return SCE_NSIS_VARIABLE;
90 }
91
92 return SCE_NSIS_DEFAULT;
93 }
94
95 static void ColouriseNsisDoc(unsigned int startPos, int length, int, WordList *keywordLists[], Accessor &styler)
96 {
97 int state = SCE_NSIS_DEFAULT;
98 styler.StartAt( startPos );
99 styler.GetLine( startPos );
100
101 unsigned int nLengthDoc = startPos + length;
102 styler.StartSegment( startPos );
103
104 char cCurrChar;
105 bool bVarInString = true;
106
107 unsigned int i;
108 for( i = startPos; i < nLengthDoc; i++ )
109 {
110 cCurrChar = styler.SafeGetCharAt( i );
111 char cNextChar = styler.SafeGetCharAt( i+1, EOF );
112
113
114
115 switch(state)
116 {
117 case SCE_NSIS_DEFAULT:
118 if( cNextChar == EOF )
119 {
120 styler.ColourTo(i,SCE_NSIS_DEFAULT);
121 break;
122 }
123 if( cCurrChar == ';' || cCurrChar == '#' ) // we have a comment line
124 {
125 styler.ColourTo(i-1, state );
126 state = SCE_NSIS_COMMENT;
127 break;
128 }
129 if( cCurrChar == '"' )
130 {
131 styler.ColourTo(i-1, state );
132 state = SCE_NSIS_STRINGDQ;
133 bVarInString = false;
134 break;
135 }
136 if( cCurrChar == '\'' )
137 {
138 styler.ColourTo(i-1, state );
139 state = SCE_NSIS_STRINGRQ;
140 bVarInString = false;
141 break;
142 }
143 if( cCurrChar == '`' )
144 {
145 styler.ColourTo(i-1, state );
146 state = SCE_NSIS_STRINGLQ;
147 bVarInString = false;
148 break;
149 }
150
151 // NSIS KeyWord,Function, Variable, UserDefined:
152 if( cCurrChar == '$' || iswordchar(cCurrChar) || cCurrChar == '!' )
153 {
154 styler.ColourTo(i-1,state);
155 state = SCE_NSIS_FUNCTION;
156 break;
157 }
158 break;
159 case SCE_NSIS_COMMENT:
160 if( cNextChar == '\n' || cNextChar == '\r' || cNextChar == EOF )
161 {
162 styler.ColourTo(i,state);
163 state = SCE_NSIS_DEFAULT;
164 }
165 break;
166 case SCE_NSIS_STRINGDQ:
167 if( cCurrChar == '"' || cNextChar == '\r' || cNextChar == '\n' )
168 {
169 styler.ColourTo(i,SCE_NSIS_STRINGDQ);
170 state = SCE_NSIS_DEFAULT;
171 }
172 break;
173 case SCE_NSIS_STRINGLQ:
174 if( cCurrChar == '`' || cNextChar == '\r' || cNextChar == '\n' )
175 {
176 styler.ColourTo(i,SCE_NSIS_STRINGLQ);
177 state = SCE_NSIS_DEFAULT;
178 }
179 break;
180 case SCE_NSIS_STRINGRQ:
181 if( cCurrChar == '\'' || cNextChar == '\r' || cNextChar == '\n' )
182 {
183 styler.ColourTo(i,SCE_NSIS_STRINGRQ);
184 state = SCE_NSIS_DEFAULT;
185 }
186 break;
187 case SCE_NSIS_FUNCTION:
188
189 // NSIS KeyWord:
190 if( (iswordchar(cCurrChar) && !iswordchar( cNextChar) && cNextChar != '}') || cCurrChar == '}' )
191 {
192 state = classifyWordNsis( styler.GetStartSegment(), i, keywordLists, styler);
193 styler.ColourTo( i, state);
194 state = SCE_NSIS_DEFAULT; // Everything after goes back to the default state
195 }
196 else if( !iswordchar( cCurrChar ) && cCurrChar != '{' && cCurrChar != '}' )
197 {
198 state = SCE_NSIS_DEFAULT;
199
200 if( cCurrChar == '"' ) // Next
201 {
202 state = SCE_NSIS_STRINGDQ;
203 bVarInString = false;
204 }
205 if( cCurrChar == '`' )
206 {
207 state = SCE_NSIS_STRINGLQ;
208 bVarInString = false;
209 }
210 if( cCurrChar == '\'' )
211 {
212 state = SCE_NSIS_STRINGRQ;
213 bVarInString = false;
214 }
215 if( cCurrChar == '#' || cCurrChar == ';' )
216 state = SCE_NSIS_COMMENT;
217
218 styler.ColourTo( i, state);
219 }
220 break;
221 }
222
223 if( state == SCE_NSIS_COMMENT )
224 {
225 styler.ColourTo(i,state);
226 }
227 else if( state == SCE_NSIS_STRINGDQ || state == SCE_NSIS_STRINGLQ || state == SCE_NSIS_STRINGRQ )
228 {
229 // Check for var in String..
230 if( bVarInString && (iswordchar(cCurrChar) || cCurrChar == '}') ) // || cCurrChar == '{' ) )
231 {
232 int nWordState = classifyWordNsis( styler.GetStartSegment(), i, keywordLists, styler);
233 if( nWordState == SCE_NSIS_VARIABLE )
234 {
235 styler.ColourTo( i, SCE_NSIS_STRINGVAR);
236 bVarInString = false;
237 }
238 }
239 if( cCurrChar == '$' )
240 {
241 styler.ColourTo( i-1, state);
242 bVarInString = true;
243 }
244 }
245 }
246 }
247
248
249 static void FoldNsisDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler)
250 {
251 // No folding enabled, no reason to continue...
252 if( styler.GetPropertyInt("fold") == 0 )
253 return;
254
255 unsigned int endPos = startPos + length;
256 int lineCurrent = styler.GetLine(startPos);
257 int levelCurrent = SC_FOLDLEVELBASE;
258 if (lineCurrent > 0)
259 levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
260 int levelNext = levelCurrent;
261 char chNext = styler[startPos];
262 int styleNext = styler.StyleAt(startPos);
263 int style;
264
265 for (unsigned int i = startPos; i < endPos; i++)
266 {
267 char ch = chNext;
268 chNext = styler.SafeGetCharAt(i + 1);
269 style = styleNext;
270 styleNext = styler.StyleAt(i + 1);
271 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
272 // Functions Start: Function, Section, SubSection
273 // Functions End: FunctionEnd, SectionEnd, SubSectionEnd
274 // Label Start: !ifdef, !ifndef
275 // Label End: !endif
276
277 if( style == SCE_NSIS_FUNCTION )
278 {
279 if( styler.Match(i, "FunctionEnd") )
280 levelNext--;
281 else if( styler.Match(i, "Function") )
282 levelNext++;
283 }
284 else if( style == SCE_NSIS_SECTIONDEF )
285 {
286 if( styler.Match(i, "SectionEnd") )
287 levelNext--;
288 else if( styler.Match(i, "Section") )
289 levelNext++;
290 }
291 else if( style == SCE_NSIS_SUBSECTIONDEF )
292 {
293 if( styler.Match(i, "SubSectionEnd") )
294 levelNext--;
295 else if( styler.Match(i, "SubSection") )
296 levelNext++;
297 }
298 else if( style == SCE_NSIS_IFDEFINEDEF )
299 {
300 if( styler.Match(i, "!endif") )
301 levelNext--;
302 else if( styler.Match(i, "!ifdef") || styler.Match(i, "!ifndef"))
303 levelNext++;
304 }
305 else if( style == SCE_NSIS_MACRODEF )
306 {
307 if( styler.Match(i, "!macroend") )
308 levelNext--;
309 else if( styler.Match(i, "!macro") )
310 levelNext++;
311 }
312
313 if( atEOL )
314 {
315 int levelUse = levelCurrent;
316 int lev = levelUse | levelNext << 16;
317 if (levelUse < levelNext)
318 lev |= SC_FOLDLEVELHEADERFLAG;
319 if (lev != styler.LevelAt(lineCurrent))
320 {
321 styler.SetLevel(lineCurrent, lev);
322 }
323 lineCurrent++;
324 levelCurrent = levelNext;
325 }
326 }
327
328 int levelUse = levelCurrent;
329 int lev = levelUse | levelNext << 16;
330 if (levelUse < levelNext)
331 lev |= SC_FOLDLEVELHEADERFLAG;
332 if (lev != styler.LevelAt(lineCurrent))
333 {
334 styler.SetLevel(lineCurrent, lev);
335 }
336 }
337
338 static const char * const nsisWordLists[] = {
339 "Functions",
340 "Variables",
341 "Lables",
342 "UserDefined",
343 0, };
344
345
346 LexerModule lmNsis(SCLEX_NSIS, ColouriseNsisDoc, "nsis", FoldNsisDoc, nsisWordLists);