summaryrefslogtreecommitdiff
path: root/pest_consume_macros/src/make_parser.rs
diff options
context:
space:
mode:
authorNadrieril2019-09-10 16:29:48 +0200
committerNadrieril2019-09-10 16:38:28 +0200
commit26f54b10314f90dc5457cd0760af7f109be78e5f (patch)
tree892fd7e0be529e9a5c67f1893b854fb3e2a7172b /pest_consume_macros/src/make_parser.rs
parent65fd9a895ba3093f9b5d9d02fb8bd18a9be61808 (diff)
Use an enum instead of strings for rule aliasing
Diffstat (limited to '')
-rw-r--r--pest_consume_macros/src/make_parser.rs42
1 files changed, 33 insertions, 9 deletions
diff --git a/pest_consume_macros/src/make_parser.rs b/pest_consume_macros/src/make_parser.rs
index 4165046..e8d861f 100644
--- a/pest_consume_macros/src/make_parser.rs
+++ b/pest_consume_macros/src/make_parser.rs
@@ -136,6 +136,15 @@ fn collect_aliases(
is_shortcut: args.is_shortcut,
},
);
+ } else {
+ // Self entry
+ alias_map
+ .entry(fn_name.clone())
+ .or_insert_with(Vec::new)
+ .push(AliasSrc {
+ ident: fn_name,
+ is_shortcut: false,
+ });
}
if let Some(attr) = alias_attrs.next() {
return Err(Error::new(
@@ -238,17 +247,22 @@ fn apply_special_attrs(f: &mut ParsedFn, rule_enum: &Path) -> Result<()> {
}
// `alias` attr
- if !f.alias_srcs.is_empty() {
- let aliases = f.alias_srcs.iter().map(|src| &src.ident);
+ // f.alias_srcs has always at least 1 element because it has an entry pointing from itself.
+ if f.alias_srcs.len() > 1 {
+ let aliases = f
+ .alias_srcs
+ .iter()
+ .map(|src| &src.ident)
+ .filter(|i| i != &fn_name);
let block = &function.block;
+ let self_ty = quote!(<Self as ::pest_consume::PestConsumer>);
function.block = parse_quote!({
let mut #input_arg = #input_arg;
// While the current rule allows shortcutting, and there is a single child, and the
// child can still be parsed by the current function, then skip to that child.
- while <Self as pest_consume::PestConsumer>::allows_shortcut(#input_arg.as_rule()) {
+ while #self_ty::allows_shortcut(#input_arg.as_rule()) {
if let Some(child) = #input_arg.single_child() {
- if &<Self as pest_consume::PestConsumer>::rule_alias(child.as_rule())
- == stringify!(#fn_name) {
+ if child.as_rule_alias::<Self>() == #self_ty::AliasedRule::#fn_name {
#input_arg = child;
continue;
}
@@ -287,10 +301,12 @@ pub fn make_parser(
.map(|(tgt, src)| {
let ident = &src.ident;
quote!(
- #rule_enum::#ident => stringify!(#tgt).to_string(),
+ #rule_enum::#ident => Self::AliasedRule::#tgt,
)
})
.collect();
+ let aliased_rule_variants: Vec<_> =
+ alias_map.iter().map(|(tgt, _)| tgt.clone()).collect();
let shortcut_branches: Vec<_> = alias_map
.iter()
.flat_map(|(_tgt, srcs)| srcs)
@@ -353,13 +369,21 @@ pub fn make_parser(
let ty = &imp.self_ty;
let (impl_generics, _, where_clause) = imp.generics.split_for_impl();
Ok(quote!(
- impl #impl_generics pest_consume::PestConsumer for #ty #where_clause {
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
+ #[allow(non_camel_case_types)]
+ enum AliasedRule {
+ #(#aliased_rule_variants,)*
+ }
+
+ impl #impl_generics ::pest_consume::PestConsumer for #ty #where_clause {
type Rule = #rule_enum;
+ type AliasedRule = AliasedRule;
type Parser = #parser;
- fn rule_alias(rule: Self::Rule) -> String {
+ fn rule_alias(rule: Self::Rule) -> Self::AliasedRule {
match rule {
#(#rule_alias_branches)*
- r => format!("{:?}", r),
+ // TODO: return a proper error ?
+ r => unreachable!("Rule {:?} does not have a corresponding parsing method", r),
}
}
fn allows_shortcut(rule: Self::Rule) -> bool {