]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/KeyWords.cxx
5e4de668d04ebf747f196811cb546349a290aa34
[wxWidgets.git] / src / stc / scintilla / src / KeyWords.cxx
1 // Scintilla source code edit control
2 /** @file KeyWords.cxx
3 ** Colourise for particular languages.
4 **/
5 // Copyright 1998-2002 by Neil Hodgson <neilh@scintilla.org>
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 #ifdef SCI_NAMESPACE
23 using namespace Scintilla;
24 #endif
25
26 /**
27 * Creates an array that points into each word in the string and puts \0 terminators
28 * after each word.
29 */
30 static char **ArrayFromWordList(char *wordlist, int *len, bool onlyLineEnds = false) {
31 int prev = '\n';
32 int words = 0;
33 // For rapid determination of whether a character is a separator, build
34 // a look up table.
35 bool wordSeparator[256];
36 for (int i=0;i<256; i++) {
37 wordSeparator[i] = false;
38 }
39 wordSeparator['\r'] = true;
40 wordSeparator['\n'] = true;
41 if (!onlyLineEnds) {
42 wordSeparator[' '] = true;
43 wordSeparator['\t'] = true;
44 }
45 for (int j = 0; wordlist[j]; j++) {
46 int curr = static_cast<unsigned char>(wordlist[j]);
47 if (!wordSeparator[curr] && wordSeparator[prev])
48 words++;
49 prev = curr;
50 }
51 char **keywords = new char *[words + 1];
52 if (keywords) {
53 words = 0;
54 prev = '\0';
55 size_t slen = strlen(wordlist);
56 for (size_t k = 0; k < slen; k++) {
57 if (!wordSeparator[static_cast<unsigned char>(wordlist[k])]) {
58 if (!prev) {
59 keywords[words] = &wordlist[k];
60 words++;
61 }
62 } else {
63 wordlist[k] = '\0';
64 }
65 prev = wordlist[k];
66 }
67 keywords[words] = &wordlist[slen];
68 *len = words;
69 } else {
70 *len = 0;
71 }
72 return keywords;
73 }
74
75 void WordList::Clear() {
76 if (words) {
77 delete []list;
78 delete []words;
79 }
80 words = 0;
81 list = 0;
82 len = 0;
83 sorted = false;
84 }
85
86 void WordList::Set(const char *s) {
87 list = new char[strlen(s) + 1];
88 strcpy(list, s);
89 sorted = false;
90 words = ArrayFromWordList(list, &len, onlyLineEnds);
91 }
92
93 extern "C" int cmpString(const void *a1, const void *a2) {
94 // Can't work out the correct incantation to use modern casts here
95 return strcmp(*(char**)(a1), *(char**)(a2));
96 }
97
98 static void SortWordList(char **words, unsigned int len) {
99 qsort(reinterpret_cast<void*>(words), len, sizeof(*words),
100 cmpString);
101 }
102
103 bool WordList::InList(const char *s) {
104 if (0 == words)
105 return false;
106 if (!sorted) {
107 sorted = true;
108 SortWordList(words, len);
109 for (unsigned int k = 0; k < (sizeof(starts) / sizeof(starts[0])); k++)
110 starts[k] = -1;
111 for (int l = len - 1; l >= 0; l--) {
112 unsigned char indexChar = words[l][0];
113 starts[indexChar] = l;
114 }
115 }
116 unsigned char firstChar = s[0];
117 int j = starts[firstChar];
118 if (j >= 0) {
119 while ((unsigned char)words[j][0] == firstChar) {
120 if (s[1] == words[j][1]) {
121 const char *a = words[j] + 1;
122 const char *b = s + 1;
123 while (*a && *a == *b) {
124 a++;
125 b++;
126 }
127 if (!*a && !*b)
128 return true;
129 }
130 j++;
131 }
132 }
133 j = starts['^'];
134 if (j >= 0) {
135 while (words[j][0] == '^') {
136 const char *a = words[j] + 1;
137 const char *b = s;
138 while (*a && *a == *b) {
139 a++;
140 b++;
141 }
142 if (!*a)
143 return true;
144 j++;
145 }
146 }
147 return false;
148 }
149
150 /** similar to InList, but word s can be a substring of keyword.
151 * eg. the keyword define is defined as def~ine. This means the word must start
152 * with def to be a keyword, but also defi, defin and define are valid.
153 * The marker is ~ in this case.
154 */
155 bool WordList::InListAbbreviated(const char *s, const char marker) {
156 if (0 == words)
157 return false;
158 if (!sorted) {
159 sorted = true;
160 SortWordList(words, len);
161 for (unsigned int k = 0; k < (sizeof(starts) / sizeof(starts[0])); k++)
162 starts[k] = -1;
163 for (int l = len - 1; l >= 0; l--) {
164 unsigned char indexChar = words[l][0];
165 starts[indexChar] = l;
166 }
167 }
168 unsigned char firstChar = s[0];
169 int j = starts[firstChar];
170 if (j >= 0) {
171 while (words[j][0] == firstChar) {
172 bool isSubword = false;
173 int start = 1;
174 if (words[j][1] == marker) {
175 isSubword = true;
176 start++;
177 }
178 if (s[1] == words[j][start]) {
179 const char *a = words[j] + start;
180 const char *b = s + 1;
181 while (*a && *a == *b) {
182 a++;
183 if (*a == marker) {
184 isSubword = true;
185 a++;
186 }
187 b++;
188 }
189 if ((!*a || isSubword) && !*b)
190 return true;
191 }
192 j++;
193 }
194 }
195 j = starts['^'];
196 if (j >= 0) {
197 while (words[j][0] == '^') {
198 const char *a = words[j] + 1;
199 const char *b = s;
200 while (*a && *a == *b) {
201 a++;
202 b++;
203 }
204 if (!*a)
205 return true;
206 j++;
207 }
208 }
209 return false;
210 }
211
212 const LexerModule *LexerModule::base = 0;
213 int LexerModule::nextLanguage = SCLEX_AUTOMATIC+1;
214
215 LexerModule::LexerModule(int language_,
216 LexerFunction fnLexer_,
217 const char *languageName_,
218 LexerFunction fnFolder_,
219 const char * const wordListDescriptions_[],
220 int styleBits_) :
221 language(language_),
222 fnLexer(fnLexer_),
223 fnFolder(fnFolder_),
224 wordListDescriptions(wordListDescriptions_),
225 styleBits(styleBits_),
226 languageName(languageName_) {
227 next = base;
228 base = this;
229 if (language == SCLEX_AUTOMATIC) {
230 language = nextLanguage;
231 nextLanguage++;
232 }
233 }
234
235 int LexerModule::GetNumWordLists() const {
236 if (wordListDescriptions == NULL) {
237 return -1;
238 } else {
239 int numWordLists = 0;
240
241 while (wordListDescriptions[numWordLists]) {
242 ++numWordLists;
243 }
244
245 return numWordLists;
246 }
247 }
248
249 const char *LexerModule::GetWordListDescription(int index) const {
250 static const char *emptyStr = "";
251
252 PLATFORM_ASSERT(index < GetNumWordLists());
253 if (index >= GetNumWordLists()) {
254 return emptyStr;
255 } else {
256 return wordListDescriptions[index];
257 }
258 }
259
260 int LexerModule::GetStyleBitsNeeded() const {
261 return styleBits;
262 }
263
264 const LexerModule *LexerModule::Find(int language) {
265 const LexerModule *lm = base;
266 while (lm) {
267 if (lm->language == language) {
268 return lm;
269 }
270 lm = lm->next;
271 }
272 return 0;
273 }
274
275 const LexerModule *LexerModule::Find(const char *languageName) {
276 if (languageName) {
277 const LexerModule *lm = base;
278 while (lm) {
279 if (lm->languageName && 0 == strcmp(lm->languageName, languageName)) {
280 return lm;
281 }
282 lm = lm->next;
283 }
284 }
285 return 0;
286 }
287
288 void LexerModule::Lex(unsigned int startPos, int lengthDoc, int initStyle,
289 WordList *keywordlists[], Accessor &styler) const {
290 if (fnLexer)
291 fnLexer(startPos, lengthDoc, initStyle, keywordlists, styler);
292 }
293
294 void LexerModule::Fold(unsigned int startPos, int lengthDoc, int initStyle,
295 WordList *keywordlists[], Accessor &styler) const {
296 if (fnFolder) {
297 int lineCurrent = styler.GetLine(startPos);
298 // Move back one line in case deletion wrecked current line fold state
299 if (lineCurrent > 0) {
300 lineCurrent--;
301 int newStartPos = styler.LineStart(lineCurrent);
302 lengthDoc += startPos - newStartPos;
303 startPos = newStartPos;
304 initStyle = 0;
305 if (startPos > 0) {
306 initStyle = styler.StyleAt(startPos - 1);
307 }
308 }
309 fnFolder(startPos, lengthDoc, initStyle, keywordlists, styler);
310 }
311 }
312
313 // Alternative historical name for Scintilla_LinkLexers
314 int wxForceScintillaLexers(void) {
315 return Scintilla_LinkLexers();
316 }
317
318 // To add or remove a lexer, add or remove its file and run LexGen.py.
319
320 // Force a reference to all of the Scintilla lexers so that the linker will
321 // not remove the code of the lexers.
322 int Scintilla_LinkLexers() {
323 static int forcer = 0;
324
325 // Shorten the code that declares a lexer and ensures it is linked in by calling a method.
326 #define LINK_LEXER(lexer) extern LexerModule lexer; forcer += lexer.GetLanguage();
327
328 //++Autogenerated -- run src/LexGen.py to regenerate
329 //**\(\tLINK_LEXER(\*);\n\)
330 LINK_LEXER(lmAbaqus);
331 LINK_LEXER(lmAda);
332 LINK_LEXER(lmAns1);
333 LINK_LEXER(lmAPDL);
334 LINK_LEXER(lmAsm);
335 LINK_LEXER(lmASY);
336 LINK_LEXER(lmAU3);
337 LINK_LEXER(lmAVE);
338 LINK_LEXER(lmBaan);
339 LINK_LEXER(lmBash);
340 LINK_LEXER(lmBatch);
341 LINK_LEXER(lmBlitzBasic);
342 LINK_LEXER(lmBullant);
343 LINK_LEXER(lmCaml);
344 LINK_LEXER(lmClw);
345 LINK_LEXER(lmClwNoCase);
346 LINK_LEXER(lmCmake);
347 LINK_LEXER(lmCOBOL);
348 LINK_LEXER(lmConf);
349 LINK_LEXER(lmCPP);
350 LINK_LEXER(lmCPPNoCase);
351 LINK_LEXER(lmCsound);
352 LINK_LEXER(lmCss);
353 LINK_LEXER(lmD);
354 LINK_LEXER(lmDiff);
355 LINK_LEXER(lmEiffel);
356 LINK_LEXER(lmEiffelkw);
357 LINK_LEXER(lmErlang);
358 LINK_LEXER(lmErrorList);
359 LINK_LEXER(lmESCRIPT);
360 LINK_LEXER(lmF77);
361 LINK_LEXER(lmFlagShip);
362 LINK_LEXER(lmForth);
363 LINK_LEXER(lmFortran);
364 LINK_LEXER(lmFreeBasic);
365 LINK_LEXER(lmGAP);
366 LINK_LEXER(lmGui4Cli);
367 LINK_LEXER(lmHaskell);
368 LINK_LEXER(lmHTML);
369 LINK_LEXER(lmInno);
370 LINK_LEXER(lmKix);
371 LINK_LEXER(lmLatex);
372 LINK_LEXER(lmLISP);
373 LINK_LEXER(lmLot);
374 LINK_LEXER(lmLout);
375 LINK_LEXER(lmLua);
376 LINK_LEXER(lmMagikSF);
377 LINK_LEXER(lmMake);
378 LINK_LEXER(lmMarkdown);
379 LINK_LEXER(lmMatlab);
380 LINK_LEXER(lmMETAPOST);
381 LINK_LEXER(lmMMIXAL);
382 LINK_LEXER(lmMSSQL);
383 LINK_LEXER(lmMySQL);
384 LINK_LEXER(lmNimrod);
385 LINK_LEXER(lmNncrontab);
386 LINK_LEXER(lmNsis);
387 LINK_LEXER(lmNull);
388 LINK_LEXER(lmOctave);
389 LINK_LEXER(lmOpal);
390 LINK_LEXER(lmPascal);
391 LINK_LEXER(lmPB);
392 LINK_LEXER(lmPerl);
393 LINK_LEXER(lmPHPSCRIPT);
394 LINK_LEXER(lmPLM);
395 LINK_LEXER(lmPo);
396 LINK_LEXER(lmPOV);
397 LINK_LEXER(lmPowerPro);
398 LINK_LEXER(lmPowerShell);
399 LINK_LEXER(lmProgress);
400 LINK_LEXER(lmProps);
401 LINK_LEXER(lmPS);
402 LINK_LEXER(lmPureBasic);
403 LINK_LEXER(lmPython);
404 LINK_LEXER(lmR);
405 LINK_LEXER(lmREBOL);
406 LINK_LEXER(lmRuby);
407 LINK_LEXER(lmScriptol);
408 LINK_LEXER(lmSmalltalk);
409 LINK_LEXER(lmSML);
410 LINK_LEXER(lmSorc);
411 LINK_LEXER(lmSpecman);
412 LINK_LEXER(lmSpice);
413 LINK_LEXER(lmSQL);
414 LINK_LEXER(lmTACL);
415 LINK_LEXER(lmTADS3);
416 LINK_LEXER(lmTAL);
417 LINK_LEXER(lmTCL);
418 LINK_LEXER(lmTeX);
419 LINK_LEXER(lmVB);
420 LINK_LEXER(lmVBScript);
421 LINK_LEXER(lmVerilog);
422 LINK_LEXER(lmVHDL);
423 LINK_LEXER(lmXML);
424 LINK_LEXER(lmYAML);
425
426 //--Autogenerated -- end of automatically generated section
427
428 return 1;
429 }