watermarkhu / textmate-grammar-python

Python lexer and tokenizer based on textmate grammars
https://textmate-grammar-python.readthedocs.io
MIT License
10 stars 2 forks source link

Class definition attributes not parsed correctly #67

Open joeced opened 4 months ago

joeced commented 4 months ago

I started working with textmate-grammar-python for the https://github.com/sphinx-contrib/matlabdomain package. Thanks for starting this work - it's much more structured than the pygments tokens.

Given this MATLAB class definition

classdef (AllowedSubclasses = {?SubClass1,?SubClass2}) SuperClass
end

is parsed to these tokens with textmate-grammar-python==0.6.0:

{'token': 'source.matlab',
 'children': [{'token': 'meta.class.matlab',
               'begin': [{'token': 'storage.type.class.matlab', 'content': 'classdef'}],
               'end': [{'token': 'storage.type.class.end.matlab', 'content': 'end'}],
               'children': [{'token': 'meta.class.declaration.matlab',
                             'children': [{'token': 'punctuation.section.parens.begin.matlab', 'content': '('},
                                          {'token': 'storage.modifier.class.matlab', 'content': 'AllowedSubclasses'},    
                                          {'token': 'keyword.operator.assignment.matlab', 'content': '='},
                                          {'token': 'punctuation.separator.modifier.comma.matlab', 'content': ','},      
                                          {'token': 'storage.modifier.class.matlab', 'content': 'SubClass2'},
                                          {'token': 'punctuation.section.parens.end.matlab', 'content': ')'},
                                          {'token': 'entity.name.type.class.matlab', 'content': 'SuperClass'}]}]}]}      

As one can see, we are missing:

watermarkhu commented 4 months ago

Hi @joeced @apozharski

Good to see that this is being picked up.

Regarding the output of textmate grammars, there are two issues:

Anyhow, this will still be an ongoing project of mine. Let's keep in touch.

apozharski commented 4 months ago

I have opened a PR against MATLAB-Language-grammar that (mostly, within the limitations of textmate grammars) addresses this issue: https://github.com/mathworks/MATLAB-Language-grammar/pull/88

Your example A slightly more complex example:

classdef (AllowedSubclasses = {?SubClass1,?SubClass2}, Hidden=true) SuperClass < foo ...
    & bar
end

would now be parsed as:

{'token': 'meta.class.declaration.matlab',
 'children': [{'token': 'storage.modifier.section.class.matlab',
               'children': [{'token': 'storage.modifier.class.matlab', 'content': 'AllowedSubclasses'},
                            {'token': 'keyword.operator.assignment.matlab', 'content': '='},
                            {'token': 'meta.cell.literal.matlab',
                             'begin': [{'token': 'punctuation.section.braces.begin.matlab', 'content': '{'}],
                             'end': [{'token': 'punctuation.section.braces.end.matlab', 'content': '}'}],
                             'children': [{'token': 'keyword.operator.other.question.matlab', 'content': '?'},
                                          {'token': 'variable.other.readwrite.matlab', 'content': 'SubClass1'},
                                          {'token': 'punctuation.separator.comma.matlab', 'content': ','},
                                          {'token': 'keyword.operator.other.question.matlab', 'content': '?'},
                                          {'token': 'variable.other.readwrite.matlab', 'content': 'SubClass2'}]},
                            {'token': 'punctuation.separator.modifier.comma.matlab', 'content': ','},
                            {'token': 'storage.modifier.class.matlab', 'content': 'Hidden'},
                            {'token': 'keyword.operator.assignment.matlab', 'content': '='},
                            {'token': 'constant.language.boolean.matlab', 'content': 'true'}]},
              {'token': 'entity.name.type.class.matlab', 'content': 'SuperClass'},
              {'token': 'punctuation.separator.lt.inheritance.matlab', 'content': '<'},
              {'token': 'meta.inherited-class.matlab', 'children': [{'token': 'entity.other.inherited-class.matlab', 'content': 'foo'}]},
              {'token': 'meta.continuation.line.matlab', 'children': [{'token': 'punctuation.separator.continuation.line.matlab', 'content': '...'}]},
              {'token': 'keyword.operator.type.matlab', 'content': '&'},
              {'token': 'meta.inherited-class.matlab', 'children': [{'token': 'entity.other.inherited-class.matlab', 'content': 'bar'}]}]}