| 1 |
extern crate proc_macro; |
| 2 |
extern crate proc_macro2; |
| 3 |
extern crate quote; |
| 4 |
|
| 5 |
use peg::Parse; |
| 6 |
use quote::quote_spanned; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
extern crate peg_runtime as peg; |
| 11 |
|
| 12 |
mod analysis; |
| 13 |
mod ast; |
| 14 |
mod grammar; |
| 15 |
mod tokens; |
| 16 |
mod translate; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#[proc_macro] |
| 22 |
pub 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 |
} |