diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2024-05-07 00:57:09 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2024-05-07 20:28:41 +0200 |
commit | c3183d546aa6cc5fae2ebb645c582ea3073fbae3 (patch) | |
tree | 9cc6a2e65145887cd849f8175a0b8fbdbe9c67eb | |
parent | 4a0e9147eb4402f84cc4d61c544a1f1e3423d76b (diff) | |
download | rebel-c3183d546aa6cc5fae2ebb645c582ea3073fbae3.tar rebel-c3183d546aa6cc5fae2ebb645c582ea3073fbae3.zip |
rebel-parse: ast: use derive-into-owned crate to derive into_owned() and borrowed() methods
-rw-r--r-- | Cargo.lock | 11 | ||||
-rw-r--r-- | crates/rebel-lang/src/typing.rs | 2 | ||||
-rw-r--r-- | crates/rebel-lang/src/value.rs | 2 | ||||
-rw-r--r-- | crates/rebel-parse/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/rebel-parse/src/ast/expr.rs | 21 | ||||
-rw-r--r-- | crates/rebel-parse/src/ast/mod.rs | 15 | ||||
-rw-r--r-- | crates/rebel-parse/src/ast/pat.rs | 12 | ||||
-rw-r--r-- | crates/rebel-parse/src/ast/typ.rs | 8 |
8 files changed, 44 insertions, 28 deletions
@@ -296,6 +296,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b390250ca2b6862735ef39a7b37b0266a8d5cd9d1e579260c95b1dc27761d6ad" [[package]] +name = "derive-into-owned" +version = "0.2.0" +source = "git+https://github.com/neocturne/derive-into-owned.git?branch=more-types#e692ae6da9220ff3a45b0a1c380e1f4a90c5b890" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.61", +] + +[[package]] name = "digest" version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -820,6 +830,7 @@ name = "rebel-parse" version = "0.1.0" dependencies = [ "clap", + "derive-into-owned", "divan", "peg", "phf", diff --git a/crates/rebel-lang/src/typing.rs b/crates/rebel-lang/src/typing.rs index 87776d0..e85a588 100644 --- a/crates/rebel-lang/src/typing.rs +++ b/crates/rebel-lang/src/typing.rs @@ -235,7 +235,7 @@ impl<'scope> Context<'scope> { return Ok(Type::unit()); }; - self.assign_destr_pat_type(&pat::DestrPat::from(pat), expr_type)? + self.assign_destr_pat_type(&pat::DestrPat::from(pat.borrowed()), expr_type)? } ast::BlockStmt::Assign { dest, expr } => { let expr_type = self.type_expr(expr)?; diff --git a/crates/rebel-lang/src/value.rs b/crates/rebel-lang/src/value.rs index ce766cb..6c9a5b0 100644 --- a/crates/rebel-lang/src/value.rs +++ b/crates/rebel-lang/src/value.rs @@ -139,7 +139,7 @@ impl<'scope> Context<'scope> { return Ok(Value::unit()); }; - self.assign_destr_pat_value(&pat::DestrPat::from(pat), value)? + self.assign_destr_pat_value(&pat::DestrPat::from(pat.borrowed()), value)? } ast::BlockStmt::Assign { dest, expr } => { let value = self.eval_expr(expr)?; diff --git a/crates/rebel-parse/Cargo.toml b/crates/rebel-parse/Cargo.toml index 95d2acd..d116736 100644 --- a/crates/rebel-parse/Cargo.toml +++ b/crates/rebel-parse/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +derive-into-owned = { git = "https://github.com/neocturne/derive-into-owned.git", branch = "more-types" } peg = "0.8.3" phf = { version = "0.11.2", features = ["macros"] } rebel-common = { path = "../rebel-common" } diff --git a/crates/rebel-parse/src/ast/expr.rs b/crates/rebel-parse/src/ast/expr.rs index 4a10f2d..86b19c6 100644 --- a/crates/rebel-parse/src/ast/expr.rs +++ b/crates/rebel-parse/src/ast/expr.rs @@ -2,11 +2,12 @@ use std::borrow::Cow; use super::{Block, DestrPat, Ident, Path, PathRoot, ValidationError}; use crate::token; +use derive_into_owned::{Borrowed, IntoOwned}; use rustc_hash::FxHashSet; pub use token::StrKind; -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, IntoOwned, Borrowed)] pub enum Expr<'a> { Binary { left: Box<Expr<'a>>, @@ -167,24 +168,24 @@ impl<'a> Expr<'a> { } } -impl<'a> From<&DestrPat<'a>> for Expr<'a> { - fn from(value: &DestrPat<'a>) -> Self { +impl<'a> From<DestrPat<'a>> for Expr<'a> { + fn from(value: DestrPat<'a>) -> Self { match value { DestrPat::Index { base, index } => Expr::Index { - base: Box::new(base.as_ref().into()), + base: Box::new((*base).into()), index: index.clone(), }, DestrPat::Field { base, field } => Expr::Field { - base: Box::new(base.as_ref().into()), + base: Box::new((*base).into()), field: field.clone(), }, - DestrPat::Paren(pat) => Expr::Paren(Box::new(pat.as_ref().into())), + DestrPat::Paren(pat) => Expr::Paren(Box::new((*pat).into())), DestrPat::Path(path) => Expr::Path(path.clone()), } } } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, IntoOwned, Borrowed)] pub enum Literal<'a> { Unit, Bool(bool), @@ -248,7 +249,7 @@ impl<'a> Literal<'a> { } } -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, IntoOwned, Borrowed)] pub enum StrPiece<'a> { Chars(Cow<'a, str>), Escape(char), @@ -271,13 +272,13 @@ impl<'a> TryFrom<&token::StrPiece<'a>> for StrPiece<'a> { } } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, IntoOwned, Borrowed)] pub struct MapEntry<'a> { pub key: Expr<'a>, pub value: Expr<'a>, } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, IntoOwned, Borrowed)] pub struct StructField<'a> { pub name: Cow<'a, str>, pub value: Expr<'a>, diff --git a/crates/rebel-parse/src/ast/mod.rs b/crates/rebel-parse/src/ast/mod.rs index 8d39d98..bc8e5a1 100644 --- a/crates/rebel-parse/src/ast/mod.rs +++ b/crates/rebel-parse/src/ast/mod.rs @@ -1,5 +1,6 @@ use std::borrow::Cow; +use derive_into_owned::{Borrowed, IntoOwned}; use rustc_hash::FxHashSet; pub mod expr; @@ -52,7 +53,7 @@ impl<'a> RecipeStmt<'a> { } } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, IntoOwned, Borrowed)] pub struct Block<'a>(pub Vec<BlockStmt<'a>>); impl<'a> Block<'a> { @@ -64,7 +65,7 @@ impl<'a> Block<'a> { } } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, IntoOwned, Borrowed)] pub enum BlockStmt<'a> { Let { dest: Box<TypedPat<'a>>, @@ -96,7 +97,7 @@ impl<'a> BlockStmt<'a> { ) -> Self { let expr = match op { Some(op) => { - let dest_expr = Expr::from(&dest); + let dest_expr = Expr::from(dest.clone()); if swapped { Expr::binary(expr, op, dest_expr) } else { @@ -132,13 +133,13 @@ impl<'a> BlockStmt<'a> { } } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, IntoOwned, Borrowed)] pub struct TypedPat<'a> { pub pat: Pat<'a>, pub typ: Option<Type<'a>>, } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, IntoOwned, Borrowed)] pub struct FuncParam<'a> { pub name: Ident<'a>, pub typ: Type<'a>, @@ -152,13 +153,13 @@ pub enum PathRoot { Task, } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, IntoOwned, Borrowed)] pub struct Path<'a> { pub root: PathRoot, pub components: Vec<Ident<'a>>, } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, IntoOwned, Borrowed)] pub struct Ident<'a> { pub name: Cow<'a, str>, } diff --git a/crates/rebel-parse/src/ast/pat.rs b/crates/rebel-parse/src/ast/pat.rs index 82c587a..c85f625 100644 --- a/crates/rebel-parse/src/ast/pat.rs +++ b/crates/rebel-parse/src/ast/pat.rs @@ -1,6 +1,6 @@ use super::*; -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, IntoOwned, Borrowed)] pub enum Pat<'a> { Paren(Box<Pat<'a>>), Ident(Ident<'a>), @@ -15,7 +15,7 @@ impl<'a> Pat<'a> { } } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, IntoOwned, Borrowed)] pub enum DestrPat<'a> { Index { base: Box<DestrPat<'a>>, @@ -44,13 +44,13 @@ impl<'a> DestrPat<'a> { } } -impl<'a> From<&Pat<'a>> for DestrPat<'a> { - fn from(value: &Pat<'a>) -> Self { +impl<'a> From<Pat<'a>> for DestrPat<'a> { + fn from(value: Pat<'a>) -> Self { match value { - Pat::Paren(pat) => DestrPat::Paren(Box::new(pat.as_ref().into())), + Pat::Paren(pat) => DestrPat::Paren(Box::new((*pat).into())), Pat::Ident(ident) => DestrPat::Path(Path { root: PathRoot::Relative, - components: vec![ident.clone()], + components: vec![ident], }), } } diff --git a/crates/rebel-parse/src/ast/typ.rs b/crates/rebel-parse/src/ast/typ.rs index 781962c..5ad7509 100644 --- a/crates/rebel-parse/src/ast/typ.rs +++ b/crates/rebel-parse/src/ast/typ.rs @@ -1,15 +1,17 @@ use std::borrow::Cow; +use derive_into_owned::{Borrowed, IntoOwned}; + use super::Path; -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, IntoOwned, Borrowed)] pub enum Type<'a> { Paren(Box<Type<'a>>), Path(Path<'a>), Literal(Literal<'a>), } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, IntoOwned, Borrowed)] pub enum Literal<'a> { Unit, Tuple(Vec<Type<'a>>), @@ -18,7 +20,7 @@ pub enum Literal<'a> { Struct(Vec<StructField<'a>>), } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, IntoOwned, Borrowed)] pub struct StructField<'a> { pub name: Cow<'a, str>, pub typ: Type<'a>, |