profiler: bgo#633018 crash in Anjuta IDE: misuse of xmlCleanupParser
[anjuta-extras.git] / plugins / scintilla / scintilla / KeyWords.cxx
blob5e4de668d04ebf747f196811cb546349a290aa34
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.
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <stdio.h>
12 #include <stdarg.h>
14 #include "Platform.h"
16 #include "PropSet.h"
17 #include "Accessor.h"
18 #include "KeyWords.h"
19 #include "Scintilla.h"
20 #include "SciLexer.h"
22 #ifdef SCI_NAMESPACE
23 using namespace Scintilla;
24 #endif
26 /**
27 * Creates an array that points into each word in the string and puts \0 terminators
28 * after each word.
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;
39 wordSeparator['\r'] = true;
40 wordSeparator['\n'] = true;
41 if (!onlyLineEnds) {
42 wordSeparator[' '] = true;
43 wordSeparator['\t'] = true;
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;
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++;
62 } else {
63 wordlist[k] = '\0';
65 prev = wordlist[k];
67 keywords[words] = &wordlist[slen];
68 *len = words;
69 } else {
70 *len = 0;
72 return keywords;
75 void WordList::Clear() {
76 if (words) {
77 delete []list;
78 delete []words;
80 words = 0;
81 list = 0;
82 len = 0;
83 sorted = false;
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);
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));
98 static void SortWordList(char **words, unsigned int len) {
99 qsort(reinterpret_cast<void*>(words), len, sizeof(*words),
100 cmpString);
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;
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++;
127 if (!*a && !*b)
128 return true;
130 j++;
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++;
142 if (!*a)
143 return true;
144 j++;
147 return false;
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.
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;
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++;
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++;
187 b++;
189 if ((!*a || isSubword) && !*b)
190 return true;
192 j++;
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++;
204 if (!*a)
205 return true;
206 j++;
209 return false;
212 const LexerModule *LexerModule::base = 0;
213 int LexerModule::nextLanguage = SCLEX_AUTOMATIC+1;
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++;
235 int LexerModule::GetNumWordLists() const {
236 if (wordListDescriptions == NULL) {
237 return -1;
238 } else {
239 int numWordLists = 0;
241 while (wordListDescriptions[numWordLists]) {
242 ++numWordLists;
245 return numWordLists;
249 const char *LexerModule::GetWordListDescription(int index) const {
250 static const char *emptyStr = "";
252 PLATFORM_ASSERT(index < GetNumWordLists());
253 if (index >= GetNumWordLists()) {
254 return emptyStr;
255 } else {
256 return wordListDescriptions[index];
260 int LexerModule::GetStyleBitsNeeded() const {
261 return styleBits;
264 const LexerModule *LexerModule::Find(int language) {
265 const LexerModule *lm = base;
266 while (lm) {
267 if (lm->language == language) {
268 return lm;
270 lm = lm->next;
272 return 0;
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;
282 lm = lm->next;
285 return 0;
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);
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);
309 fnFolder(startPos, lengthDoc, initStyle, keywordlists, styler);
313 // Alternative historical name for Scintilla_LinkLexers
314 int wxForceScintillaLexers(void) {
315 return Scintilla_LinkLexers();
318 // To add or remove a lexer, add or remove its file and run LexGen.py.
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;
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();
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);
426 //--Autogenerated -- end of automatically generated section
428 return 1;