-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
2,080 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,6 @@ Cargo.lock | |
|
||
/target | ||
|
||
# CLion | ||
# Clion | ||
|
||
/.idea/ | ||
/.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[package] | ||
name = "parce_macros" | ||
version = "0.0.0" | ||
authors = ["Joel Courtney <[email protected]>"] | ||
edition = "2018" | ||
description = "Proc macros for the parce crate" | ||
readme = "README.md" | ||
repository = "https://github.com/JoelCourtney/parce_macros" | ||
license = "MIT OR Apache-2.0" | ||
keywords = ["parser"] | ||
|
||
[dependencies] | ||
syn = { version = "1.0.73", features = ["full"] } | ||
proc-macro2 = "1.0.27" | ||
quote = "1.0.9" | ||
proc-macro-error = "1.0.4" | ||
Inflector = { version="0.11.4" } | ||
check_keyword = "0.1.1" | ||
regex = "1.5.4" | ||
lazy_static = "1.4.0" | ||
|
||
[lib] | ||
proc-macro = true | ||
path = "src/lib.rs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use quote::ToTokens; | ||
use syn::Attribute; | ||
|
||
pub(crate) struct ParceMacroError(pub Box<dyn ToTokens>, pub String); | ||
|
||
pub(crate) fn has_attr(s: &str, attrs: &Vec<Attribute>) -> bool { | ||
for attr in attrs { | ||
if let Some(ident) = attr.path.get_ident() { | ||
if ident.to_string() == s { | ||
return true; | ||
} | ||
} | ||
} | ||
false | ||
} | ||
|
||
pub(crate) fn get_attr<'a>(s: &str, attrs: &'a Vec<Attribute>) -> Option<&'a Attribute> { | ||
for attr in attrs { | ||
if let Some(ident) = attr.path.get_ident() { | ||
if ident.to_string() == s { | ||
return Some(attr); | ||
} | ||
} | ||
} | ||
None | ||
} | ||
|
||
pub(crate) fn get_attr_mut<'a>(s: &str, attrs: &'a mut Vec<Attribute>) -> Option<&'a mut Attribute> { | ||
for attr in attrs { | ||
if let Some(ident) = attr.path.get_ident() { | ||
if ident.to_string() == s { | ||
return Some(attr); | ||
} | ||
} | ||
} | ||
None | ||
} | ||
|
||
pub(crate) fn get_ident_list(s: &str, attrs: &Vec<Attribute>) -> Option<Vec<String>> { | ||
let attr = get_attr(s, attrs)?; | ||
match attr.parse_meta() { | ||
Ok(syn::Meta::List(syn::MetaList {nested,..})) => { | ||
let mut list = vec![]; | ||
for nest in nested { | ||
match nest { | ||
syn::NestedMeta::Meta(syn::Meta::Path(path)) => { | ||
match path.get_ident() { | ||
Some(id) => list.push(id.to_string()), | ||
_ => return None | ||
} | ||
} | ||
_ => return None | ||
} | ||
} | ||
Some(list) | ||
}, | ||
_ => return None | ||
} | ||
} | ||
|
||
pub(crate) fn get_lexer_ident(meta: &syn::Meta) -> Option<syn::Ident> { | ||
match meta { | ||
syn::Meta::Path(path) => { | ||
match path.get_ident() { | ||
Some(i) => Some(i.clone()), | ||
None => None | ||
} | ||
} | ||
_ => None | ||
} | ||
} | ||
|
||
pub(crate) fn get_lexer_path(meta: &syn::Meta) -> Option<syn::Path> { | ||
match meta { | ||
syn::Meta::Path(path) => Some(path.clone()), | ||
_ => None | ||
} | ||
} | ||
|
||
pub(crate) fn get_pattern(variant: &syn::Variant) -> Result<String, ParceMacroError> { | ||
match &variant.discriminant { | ||
Some((_, syn::Expr::Lit(syn::ExprLit { | ||
lit: syn::Lit::Str(lit_str), .. | ||
}))) => Ok(lit_str.value()), | ||
Some((_, syn::Expr::Lit(syn::ExprLit { | ||
lit: syn::Lit::Char(lit_char), .. | ||
}))) => Ok(format!("'{}'", lit_char.value())), | ||
_ => { | ||
return Err(ParceMacroError(Box::new(variant.clone()), "discriminant must a str literal".to_string())); | ||
} | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] | ||
pub(crate) enum RangeRuleMax { | ||
Infinite, | ||
Fixed, | ||
Some(usize) | ||
} | ||
|
||
lazy_static::lazy_static! { | ||
pub(crate) static ref COUNT_PARSER: regex::Regex = regex::Regex::new(r"\{(\d+),?(\d+)?\}").unwrap(); | ||
} |
Oops, something went wrong.