diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2024-05-05 23:17:02 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2024-05-05 23:17:02 +0200 |
commit | d0c4f8aaedd31e1f4a41b3be877f45f62c48ee63 (patch) | |
tree | fbb2f4a9d4fc6fef59c38ac9f16abd367ce347e5 /crates/rebel-parse | |
parent | 6718f07f55b742c66b1ab0ed2d8e32d05b2a6f22 (diff) | |
download | rebel-d0c4f8aaedd31e1f4a41b3be877f45f62c48ee63.tar rebel-d0c4f8aaedd31e1f4a41b3be877f45f62c48ee63.zip |
rebel-parse: add support for struct definition shorthand syntax
Diffstat (limited to 'crates/rebel-parse')
-rw-r--r-- | crates/rebel-parse/src/ast/expr.rs | 15 | ||||
-rw-r--r-- | crates/rebel-parse/src/grammar/recipe.rs | 4 |
2 files changed, 16 insertions, 3 deletions
diff --git a/crates/rebel-parse/src/ast/expr.rs b/crates/rebel-parse/src/ast/expr.rs index 0a47cb2..0a9ae3f 100644 --- a/crates/rebel-parse/src/ast/expr.rs +++ b/crates/rebel-parse/src/ast/expr.rs @@ -1,4 +1,4 @@ -use super::{Block, DestrPat, Ident, Path, ValidationError}; +use super::{Block, DestrPat, Ident, Path, PathRoot, ValidationError}; use crate::token; use rustc_hash::FxHashSet; @@ -281,6 +281,19 @@ pub struct StructField<'a> { pub value: Expr<'a>, } +impl<'a> StructField<'a> { + pub(crate) fn new(field: Ident<'a>, value: Option<Expr<'a>>) -> Self { + let value = value.unwrap_or(Expr::Path(Path { + root: PathRoot::Relative, + components: vec![field], + })); + StructField { + name: field.name, + value, + } + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum OpUnary { Not, diff --git a/crates/rebel-parse/src/grammar/recipe.rs b/crates/rebel-parse/src/grammar/recipe.rs index 1c417d3..5319498 100644 --- a/crates/rebel-parse/src/grammar/recipe.rs +++ b/crates/rebel-parse/src/grammar/recipe.rs @@ -194,8 +194,8 @@ peg::parser! { } rule struct_field() -> expr::StructField<'a> - = field:field() p(':') value:expr() { - expr::StructField { name: field.name, value } + = field:field() value:tagged(<p(':')>, <expr()>)? { + expr::StructField::new(field, value) } rule path() -> ast::Path<'a> |