]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexFortran.cxx
listbox cleanup
[wxWidgets.git] / src / stc / scintilla / src / LexFortran.cxx
1 // Scintilla source code edit control
2 /** @file LexFortran.cxx
3 ** Lexer for Fortran.
4 ** Writen by Chuan-jian Shen, Last changed Sep. 2003
5 **/
6 // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
7 // The License.txt file describes the conditions under which this software may be distributed.
8 /***************************************/
9 #include <stdlib.h>
10 #include <string.h>
11 #include <ctype.h>
12 #include <stdio.h>
13 #include <stdarg.h>
14 /***************************************/
15 #include "Platform.h"
16 #include "PropSet.h"
17 #include "Accessor.h"
18 #include "StyleContext.h"
19 #include "KeyWords.h"
20 #include "Scintilla.h"
21 #include "SciLexer.h"
22 /***********************************************/
23 static inline bool IsAWordChar(const int ch) {
24 return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '%');
25 }
26 /**********************************************/
27 static inline bool IsAWordStart(const int ch) {
28 return (ch < 0x80) && (isalnum(ch));
29 }
30 /***************************************/
31 inline bool IsABlank(unsigned int ch) {
32 return (ch == ' ') || (ch == 0x09) || (ch == 0x0b) ;
33 }
34 /***************************************/
35 inline bool IsALineEnd(char ch) {
36 return ((ch == '\n') || (ch == '\r')) ;
37 }
38 /***************************************/
39 unsigned int GetContinuedPos(unsigned int pos, Accessor &styler) {
40 while (!IsALineEnd(styler.SafeGetCharAt(pos++))) continue;
41 if (styler.SafeGetCharAt(pos) == '\n') pos++;
42 while (IsABlank(styler.SafeGetCharAt(pos++))) continue;
43 char chCur = styler.SafeGetCharAt(pos);
44 if (chCur == '&') {
45 while (IsABlank(styler.SafeGetCharAt(++pos))) continue;
46 return pos;
47 } else {
48 return pos;
49 }
50 }
51 /***************************************/
52 static void ColouriseFortranDoc(unsigned int startPos, int length, int initStyle,
53 WordList *keywordlists[], Accessor &styler, bool isFixFormat) {
54 WordList &keywords = *keywordlists[0];
55 WordList &keywords2 = *keywordlists[1];
56 WordList &keywords3 = *keywordlists[2];
57 /***************************************/
58 int posLineStart = 0, numNonBlank = 0, prevState = 0;
59 int endPos = startPos + length;
60 /***************************************/
61 // backtrack to the nearest keyword
62 while ((startPos > 1) && (styler.StyleAt(startPos) != SCE_F_WORD)) {
63 startPos--;
64 }
65 startPos = styler.LineStart(styler.GetLine(startPos));
66 initStyle = styler.StyleAt(startPos - 1);
67 StyleContext sc(startPos, endPos-startPos, initStyle, styler);
68 /***************************************/
69 for (; sc.More(); sc.Forward()) {
70 // remember the start position of the line
71 if (sc.atLineStart) {
72 posLineStart = sc.currentPos;
73 numNonBlank = 0;
74 sc.SetState(SCE_F_DEFAULT);
75 }
76 if (!IsASpaceOrTab(sc.ch)) numNonBlank ++;
77 /***********************************************/
78 // Handle the fix format generically
79 int toLineStart = sc.currentPos - posLineStart;
80 if (isFixFormat && (toLineStart < 6 || toLineStart > 72)) {
81 if (toLineStart == 0 && (tolower(sc.ch) == 'c' || sc.ch == '*') || sc.ch == '!') {
82 sc.SetState(SCE_F_COMMENT);
83 while (!sc.atLineEnd && sc.More()) sc.Forward(); // Until line end
84 } else if (toLineStart > 72) {
85 sc.SetState(SCE_F_COMMENT);
86 while (!sc.atLineEnd && sc.More()) sc.Forward(); // Until line end
87 } else if (toLineStart < 5) {
88 if (IsADigit(sc.ch))
89 sc.SetState(SCE_F_LABEL);
90 else
91 sc.SetState(SCE_F_DEFAULT);
92 } else if (toLineStart == 5) {
93 if (!IsASpace(sc.ch) && sc.ch != '0') {
94 sc.SetState(SCE_F_CONTINUATION);
95 sc.ForwardSetState(prevState);
96 } else
97 sc.SetState(SCE_F_DEFAULT);
98 }
99 continue;
100 }
101 /***************************************/
102 // Handle line continuation generically.
103 if (!isFixFormat && sc.ch == '&') {
104 char chTemp = ' ';
105 int j = 1;
106 while (IsABlank(chTemp) && j<132) {
107 chTemp = static_cast<char>(sc.GetRelative(j));
108 j++;
109 }
110 if (chTemp == '!') {
111 sc.SetState(SCE_F_CONTINUATION);
112 if (sc.chNext == '!') sc.ForwardSetState(SCE_F_COMMENT);
113 } else if (chTemp == '\r' || chTemp == '\n') {
114 int currentState = sc.state;
115 sc.SetState(SCE_F_CONTINUATION);
116 sc.ForwardSetState(SCE_F_DEFAULT);
117 while (IsASpace(sc.ch) && sc.More()) sc.Forward();
118 if (sc.ch == '&') {
119 sc.SetState(SCE_F_CONTINUATION);
120 sc.Forward();
121 }
122 sc.SetState(currentState);
123 }
124 }
125 /***************************************/
126 // Determine if the current state should terminate.
127 if (sc.state == SCE_F_OPERATOR) {
128 sc.SetState(SCE_F_DEFAULT);
129 } else if (sc.state == SCE_F_NUMBER) {
130 if (!(IsAWordChar(sc.ch) || sc.ch=='\'' || sc.ch=='\"' || sc.ch=='.')) {
131 sc.SetState(SCE_F_DEFAULT);
132 }
133 } else if (sc.state == SCE_F_IDENTIFIER) {
134 if (!IsAWordChar(sc.ch) || (sc.ch == '%')) {
135 char s[100];
136 sc.GetCurrentLowered(s, sizeof(s));
137 if (keywords.InList(s)) {
138 sc.ChangeState(SCE_F_WORD);
139 } else if (keywords2.InList(s)) {
140 sc.ChangeState(SCE_F_WORD2);
141 } else if (keywords3.InList(s)) {
142 sc.ChangeState(SCE_F_WORD3);
143 }
144 sc.SetState(SCE_F_DEFAULT);
145 }
146 } else if (sc.state == SCE_F_COMMENT || sc.state == SCE_F_PREPROCESSOR) {
147 if (sc.ch == '\r' || sc.ch == '\n') {
148 sc.SetState(SCE_F_DEFAULT);
149 }
150 } else if (sc.state == SCE_F_STRING1) {
151 prevState = sc.state;
152 if (sc.ch == '\'') {
153 if (sc.chNext == '\'') {
154 sc.Forward();
155 } else {
156 sc.ForwardSetState(SCE_F_DEFAULT);
157 prevState = SCE_F_DEFAULT;
158 }
159 } else if (sc.atLineEnd) {
160 sc.ChangeState(SCE_F_STRINGEOL);
161 sc.ForwardSetState(SCE_F_DEFAULT);
162 }
163 } else if (sc.state == SCE_F_STRING2) {
164 prevState = sc.state;
165 if (sc.atLineEnd) {
166 sc.ChangeState(SCE_F_STRINGEOL);
167 sc.ForwardSetState(SCE_F_DEFAULT);
168 } else if (sc.ch == '\"') {
169 if (sc.chNext == '\"') {
170 sc.Forward();
171 } else {
172 sc.ForwardSetState(SCE_F_DEFAULT);
173 prevState = SCE_F_DEFAULT;
174 }
175 }
176 } else if (sc.state == SCE_F_OPERATOR2) {
177 if (sc.ch == '.') {
178 sc.ForwardSetState(SCE_F_DEFAULT);
179 }
180 } else if (sc.state == SCE_F_CONTINUATION) {
181 sc.SetState(SCE_F_DEFAULT);
182 } else if (sc.state == SCE_F_LABEL) {
183 if (!IsADigit(sc.ch)) {
184 sc.SetState(SCE_F_DEFAULT);
185 } else {
186 if (isFixFormat && sc.currentPos-posLineStart > 4)
187 sc.SetState(SCE_F_DEFAULT);
188 else if (numNonBlank > 5)
189 sc.SetState(SCE_F_DEFAULT);
190 }
191 }
192 /***************************************/
193 // Determine if a new state should be entered.
194 if (sc.state == SCE_F_DEFAULT) {
195 if (sc.ch == '!') {
196 if (sc.chNext == '$') {
197 sc.SetState(SCE_F_PREPROCESSOR);
198 } else {
199 sc.SetState(SCE_F_COMMENT);
200 }
201 } else if ((!isFixFormat) && IsADigit(sc.ch) && numNonBlank == 1) {
202 sc.SetState(SCE_F_LABEL);
203 } else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
204 sc.SetState(SCE_F_NUMBER);
205 } else if ((tolower(sc.ch) == 'b' || tolower(sc.ch) == 'o' ||
206 tolower(sc.ch) == 'z') && (sc.chNext == '\"' || sc.chNext == '\'')) {
207 sc.SetState(SCE_F_NUMBER);
208 sc.Forward();
209 } else if (sc.ch == '.' && isalpha(sc.chNext)) {
210 sc.SetState(SCE_F_OPERATOR2);
211 } else if (IsAWordStart(sc.ch)) {
212 sc.SetState(SCE_F_IDENTIFIER);
213 } else if (sc.ch == '\"') {
214 sc.SetState(SCE_F_STRING2);
215 } else if (sc.ch == '\'') {
216 sc.SetState(SCE_F_STRING1);
217 } else if (isoperator(static_cast<char>(sc.ch))) {
218 sc.SetState(SCE_F_OPERATOR);
219 }
220 }
221 }
222 sc.Complete();
223 }
224 /***************************************/
225 // To determine the folding level depending on keywords
226 static int classifyFoldPointFortran(const char* s, const char* prevWord, const char chNextNonBlank) {
227 int lev = 0;
228 if ((strcmp(prevWord, "else") == 0 && strcmp(s, "if") == 0) || strcmp(s, "elseif") == 0)
229 return -1;
230 if (strcmp(s, "associate") == 0 || strcmp(s, "block") == 0
231 || strcmp(s, "blockdata") == 0 || strcmp(s, "select") == 0
232 || strcmp(s, "do") == 0 || strcmp(s, "enum") ==0
233 || strcmp(s, "function") == 0 || strcmp(s, "interface") == 0
234 || strcmp(s, "module") == 0 || strcmp(s, "program") == 0
235 || strcmp(s, "subroutine") == 0 || strcmp(s, "then") == 0
236 || (strcmp(s, "type") == 0 && chNextNonBlank != '(') ){
237 if (strcmp(prevWord, "end") == 0)
238 lev = 0;
239 else
240 lev = 1;
241 } else if (strcmp(s, "end") == 0 && chNextNonBlank != '='
242 || strcmp(s, "endassociate") == 0 || strcmp(s, "endblock") == 0
243 || strcmp(s, "endblockdata") == 0 || strcmp(s, "endselect") == 0
244 || strcmp(s, "enddo") == 0 || strcmp(s, "endenum") ==0
245 || strcmp(s, "endif") == 0 || strcmp(s, "endforall") == 0
246 || strcmp(s, "endfunction") == 0 || strcmp(s, "endinterface") == 0
247 || strcmp(s, "endmodule") == 0 || strcmp(s, "endprogram") == 0
248 || strcmp(s, "endsubroutine") == 0 || strcmp(s, "endtype") == 0
249 || strcmp(s, "endwhere") == 0
250 || strcmp(s, "procedure") == 0 ) { // Take care of the module procedure statement
251 lev = -1;
252 } else if (strcmp(prevWord, "end") == 0 && strcmp(s, "if") == 0){ // end if
253 lev = 0;
254 }
255 return lev;
256 }
257 // Folding the code
258 static void FoldFortranDoc(unsigned int startPos, int length, int initStyle,
259 Accessor &styler, bool isFixFormat) {
260 //
261 // bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
262 // Do not know how to fold the comment at the moment.
263 //
264 bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
265 unsigned int endPos = startPos + length;
266 int visibleChars = 0;
267 int lineCurrent = styler.GetLine(startPos);
268 int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
269 int levelCurrent = levelPrev;
270 char chNext = styler[startPos];
271 char chNextNonBlank;
272 int styleNext = styler.StyleAt(startPos);
273 int style = initStyle;
274 /***************************************/
275 int lastStart = 0;
276 char prevWord[32] = "";
277 char Label[6] = "";
278 // Variables for do label folding.
279 static int doLabels[100];
280 static int posLabel=-1;
281 /***************************************/
282 for (unsigned int i = startPos; i < endPos; i++) {
283 char ch = chNext;
284 chNext = styler.SafeGetCharAt(i + 1);
285 chNextNonBlank = chNext;
286 unsigned int j=i+1;
287 while(IsABlank(chNextNonBlank) && j<endPos) {
288 j ++ ;
289 chNextNonBlank = styler.SafeGetCharAt(j);
290 }
291 int stylePrev = style;
292 style = styleNext;
293 styleNext = styler.StyleAt(i + 1);
294 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
295 //
296 if (stylePrev == SCE_F_DEFAULT && (style == SCE_F_WORD || style == SCE_F_LABEL)) {
297 // Store last word and label start point.
298 lastStart = i;
299 }
300 /***************************************/
301 if (style == SCE_F_WORD) {
302 if(iswordchar(ch) && !iswordchar(chNext)) {
303 char s[32];
304 unsigned int k;
305 for(k=0; (k<31 ) && (k<i-lastStart+1 ); k++) {
306 s[k] = static_cast<char>(tolower(styler[lastStart+k]));
307 }
308 s[k] = '\0';
309 // Handle the forall and where statement and structure.
310 if (strcmp(s, "forall") == 0 || strcmp(s, "where") == 0) {
311 if (strcmp(prevWord, "end") != 0) {
312 j = i + 1;
313 char chBrace = '(', chSeek = ')', ch1 = styler.SafeGetCharAt(j);
314 // Find the position of the first (
315 while (ch1 != chBrace && j<endPos) {
316 j++;
317 ch1 = styler.SafeGetCharAt(j);
318 }
319 char styBrace = styler.StyleAt(j);
320 int depth = 1;
321 char chAtPos;
322 char styAtPos;
323 while (j<endPos) {
324 j++;
325 chAtPos = styler.SafeGetCharAt(j);
326 styAtPos = styler.StyleAt(j);
327 if (styAtPos == styBrace) {
328 if (chAtPos == chBrace) depth++;
329 if (chAtPos == chSeek) depth--;
330 if (depth == 0) break;
331 }
332 }
333 while (j<endPos) {
334 j++;
335 chAtPos = styler.SafeGetCharAt(j);
336 styAtPos = styler.StyleAt(j);
337 if (styAtPos == SCE_F_COMMENT || IsABlank(chAtPos)) continue;
338 if (isFixFormat) {
339 if (!IsALineEnd(chAtPos)) {
340 break;
341 } else {
342 if (lineCurrent < styler.GetLine(styler.Length()-1)) {
343 j = styler.LineStart(lineCurrent+1);
344 if (styler.StyleAt(j+5) == SCE_F_CONTINUATION) {
345 j += 5;
346 continue;
347 } else {
348 levelCurrent++;
349 break;
350 }
351 }
352 }
353 } else {
354 if (chAtPos == '&' && styler.StyleAt(j) == SCE_F_CONTINUATION) {
355 j = GetContinuedPos(j+1, styler);
356 continue;
357 } else if (IsALineEnd(chAtPos)) {
358 levelCurrent ++;
359 break;
360 } else {
361 break;
362 }
363 }
364 }
365 }
366 } else {
367 levelCurrent += classifyFoldPointFortran(s, prevWord, chNextNonBlank);
368 // Store the do Labels into array
369 if (strcmp(s, "do") == 0 && IsADigit(chNextNonBlank)) {
370 unsigned int k = 0;
371 for (i=j; (i<j+5 && i<endPos); i++) {
372 ch = styler.SafeGetCharAt(i);
373 if (IsADigit(ch))
374 Label[k++] = ch;
375 else
376 break;
377 }
378 Label[k] = '\0';
379 posLabel ++;
380 doLabels[posLabel] = atoi(Label);
381 }
382 }
383 strcpy(prevWord, s);
384 }
385 } else if (style == SCE_F_LABEL) {
386 if(IsADigit(ch) && !IsADigit(chNext)) {
387 for(j = 0; ( j < 5 ) && ( j < i-lastStart+1 ); j++) {
388 ch = styler.SafeGetCharAt(lastStart + j);
389 if (IsADigit(ch) && styler.StyleAt(lastStart+j) == SCE_F_LABEL)
390 Label[j] = ch;
391 else
392 break;
393 }
394 Label[j] = '\0';
395 while (doLabels[posLabel] == atoi(Label) && posLabel > -1) {
396 levelCurrent--;
397 posLabel--;
398 }
399 }
400 }
401 if (atEOL) {
402 int lev = levelPrev;
403 if (visibleChars == 0 && foldCompact)
404 lev |= SC_FOLDLEVELWHITEFLAG;
405 if ((levelCurrent > levelPrev) && (visibleChars > 0))
406 lev |= SC_FOLDLEVELHEADERFLAG;
407 if (lev != styler.LevelAt(lineCurrent)) {
408 styler.SetLevel(lineCurrent, lev);
409 }
410 lineCurrent++;
411 levelPrev = levelCurrent;
412 visibleChars = 0;
413 strcpy(prevWord, "");
414 }
415 /***************************************/
416 if (!isspacechar(ch)) visibleChars++;
417 }
418 /***************************************/
419 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
420 int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
421 styler.SetLevel(lineCurrent, levelPrev | flagsNext);
422 }
423 /***************************************/
424 static const char * const FortranWordLists[] = {
425 "Primary keywords and identifiers",
426 "Intrinsic functions",
427 "Extended and user defined functions",
428 0,
429 };
430 /***************************************/
431 static void ColouriseFortranDocFreeFormat(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
432 Accessor &styler) {
433 ColouriseFortranDoc(startPos, length, initStyle, keywordlists, styler, false);
434 }
435 /***************************************/
436 static void ColouriseFortranDocFixFormat(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
437 Accessor &styler) {
438 ColouriseFortranDoc(startPos, length, initStyle, keywordlists, styler, true);
439 }
440 /***************************************/
441 static void FoldFortranDocFreeFormat(unsigned int startPos, int length, int initStyle,
442 WordList *[], Accessor &styler) {
443 FoldFortranDoc(startPos, length, initStyle,styler, false);
444 }
445 /***************************************/
446 static void FoldFortranDocFixFormat(unsigned int startPos, int length, int initStyle,
447 WordList *[], Accessor &styler) {
448 FoldFortranDoc(startPos, length, initStyle,styler, true);
449 }
450 /***************************************/
451 LexerModule lmFortran(SCLEX_FORTRAN, ColouriseFortranDocFreeFormat, "fortran", FoldFortranDocFreeFormat, FortranWordLists);
452 LexerModule lmF77(SCLEX_F77, ColouriseFortranDocFixFormat, "f77", FoldFortranDocFixFormat, FortranWordLists);