Hi everyone,
I've made myself some folding markers for Fortran which look like this :
foldingStartMarker = '(^[\t ]*((?i:real|complex|integer)(.*)\s)*\b (?i:subroutine|program|module|function)\b(.*)$)|(^\s*\b(\w*)\s*:\s* \b(?i:do)\b|^\s*\b(?i:do)\b)|(^\s*(\b(?i:IF)\b)(.*)(\b(?i:THEN)\b))|(^\s* \b(\w*)\s*:\s*(\b(?i:IF)\b)(.*)(\b(?i:THEN)\b))'; foldingStopMarker = '^\s*\b(?i:end\sprogram|end\sfunction| end\ssubroutine|end\smodule|endif|enddo|end\sif|end\sdo)\b';
They seem to work quite allright except for the old-fashioned do-loop of Fortran 77 which reads :
do 10 i=1,10 some code here ....... 10 continue
Note that the “continue” statement which ends the loop has the label “10” which matches the loop one. I've no idea how to implement folding markers for this syntax and any help is appreciated :)
TIA
On 14/8/2006, at 11:45, guerom00 wrote:
I've made myself some folding markers for Fortran which look like this :
foldingStartMarker = '(^[\t ]*((?i:real|complex|integer)(.*)\s)*\b (?i:subroutine|program|module|function)\b(.*)$)|(^\s*\b(\w*)\s*:\s* \b(?i:do)\b|^\s*\b(?i:do)\b)|(^\s*(\b(?i:IF)\b)(.*)(\b(?i:THEN)\b))| (^\s* \b(\w*)\s*:\s*(\b(?i:IF)\b)(.*)(\b(?i:THEN)\b))'; foldingStopMarker = '^\s*\b(?i:end\sprogram|end\sfunction| end\ssubroutine|end\smodule|endif|enddo|end\sif|end\sdo)\b';
Not sure if I broke anything, but here is my swing at a simplified version:
foldingStartMarker = '(?ix)^\s*( ( (real|complex|integer) .* \s )* (subroutine|program|module|function)\b | (\w+ \s* : \s*)? (do | IF \b .+ \b THEN) \b )';
foldingStopMarker = '(?ix)^\s*( end (do|if) | end \s (do|if|function|module|program|subroutine) )\b';
I took out all the (?i:…) wrapping and put (?i) at the start, switch to extendedmode (?x) for multi-line and white-space, removed many of the word-boundary anchors (these seemed redundant when followed by \s etc., merged a few things, e.g. make the label match just option and then an alternation for do versus IF … THEN instead of having four alternations. I also switched e.g. \w* to \w+ and .* to .+ -- I think at least one match would be required.
If it works for you, we can put them in the default Fortran bundle.
I didn’t fully understand the first branch of the start pattern. You can have type + random text repeated zero or more times, and then the subroutine/program/module/function keyword?
They seem to work quite allright except for the old-fashioned do-loop of Fortran 77 which reads :
do 10 i=1,10 some code here .......
10 continue
Note that the “continue” statement which ends the loop has the label “10” which matches the loop one. I've no idea how to implement folding markers for this syntax and any help is appreciated :)
Currently it is required that fold start and stop patterns have the same indent, so I am afraid this one is not possible to match.
Allan Odgaard <throw-away-1@...> writes:
Thanks for the simplification. As you may have noticed, I am rather a newbie with RegExp... Your simplified version seems to work fine.
I didn’t fully understand the first branch of the start pattern. You can have type + random text repeated zero or more times, and then the subroutine/program/module/function keyword?
I wanted to take care of the “function” declaration where you can indicate the type of the function on the very first line like so :
real*8 function blabla() some code here end function blabla
So the start marker must not simply be '^\s*(function)\b' or so... Because I always use an asterisk in my type declaration, I made it detect that (I always use : real*4, real*8, integer*4, complex*8 for type declaration) and not the “end function” line (which should be picked by the stop marker).
Currently it is required that fold start and stop patterns have the same indent, so I am afraid this one is not possible to match.
OK. In that case, maybe exclude from the start markers the “do” line like those :
do 10 i=...
i.e. a “do” word immediatly followed by a number. It will exclude folding alltogether for those loops but at least, in the whole program, there should be the same number of start and stop markers so that it is possible to fold the whole program.
On 15/8/2006, at 12:55, guerom00 wrote:
I didn’t fully understand the first branch of the start pattern. You can have type + random text repeated zero or more times, and then the subroutine/program/module/function keyword?
I wanted to take care of the “function” declaration where you can indicate the type of the function on the very first line like so :
real*8 function blabla() some code here end function blabla
So the start marker must not simply be '^\s*(function)\b' or so... Because I always use an asterisk in my type declaration, I made it detect that (I always use : real*4, real*8, integer*4, complex*8 for type declaration) and not the “end function” line (which should be picked by the stop marker).
Okay, the actual rule matches multiple types in front of function, like:
real*8 complex*4 function blabla()
So that rule could also be simplified slightly.