Skip to content

Commit

Permalink
Macros work yay!
Browse files Browse the repository at this point in the history
  • Loading branch information
kuviman committed Oct 2, 2024
1 parent 73f2794 commit d94e816
Show file tree
Hide file tree
Showing 13 changed files with 503 additions and 102 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ rand.workspace = true
futures.workspace = true
async-notify = "0.3.0"
async-executor = "1.13.1"
async-trait = "0.1.83"
2 changes: 1 addition & 1 deletion crates/ast/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub enum StringType {
}

#[allow(dead_code)]
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Token {
Ident {
raw: String,
Expand Down
53 changes: 53 additions & 0 deletions crates/ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,50 @@ pub enum Ast<Data = Span> {
},
}

impl<Data: PartialEq> PartialEq for Ast<Data> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(
Self::Simple {
token: l_token,
data: l_data,
},
Self::Simple {
token: r_token,
data: r_data,
},
) => l_token == r_token && l_data == r_data,
(
Self::Complex {
definition: l_definition,
values: l_values,
data: l_data,
},
Self::Complex {
definition: r_definition,
values: r_values,
data: r_data,
},
) => {
Arc::ptr_eq(l_definition, r_definition) && l_values == r_values && l_data == r_data
}
(
Self::SyntaxDefinition {
def: l_def,
data: l_data,
},
Self::SyntaxDefinition {
def: r_def,
data: r_data,
},
) => Arc::ptr_eq(l_def, r_def) && l_data == r_data,
_ => false,
}
}
}

impl<Data: Eq> Eq for Ast<Data> {}

impl<Data> Ast<Data> {
pub fn data(&self) -> &Data {
match self {
Expand All @@ -39,6 +83,15 @@ impl<Data> Ast<Data> {
| Ast::SyntaxDefinition { data, .. } => data,
}
}
pub fn as_ident(&self) -> Option<&str> {
match self {
Ast::Simple {
token: Token::Ident { name, .. },
..
} => Some(name),
_ => None,
}
}
}

impl Ast {
Expand Down
45 changes: 45 additions & 0 deletions crates/util/src/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ impl<T> Tuple<T> {
self.named_order.push(name.clone());
self.named.insert(name, value);
}
pub fn add(&mut self, name: Option<String>, value: T) {
match name {
Some(name) => self.add_named(name, value),
None => self.add_unnamed(value),
}
}
pub fn fmt_with_name<'a>(&'a self, name: &'a str) -> NamedTupleFmt<'a, T> {
NamedTupleFmt { name, tuple: self }
}
Expand Down Expand Up @@ -88,6 +94,41 @@ impl<T> Tuple<T> {
named_order: self.named_order,
}
}
pub fn values(&self) -> impl Iterator<Item = &T> + '_ {
self.unnamed
.iter()
.chain(self.named_order.iter().map(|name| &self.named[name]))
}
}

pub struct TupleIntoIter<T> {
unnamed: <Vec<T> as IntoIterator>::IntoIter,
names: <Vec<String> as IntoIterator>::IntoIter,
named: BTreeMap<String, T>,
}

impl<T> Iterator for TupleIntoIter<T> {
type Item = (Option<String>, T);
fn next(&mut self) -> Option<Self::Item> {
if let Some(value) = self.unnamed.next() {
return Some((None, value));
}
let name = self.names.next()?;
let value = self.named.remove(&name).unwrap();
Some((Some(name), value))
}
}

impl<T> IntoIterator for Tuple<T> {
type Item = (Option<String>, T);
type IntoIter = TupleIntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
TupleIntoIter {
unnamed: self.unnamed.into_iter(),
names: self.named_order.into_iter(),
named: self.named,
}
}
}

#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -234,9 +275,13 @@ impl<T> Tuple<T> {
}
}

#[derive(Debug, thiserror::Error)]
pub enum TupleZipError {
#[error("different amount of unnamed fields: {0} vs {1}")]
DifferentUnnamedAmount(usize, usize),
#[error("field {0} not present in other")]
NamedNotPresentInOther(String),
#[error("field {0} is only present in other")]
NamedOnlyPresentInOther(String),
}

Expand Down
Loading

0 comments on commit d94e816

Please sign in to comment.