sphinx-contrib / matlabdomain

A Sphinx extension for documenting Matlab code
http://sphinxcontrib-matlabdomain.readthedocs.io/
Other
70 stars 45 forks source link

Wrong function name parsed when method escapes first argument with ~ #90

Closed PekaryGergelyR closed 5 years ago

PekaryGergelyR commented 5 years ago

The following class:

classdef example < handle
    methods
        function output = someMethod1(obj, argument)
            % Docstring
        end
        function output = someMethod2(~, argument)
            % Docstring
        end
    end
end

Will be tokenized as:

[(Token.Keyword, 'classdef')
 (Token.Text, ' ')
 (Token.Name, 'example')
 (Token.Text, ' ')
 (Token.Operator, '<')
 (Token.Text, ' ')
 (Token.Name, 'handle')
 (Token.Text, '\n')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Keyword, 'methods')
 (Token.Text, '\n')
 (Token.Keyword, '        function')
 (Token.Text.Whitespace, ' ')
 (Token.Text, 'output ')
 (Token.Punctuation, '=')
 (Token.Text.Whitespace, ' ')
 (Token.Name.Function, 'someMethod1')   <---------------- Good method name
 (Token.Punctuation, '(')
 (Token.Text, 'obj, argument')
 (Token.Punctuation, ')')
 (Token.Text.Whitespace, '\n            ')
 (Token.Comment, '% Docstring')
 (Token.Text, '\n')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Keyword, 'end')
 (Token.Text, '\n')
 (Token.Keyword, '        function')
 (Token.Text.Whitespace, ' ')
 (Token.Text, 'output ')
 (Token.Punctuation, '=')
 (Token.Text.Whitespace, ' ')
 (Token.Name.Function, 'someMethod2()') <---------------- Bad method name with ()
 (Token.Punctuation, '(')
 (Token.Text, '~, argument')
 (Token.Punctuation, ')')
 (Token.Text.Whitespace, '\n            ')
 (Token.Comment, '% Docstring')
 (Token.Text, '\n')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Keyword, 'end')
 (Token.Text, '\n')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Text, ' ')
 (Token.Keyword, 'end')
 (Token.Text, '\n')
 (Token.Keyword, 'end')
 (Token.Text, '\n')]

This happens to functions as well, however, this issue is not so common there probably.

I'd suggest the following changes in MatFunction class:

    def __init__(self, name, modname, tokens):
        if name is not None:
            name = re.sub('\(\)$', '', name)
        super(MatFunction, self).__init__(name)

and

# =====================================================================
            # function name
            func_name = tks.pop()
            temp_name = re.sub('\(\)$', '', func_name[1])
            if (Token.Name.Function, temp_name) != (Token.Name.Function, self.name):  # @UndefinedVariable
                if isinstance(self, MatMethod):
                    self.name = temp_name
                else:
                    msg = '[sphinxcontrib-matlabdomain] Unexpected function name: "%s".' % name
                    msg += ' Expected "{}" in module "{}".'.format(name, modname)
                    logger.warning(msg)

The first code should fix functions and the second the class methods.

joeced commented 5 years ago

Thanks for the bug-report. The detail of the reports are really appreciated!