diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2024-05-01 01:28:06 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2024-05-01 10:42:48 +0200 |
commit | 9d3b1c1c75cc44a67fc68d81fbe284b42fb466ec (patch) | |
tree | 7e2b94d33be8f8c9c464359e25d8126b0528d685 /crates/rebel-parse | |
parent | d267148ba107e8f7e622e1722c03c80abb51a65f (diff) | |
download | rebel-9d3b1c1c75cc44a67fc68d81fbe284b42fb466ec.tar rebel-9d3b1c1c75cc44a67fc68d81fbe284b42fb466ec.zip |
rebel-parse: implement pattern validation
Diffstat (limited to 'crates/rebel-parse')
-rw-r--r-- | crates/rebel-parse/src/ast/mod.rs | 13 | ||||
-rw-r--r-- | crates/rebel-parse/src/ast/pat.rs | 24 |
2 files changed, 32 insertions, 5 deletions
diff --git a/crates/rebel-parse/src/ast/mod.rs b/crates/rebel-parse/src/ast/mod.rs index 1afa9a1..0598cbd 100644 --- a/crates/rebel-parse/src/ast/mod.rs +++ b/crates/rebel-parse/src/ast/mod.rs @@ -102,16 +102,18 @@ impl<'a> BlockStmt<'a> { pub fn validate(&self) -> Result<(), ValidationError> { match self { - BlockStmt::Let { dest: _, expr } => { - // TODO: Destination validation + BlockStmt::Let { dest, expr } => { + let TypedPat { pat, typ: _ } = dest.as_ref(); + pat.validate()?; if let Some(expr) = expr { expr.validate()?; } Ok(()) } - BlockStmt::Assign { dest: _, expr } => { - // TODO: Destination validation - expr.validate() + BlockStmt::Assign { dest, expr } => { + dest.validate()?; + expr.validate()?; + Ok(()) } BlockStmt::Expr { expr } => expr.validate(), BlockStmt::Empty => Ok(()), @@ -154,4 +156,5 @@ pub struct Ident<'a> { pub enum ValidationError { DuplicateKey, NeedsParens, + InvalidLet, } diff --git a/crates/rebel-parse/src/ast/pat.rs b/crates/rebel-parse/src/ast/pat.rs index d1baf68..1a48372 100644 --- a/crates/rebel-parse/src/ast/pat.rs +++ b/crates/rebel-parse/src/ast/pat.rs @@ -6,6 +6,15 @@ pub enum Pat<'a> { Ident(Ident<'a>), } +impl<'a> Pat<'a> { + pub fn validate(&self) -> Result<(), ValidationError> { + match self { + Pat::Paren(pat) => pat.validate(), + Pat::Ident(_) => Ok(()), + } + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum DestrPat<'a> { Index { @@ -19,3 +28,18 @@ pub enum DestrPat<'a> { Paren(Box<DestrPat<'a>>), Path(Path<'a>), } + +impl<'a> DestrPat<'a> { + pub fn validate(&self) -> Result<(), ValidationError> { + match self { + DestrPat::Index { pat, index } => { + pat.validate()?; + index.validate()?; + Ok(()) + } + DestrPat::Field { pat, field: _ } => pat.validate(), + DestrPat::Paren(pat) => pat.validate(), + DestrPat::Path(_) => Ok(()), + } + } +} |