#![allow(dead_code)]
mod lexed_tree;
mod typed_tree;
use sway_ast::attribute::{Annotated, Attribute, AttributeArg};
use sway_ast::{AttributeDecl, ItemFn, PathType};
pub(crate) use typed_tree::matchers as ty_match;
pub(crate) use typed_tree::predicates::*;
pub(crate) use lexed_tree::matchers as lexed_match;
pub(crate) use lexed_tree::matchers_mut as lexed_match_mut;
pub(crate) use lexed_tree::predicates::*;
pub(crate) trait TyElementsMatcher<T> {
fn match_elems<'a, P>(&'a self, predicate: P) -> impl Iterator<Item = &'a T>
where
P: Fn(&&'a T) -> bool + Clone + 'a,
T: 'a;
}
pub(crate) trait TyElementsMatcherDeep<T> {
fn match_elems_deep<'a, F>(&'a self, predicate: F) -> Vec<&'a T>
where
F: Fn(&&'a T) -> bool + Clone + 'a,
T: 'a;
}
pub(crate) trait TyLocate<Lexed, Ty> {
fn locate(&self, lexed_element: &Lexed) -> Option<&Ty>;
}
pub(crate) trait LexedElementsMatcherMut<T> {
fn match_elems_mut<'a, F>(&'a mut self, predicate: F) -> impl Iterator<Item = &'a mut T>
where
F: Fn(&&'a mut T) -> bool + Clone + 'a,
T: 'a;
}
pub(crate) trait LexedElementsMatcher<T> {
fn match_elems<'a, F>(&'a self, predicate: F) -> impl Iterator<Item = &'a T>
where
F: Fn(&&'a T) -> bool + Clone + 'a,
T: 'a;
}
pub(crate) trait LexedElementsMatcherDeepMut<T> {
fn match_elems_deep_mut<'a, F>(&'a mut self, predicate: F) -> Vec<&'a mut T>
where
F: Fn(&&'a mut T) -> bool + Clone + 'a,
T: 'a;
}
pub(crate) trait LexedElementsMatcherDeep<T> {
fn match_elems_deep<'a, F>(&'a self, predicate: F) -> Vec<&'a T>
where
F: Fn(&&'a T) -> bool + Clone + 'a,
T: 'a;
}
pub(crate) trait LexedLocateMut<Ty, Lexed> {
fn locate_mut(&mut self, ty_element: &Ty) -> Option<&mut Lexed>;
}
pub(crate) trait LexedLocate<Ty, Lexed> {
fn locate(&self, ty_element: &Ty) -> Option<&Lexed>;
}
pub(crate) trait LexedLocateAnnotatedMut<Ty, Lexed> {
fn locate_annotated_mut<'a>(
&'a mut self,
ty_element: &Ty,
) -> Option<(&'a mut Vec<AttributeDecl>, &'a mut Lexed)>;
}
pub(crate) trait LexedLocateAnnotated<Ty, Lexed> {
fn locate_annotated<'a>(
&'a self,
ty_element: &Ty,
) -> Option<(&'a Vec<AttributeDecl>, &'a Lexed)>;
}
pub(crate) trait LexedLocateAsAnnotatedMut<Ty, LexedAnnotated> {
fn locate_as_annotated_mut(
&mut self,
ty_element: &Ty,
) -> Option<&mut Annotated<LexedAnnotated>>;
}
pub(crate) trait LexedLocateAsAnnotated<Ty, LexedAnnotated> {
fn locate_as_annotated(&self, ty_element: &Ty) -> Option<&Annotated<LexedAnnotated>>;
}
impl<T, Ty, Lexed> LexedLocateMut<Ty, Lexed> for T
where
T: LexedLocateAnnotatedMut<Ty, Lexed>,
{
fn locate_mut(&mut self, ty_element: &Ty) -> Option<&mut Lexed> {
self.locate_annotated_mut(ty_element)
.map(|annotated| annotated.1)
}
}
impl<T, Ty, Lexed> LexedLocate<Ty, Lexed> for T
where
T: LexedLocateAnnotated<Ty, Lexed>,
{
fn locate(&self, ty_element: &Ty) -> Option<&Lexed> {
self.locate_annotated(ty_element)
.map(|annotated| annotated.1)
}
}
pub(crate) fn any<T>(_t: &&T) -> bool {
true
}
pub(crate) fn any_mut<T>(_t: &&mut T) -> bool {
true
}
#[macro_export]
macro_rules! all_of {
($($i:expr),+) => {
$crate::matching::all_of([$($i, )*].as_slice())
};
}
#[allow(dead_code)]
pub(crate) fn all_of<T, P>(predicates: &[P]) -> impl Fn(&&T) -> bool + Clone + '_
where
P: Fn(&&T) -> bool + Clone,
{
move |t: &&T| {
let mut res = true;
for predicate in predicates {
res &= predicate(t);
}
res
}
}
#[macro_export]
macro_rules! all_of_mut {
($($i:expr),+) => {
$crate::matching::all_of_mut([$($i, )*].as_slice())
};
}
#[allow(dead_code)]
pub(crate) fn all_of_mut<T, P>(predicates: &[P]) -> impl Fn(&&mut T) -> bool + Clone + '_
where
P: Fn(&&mut T) -> bool + Clone,
{
move |t: &&mut T| {
let mut res = true;
for predicate in predicates {
res &= predicate(t);
}
res
}
}
#[macro_export]
macro_rules! any_of {
($($i:expr),+) => {
$crate::matching::any_of([$($i, )*].as_slice())
};
}
#[allow(dead_code)]
pub(crate) fn any_of<T, P>(predicates: &[P]) -> impl Fn(&&T) -> bool + Clone + '_
where
P: Fn(&&T) -> bool + Clone,
{
move |t: &&T| {
let mut res = false;
for predicate in predicates {
res |= predicate(t);
}
res
}
}
#[macro_export]
macro_rules! any_of_mut {
($($i:expr),+) => {
$crate::matching::any_of_mut([$($i, )*].as_slice())
};
}
#[allow(dead_code)]
pub(crate) fn any_of_mut<T, P>(predicates: &[P]) -> impl Fn(&&mut T) -> bool + Clone + '_
where
P: Fn(&&mut T) -> bool + Clone,
{
move |t: &&mut T| {
let mut res = false;
for predicate in predicates {
res |= predicate(t);
}
res
}
}
pub(crate) trait WithName {
fn with_name<N: AsRef<str> + ?Sized>(&self, name: &N) -> bool;
}
pub(crate) fn with_name<T, N>(name: &N) -> impl Fn(&&T) -> bool + Clone + '_
where
T: WithName,
N: AsRef<str> + ?Sized,
{
move |t: &&T| t.with_name(name)
}
pub(crate) fn with_name_mut<T, N>(name: &N) -> impl Fn(&&mut T) -> bool + Clone + '_
where
T: WithName,
N: AsRef<str> + ?Sized,
{
move |t: &&mut T| t.with_name(name)
}
impl WithName for Attribute {
fn with_name<N: AsRef<str> + ?Sized>(&self, name: &N) -> bool {
self.name.as_str() == name.as_ref()
}
}
impl WithName for AttributeArg {
fn with_name<N: AsRef<str> + ?Sized>(&self, name: &N) -> bool {
self.name.as_str() == name.as_ref()
}
}
impl WithName for PathType {
fn with_name<N: AsRef<str> + ?Sized>(&self, name: &N) -> bool {
self.last_segment().name.as_str() == name.as_ref()
}
}
impl WithName for ItemFn {
fn with_name<N: AsRef<str> + ?Sized>(&self, name: &N) -> bool {
self.fn_signature.name.as_str() == name.as_ref()
}
}