[changeset] parsing nested functions

David Grundberg individ at acc.umu.se
Thu Jul 30 04:56:37 CDT 2009


John W. Eaton skrev:
> On 29-Jul-2009, David Grundberg wrote:
>
> | Hi all,
> | 
> | I've modified the parser to be able to properly parse nested functions. 
> | So now a function body can contain nested functions anywhere, and not 
> | just in the beginning. The nested function still work like subfunctions 
> | to the primary function though, regardless where they are declared (or 
> | in which depth). This changeset adds a warning message if any nested 
> | function has been declared. Since they work as subfunctions, I thought 
> | it reasonable.
> | 
> | An error is raised if the functionends are incorrectly balanced. If one 
> | function is explicitly ended, so must all the others.
> | 
> | One mayor syntax change is that eval_string will require all function to 
> | be explicitly ended. I removed the condition in the action of 
> | function_end that allowed implicit ends (at END_OF_INPUT). Since Matlab 
> | do not allow function declarations in eval strings, I think Octave could 
> | be stricter.
> | 
> | The parser now have a special way of parsing function files, and 
> | statements are not allowed outside first-level functions.
> | 
> | As I worked on this, I removed some intelligence from the lexer and 
> | moved them into the parser. I also removed the parent_scope property 
> | from the symbol table, since I couldn't find it used anywhere else than 
> | in the parser.
>
> Thanks for looking at this.  I'd like to apply it, but before I do,
> can you look at the following problem?
>
> If I have a file containing
>
>   # This is a doc string
>
>   function fcn ()
>     if (nargin == 0)
>       sub ();
>     else
>       print_usage ();
>     endif
>
>   function sub ()
>     printf ("foobar:sub!\n");
>
> and call it with
>
>   fcn ()
>
> it works, but if I call it with
>
>   fcn (1)
>
> I see
>
>   error: print_usage: `fcn' is not documented
>
> Instead of this message, I expect to get the usual print_usage help
> text and the doc string for fcn.  If I change the function file to be
>
>
>   # This is a doc string
>
>   function fcn ()
>     if (nargin == 0)
>       sub ();
>     else
>       print_usage ();
>     endif
>   end
>
>   function sub ()
>     printf ("foobar:sub!\n");
>   end
>
> then it works as expected, but I think these two cases should be
> equivalent (and need to be, for compatibility with Matlab).
>
> I haven't tried to debug this yet, but it seems that the doc string is
> somehow not attached to the main function when the structure is
> rearranged when no endfunction tokens are found.  Can you take a look
> and see if this is easy to fix?
>
> Thanks,
>
> jwe
>   
The current diff is getting quite large so I'd rather work on the help 
bug in a separate changeset. All code involving help_buf was 
malfunctioning, and I think the changes needed would be rather involved.

I modified my previous changeset to make it store help text for the 
primary function. The examples you provided then works identically and 
the tests in strfind also passes. I also added a shameless copyright 
statement in parse.y. The new changeset is attached to this mail.

Regards,
David
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: nested2.changeset
Url: https://www-old.cae.wisc.edu/pipermail/octave-maintainers/attachments/20090730/bd56adbf/attachment-0001.ksh 


More information about the Octave-maintainers mailing list