peg_macros/
lib.rs

1extern crate proc_macro;
2extern crate proc_macro2;
3extern crate quote;
4
5use peg::Parse;
6use quote::quote_spanned;
7
8// This can't use the `peg` crate as it would be a circular dependency, but the generated code in grammar.rs
9// requires `::peg` paths.
10extern crate peg_runtime as peg;
11
12mod analysis;
13mod ast;
14mod grammar;
15mod tokens;
16mod translate;
17
18/// The main macro for creating a PEG parser.
19///
20/// For the grammar syntax, see the `peg` crate documentation.
21#[proc_macro]
22pub fn parser(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
23    let tokens = tokens::FlatTokenStream::new(input.into());
24    let grammar = match grammar::peg::peg_grammar(&tokens) {
25        Ok(g) => g,
26        Err(err) => {
27            let msg = if tokens.is_eof(err.location.1) {
28                format!("expected {} at end of input", err.expected)
29            } else {
30                format!("expected {}", err.expected)
31            };
32            return quote_spanned!(err.location.0=> compile_error!(#msg);).into();
33        }
34    };
35
36    translate::compile_grammar(&grammar).into()
37}