summaryrefslogtreecommitdiffstats
path: root/crates/rebel-parse/src/grammar/recipe.rs
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2024-04-29 19:02:34 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2024-04-29 19:02:34 +0200
commit684a6e62b40389dad5875c86c8d50658d676128d (patch)
treec8665f787b38d56ec797b7f48e840f94f6a68c6e /crates/rebel-parse/src/grammar/recipe.rs
parent8226a8ec820987c99c24b5f6e76a8b379f9efa0a (diff)
downloadrebel-684a6e62b40389dad5875c86c8d50658d676128d.tar
rebel-684a6e62b40389dad5875c86c8d50658d676128d.zip
rebel-parse: ast: typ: allow to distinguish between free and dynamic array length
Diffstat (limited to 'crates/rebel-parse/src/grammar/recipe.rs')
-rw-r--r--crates/rebel-parse/src/grammar/recipe.rs18
1 files changed, 13 insertions, 5 deletions
diff --git a/crates/rebel-parse/src/grammar/recipe.rs b/crates/rebel-parse/src/grammar/recipe.rs
index 893d4b7..73ddb2e 100644
--- a/crates/rebel-parse/src/grammar/recipe.rs
+++ b/crates/rebel-parse/src/grammar/recipe.rs
@@ -66,16 +66,24 @@ peg::parser! {
/ p('(') elements:(typ() ** p(',')) p(',')? p(')') {
typ::Literal::Tuple(elements)
}
- / p('[') typ:typ() len:tagged(<p(';')>, <number()>)? p(']') { ?
- let len = len
- .map(|n| u32::try_from(n).or(Err("Invalid array length")))
- .transpose()?;
- Ok(typ::Literal::Array(Box::new(typ), len))
+ / p('[') typ:typ() len:array_len() p(']') {
+ typ::Literal::Array(Box::new(typ), len)
}
/ p('{') entries:delimited(<struct_field_typ()>, <p(',')>) p('}') {
typ::Literal::Struct(entries)
}
+ rule array_len() -> typ::ArrayLen
+ = p(';') len:number() { ?
+ Ok(typ::ArrayLen::Fixed(len.try_into().or(Err("Invalid array length"))?))
+ }
+ / p(';') [Token::Ident(name) if *name == "_"] {
+ typ::ArrayLen::Free
+ }
+ / {
+ typ::ArrayLen::Dynamic
+ }
+
rule pat() -> Pat<'a>
= path:path() { Pat::Path(path) }