1 // Scintilla source code edit control
2 /** @file LexFortran.cxx
4 ** Writen by Chuan-jian Shen, Last changed Sep. 2003
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 /***************************************/
14 /***************************************/
18 #include "StyleContext.h"
20 #include "Scintilla.h"
24 using namespace Scintilla
;
27 /***********************************************/
28 static inline bool IsAWordChar(const int ch
) {
29 return (ch
< 0x80) && (isalnum(ch
) || ch
== '_' || ch
== '%');
31 /**********************************************/
32 static inline bool IsAWordStart(const int ch
) {
33 return (ch
< 0x80) && (isalnum(ch
));
35 /***************************************/
36 inline bool IsABlank(unsigned int ch
) {
37 return (ch
== ' ') || (ch
== 0x09) || (ch
== 0x0b) ;
39 /***************************************/
40 inline bool IsALineEnd(char ch
) {
41 return ((ch
== '\n') || (ch
== '\r')) ;
43 /***************************************/
44 unsigned int GetContinuedPos(unsigned int pos
, Accessor
&styler
) {
45 while (!IsALineEnd(styler
.SafeGetCharAt(pos
++))) continue;
46 if (styler
.SafeGetCharAt(pos
) == '\n') pos
++;
47 while (IsABlank(styler
.SafeGetCharAt(pos
++))) continue;
48 char chCur
= styler
.SafeGetCharAt(pos
);
50 while (IsABlank(styler
.SafeGetCharAt(++pos
))) continue;
56 /***************************************/
57 static void ColouriseFortranDoc(unsigned int startPos
, int length
, int initStyle
,
58 WordList
*keywordlists
[], Accessor
&styler
, bool isFixFormat
) {
59 WordList
&keywords
= *keywordlists
[0];
60 WordList
&keywords2
= *keywordlists
[1];
61 WordList
&keywords3
= *keywordlists
[2];
62 /***************************************/
63 int posLineStart
= 0, numNonBlank
= 0, prevState
= 0;
64 int endPos
= startPos
+ length
;
65 /***************************************/
66 // backtrack to the nearest keyword
67 while ((startPos
> 1) && (styler
.StyleAt(startPos
) != SCE_F_WORD
)) {
70 startPos
= styler
.LineStart(styler
.GetLine(startPos
));
71 initStyle
= styler
.StyleAt(startPos
- 1);
72 StyleContext
sc(startPos
, endPos
-startPos
, initStyle
, styler
);
73 /***************************************/
74 for (; sc
.More(); sc
.Forward()) {
75 // remember the start position of the line
77 posLineStart
= sc
.currentPos
;
79 sc
.SetState(SCE_F_DEFAULT
);
81 if (!IsASpaceOrTab(sc
.ch
)) numNonBlank
++;
82 /***********************************************/
83 // Handle the fix format generically
84 int toLineStart
= sc
.currentPos
- posLineStart
;
85 if (isFixFormat
&& (toLineStart
< 6 || toLineStart
> 72)) {
86 if (toLineStart
== 0 && (tolower(sc
.ch
) == 'c' || sc
.ch
== '*') || sc
.ch
== '!') {
87 sc
.SetState(SCE_F_COMMENT
);
88 while (!sc
.atLineEnd
&& sc
.More()) sc
.Forward(); // Until line end
89 } else if (toLineStart
> 72) {
90 sc
.SetState(SCE_F_COMMENT
);
91 while (!sc
.atLineEnd
&& sc
.More()) sc
.Forward(); // Until line end
92 } else if (toLineStart
< 5) {
94 sc
.SetState(SCE_F_LABEL
);
96 sc
.SetState(SCE_F_DEFAULT
);
97 } else if (toLineStart
== 5) {
98 if (!IsASpace(sc
.ch
) && sc
.ch
!= '0') {
99 sc
.SetState(SCE_F_CONTINUATION
);
100 sc
.ForwardSetState(prevState
);
102 sc
.SetState(SCE_F_DEFAULT
);
106 /***************************************/
107 // Handle line continuation generically.
108 if (!isFixFormat
&& sc
.ch
== '&') {
111 while (IsABlank(chTemp
) && j
<132) {
112 chTemp
= static_cast<char>(sc
.GetRelative(j
));
116 sc
.SetState(SCE_F_CONTINUATION
);
117 if (sc
.chNext
== '!') sc
.ForwardSetState(SCE_F_COMMENT
);
118 } else if (chTemp
== '\r' || chTemp
== '\n') {
119 int currentState
= sc
.state
;
120 sc
.SetState(SCE_F_CONTINUATION
);
121 sc
.ForwardSetState(SCE_F_DEFAULT
);
122 while (IsASpace(sc
.ch
) && sc
.More()) sc
.Forward();
124 sc
.SetState(SCE_F_CONTINUATION
);
127 sc
.SetState(currentState
);
130 /***************************************/
131 // Determine if the current state should terminate.
132 if (sc
.state
== SCE_F_OPERATOR
) {
133 sc
.SetState(SCE_F_DEFAULT
);
134 } else if (sc
.state
== SCE_F_NUMBER
) {
135 if (!(IsAWordChar(sc
.ch
) || sc
.ch
=='\'' || sc
.ch
=='\"' || sc
.ch
=='.')) {
136 sc
.SetState(SCE_F_DEFAULT
);
138 } else if (sc
.state
== SCE_F_IDENTIFIER
) {
139 if (!IsAWordChar(sc
.ch
) || (sc
.ch
== '%')) {
141 sc
.GetCurrentLowered(s
, sizeof(s
));
142 if (keywords
.InList(s
)) {
143 sc
.ChangeState(SCE_F_WORD
);
144 } else if (keywords2
.InList(s
)) {
145 sc
.ChangeState(SCE_F_WORD2
);
146 } else if (keywords3
.InList(s
)) {
147 sc
.ChangeState(SCE_F_WORD3
);
149 sc
.SetState(SCE_F_DEFAULT
);
151 } else if (sc
.state
== SCE_F_COMMENT
|| sc
.state
== SCE_F_PREPROCESSOR
) {
152 if (sc
.ch
== '\r' || sc
.ch
== '\n') {
153 sc
.SetState(SCE_F_DEFAULT
);
155 } else if (sc
.state
== SCE_F_STRING1
) {
156 prevState
= sc
.state
;
158 if (sc
.chNext
== '\'') {
161 sc
.ForwardSetState(SCE_F_DEFAULT
);
162 prevState
= SCE_F_DEFAULT
;
164 } else if (sc
.atLineEnd
) {
165 sc
.ChangeState(SCE_F_STRINGEOL
);
166 sc
.ForwardSetState(SCE_F_DEFAULT
);
168 } else if (sc
.state
== SCE_F_STRING2
) {
169 prevState
= sc
.state
;
171 sc
.ChangeState(SCE_F_STRINGEOL
);
172 sc
.ForwardSetState(SCE_F_DEFAULT
);
173 } else if (sc
.ch
== '\"') {
174 if (sc
.chNext
== '\"') {
177 sc
.ForwardSetState(SCE_F_DEFAULT
);
178 prevState
= SCE_F_DEFAULT
;
181 } else if (sc
.state
== SCE_F_OPERATOR2
) {
183 sc
.ForwardSetState(SCE_F_DEFAULT
);
185 } else if (sc
.state
== SCE_F_CONTINUATION
) {
186 sc
.SetState(SCE_F_DEFAULT
);
187 } else if (sc
.state
== SCE_F_LABEL
) {
188 if (!IsADigit(sc
.ch
)) {
189 sc
.SetState(SCE_F_DEFAULT
);
191 if (isFixFormat
&& sc
.currentPos
-posLineStart
> 4)
192 sc
.SetState(SCE_F_DEFAULT
);
193 else if (numNonBlank
> 5)
194 sc
.SetState(SCE_F_DEFAULT
);
197 /***************************************/
198 // Determine if a new state should be entered.
199 if (sc
.state
== SCE_F_DEFAULT
) {
201 if (sc
.chNext
== '$') {
202 sc
.SetState(SCE_F_PREPROCESSOR
);
204 sc
.SetState(SCE_F_COMMENT
);
206 } else if ((!isFixFormat
) && IsADigit(sc
.ch
) && numNonBlank
== 1) {
207 sc
.SetState(SCE_F_LABEL
);
208 } else if (IsADigit(sc
.ch
) || (sc
.ch
== '.' && IsADigit(sc
.chNext
))) {
209 sc
.SetState(SCE_F_NUMBER
);
210 } else if ((tolower(sc
.ch
) == 'b' || tolower(sc
.ch
) == 'o' ||
211 tolower(sc
.ch
) == 'z') && (sc
.chNext
== '\"' || sc
.chNext
== '\'')) {
212 sc
.SetState(SCE_F_NUMBER
);
214 } else if (sc
.ch
== '.' && isalpha(sc
.chNext
)) {
215 sc
.SetState(SCE_F_OPERATOR2
);
216 } else if (IsAWordStart(sc
.ch
)) {
217 sc
.SetState(SCE_F_IDENTIFIER
);
218 } else if (sc
.ch
== '\"') {
219 sc
.SetState(SCE_F_STRING2
);
220 } else if (sc
.ch
== '\'') {
221 sc
.SetState(SCE_F_STRING1
);
222 } else if (isoperator(static_cast<char>(sc
.ch
))) {
223 sc
.SetState(SCE_F_OPERATOR
);
229 /***************************************/
230 // To determine the folding level depending on keywords
231 static int classifyFoldPointFortran(const char* s
, const char* prevWord
, const char chNextNonBlank
) {
233 if ((strcmp(prevWord
, "else") == 0 && strcmp(s
, "if") == 0) || strcmp(s
, "elseif") == 0)
235 if (strcmp(s
, "associate") == 0 || strcmp(s
, "block") == 0
236 || strcmp(s
, "blockdata") == 0 || strcmp(s
, "select") == 0
237 || strcmp(s
, "do") == 0 || strcmp(s
, "enum") ==0
238 || strcmp(s
, "function") == 0 || strcmp(s
, "interface") == 0
239 || strcmp(s
, "module") == 0 || strcmp(s
, "program") == 0
240 || strcmp(s
, "subroutine") == 0 || strcmp(s
, "then") == 0
241 || (strcmp(s
, "type") == 0 && chNextNonBlank
!= '(') ){
242 if (strcmp(prevWord
, "end") == 0)
246 } else if (strcmp(s
, "end") == 0 && chNextNonBlank
!= '='
247 || strcmp(s
, "endassociate") == 0 || strcmp(s
, "endblock") == 0
248 || strcmp(s
, "endblockdata") == 0 || strcmp(s
, "endselect") == 0
249 || strcmp(s
, "enddo") == 0 || strcmp(s
, "endenum") ==0
250 || strcmp(s
, "endif") == 0 || strcmp(s
, "endforall") == 0
251 || strcmp(s
, "endfunction") == 0 || strcmp(s
, "endinterface") == 0
252 || strcmp(s
, "endmodule") == 0 || strcmp(s
, "endprogram") == 0
253 || strcmp(s
, "endsubroutine") == 0 || strcmp(s
, "endtype") == 0
254 || strcmp(s
, "endwhere") == 0
255 || strcmp(s
, "procedure") == 0 ) { // Take care of the module procedure statement
257 } else if (strcmp(prevWord
, "end") == 0 && strcmp(s
, "if") == 0){ // end if
263 static void FoldFortranDoc(unsigned int startPos
, int length
, int initStyle
,
264 Accessor
&styler
, bool isFixFormat
) {
266 // bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
267 // Do not know how to fold the comment at the moment.
269 bool foldCompact
= styler
.GetPropertyInt("fold.compact", 1) != 0;
270 unsigned int endPos
= startPos
+ length
;
271 int visibleChars
= 0;
272 int lineCurrent
= styler
.GetLine(startPos
);
273 int levelPrev
= styler
.LevelAt(lineCurrent
) & SC_FOLDLEVELNUMBERMASK
;
274 int levelCurrent
= levelPrev
;
275 char chNext
= styler
[startPos
];
277 int styleNext
= styler
.StyleAt(startPos
);
278 int style
= initStyle
;
279 /***************************************/
281 char prevWord
[32] = "";
283 // Variables for do label folding.
284 static int doLabels
[100];
285 static int posLabel
=-1;
286 /***************************************/
287 for (unsigned int i
= startPos
; i
< endPos
; i
++) {
289 chNext
= styler
.SafeGetCharAt(i
+ 1);
290 chNextNonBlank
= chNext
;
292 while(IsABlank(chNextNonBlank
) && j
<endPos
) {
294 chNextNonBlank
= styler
.SafeGetCharAt(j
);
296 int stylePrev
= style
;
298 styleNext
= styler
.StyleAt(i
+ 1);
299 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
301 if (stylePrev
== SCE_F_DEFAULT
&& (style
== SCE_F_WORD
|| style
== SCE_F_LABEL
)) {
302 // Store last word and label start point.
305 /***************************************/
306 if (style
== SCE_F_WORD
) {
307 if(iswordchar(ch
) && !iswordchar(chNext
)) {
310 for(k
=0; (k
<31 ) && (k
<i
-lastStart
+1 ); k
++) {
311 s
[k
] = static_cast<char>(tolower(styler
[lastStart
+k
]));
314 // Handle the forall and where statement and structure.
315 if (strcmp(s
, "forall") == 0 || strcmp(s
, "where") == 0) {
316 if (strcmp(prevWord
, "end") != 0) {
318 char chBrace
= '(', chSeek
= ')', ch1
= styler
.SafeGetCharAt(j
);
319 // Find the position of the first (
320 while (ch1
!= chBrace
&& j
<endPos
) {
322 ch1
= styler
.SafeGetCharAt(j
);
324 char styBrace
= styler
.StyleAt(j
);
330 chAtPos
= styler
.SafeGetCharAt(j
);
331 styAtPos
= styler
.StyleAt(j
);
332 if (styAtPos
== styBrace
) {
333 if (chAtPos
== chBrace
) depth
++;
334 if (chAtPos
== chSeek
) depth
--;
335 if (depth
== 0) break;
340 chAtPos
= styler
.SafeGetCharAt(j
);
341 styAtPos
= styler
.StyleAt(j
);
342 if (styAtPos
== SCE_F_COMMENT
|| IsABlank(chAtPos
)) continue;
344 if (!IsALineEnd(chAtPos
)) {
347 if (lineCurrent
< styler
.GetLine(styler
.Length()-1)) {
348 j
= styler
.LineStart(lineCurrent
+1);
349 if (styler
.StyleAt(j
+5) == SCE_F_CONTINUATION
) {
359 if (chAtPos
== '&' && styler
.StyleAt(j
) == SCE_F_CONTINUATION
) {
360 j
= GetContinuedPos(j
+1, styler
);
362 } else if (IsALineEnd(chAtPos
)) {
372 levelCurrent
+= classifyFoldPointFortran(s
, prevWord
, chNextNonBlank
);
373 // Store the do Labels into array
374 if (strcmp(s
, "do") == 0 && IsADigit(chNextNonBlank
)) {
376 for (i
=j
; (i
<j
+5 && i
<endPos
); i
++) {
377 ch
= styler
.SafeGetCharAt(i
);
385 doLabels
[posLabel
] = atoi(Label
);
390 } else if (style
== SCE_F_LABEL
) {
391 if(IsADigit(ch
) && !IsADigit(chNext
)) {
392 for(j
= 0; ( j
< 5 ) && ( j
< i
-lastStart
+1 ); j
++) {
393 ch
= styler
.SafeGetCharAt(lastStart
+ j
);
394 if (IsADigit(ch
) && styler
.StyleAt(lastStart
+j
) == SCE_F_LABEL
)
400 while (doLabels
[posLabel
] == atoi(Label
) && posLabel
> -1) {
408 if (visibleChars
== 0 && foldCompact
)
409 lev
|= SC_FOLDLEVELWHITEFLAG
;
410 if ((levelCurrent
> levelPrev
) && (visibleChars
> 0))
411 lev
|= SC_FOLDLEVELHEADERFLAG
;
412 if (lev
!= styler
.LevelAt(lineCurrent
)) {
413 styler
.SetLevel(lineCurrent
, lev
);
416 levelPrev
= levelCurrent
;
418 strcpy(prevWord
, "");
420 /***************************************/
421 if (!isspacechar(ch
)) visibleChars
++;
423 /***************************************/
424 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
425 int flagsNext
= styler
.LevelAt(lineCurrent
) & ~SC_FOLDLEVELNUMBERMASK
;
426 styler
.SetLevel(lineCurrent
, levelPrev
| flagsNext
);
428 /***************************************/
429 static const char * const FortranWordLists
[] = {
430 "Primary keywords and identifiers",
431 "Intrinsic functions",
432 "Extended and user defined functions",
435 /***************************************/
436 static void ColouriseFortranDocFreeFormat(unsigned int startPos
, int length
, int initStyle
, WordList
*keywordlists
[],
438 ColouriseFortranDoc(startPos
, length
, initStyle
, keywordlists
, styler
, false);
440 /***************************************/
441 static void ColouriseFortranDocFixFormat(unsigned int startPos
, int length
, int initStyle
, WordList
*keywordlists
[],
443 ColouriseFortranDoc(startPos
, length
, initStyle
, keywordlists
, styler
, true);
445 /***************************************/
446 static void FoldFortranDocFreeFormat(unsigned int startPos
, int length
, int initStyle
,
447 WordList
*[], Accessor
&styler
) {
448 FoldFortranDoc(startPos
, length
, initStyle
,styler
, false);
450 /***************************************/
451 static void FoldFortranDocFixFormat(unsigned int startPos
, int length
, int initStyle
,
452 WordList
*[], Accessor
&styler
) {
453 FoldFortranDoc(startPos
, length
, initStyle
,styler
, true);
455 /***************************************/
456 LexerModule
lmFortran(SCLEX_FORTRAN
, ColouriseFortranDocFreeFormat
, "fortran", FoldFortranDocFreeFormat
, FortranWordLists
);
457 LexerModule
lmF77(SCLEX_F77
, ColouriseFortranDocFixFormat
, "f77", FoldFortranDocFixFormat
, FortranWordLists
);