Skip to content

Commit

Permalink
Generator explicit include_str!("grammar.pest") flag
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Oct 1, 2018
1 parent 6e6a099 commit 7b40d79
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,5 +273,5 @@ use proc_macro::TokenStream;

#[proc_macro_derive(Parser, attributes(grammar))]
pub fn derive_parser(input: TokenStream) -> TokenStream {
pest_generator::derive_parser(input.into()).into()
pest_generator::derive_parser(input.into(), true).into()
}
11 changes: 8 additions & 3 deletions generator/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ pub fn generate(
generics: &Generics,
path: &Path,
rules: Vec<OptimizedRule>,
defaults: Vec<&str>
defaults: Vec<&str>,
include_grammar: bool
) -> TokenStream {
let uses_eoi = defaults.iter().any(|name| *name == "EOI");

let builtins = generate_builtin_rules();
let include_fix = generate_include(&name, &path.to_str().expect("non-Unicode path"));
let include_fix = if include_grammar {
generate_include(&name, &path.to_str().expect("non-Unicode path"))
} else {
quote!()
};
let rule_enum = generate_enum(&rules, uses_eoi);
let patterns = generate_patterns(&rules, uses_eoi);
let skip = generate_skip(&rules);
Expand Down Expand Up @@ -897,7 +902,7 @@ mod tests {
let defaults = vec!["ANY"];

assert_eq!(
generate(name, &generics, Path::new("test.pest"), rules, defaults).to_string(),
generate(name, &generics, Path::new("test.pest"), rules, defaults, true).to_string(),
quote! {
#[allow(non_upper_case_globals)]
#[cfg(debug_assertions)]
Expand Down
5 changes: 3 additions & 2 deletions generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod generator;
use pest_meta::{optimizer, unwrap_or_report, validator};
use pest_meta::parser::{self, Rule};

pub fn derive_parser(input: TokenStream) -> TokenStream {
pub fn derive_parser(input: TokenStream, include_grammar: bool) -> TokenStream {
let ast: DeriveInput = syn::parse2(input).unwrap();
let (name, generics, path) = parse_derive(ast);

Expand Down Expand Up @@ -87,7 +87,8 @@ pub fn derive_parser(input: TokenStream) -> TokenStream {
let defaults = unwrap_or_report(validator::validate_pairs(pairs.clone()));
let ast = unwrap_or_report(parser::consume_rules(pairs));
let optimized = optimizer::optimize(ast);
let generated = generator::generate(name, &generics, &path, optimized, defaults);
let generated =
generator::generate(name, &generics, &path, optimized, defaults, include_grammar);

generated.into()
}
Expand Down

0 comments on commit 7b40d79

Please sign in to comment.