Getting started¶
Install the module with:
pip install textmate-grammar-python
Or, for development purposes, clone the repository and install locally with poetry, and setup pre-commit such that code is linted and formatted with Ruff and checked with mypy.
pip install poetry
git clone https://github.com/watermarkhu/textmate-grammar-python
cd textmate-grammar-python
poetry install
pre-commit install
For instructions on running the unit and regression tests see CONTRIBUTING.md
Usage¶
Before tokenization is possible, a LanguageParser
needs to be initialized using a loaded grammar.
from textmate_grammar.language import LanguageParser
from textmate_grammar.grammars import matlab
parser = LanguageParser(matlab.GRAMMAR)
After this, one can either choose to call parser.parsing_string
to parse a input string directly, or call parser.parse_file
with the path to the appropiate source file as the first argument, such as in the example example.py
.
The parsed element
object can be displayed directly by calling the print
method. By default the element is printed as an element tree in a dictionary format.
>>> element = parser.parse_string("value = num2str(10);")
>>> element.print()
{'token': 'source.matlab',
'children': [{'token': 'meta.assignment.variable.single.matlab',
'children': [{'token': 'variable.other.readwrite.matlab', 'content': 'value'}]},
{'token': 'keyword.operator.assignment.matlab', 'content': '='},
{'token': 'meta.function-call.parens.matlab',
'begin': [{'token': 'entity.name.function.matlab', 'content': 'num2str'},
{'token': 'punctuation.section.parens.begin.matlab', 'content': '('}],
'end': [{'token': 'punctuation.section.parens.end.matlab', 'content': ')'}],
'children': [{'token': 'constant.numeric.decimal.matlab', 'content': '10'}]},
{'token': 'punctuation.terminator.semicolon.matlab', 'content': ';'}]}