Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python syntax highlighter for Python

Is there a simple (possibly with no dependencies) syntax highlighter witch tokenizes Python source code? I need it to return the token type, the offset from the beginning of the code and token length.

I wanted to use Python build-in tokenize but it does not return the offsets.

I also looked to a few of standard syntax highlighters but they all tokenize other languages as well, which is overkill for my case. All I'm looking for is simple (and fast) Python syntax highlighter.

Thanks in advance!

like image 909
Ecir Hana Avatar asked Jun 03 '26 01:06

Ecir Hana


1 Answers

I don't know about the offset but you can get back a stream of tokens from a Python snippet using pygments. It's a single standalone Python library as far as I know. The stream of tokens can then be used to do whatever you want. I have an example that renders a snippet of Python onto a pygame surface with syntax highlighting using Pygments. It tokenises the input line by line.

like image 179
Noufal Ibrahim Avatar answered Jun 05 '26 16:06

Noufal Ibrahim