#![warn(
absolute_paths_not_starting_with_crate,
anonymous_parameters,
bare_trait_objects,
box_pointers,
deprecated_in_future,
elided_lifetimes_in_paths,
ellipsis_inclusive_range_patterns,
explicit_outlives_requirements,
keyword_idents,
macro_use_extern_crate,
missing_copy_implementations,
missing_debug_implementations,
missing_docs,
missing_doc_code_examples,
private_doc_tests,
question_mark_macro_sep,
single_use_lifetimes,
trivial_casts,
trivial_numeric_casts,
unreachable_pub,
unsafe_code,
unstable_features,
unused_extern_crates,
unused_import_braces,
unused_labels,
unused_lifetimes,
unused_qualifications,
unused_results,
variant_size_differences,
clippy::cargo,
clippy::nursery,
clippy::pedantic,
clippy::restriction
)]
#![allow(
clippy::implicit_return, // Omitting the return keyword is idiomatic Rust code.
clippy::missing_inline_in_public_items, // Generally not bad and there are issues with derived traits.
)]
#![allow(single_use_lifetimes)]
pub mod prelude;
mod atom;
mod repetition;
pub use crate::{
atom::{Ch, Class},
repetition::{
btwn, exact, lazy_btwn, lazy_max, lazy_min, lazy_opt, lazy_some, lazy_var, max, min, opt,
some, var,
},
};
pub use regex::{Match, Regex};
use crate::prelude::{Element, Rec};
use core::{ops::Deref, str::FromStr};
use regex::Captures;
#[macro_export]
macro_rules! tkn {
($name:expr => $elmt:expr) => {
Rec::from(format!("(?P<{}>{})", $name, $elmt.to_regex()).as_str())
};
}
#[derive(Clone, Debug)]
pub struct Pattern {
re: Regex,
}
impl Pattern {
#[allow(clippy::needless_pass_by_value)] pub fn new<T: Element>(element: T) -> Self {
Self {
re: Rec::from(element.to_regex()).build(),
}
}
pub fn find_str<'t>(&self, text: &'t str) -> Option<&'t str> {
self.find(text).map(|m| m.as_str())
}
pub fn tokenize<'t>(&self, text: &'t str) -> Option<Tokens<'t>> {
self.captures(text).map(|captures| Tokens { captures })
}
pub fn name_str<'t>(&self, text: &'t str, name: &str) -> Option<&'t str> {
self.tokenize(text)
.and_then(|t| t.name(name).map(|m| m.as_str()))
}
}
impl Deref for Pattern {
type Target = Regex;
fn deref(&self) -> &Self::Target {
&self.re
}
}
impl FromStr for Pattern {
type Err = regex::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Rec::from(s).try_build().map(|x| Self { re: x })
}
}
#[derive(Debug)]
pub struct Tokens<'t> {
captures: Captures<'t>,
}
impl Tokens<'_> {
pub fn name_str(&self, name: &str) -> Option<&str> {
self.name(name).map(|m| m.as_str())
}
}
impl<'t> Deref for Tokens<'t> {
type Target = Captures<'t>;
fn deref(&self) -> &Self::Target {
&self.captures
}
}