Skip to content

Latest commit

 

History

History
32 lines (23 loc) · 2.06 KB

File metadata and controls

32 lines (23 loc) · 2.06 KB

Gnag (a parser generator)

This is a PEG parser generator, it generates rust code from an abstract description. You can find the self-hosted grammar of the gnag language here.

Rewrite underway

A vscode extensions and an LSP server are planned, and were at one point implemented, but are currently thoroughly broken due to an ongoing rewrite.

The rewrite is almost done (famous last words) and will improve the following:

  • Propagate source spans through the whole compilations.

  • Keep the parsing expression in a structured form all the way through.

    The previous version converted it to a graph, then did optimizations, then structured it again. This was really complicated and resulted in some loss of control over the shape of the generated code.

  • Generally learn from previous experience and make this version better.

Example

The docs/ directory contains a grammar describing simple math expressions. There are debug dumps of various stages of the grammar compilation:

  • expression.gng - the grammar itself
  • expression.cst - the concrete syntax tree of the grammar file (trivia tokens are excluded from the debug dump)
  • expression.ast - the abstract form of the grammar, constructs like pratt recursion are lowered to a recursive rule
  • expression.code.rs - the generated rust-like code, I need to write some passes to clean it up

Interesting files

  • grammar.gng - the self hosted grammar of the grammar language
  • gnag-parser/src/lib.rs - currently used to parse the grammar language, it was generated by the version before the rewrite and has since been modified to keep up with the runtime.
  • gnag-runtime/ - the parser runtime which is driven by the parser to construct the CST.
  • code-render-macro/ - a rust procedural macro, similar to the syn library. It allows to efficiently compose interpolated text, it is used to generate the parser code.