pegcel-macros

Crates.iopegcel-macros
lib.rspegcel-macros
version0.1.0
sourcesrc
created_at2019-05-12 03:23:16.16534
updated_at2019-05-12 03:23:16.16534
descriptionA PEG parser generator for Syn
homepage
repositoryhttps://github.com/CAD97/pegcel
max_upload_size
id133640
size63,588
Christopher Durham (CAD97)

documentation

README

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.

Commit count: 5

cargo fmt