-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from watermarkhu/development
Fix doc
- Loading branch information
Showing
4 changed files
with
70 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,7 @@ instance/ | |
|
||
# Sphinx documentation | ||
docs/_build/ | ||
docs/apidocs/ | ||
|
||
# PyBuilder | ||
.pybuilder/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Getting started | ||
|
||
Install the module with: | ||
```bash | ||
pip install textmate-grammar-python | ||
``` | ||
|
||
Or, for development purposes, clone the repository and install locally with [poetry](https://python-poetry.org/), and setup [pre-commit](https://pre-commit.com/) such that code is linted and formatted with [Ruff](https://docs.astral.sh/ruff/) and checked with [mypy](https://mypy-lang.org/). | ||
|
||
```bash | ||
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](https://github.com/watermarkhu/textmate-grammar-python/blob/main/CONTRIBUTING.md) | ||
|
||
|
||
# Usage | ||
Before tokenization is possible, a [`LanguageParser`](#textmate_grammar.language.LanguageParser) needs to be initialized using a loaded grammar. | ||
|
||
```python | ||
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`](#textmate_grammar.language.LanguageParser.parse_string) to parse a input string directly, or call [`parser.parse_file`](#textmate_grammar.language.LanguageParser.parse_file) with the path to the appropiate source file as the first argument, such as in the example [`example.py`](../example.py). | ||
|
||
The parsed `element` object can be displayed directly by calling the [`print`](#textmate_grammar.elements.ContentElement.print) method. By default the element is printed as an element tree in a dictionary format. | ||
|
||
```python | ||
>>> 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': ';'}]} | ||
|
||
``` | ||
|