Working mut pattern
This commit is contained in:
@@ -243,30 +243,30 @@ impl GenRust for Pattern {
|
||||
}
|
||||
Self::Literal(lit) => lit.gen_rust(ctx, cg),
|
||||
|
||||
Self::Struct(path, ids) => {
|
||||
Self::Struct(path, inner) => {
|
||||
cg.add(&path.get_rust());
|
||||
cg.add(" {");
|
||||
for id in ids {
|
||||
cg.add(&id.get_rust());
|
||||
for pat in inner {
|
||||
pat.gen_rust(ctx, cg);
|
||||
cg.add(",");
|
||||
}
|
||||
cg.add("}");
|
||||
}
|
||||
|
||||
Self::NamedTuple(path, ids) => {
|
||||
Self::NamedTuple(path, inner) => {
|
||||
cg.add(&path.get_rust());
|
||||
cg.add(" (");
|
||||
for id in ids {
|
||||
cg.add(&id.get_rust());
|
||||
for pat in inner {
|
||||
pat.gen_rust(ctx, cg);
|
||||
cg.add(",");
|
||||
}
|
||||
cg.add(")");
|
||||
}
|
||||
|
||||
Self::Tuple(ids) => {
|
||||
Self::Tuple(inner) => {
|
||||
cg.add("(");
|
||||
for id in ids {
|
||||
cg.add(&id.get_rust());
|
||||
for pat in inner {
|
||||
pat.gen_rust(ctx, cg);
|
||||
cg.add(",");
|
||||
}
|
||||
cg.add(")");
|
||||
|
||||
@@ -32,9 +32,9 @@ pub enum Literal {
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum Pattern {
|
||||
NamedTuple(Path, Vec<Identifier>),
|
||||
Struct(Path, Vec<Identifier>),
|
||||
Tuple(Vec<Identifier>),
|
||||
NamedTuple(Path, Vec<Box<Pattern>>),
|
||||
Struct(Path, Vec<Box<Pattern>>),
|
||||
Tuple(Vec<Box<Pattern>>),
|
||||
Literal(Literal),
|
||||
Path(bool, Path),
|
||||
}
|
||||
|
||||
@@ -135,7 +135,7 @@ literal = {
|
||||
// ======================================================
|
||||
|
||||
multi_pat_template = _{
|
||||
(identifier ~ ("," ~ identifier)*)?
|
||||
(pattern ~ ("," ~ pattern)*)?
|
||||
}
|
||||
|
||||
tuple_pattern = {
|
||||
|
||||
@@ -7,7 +7,7 @@ use crate::{
|
||||
Rule,
|
||||
ast::*,
|
||||
ast_ensure, ast_expr,
|
||||
error::{AstError, AstResult, IntoErr, collect_recovered},
|
||||
error::{AstError, AstResult, IntoErr, collect_recovered, collect_recovered_map},
|
||||
parser::{consume_rule, listen_rule},
|
||||
};
|
||||
|
||||
@@ -60,16 +60,18 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Pattern {
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
match rule {
|
||||
Rule::tuple_pattern => ast_expr!(Pattern::Tuple(collect_recovered(pair.into_inner()))),
|
||||
Rule::tuple_pattern => ast_expr!(Pattern::Tuple(collect_recovered_map(inner, |v| {
|
||||
Self::try_from(v).map(Box::new)
|
||||
}))),
|
||||
|
||||
Rule::named_tuple_pattern => ast_expr!(Pattern::NamedTuple(
|
||||
Path::try_from(inner.next().unwrap()),
|
||||
collect_recovered(inner),
|
||||
collect_recovered_map(inner, |v| Self::try_from(v).map(Box::new)),
|
||||
)),
|
||||
|
||||
Rule::struct_pattern => ast_expr!(Pattern::Struct(
|
||||
Path::try_from(inner.next().unwrap()),
|
||||
collect_recovered(inner),
|
||||
collect_recovered_map(inner, |v| Self::try_from(v).map(Box::new)),
|
||||
)),
|
||||
|
||||
Rule::literal => ast_expr!(Pattern::Literal(pair.try_into())),
|
||||
|
||||
Reference in New Issue
Block a user