I Rewrote My Tree-Sitter Grammar From Scratch. It Got 10x Smaller.
A complete rewrite of the tree-sitter-al grammar reduced parser.c from 106 MB to 10.6 MB and simplified the grammar from 8,500 to 3,100 lines while achieving 100% parse success on 15,358 production files. The new design replaced 291 property rules with an external scanner and single generic property rule, enabling keyword visibility for queries and syntax highlighting.
Open in Claudehttps-sshadows-dk-blog-tree-sitter-al-v2-rewrite--18455b4f15.mdRead the original on SShadowS

post SShadowS
"It's the compiler's job to validate property values, not the parser's."
Key points
- Parser file reduced from 106 MB (unpushable to GitHub) to 10.6 MB through architectural redesign
- Grammar simplified from 8,500 lines to 3,100 lines and symbol count cut from 2,249 to ~740
- Replaced 291 individual property rules with one generic rule plus external scanner for lookahead
- Achieved 100% parse success rate on all 15,358 production AL files after fixing seven edge cases
- All 82 keywords now visible to tree-sitter queries for syntax highlighting and code navigation
Quotes
- “It’s the compiler’s job to validate property values, not the parser’s.” (Key architectural insight that led to replacing 291 validation rules with a single generic rule)
Heads up
- gotcha: “begin and end can never be named nodes. Not as named rules, not with alias().”
Context
- Features: tree-sitter grammar for AL, external scanner for lookahead, generic property parsing, keyword visibility in queries, syntax highlighting queries, code navigation, scope analysis, indentation and folding
Source: SShadowS, community blog. Summary, key points and quotes are derived (CONTENT-NOTICE.md); read the original for the full text.