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