# Lezer
Lezer is an incremental parser system created by [[Marijn Haverbeke]], released under the MIT license. It is the parsing engine behind [[CodeMirror]] 6.
You write a grammar file, Lezer generates a parser from it, and [[CodeMirror]] consumes the resulting syntax tree to drive highlighting, folding, indentation, and structural selection.
## The two properties that matter
Ordinary parsers are built for compilers. They assume a complete, syntactically valid file, parsed once. An editor breaks both assumptions constantly.
**Incremental**. When you type one character in a 3000-line file, re-parsing everything is wasted work. Lezer reuses the parts of the existing tree that didn't change and only re-parses what actually moved. That's what keeps highlighting responsive while you type instead of stuttering.
**Error-tolerant**. Code in an editor is broken most of the time. You're mid-word, mid-expression, missing a closing brace. A compiler parser throws. Lezer produces a usable tree anyway, marking the damaged regions, so highlighting and indentation keep working on the parts that make sense.
Those two properties are the whole reason Lezer exists. Everything else follows from them.
## How it works
- Grammars are written in a declarative `.grammar` file, then compiled to a parser
- Parsing is LR-based, with GLR-style handling where the grammar is ambiguous
- The output tree is compact and read-only, designed to be cheap to keep in memory
- Node types carry tags that [[CodeMirror]] maps to highlighting styles
- Grammars exist for most common languages, and you can write your own
## Why this matters if you use Obsidian
Every time [[Obsidian]] highlights a code block, folds a section, or renders Markdown live as you type, a Lezer parse is running underneath. It's invisible when it works, which is the point.
## Lezer vs Acorn
[[Acorn]] parses a whole JavaScript file into an AST for build tools. Lezer keeps a tree in sync with an editor buffer that's constantly changing and frequently invalid. Same author, same underlying obsession with structure, opposite ends of the tradeoff.
## References
- https://lezer.codemirror.net/
- https://marijnhaverbeke.nl/blog/lezer.html
- https://github.com/lezer-parser
## Related
- [[CodeMirror]]
- [[Acorn]]
- [[Marijn Haverbeke]]
- [[Obsidian]]
- [[Open Source]]