Skip to main content

TextMate grammar parser for python

Project description

TextMate grammar parser for python

Install

pip install python-textmate

Usage

The parser can parse one line, to parse multiple lines iterate over each line and call parse()

>>> from textmate import TextMateGrammar, TextMateGrammarRepository
>>> import json
>>> 
>>> def load_json(file_path):
...     with open(file_path, "r") as f:
...         return json.load(f)
... 
>>> # Initialize the grammar repository
>>> repository = TextMateGrammarRepository([
...     load_json("MagicPython.tmLanguage.json"),
...     load_json("MagicRegExp.tmLanguage.json")
... ])
>>> 
>>> # Get the Python grammar
>>> python_grammar = repository.get_grammar_by_language("python")
>>> 
>>> if python_grammar:
...     # Initiatee the grammar parser
...     grammar = TextMateGrammar(python_grammar, repository)
...     3 Parse one line of code
...     code = "print(True) # the parser only parses one line."
...     result = grammar.parse(code)
...     print(result)
... 
[('punctuation.definition.comment.python', (12, 13)),
 ('comment.line.number-sign.python', (12, 46)),
 ('constant.language.python', (6, 10)),
 ('punctuation.parenthesis.begin.python', (5, 6)),
 ('punctuation.parenthesis.end.python', (10, 11)),
 ('constant.language.python', (6, 10)),
 ('punctuation.definition.arguments.end.python', (10, 11)),
 ('meta.function-call.python', (0, 11)),
 ('keyword.illegal.name.python', (6, 10)),
 ('support.function.builtin.python', (0, 5)),
 ('meta.function-call.generic.python', (0, 5)),
 ('meta.function-call.generic.python', (6, 10)),
 ('meta.function-call.arguments.python', (6, 10)),
 ('constant.language.python', (6, 10)),
 ('support.function.builtin.python', (0, 5)),
 ('punctuation.separator.period.python', (45, 46)),
 ('meta.member.access.python', (45, 46))]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_textmate-0.2.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

python_textmate-0.2.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file python_textmate-0.2.0.tar.gz.

File metadata

  • Download URL: python_textmate-0.2.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.11 Linux/6.9.3-76060903-generic

File hashes

Hashes for python_textmate-0.2.0.tar.gz
Algorithm Hash digest
SHA256 620417891826cc79a3c16ff15ca04b033b63bc4470f66fd92e96041a2009a1a6
MD5 21c2c8c48bbcdfc4cb70ca83875dd2de
BLAKE2b-256 3689ac5e7e75f99116c1a1b687d73eb60c6a2415ec1871c122efda3a8238d118

See more details on using hashes here.

File details

Details for the file python_textmate-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: python_textmate-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.11 Linux/6.9.3-76060903-generic

File hashes

Hashes for python_textmate-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b98678769583339589e04d27b9b482ba9830a951a910e0909a015cb8a81e8a2a
MD5 499c609a03c6e29d4f3cdbd0b9bdf634
BLAKE2b-256 aac5130c9a7bc50a94466608d839e7eccb551a3bcfbf2233edfc6b308814d27b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page