Skip to content

Commit

Permalink
merge parce_macros into parce
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelCourtney committed Jun 23, 2021
2 parents fe971dc + 8cacc27 commit dc744ff
Show file tree
Hide file tree
Showing 7 changed files with 2,080 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ Cargo.lock

/target

# CLion
# Clion

/.idea/
/.idea/
24 changes: 24 additions & 0 deletions parce_macros/Cargo.toml
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"
103 changes: 103 additions & 0 deletions parce_macros/src/common.rs
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();
}
Loading

0 comments on commit dc744ff

Please sign in to comment.