Pegen PEG Grammar Highlighting
Syntax highlighting in VS Code for .gram files written in the PEG syntax used by
pegen, the parser generator behind
CPython's parser. It covers the full syntax used in CPython's
Grammar/python.gram.

Features
- Rule definitions, with return types (
rule[asdl_stmt_seq*], rule[frozenset[str]])
and flags (rule[expr_ty] (memo))
- Meta directives like
@trailer, @subheader and @include, including
triple-quoted blocks
- Clickable
@include paths: Cmd/Ctrl+Click opens the included file, resolved
relative to the current .gram file
- Go to Definition and Find All References for rules: Cmd/Ctrl+Click a rule to jump
to its definition, or Shift+F12 to list every use. Rules defined in
@included files are found too
- Keywords and soft keywords:
'if' and "match" are highlighted differently
- Tokens such as
NAME, NEWLINE and ENDMARKER
- Named items, including typed ones (
a=expression, a[asdl_expr_seq*]=','.NAME+)
- All PEG operators: alternatives
|, lookaheads & / !, forced tokens &&,
cut ~, quantifiers ? * +, gather s.e+, optional [...] and grouping (...)
- Actions (
{ ... }), including actions that span several lines or contain
nested braces, plus strings, comments and numbers inside them
- Comment toggling, bracket matching and auto-closing of brackets and quotes
Installation
Install Pegen PEG Grammar Highlighting from the VS Code Marketplace, or run:
code --install-extension lysnikolaou.pegen-peg
Any file with the .gram extension is then highlighted.
Development
- Clone this repository and run
npm install.
- Open it in VS Code and press F5. This compiles the TypeScript in
src/ in
watch mode and starts an Extension Development Host with the extension loaded.
- Open a
.gram file, e.g. CPython's Grammar/python.gram.
The grammar is in syntaxes/pegen.tmLanguage.json.
To see which scope a token gets, run Developer: Inspect Editor Tokens and Scopes
from the Command Palette. After you change the grammar, run Developer: Reload Window
in the development host.
Releasing
Pushing a v* tag publishes the extension to the VS Code Marketplace and creates a
GitHub release with the .vsix attached. The tag has to match the version in
package.json.
Bump version in package.json and add a section to CHANGELOG.md.
Commit, then tag and push:
git tag v0.4.0
git push origin main v0.4.0
Known Issues
Code inside actions isn't highlighted as C or Python. Only strings, comments, numbers
and NULL / True / False / None get highlighted there.
License
MIT