1 unstable release

0.1.0 May 12, 2019

#65 in #syn

MIT license

63KB
1.5K SLoC

Pegcel: a PEG parser generator for Syn

Chat on Discord Travis status

Pegcel does the boring parts of creating Syn-style Syntax tree types from a grammar for you, while still allowing you to have all of the power of manually implemented parsing.

In short, Pegcel takes input like this:

use syn
use crate::manual_grammar::UnnamedItem

Grammar: {
    kind: GrammarUse
    uses: {&"use" Use}*
    items: NamedItem*
}

GrammarUse: {
    / Syn: {"use" "syn"}
}

Use: {
    r#use: "use"
    anchor: "::"?
    tree: syn::UseTree
}

Item: {
    / Named: {&{syn::Ident !"::" ":"} NamedItem}
    / Unnamed: UnnamedItem
}

NamedItem: {
    name: syn::Ident
    colon: ":"
    item: UnnamedItem
}

and generates all the types described, along with implementations for syn::parse::Parse and quote::ToTokens, so you can use them as if the were normal AST types provided by Syn.

Pegcel is self-hosting, so check out the meta grammar at pegcel/macros/tests/meta.rs and the generated output at pegcel/macros/src/grammar.rs.

Notes

Pegcel expects you to have syn:^0.15.34, quote:^0.6.12, and proc-macro2:^0.4.27 available as cargo dependencies. They must not be renamed by cargo. Obviously, syn and quote become public dependencies of your crate if you export the AST types.

A Token macro is generated by Pegcel to support any custom tokens used in your grammar as well as those provided by Syn. Due to current restrictions on proc-macro crates, this macro is pub(crate) and cannot be made more public by default. An opt-in to making it public will be offered in the future.

Tutorial

Actually, I'm uncertain how to explain how to use Pegcel! Hit me up at #1 and on Discord and ask me questions, and then we can document it together.

Dependencies

~4MB
~82K SLoC