Discussion:
Matlab related HRC Files - function
Bob Kagy
2007-05-17 03:41:32 UTC
Permalink
Matlab originally only had functions and subfunctions:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f4-70666.html
So you might have something like:
function v = myGlobalFunc( k )
v = myLocalOffset(k) + myLocalRecursion( k - 1 );
function o = myLocalOffset(k)
o = 2 * k;
function o = myLocalRecursion( k )
if k <= 1
o = k;
else
o = myLocalRecursion( k - 1 );
end
Only the first function in the file is visible outside the file. The two
myLocal* functions are only visible from within the current file. A
function ends when another line starts with the function keyword.

Sometime in the past couple of years MathWorks introduced the concept of a
nested function.
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f4-39683.html
Nested functions are different than subfunctions because there are rules
giving the nested function access to variables in the scope of the parent
function but not explicitly passed. The important bit for this discussion,
is that to enable this the MathWorks had to change the rules for
subfunctions and require a matching end for each function keyword. You
didn't have to nest, so you could just rewrite the above as:
function v = myGlobalFunc( k )
v = myLocalOffset(k) + myLocalRecursion( k - 1 );

function o = myLocalOffset(k)
o = 2 * k;
end

function o = myLocalRecursion( k )
if k <= 1
o = k;
else
o = myLocalRecursion( k - 1 );
end
end

end

(There are also anonymous functions, which are expressions of the form
@(x,y) x+y^2. I just added a bit to handle them.
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f4-70115.html
)

The problem I had came in when setting up the Outline and matching between
function & end keywords.

As I have it now, I use the relatively simple regexp.for recognizing the
function keyword. In the Outline view, both versions have the same outline:
* myGlobalFunc
* myLocalOffset
* myLocalRecursion
* if k <= 1
and in the second example there is no indication that functions are nested
from the Outline, and no matching of function to end keyword.

When I use the following block instead of a regexp:
<block
start="/^\s*((function))\s+((\[?)[^\]]+(\]?)\s*(=)\s*)?([A-Za-z]\w*)\b/"
end="/^\s*(((end)|(function)))/"
region01="def:PairStart" region02="mlKeyWord"
region11="def:PairEnd" region13="mlKeyWord"
region04="mlSymb" region05="mlSymb" region06="mlSymb"
region07="mlFunction"
scheme="matlab" inner-region="yes" />
the second example works out wonderfully. I get the function and end
keywords matched, and my Outline looks like:
* myGlobalFunc
* myLocalOffset
* myLocalRecursion
* if k <= 1
Unfortunately my first example looks like:
* myGlobalFunc
* myLocalOffset
* myLocalRecursion
* if <= 1
Every subfunction is nested in the one above, making the Outline unusable.
In large files with many subfunctions, the tree gets really deep and the
Outline seems to lock up Eclipse.

I've played around with the end regular expression a bit, but can't seem to
find a version of block that works for both the subfunction pattern and the
nested function pattern.

Bob
Date: Mon, 7 May 2007 18:51:16 +0400
Subject: Re: Matlab related HRC Files
Content-Type: text/plain; charset=UTF-8; format=flowed
Bob,
Thanks for your contribution, I'll include the changes into the
distribution. Feel free to send any updates.
As for the problem you've stated, I see a commented out block for
function matching. Why doesn't it works? Although a function without
end indication is strange, believe it still may be handled with the
logic you've commented.
I didn't get also your problem with line continuation chars, could you
explain it in more details?
--
Igor
Loading...