gll_pg_macros/
lib.rs

1//! A parser generator for GLL grammar, macros part
2//!
3//! This library includes the proc macro to generate code.
4//!
5#![feature(proc_macro_diagnostic)]
6
7extern crate proc_macro;
8
9mod gen;
10
11use syn::{parse_macro_input, ItemImpl};
12
13/// The macro to generate a GLL parser.
14///
15/// It should be used on a `impl` block with a lexer:
16///
17/// ```ignore
18/// use gll_pg_core::*;
19/// use gll_pg_macros::gll;
20///
21/// struct Parser {}
22/// #[gll(S, Token)]
23/// impl Parser {
24///     // ...
25///     #[rule(S -> Ta)]
26///     fn rule1(a: LogosToken<Token>) -> () { }
27/// }
28/// ```
29///
30/// Here `S` is the start symbol and `Token` is a lexer deriving `Logos`.
31///
32/// Each rule is of form `lhs -> rhs1 rhs2 ...`.
33/// If you want to express `lhs -> Eps` rule, use `lhs -> `.
34///
35/// In each method, you can use `&self`, `&mut self` or simply omit it.
36///
37/// A type is associated with each `rhs` and return type is associated with `lhs`.
38/// Every terminal uses `LogosToken<Token>` type.
39/// Each nonterminal uses one custom type which derives `Clone` through out the grammar.
40///
41/// The generated `Parser::parse` function has the same return type as start symbol.
42#[proc_macro_attribute]
43pub fn gll(
44    attr: proc_macro::TokenStream,
45    input: proc_macro::TokenStream,
46) -> proc_macro::TokenStream {
47    let input = parse_macro_input!(input as ItemImpl);
48    gen::generate(attr, input)
49}