Skip to content

feat: a Python highlighting grammar - #227

Merged
jonruttan merged 2 commits into
mainfrom
feat/python-grammar
Nov 5, 2026
Merged

jonruttan merged 2 commits into
mainfrom
feat/python-grammar

Conversation

@jonruttan

Copy link
Copy Markdown
Owner

x-python declares a highlighting grammar in lang.xon, the way x-ash's sh and x-make's make do:

(grammar python (module python/syntax) (files ".py" ".pyw" ".pyi") (interpreters "python" "python3"))

With x-python installed, vi's :set sy (and every other x/tool/syntax user) colours Python files: by suffix, by a #! line naming python or python3, or by a modeline.

  • python/lexer.x (new): the Lexer rules the line painter used, moved out of python/line.x so the painter and the grammar read one set. It imports x-lang libraries only, so the grammar loads without the interpreter.
  • python/syntax.x (new): registers the grammar. A name is classed as the painter classes it: keyword, then True/False/None as constants, then builtin (name), then a capitalised name as a class. A decorator is a keyword. Brackets, , . : ; are punctuation; other plain bytes are operators. A window syncs back to a line starting at column 0 with a name, # or @.
  • python/line.x: builds its Lexer from python-lexer-rules; no other change.
  • tests/specs/C0-syntax.spec.md: the catalogue row and file lookup, spans of a sample file, string prefixes and triple quotes left open, a synced window, and the grammar's keyword and builtin lists held equal to python/tokens' and python/core's.

A small known cost, my call: the Lexer appends a space to each read, so a blank run at the very end of the text is not a token and the any rule hands the trailing newline over as plain. The grammar gives a plain blank byte no class, so it draws nothing; the painter is unchanged.

Local, v0.44.0: C0-syntax 5/5 and 09-line 7/7 pass; lang-kit lint --strict is clean on both new modules. python/line.x keeps the 3 provide findings main already has.

🤖 Generated with Claude Code

jonruttan and others added 2 commits November 4, 2026 17:52
lang.xon declares (grammar python (module python/syntax) ...): .py, .pyw
and .pyi files and the python and python3 interpreters, so vi and any other
x/tool/syntax user colour Python with x-python installed.

The line painter's Lexer rules move to python/lexer.x, which imports x-lang
libraries only; the painter and the grammar both read them.  python/syntax
classes names as the painter does -- keywords, True/False/None as
constants, builtins, capitalised names as classes -- from its own copy of
the interpreter's keyword and builtin lists, which C0-syntax holds equal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jonruttan
jonruttan merged commit ae60d38 into main Nov 5, 2026
12 checks passed
@jonruttan
jonruttan deleted the feat/python-grammar branch November 5, 2026 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant