*** empty log message ***
[anjuta-git-plugin.git] / scintilla / LexScriptol.cxx
blobfaaa2d46dfad78c264fd4d52d8fa51b0b614a402
1 // Scintilla source code edit control
2 /** @file LexScriptol.cxx
3 ** Lexer for Scriptol.
4 **/
6 #include <stdlib.h>
7 #include <string.h>
8 #include <ctype.h>
9 #include <stdio.h>
10 #include <stdarg.h>
12 #include "Platform.h"
14 #include "PropSet.h"
15 #include "Accessor.h"
16 #include "KeyWords.h"
17 #include "Scintilla.h"
18 #include "SciLexer.h"
20 static void ClassifyWordSol(unsigned int start, unsigned int end, WordList &keywords, Accessor &styler, char *prevWord)
22 char s[100];
23 bool wordIsNumber = isdigit(styler[start]) != 0;
24 for (unsigned int i = 0; i < end - start + 1 && i < 30; i++)
26 s[i] = styler[start + i];
27 s[i + 1] = '\0';
29 char chAttr = SCE_SCRIPTOL_IDENTIFIER;
30 if (0 == strcmp(prevWord, "class")) chAttr = SCE_SCRIPTOL_CLASSNAME;
31 else if (wordIsNumber) chAttr = SCE_SCRIPTOL_NUMBER;
32 else if (keywords.InList(s)) chAttr = SCE_SCRIPTOL_KEYWORD;
33 else for (unsigned int i = 0; i < end - start + 1; i++) // test dotted idents
35 if (styler[start + i] == '.')
37 styler.ColourTo(start + i - 1, chAttr);
38 styler.ColourTo(start + i, SCE_SCRIPTOL_OPERATOR);
41 styler.ColourTo(end, chAttr);
42 strcpy(prevWord, s);
45 static bool IsSolComment(Accessor &styler, int pos, int len)
47 char c;
48 if(len > 0)
50 c = styler[pos];
51 if(c == '`') return true;
52 if(len > 1)
54 if(c == '/')
56 c = styler[pos + 1];
57 if(c == '/') return true;
58 if(c == '*') return true;
62 return false;
65 static bool IsSolStringStart(char ch)
67 if (ch == '\'' || ch == '"') return true;
68 return false;
71 static bool IsSolWordStart(char ch)
73 return (iswordchar(ch) && !IsSolStringStart(ch));
77 static int GetSolStringState(Accessor &styler, int i, int *nextIndex)
79 char ch = styler.SafeGetCharAt(i);
80 char chNext = styler.SafeGetCharAt(i + 1);
82 if (ch != '\"' && ch != '\'')
84 *nextIndex = i + 1;
85 return SCE_SCRIPTOL_DEFAULT;
87 // ch is either single or double quotes in string
88 // code below seem non-sense but is here for future extensions
89 if (ch == chNext && ch == styler.SafeGetCharAt(i + 2))
91 *nextIndex = i + 3;
92 if(ch == '\"') return SCE_SCRIPTOL_TRIPLE;
93 if(ch == '\'') return SCE_SCRIPTOL_TRIPLE;
94 return SCE_SCRIPTOL_STRING;
96 else
98 *nextIndex = i + 1;
99 if (ch == '"') return SCE_SCRIPTOL_STRING;
100 else return SCE_SCRIPTOL_STRING;
105 static void ColouriseSolDoc(unsigned int startPos, int length, int initStyle,
106 WordList *keywordlists[], Accessor &styler)
109 int lengthDoc = startPos + length;
110 char stringType = '\"';
112 if (startPos > 0)
114 int lineCurrent = styler.GetLine(startPos);
115 if (lineCurrent > 0)
117 startPos = styler.LineStart(lineCurrent-1);
118 if (startPos == 0) initStyle = SCE_SCRIPTOL_DEFAULT;
119 else initStyle = styler.StyleAt(startPos-1);
123 styler.StartAt(startPos, 127);
125 WordList &keywords = *keywordlists[0];
127 int whingeLevel = styler.GetPropertyInt("tab.timmy.whinge.level");
128 char prevWord[200];
129 prevWord[0] = '\0';
130 if (length == 0) return;
132 int state = initStyle & 31;
134 int nextIndex = 0;
135 char chPrev = ' ';
136 char chPrev2 = ' ';
137 char chNext = styler[startPos];
138 styler.StartSegment(startPos);
139 bool atStartLine = true;
140 int spaceFlags = 0;
141 for (int i = startPos; i < lengthDoc; i++)
144 if (atStartLine)
146 char chBad = static_cast<char>(64);
147 char chGood = static_cast<char>(0);
148 char chFlags = chGood;
150 if (whingeLevel == 1)
152 chFlags = (spaceFlags & wsInconsistent) ? chBad : chGood;
154 else if (whingeLevel == 2)
156 chFlags = (spaceFlags & wsSpaceTab) ? chBad : chGood;
158 else if (whingeLevel == 3)
160 chFlags = (spaceFlags & wsSpace) ? chBad : chGood;
162 else if (whingeLevel == 4)
164 chFlags = (spaceFlags & wsTab) ? chBad : chGood;
166 styler.SetFlags(chFlags, static_cast<char>(state));
167 atStartLine = false;
170 char ch = chNext;
171 chNext = styler.SafeGetCharAt(i + 1);
173 if ((ch == '\r' && chNext != '\n') || (ch == '\n') || (i == lengthDoc))
175 if ((state == SCE_SCRIPTOL_DEFAULT) ||
176 (state == SCE_SCRIPTOL_TRIPLE) ||
177 (state == SCE_SCRIPTOL_COMMENTBLOCK))
179 styler.ColourTo(i, state);
181 atStartLine = true;
184 if (styler.IsLeadByte(ch))
186 chNext = styler.SafeGetCharAt(i + 2);
187 chPrev = ' ';
188 chPrev2 = ' ';
189 i += 1;
190 continue;
193 if (state == SCE_SCRIPTOL_STRINGEOL)
195 if (ch != '\r' && ch != '\n')
197 styler.ColourTo(i - 1, state);
198 state = SCE_SCRIPTOL_DEFAULT;
202 if (state == SCE_SCRIPTOL_DEFAULT)
204 if (IsSolWordStart(ch))
206 styler.ColourTo(i - 1, state);
207 state = SCE_SCRIPTOL_KEYWORD;
209 else if (ch == '`')
211 styler.ColourTo(i - 1, state);
212 state = SCE_SCRIPTOL_COMMENTLINE;
214 else if (ch == '/')
216 styler.ColourTo(i - 1, state);
217 if(chNext == '/') state = SCE_SCRIPTOL_CSTYLE;
218 if(chNext == '*') state = SCE_SCRIPTOL_COMMENTBLOCK;
221 else if (IsSolStringStart(ch))
223 styler.ColourTo(i - 1, state);
224 state = GetSolStringState(styler, i, &nextIndex);
225 if(state == SCE_SCRIPTOL_STRING)
227 stringType = ch;
229 if (nextIndex != i + 1)
231 i = nextIndex - 1;
232 ch = ' ';
233 chPrev = ' ';
234 chNext = styler.SafeGetCharAt(i + 1);
237 else if (isoperator(ch))
239 styler.ColourTo(i - 1, state);
240 styler.ColourTo(i, SCE_SCRIPTOL_OPERATOR);
243 else if (state == SCE_SCRIPTOL_KEYWORD)
245 if (!iswordchar(ch))
247 ClassifyWordSol(styler.GetStartSegment(), i - 1, keywords, styler, prevWord);
248 state = SCE_SCRIPTOL_DEFAULT;
249 if (ch == '`')
251 state = chNext == '`' ? SCE_SCRIPTOL_PERSISTENT : SCE_SCRIPTOL_COMMENTLINE;
253 else if (IsSolStringStart(ch))
255 styler.ColourTo(i - 1, state);
256 state = GetSolStringState(styler, i, &nextIndex);
257 if (nextIndex != i + 1)
259 i = nextIndex - 1;
260 ch = ' ';
261 chPrev = ' ';
262 chNext = styler.SafeGetCharAt(i + 1);
265 else if (isoperator(ch))
267 styler.ColourTo(i, SCE_SCRIPTOL_OPERATOR);
271 else
273 if (state == SCE_SCRIPTOL_COMMENTLINE ||
274 state == SCE_SCRIPTOL_PERSISTENT ||
275 state == SCE_SCRIPTOL_CSTYLE)
277 if (ch == '\r' || ch == '\n')
279 styler.ColourTo(i - 1, state);
280 state = SCE_SCRIPTOL_DEFAULT;
283 else if(state == SCE_SCRIPTOL_COMMENTBLOCK)
285 if(chPrev == '*' && ch == '/')
287 styler.ColourTo(i, state);
288 state = SCE_SCRIPTOL_DEFAULT;
291 else if ((state == SCE_SCRIPTOL_STRING) ||
292 (state == SCE_SCRIPTOL_CHARACTER))
294 if ((ch == '\r' || ch == '\n') && (chPrev != '\\'))
296 styler.ColourTo(i - 1, state);
297 state = SCE_SCRIPTOL_STRINGEOL;
299 else if (ch == '\\')
301 if (chNext == '\"' || chNext == '\'' || chNext == '\\')
303 i++;
304 ch = chNext;
305 chNext = styler.SafeGetCharAt(i + 1);
308 else if ((ch == '\"') || (ch == '\''))
310 // must match the entered quote type
311 if(ch == stringType)
313 styler.ColourTo(i, state);
314 state = SCE_SCRIPTOL_DEFAULT;
318 else if (state == SCE_SCRIPTOL_TRIPLE)
320 if ((ch == '\'' && chPrev == '\'' && chPrev2 == '\'') ||
321 (ch == '\"' && chPrev == '\"' && chPrev2 == '\"'))
323 styler.ColourTo(i, state);
324 state = SCE_SCRIPTOL_DEFAULT;
329 chPrev2 = chPrev;
330 chPrev = ch;
332 if (state == SCE_SCRIPTOL_KEYWORD)
334 ClassifyWordSol(styler.GetStartSegment(),
335 lengthDoc-1, keywords, styler, prevWord);
337 else
339 styler.ColourTo(lengthDoc-1, state);
343 static void FoldSolDoc(unsigned int startPos, int length, int initStyle,
344 WordList *[], Accessor &styler)
346 int lengthDoc = startPos + length;
348 int lineCurrent = styler.GetLine(startPos);
349 if (startPos > 0)
351 if (lineCurrent > 0)
353 lineCurrent--;
354 startPos = styler.LineStart(lineCurrent);
355 if (startPos == 0)
356 initStyle = SCE_SCRIPTOL_DEFAULT;
357 else
358 initStyle = styler.StyleAt(startPos-1);
361 int state = initStyle & 31;
362 int spaceFlags = 0;
363 int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, IsSolComment);
364 if ((state == SCE_SCRIPTOL_TRIPLE))
365 indentCurrent |= SC_FOLDLEVELWHITEFLAG;
366 char chNext = styler[startPos];
367 for (int i = startPos; i < lengthDoc; i++)
369 char ch = chNext;
370 chNext = styler.SafeGetCharAt(i + 1);
371 int style = styler.StyleAt(i) & 31;
373 if ((ch == '\r' && chNext != '\n') || (ch == '\n') || (i == lengthDoc))
375 int lev = indentCurrent;
376 int indentNext = styler.IndentAmount(lineCurrent + 1, &spaceFlags, IsSolComment);
377 if (style == SCE_SCRIPTOL_TRIPLE)
378 indentNext |= SC_FOLDLEVELWHITEFLAG;
379 if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG))
381 // Only non whitespace lines can be headers
382 if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK))
384 lev |= SC_FOLDLEVELHEADERFLAG;
386 else if (indentNext & SC_FOLDLEVELWHITEFLAG)
388 // Line after is blank so check the next - maybe should continue further?
389 int spaceFlags2 = 0;
390 int indentNext2 = styler.IndentAmount(lineCurrent + 2, &spaceFlags2, IsSolComment);
391 if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext2 & SC_FOLDLEVELNUMBERMASK))
393 lev |= SC_FOLDLEVELHEADERFLAG;
397 indentCurrent = indentNext;
398 styler.SetLevel(lineCurrent, lev);
399 lineCurrent++;
404 LexerModule lmScriptol(SCLEX_SCRIPTOL, ColouriseSolDoc, "scriptol", FoldSolDoc);