florianschanda / miss_hit

MATLAB Independent, Small & Safe, High Integrity Tools - code formatter and more
GNU General Public License v3.0
158 stars 21 forks source link

Error in MATLAB's datetime class #238

Closed PeterTillema closed 2 years ago

PeterTillema commented 2 years ago

MISS_HIT Component affected Please choose one from:

Your MATLAB/Octave environment

Your operating system and Python version

Describe the bug A clear and concise description of what the bug is. Please include source code or clear steps that reproduces the problem. We cannot fix the problem if we can't replicate it. Please also indicate what you think the bug is unless its entirely obvious. If there is tool output (e.g. a backtrace or ICE) please include that here as well.

Open the datetime class in MATLAB (via Ctrl+D) and throw it at MS_LINT. You will get the following error:

In datetime.m, line 1614
|     methods(Hidden = true, Static = true)
|     ^^^^^^^ error: expected properties|methods|events|enumeration inside classdef
MISS_HIT Lint Summary: 1 file(s) analysed, 1 error(s)

The problem is that a few lines above (1571), end is used as a function name, which probably messes up the parser. Changing that to another name fixes the problem.

Do NOT include screenshots.

florianschanda commented 2 years ago

Without checking, I suspect your theory of end messing up the parser is correct. I'll have a look as soon as I can.

florianschanda commented 2 years ago

OK, I can reproduce this with the datetime class from 2020b. I have reduced it down to this:

classdef Potato

    methods

        function n = end(this, k, n)
            n = n + k;
        end

    end

    methods

    end

end

Looks like your theory is right. I thought I had that working and supported, but maaaaybe not. Thanks for the find!

florianschanda commented 2 years ago

So this is going to be hideous to fix, but I'll try. The problem is that end needs super special treatment, and the lexer tries to classify it as a KEYWORD or IDENTIFIER based on the limited awareness of context that it has. In this case it gets the second methods wrong and thinks it's an identifier; because I think it associates the end from the function to end the first methods; and the second end ending the class. That means now methods is a normal identifier again (it's only a keywords inside classes).

florianschanda commented 2 years ago

OK, so my description above is half right, half wrong. End is always classified as a keyword; but the rest is true: it ends the special blocks (e.g. methods) no matter where it appears.

I am testing a fix now.

florianschanda commented 2 years ago

So I managed to fix it with a one-liner. I am not happy with the general state of affairs, as MATLAB is quite frankly a silly language that cannot be lexer in a sane (dragon book style) way.

I will make a new release with just this bugfix; thank you very much for the report <3

PeterTillema commented 2 years ago

Thank you very much for this quicj fox! I'm glad it wasn't too hard to fix, could have been worse.

And thanks for this tool in general! I discovered it only a few days ago, but so far it is amazing! It works like a charm, is super fast and overall clean. Comparing it to something like ANTLRv4, fabulous!

florianschanda commented 2 years ago

Thank you for the kind words. It makes me very happy to hear this :)