textmate-grammar-python 0.1.2__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- textmate_grammar_python-0.3.0/PKG-INFO +105 -0
- textmate_grammar_python-0.3.0/README.md +85 -0
- textmate_grammar_python-0.3.0/pyproject.toml +95 -0
- textmate_grammar_python-0.3.0/src/textmate_grammar/__init__.py +1 -0
- textmate_grammar_python-0.3.0/src/textmate_grammar/elements.py +549 -0
- textmate_grammar_python-0.3.0/src/textmate_grammar/grammars/__pycache__/__init__.cpython-312.pyc +0 -0
- textmate_grammar_python-0.3.0/src/textmate_grammar/grammars/markdown/__init__.py +19 -0
- textmate_grammar_python-0.3.0/src/textmate_grammar/grammars/markdown/__pycache__/__init__.cpython-312.pyc +0 -0
- textmate_grammar_python-0.3.0/src/textmate_grammar/grammars/matlab/__init__.py +27 -0
- textmate_grammar_python-0.3.0/src/textmate_grammar/grammars/matlab/__pycache__/__init__.cpython-312.pyc +0 -0
- {textmate_grammar_python-0.1.2 → textmate_grammar_python-0.3.0/src}/textmate_grammar/language.py +75 -27
- {textmate_grammar_python-0.1.2 → textmate_grammar_python-0.3.0/src}/textmate_grammar/parser.py +219 -76
- textmate_grammar_python-0.3.0/src/textmate_grammar/utils/__init__.py +0 -0
- textmate_grammar_python-0.3.0/src/textmate_grammar/utils/__pycache__/__init__.cpython-312.pyc +0 -0
- textmate_grammar_python-0.3.0/src/textmate_grammar/utils/__pycache__/cache.cpython-312.pyc +0 -0
- textmate_grammar_python-0.3.0/src/textmate_grammar/utils/__pycache__/exceptions.cpython-312.pyc +0 -0
- textmate_grammar_python-0.3.0/src/textmate_grammar/utils/__pycache__/handler.cpython-312.pyc +0 -0
- textmate_grammar_python-0.3.0/src/textmate_grammar/utils/__pycache__/logger.cpython-312.pyc +0 -0
- textmate_grammar_python-0.3.0/src/textmate_grammar/utils/cache.py +160 -0
- textmate_grammar_python-0.3.0/src/textmate_grammar/utils/exceptions.py +69 -0
- {textmate_grammar_python-0.1.2/textmate_grammar → textmate_grammar_python-0.3.0/src/textmate_grammar/utils}/handler.py +112 -36
- textmate_grammar_python-0.1.2/textmate_grammar/logging.py → textmate_grammar_python-0.3.0/src/textmate_grammar/utils/logger.py +49 -24
- textmate_grammar_python-0.1.2/PKG-INFO +0 -105
- textmate_grammar_python-0.1.2/README.md +0 -85
- textmate_grammar_python-0.1.2/pyproject.toml +0 -24
- textmate_grammar_python-0.1.2/textmate_grammar/captures.py +0 -120
- textmate_grammar_python-0.1.2/textmate_grammar/elements.py +0 -132
- textmate_grammar_python-0.1.2/textmate_grammar/exceptions.py +0 -27
- textmate_grammar_python-0.1.2/textmate_grammar/grammars/markdown/__init__.py +0 -16
- textmate_grammar_python-0.1.2/textmate_grammar/grammars/matlab/__init__.py +0 -25
- {textmate_grammar_python-0.1.2 → textmate_grammar_python-0.3.0}/LICENSE +0 -0
- {textmate_grammar_python-0.1.2/textmate_grammar → textmate_grammar_python-0.3.0/src/textmate_grammar/grammars}/__init__.py +0 -0
- {textmate_grammar_python-0.1.2 → textmate_grammar_python-0.3.0/src}/textmate_grammar/grammars/markdown/LICENSE.txt +0 -0
- {textmate_grammar_python-0.1.2 → textmate_grammar_python-0.3.0/src}/textmate_grammar/grammars/markdown/grammar.yaml +0 -0
- {textmate_grammar_python-0.1.2 → textmate_grammar_python-0.3.0/src}/textmate_grammar/grammars/matlab/grammar.yaml +0 -0
- {textmate_grammar_python-0.1.2 → textmate_grammar_python-0.3.0/src}/textmate_grammar/grammars/matlab/license.txt +0 -0
- /textmate_grammar_python-0.1.2/textmate_grammar/grammars/__init__.py → /textmate_grammar_python-0.3.0/src/textmate_grammar/py.typed +0 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: textmate-grammar-python
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: A lexer and tokenizer for grammar files as defined by TextMate and used in VSCode, implemented in Python.
|
|
5
|
+
Home-page: https://github.com/watermarkhu/textmate-grammar-python
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: textmate,tokenization
|
|
8
|
+
Author: Mark Shui Hu
|
|
9
|
+
Author-email: watermarkhu@gmail.com
|
|
10
|
+
Requires-Python: >=3.11,<4.0
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Requires-Dist: PyYAML (>=6.0.1,<7.0.0)
|
|
16
|
+
Requires-Dist: onigurumacffi (>=1.3.0,<2.0.0)
|
|
17
|
+
Project-URL: Documentation, https://textmate-grammar-python.readthedocs.io
|
|
18
|
+
Project-URL: Repository, https://github.com/watermarkhu/textmate-grammar-python
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
[](https://pypi.python.org/pypi/textmate-grammar-python)
|
|
22
|
+
[](https://github.com/watermarkhu/textmate-grammar-python/tree/main?tab=MIT-1-ov-file)
|
|
23
|
+
[](https://github.com/astral-sh/ruff)
|
|
24
|
+
[](http://mypy-lang.org/)
|
|
25
|
+
[](https://github.com/pre-commit/pre-commit)
|
|
26
|
+
[](https://pypi.python.org/pypi/textmate-grammar-python)
|
|
27
|
+
[](https://github.com/watermarkhu/textmate-grammar-python/blob/main/.github/workflows/ci.yml)
|
|
28
|
+
[](https://textmate-grammar-python.readthedocs.io)
|
|
29
|
+
|
|
30
|
+
# textmate-grammar-python
|
|
31
|
+
|
|
32
|
+
A lexer and tokenizer for grammar files as defined by TextMate and used in VSCode, implemented in Python.
|
|
33
|
+
|
|
34
|
+
Textmate grammars are made for [vscode-texmate](https://github.com/microsoft/vscode-textmate), allowing for syntax highlighting in VSCode after tokenization. This presents textmate-grammar-python with a large list of potentially supported languages.
|
|
35
|
+
|
|
36
|
+
```mermaid
|
|
37
|
+
flowchart TD
|
|
38
|
+
A[grammar file]
|
|
39
|
+
Z[code]
|
|
40
|
+
B("`vscode-textmate **js**`")
|
|
41
|
+
C("`textmate-grammar-**python**`")
|
|
42
|
+
D[tokens]
|
|
43
|
+
|
|
44
|
+
click C "https://github.com/microsoft/vscode-textmate"
|
|
45
|
+
|
|
46
|
+
Z --> B
|
|
47
|
+
Z --> C
|
|
48
|
+
A -.-> B --> D
|
|
49
|
+
A -.-> C --> D
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Usage
|
|
53
|
+
Install the module with:
|
|
54
|
+
```bash
|
|
55
|
+
pip install textmate-grammar-python
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Before tokenization is possible, a `LanguageParser` needs to be initialized using a loaded grammar.
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
from textmate_grammar.language import LanguageParser
|
|
62
|
+
from textmate_grammar.grammars import matlab
|
|
63
|
+
parser = LanguageParser(matlab.GRAMMAR)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
After this, one can either choose to call [`parser.parsing_string`](https://textmate-grammar-python.readthedocs.io/en/latest/apidocs/textmate_grammar/textmate_grammar.language.html#textmate_grammar.language.LanguageParser.parse_string) to parse a input string directly, or call [`parser.parse_file`](https://textmate-grammar-python.readthedocs.io/en/latest/apidocs/textmate_grammar/textmate_grammar.language.html#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`](https://github.com/watermarkhu/textmate-grammar-python/blob/main/example.py).
|
|
67
|
+
|
|
68
|
+
The parsed `element` object can be displayed directly by calling the [`print`](https://textmate-grammar-python.readthedocs.io/en/latest/apidocs/textmate_grammar/textmate_grammar.elements.html#textmate_grammar.elements.ContentElement.print) method. By default the element is printed as an element tree in a dictionary format.
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
>>> element = parser.parse_string("value = num2str(10);")
|
|
72
|
+
>>> element.print()
|
|
73
|
+
|
|
74
|
+
{'token': 'source.matlab',
|
|
75
|
+
'children': [{'token': 'meta.assignment.variable.single.matlab',
|
|
76
|
+
'children': [{'token': 'variable.other.readwrite.matlab', 'content': 'value'}]},
|
|
77
|
+
{'token': 'keyword.operator.assignment.matlab', 'content': '='},
|
|
78
|
+
{'token': 'meta.function-call.parens.matlab',
|
|
79
|
+
'begin': [{'token': 'entity.name.function.matlab', 'content': 'num2str'},
|
|
80
|
+
{'token': 'punctuation.section.parens.begin.matlab', 'content': '('}],
|
|
81
|
+
'end': [{'token': 'punctuation.section.parens.end.matlab', 'content': ')'}],
|
|
82
|
+
'children': [{'token': 'constant.numeric.decimal.matlab', 'content': '10'}]},
|
|
83
|
+
{'token': 'punctuation.terminator.semicolon.matlab', 'content': ';'}]}
|
|
84
|
+
|
|
85
|
+
```
|
|
86
|
+
Alternatively, with the keyword argument `flatten` the element is displayed as a list per unique token. Here the first item in the list is the starting position (line, column) of the unique tokenized element.
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
>>> element.print(flatten=True)
|
|
90
|
+
|
|
91
|
+
[[(0, 0), 'value', ['source.matlab', 'meta.assignment.variable.single.matlab', 'variable.other.readwrite.matlab']],
|
|
92
|
+
[(0, 5), ' ', ['source.matlab']],
|
|
93
|
+
[(0, 6), '=', ['source.matlab', 'keyword.operator.assignment.matlab']],
|
|
94
|
+
[(0, 7), ' ', ['source.matlab']],
|
|
95
|
+
[(0, 8), 'num2str', ['source.matlab', 'meta.function-call.parens.matlab', 'entity.name.function.matlab']],
|
|
96
|
+
[(0, 15), '(', ['source.matlab', 'meta.function-call.parens.matlab', 'punctuation.section.parens.begin.matlab']],
|
|
97
|
+
[(0, 16), '10', ['source.matlab', 'meta.function-call.parens.matlab', 'constant.numeric.decimal.matlab']],
|
|
98
|
+
[(0, 18), ')', ['source.matlab', 'meta.function-call.parens.matlab', 'punctuation.section.parens.end.matlab']],
|
|
99
|
+
[(0, 19), ';', ['source.matlab', 'punctuation.terminator.semicolon.matlab']]]
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Information
|
|
103
|
+
|
|
104
|
+
- For further information, please checkout the [documentation](https://textmate-grammar-python.readthedocs.io/en/latest/).
|
|
105
|
+
- To setup an environment for development, see [CONTRIBUTING.md](https://github.com/watermarkhu/textmate-grammar-python/blob/main/CONTRIBUTING.md)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
[](https://pypi.python.org/pypi/textmate-grammar-python)
|
|
2
|
+
[](https://github.com/watermarkhu/textmate-grammar-python/tree/main?tab=MIT-1-ov-file)
|
|
3
|
+
[](https://github.com/astral-sh/ruff)
|
|
4
|
+
[](http://mypy-lang.org/)
|
|
5
|
+
[](https://github.com/pre-commit/pre-commit)
|
|
6
|
+
[](https://pypi.python.org/pypi/textmate-grammar-python)
|
|
7
|
+
[](https://github.com/watermarkhu/textmate-grammar-python/blob/main/.github/workflows/ci.yml)
|
|
8
|
+
[](https://textmate-grammar-python.readthedocs.io)
|
|
9
|
+
|
|
10
|
+
# textmate-grammar-python
|
|
11
|
+
|
|
12
|
+
A lexer and tokenizer for grammar files as defined by TextMate and used in VSCode, implemented in Python.
|
|
13
|
+
|
|
14
|
+
Textmate grammars are made for [vscode-texmate](https://github.com/microsoft/vscode-textmate), allowing for syntax highlighting in VSCode after tokenization. This presents textmate-grammar-python with a large list of potentially supported languages.
|
|
15
|
+
|
|
16
|
+
```mermaid
|
|
17
|
+
flowchart TD
|
|
18
|
+
A[grammar file]
|
|
19
|
+
Z[code]
|
|
20
|
+
B("`vscode-textmate **js**`")
|
|
21
|
+
C("`textmate-grammar-**python**`")
|
|
22
|
+
D[tokens]
|
|
23
|
+
|
|
24
|
+
click C "https://github.com/microsoft/vscode-textmate"
|
|
25
|
+
|
|
26
|
+
Z --> B
|
|
27
|
+
Z --> C
|
|
28
|
+
A -.-> B --> D
|
|
29
|
+
A -.-> C --> D
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
Install the module with:
|
|
34
|
+
```bash
|
|
35
|
+
pip install textmate-grammar-python
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Before tokenization is possible, a `LanguageParser` needs to be initialized using a loaded grammar.
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from textmate_grammar.language import LanguageParser
|
|
42
|
+
from textmate_grammar.grammars import matlab
|
|
43
|
+
parser = LanguageParser(matlab.GRAMMAR)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
After this, one can either choose to call [`parser.parsing_string`](https://textmate-grammar-python.readthedocs.io/en/latest/apidocs/textmate_grammar/textmate_grammar.language.html#textmate_grammar.language.LanguageParser.parse_string) to parse a input string directly, or call [`parser.parse_file`](https://textmate-grammar-python.readthedocs.io/en/latest/apidocs/textmate_grammar/textmate_grammar.language.html#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`](https://github.com/watermarkhu/textmate-grammar-python/blob/main/example.py).
|
|
47
|
+
|
|
48
|
+
The parsed `element` object can be displayed directly by calling the [`print`](https://textmate-grammar-python.readthedocs.io/en/latest/apidocs/textmate_grammar/textmate_grammar.elements.html#textmate_grammar.elements.ContentElement.print) method. By default the element is printed as an element tree in a dictionary format.
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
>>> element = parser.parse_string("value = num2str(10);")
|
|
52
|
+
>>> element.print()
|
|
53
|
+
|
|
54
|
+
{'token': 'source.matlab',
|
|
55
|
+
'children': [{'token': 'meta.assignment.variable.single.matlab',
|
|
56
|
+
'children': [{'token': 'variable.other.readwrite.matlab', 'content': 'value'}]},
|
|
57
|
+
{'token': 'keyword.operator.assignment.matlab', 'content': '='},
|
|
58
|
+
{'token': 'meta.function-call.parens.matlab',
|
|
59
|
+
'begin': [{'token': 'entity.name.function.matlab', 'content': 'num2str'},
|
|
60
|
+
{'token': 'punctuation.section.parens.begin.matlab', 'content': '('}],
|
|
61
|
+
'end': [{'token': 'punctuation.section.parens.end.matlab', 'content': ')'}],
|
|
62
|
+
'children': [{'token': 'constant.numeric.decimal.matlab', 'content': '10'}]},
|
|
63
|
+
{'token': 'punctuation.terminator.semicolon.matlab', 'content': ';'}]}
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
Alternatively, with the keyword argument `flatten` the element is displayed as a list per unique token. Here the first item in the list is the starting position (line, column) of the unique tokenized element.
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
>>> element.print(flatten=True)
|
|
70
|
+
|
|
71
|
+
[[(0, 0), 'value', ['source.matlab', 'meta.assignment.variable.single.matlab', 'variable.other.readwrite.matlab']],
|
|
72
|
+
[(0, 5), ' ', ['source.matlab']],
|
|
73
|
+
[(0, 6), '=', ['source.matlab', 'keyword.operator.assignment.matlab']],
|
|
74
|
+
[(0, 7), ' ', ['source.matlab']],
|
|
75
|
+
[(0, 8), 'num2str', ['source.matlab', 'meta.function-call.parens.matlab', 'entity.name.function.matlab']],
|
|
76
|
+
[(0, 15), '(', ['source.matlab', 'meta.function-call.parens.matlab', 'punctuation.section.parens.begin.matlab']],
|
|
77
|
+
[(0, 16), '10', ['source.matlab', 'meta.function-call.parens.matlab', 'constant.numeric.decimal.matlab']],
|
|
78
|
+
[(0, 18), ')', ['source.matlab', 'meta.function-call.parens.matlab', 'punctuation.section.parens.end.matlab']],
|
|
79
|
+
[(0, 19), ';', ['source.matlab', 'punctuation.terminator.semicolon.matlab']]]
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Information
|
|
83
|
+
|
|
84
|
+
- For further information, please checkout the [documentation](https://textmate-grammar-python.readthedocs.io/en/latest/).
|
|
85
|
+
- To setup an environment for development, see [CONTRIBUTING.md](https://github.com/watermarkhu/textmate-grammar-python/blob/main/CONTRIBUTING.md)
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["poetry-core"]
|
|
3
|
+
build-backend = "poetry.core.masonry.api"
|
|
4
|
+
|
|
5
|
+
[tool.poetry]
|
|
6
|
+
name = "textmate-grammar-python"
|
|
7
|
+
version = "0.3.0"
|
|
8
|
+
description = "A lexer and tokenizer for grammar files as defined by TextMate and used in VSCode, implemented in Python."
|
|
9
|
+
authors = ["Mark Shui Hu <watermarkhu@gmail.com>"]
|
|
10
|
+
license = "MIT"
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
repository = "https://github.com/watermarkhu/textmate-grammar-python"
|
|
13
|
+
documentation = "https://textmate-grammar-python.readthedocs.io"
|
|
14
|
+
keywords = ["textmate", "tokenization"]
|
|
15
|
+
packages = [{include = "textmate_grammar", from = "src"}]
|
|
16
|
+
|
|
17
|
+
[tool.poetry.dependencies]
|
|
18
|
+
python = "^3.11"
|
|
19
|
+
onigurumacffi = "^1.3.0"
|
|
20
|
+
PyYAML = "^6.0.1"
|
|
21
|
+
|
|
22
|
+
[tool.poetry.group.test.dependencies]
|
|
23
|
+
pytest = ">=7.3.1,<9.0.0"
|
|
24
|
+
pytest-icdiff = ">=0.8,<0.10"
|
|
25
|
+
tox = "^4.11.4"
|
|
26
|
+
|
|
27
|
+
[tool.poetry.group.dev.dependencies]
|
|
28
|
+
pre-commit = "^3.6.0"
|
|
29
|
+
mypy = "^1.8.0"
|
|
30
|
+
ruff = "^0.3.0"
|
|
31
|
+
types-pyyaml = "^6.0.12.12"
|
|
32
|
+
poetry-bumpversion = "^0.3.2"
|
|
33
|
+
|
|
34
|
+
[tool.poetry.group.doc.dependencies]
|
|
35
|
+
sphinx = "^7.2.6"
|
|
36
|
+
sphinx-autodoc2 = "^0.5.0"
|
|
37
|
+
myst-parser = "^2.0.0"
|
|
38
|
+
furo = "^2024.1.29"
|
|
39
|
+
sphinxcontrib-mermaid = "^0.9.2"
|
|
40
|
+
|
|
41
|
+
[tool.poetry_bumpversion.file."src/textmate_grammar/__init__.py"]
|
|
42
|
+
|
|
43
|
+
[tool.poetry_bumpversion.file."docs/conf.py"]
|
|
44
|
+
search = 'version = "{current_version}"'
|
|
45
|
+
replace = 'version = "{new_version}"'
|
|
46
|
+
|
|
47
|
+
[tool.ruff]
|
|
48
|
+
include = ["pyproject.toml", "src/textmate_grammar/**/*.py"]
|
|
49
|
+
exclude = ["src/textmate_grammar/grammars/"]
|
|
50
|
+
line-length = 100
|
|
51
|
+
indent-width = 4
|
|
52
|
+
|
|
53
|
+
[tool.ruff.lint]
|
|
54
|
+
select = ["E", "F", "UP", "B", "SIM", "I"]
|
|
55
|
+
ignore = ["E111", "E114", "E117", "E501", "F401"]
|
|
56
|
+
|
|
57
|
+
[tool.ruff.format]
|
|
58
|
+
quote-style = "double"
|
|
59
|
+
indent-style = "space"
|
|
60
|
+
skip-magic-trailing-comma = false
|
|
61
|
+
line-ending = "auto"
|
|
62
|
+
docstring-code-format = true
|
|
63
|
+
docstring-code-line-length = "dynamic"
|
|
64
|
+
|
|
65
|
+
[tool.mypy]
|
|
66
|
+
ignore_missing_imports = true
|
|
67
|
+
exclude = ["test/*", "docs/*", "example.py"]
|
|
68
|
+
|
|
69
|
+
[tool.tox]
|
|
70
|
+
legacy_tox_ini = """
|
|
71
|
+
[tox]
|
|
72
|
+
isolated_build = true
|
|
73
|
+
|
|
74
|
+
[testenv]
|
|
75
|
+
skip_install = true
|
|
76
|
+
allowlist_externals = poetry
|
|
77
|
+
commands =
|
|
78
|
+
poetry run pytest test/unit
|
|
79
|
+
|
|
80
|
+
[testenv:mypy]
|
|
81
|
+
skip_install = true
|
|
82
|
+
allowlist_externals = mypy
|
|
83
|
+
commands =
|
|
84
|
+
mypy .
|
|
85
|
+
|
|
86
|
+
[testenv:regression]
|
|
87
|
+
skip_install = true
|
|
88
|
+
allowlist_externals = poetry, bash, sudo
|
|
89
|
+
platform = linux
|
|
90
|
+
change_dir = {tox_root}/test/regression
|
|
91
|
+
commands_pre =
|
|
92
|
+
bash install.sh
|
|
93
|
+
commands =
|
|
94
|
+
poetry run pytest .
|
|
95
|
+
"""
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.3.0"
|