Etc pattern

This commit is contained in:
2026-06-04 11:01:28 +02:00
parent 7a5e03afd6
commit fc4dd0aa3c
5 changed files with 34 additions and 19 deletions
+13 -4
View File
@@ -236,6 +236,8 @@ impl GetRust for TypeExprKind {
impl GenRust for Pattern {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
match self {
Self::Etc => cg.add(".."),
Self::Literal(lit) => lit.gen_rust(ctx, cg),
Self::Path(mutable, path) => {
if *mutable {
cg.add("mut ");
@@ -243,19 +245,26 @@ impl GenRust for Pattern {
cg.add(&path.get_rust())
}
Self::Literal(lit) => lit.gen_rust(ctx, cg),
Self::Struct(path, inner) => {
cg.add(&path.get_rust());
cg.add(" {");
for (name, pat) in inner {
for (idx, i) in inner.iter().enumerate() {
if idx > 0 {
cg.add(", ");
}
if let Some((name, pat)) = i {
cg.add(&name.get_rust());
if let Some(pat) = pat {
cg.add(": ");
pat.gen_rust(ctx, cg);
}
cg.add(",");
} else {
cg.add("..");
}
}
cg.add("}");
}
+2 -1
View File
@@ -33,10 +33,11 @@ pub enum Literal {
#[derive(Debug, Clone, Serialize)]
pub enum Pattern {
NamedTuple(Path, Vec<Box<Pattern>>),
Struct(Path, Vec<(Identifier, Option<Box<Pattern>>)>),
Struct(Path, Vec<Option<(Identifier, Option<Box<Pattern>>)>>),
Tuple(Vec<Box<Pattern>>),
Literal(Literal),
Path(bool, Path),
Etc,
}
#[derive(Debug, Clone, Serialize)]
+5 -3
View File
@@ -134,12 +134,14 @@ literal = {
// PATTERNS
// ======================================================
etc_pattern = { ".." }
multi_pat_template = _{
(pattern ~ ("," ~ pattern)*)?
((pattern | etc_pattern) ~ ("," ~ (pattern | etc_pattern))*)?
}
tuple_pattern = {
"[" ~ pattern ~ "," ~ multi_pat_template ~ "]"
"[" ~ (pattern | etc_pattern) ~ "," ~ multi_pat_template ~ "]"
}
named_tuple_pattern = {
@@ -151,7 +153,7 @@ struct_destruct = {
}
struct_pattern = {
static_path ~ "{" ~ (struct_destruct ~ ("," ~ struct_destruct)*)? ~ "}"
static_path ~ "{" ~ ((struct_destruct | etc_pattern) ~ ("," ~ (struct_destruct | etc_pattern))*)? ~ "}"
}
path_pattern = {
+10 -3
View File
@@ -72,14 +72,19 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Pattern {
Rule::struct_pattern => ast_expr!(Pattern::Struct(
Path::try_from(inner.next().unwrap()),
collect_recovered_map(inner, |v| {
let mut inner = v.into_inner();
ast_expr!((
if v.as_rule() == Rule::etc_pattern {
return Ok(None);
}
let mut inner = dbg!(v).into_inner();
Some(ast_expr!((
Identifier::try_from(inner.next().unwrap()),
inner
.next()
.map(|v| Self::try_from(v).map(Box::new).get_map(Box::new))
.transpose()
))
)))
.transpose()
}),
)),
@@ -90,6 +95,8 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Pattern {
inner.next().unwrap().try_into()
)),
Rule::etc_pattern => Ok(Pattern::Etc),
_ => AstError::bug_unimplemented(pair),
}
}
-4
View File
@@ -1,7 +1,3 @@
class Bro {
constructor([str*, str*] [idk, bro]) {}
}
void main() {
test_package::hello();
}