Etc pattern
This commit is contained in:
@@ -236,6 +236,8 @@ impl GetRust for TypeExprKind {
|
|||||||
impl GenRust for Pattern {
|
impl GenRust for Pattern {
|
||||||
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
|
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
|
||||||
match self {
|
match self {
|
||||||
|
Self::Etc => cg.add(".."),
|
||||||
|
Self::Literal(lit) => lit.gen_rust(ctx, cg),
|
||||||
Self::Path(mutable, path) => {
|
Self::Path(mutable, path) => {
|
||||||
if *mutable {
|
if *mutable {
|
||||||
cg.add("mut ");
|
cg.add("mut ");
|
||||||
@@ -243,19 +245,26 @@ impl GenRust for Pattern {
|
|||||||
|
|
||||||
cg.add(&path.get_rust())
|
cg.add(&path.get_rust())
|
||||||
}
|
}
|
||||||
Self::Literal(lit) => lit.gen_rust(ctx, cg),
|
|
||||||
|
|
||||||
Self::Struct(path, inner) => {
|
Self::Struct(path, inner) => {
|
||||||
cg.add(&path.get_rust());
|
cg.add(&path.get_rust());
|
||||||
cg.add(" {");
|
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());
|
cg.add(&name.get_rust());
|
||||||
if let Some(pat) = pat {
|
if let Some(pat) = pat {
|
||||||
cg.add(": ");
|
cg.add(": ");
|
||||||
pat.gen_rust(ctx, cg);
|
pat.gen_rust(ctx, cg);
|
||||||
}
|
}
|
||||||
cg.add(",");
|
} else {
|
||||||
|
cg.add("..");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cg.add("}");
|
cg.add("}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,10 +33,11 @@ pub enum Literal {
|
|||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub enum Pattern {
|
pub enum Pattern {
|
||||||
NamedTuple(Path, Vec<Box<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>>),
|
Tuple(Vec<Box<Pattern>>),
|
||||||
Literal(Literal),
|
Literal(Literal),
|
||||||
Path(bool, Path),
|
Path(bool, Path),
|
||||||
|
Etc,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
|||||||
@@ -134,12 +134,14 @@ literal = {
|
|||||||
// PATTERNS
|
// PATTERNS
|
||||||
// ======================================================
|
// ======================================================
|
||||||
|
|
||||||
|
etc_pattern = { ".." }
|
||||||
|
|
||||||
multi_pat_template = _{
|
multi_pat_template = _{
|
||||||
(pattern ~ ("," ~ pattern)*)?
|
((pattern | etc_pattern) ~ ("," ~ (pattern | etc_pattern))*)?
|
||||||
}
|
}
|
||||||
|
|
||||||
tuple_pattern = {
|
tuple_pattern = {
|
||||||
"[" ~ pattern ~ "," ~ multi_pat_template ~ "]"
|
"[" ~ (pattern | etc_pattern) ~ "," ~ multi_pat_template ~ "]"
|
||||||
}
|
}
|
||||||
|
|
||||||
named_tuple_pattern = {
|
named_tuple_pattern = {
|
||||||
@@ -151,7 +153,7 @@ struct_destruct = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct_pattern = {
|
struct_pattern = {
|
||||||
static_path ~ "{" ~ (struct_destruct ~ ("," ~ struct_destruct)*)? ~ "}"
|
static_path ~ "{" ~ ((struct_destruct | etc_pattern) ~ ("," ~ (struct_destruct | etc_pattern))*)? ~ "}"
|
||||||
}
|
}
|
||||||
|
|
||||||
path_pattern = {
|
path_pattern = {
|
||||||
|
|||||||
@@ -72,14 +72,19 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Pattern {
|
|||||||
Rule::struct_pattern => ast_expr!(Pattern::Struct(
|
Rule::struct_pattern => ast_expr!(Pattern::Struct(
|
||||||
Path::try_from(inner.next().unwrap()),
|
Path::try_from(inner.next().unwrap()),
|
||||||
collect_recovered_map(inner, |v| {
|
collect_recovered_map(inner, |v| {
|
||||||
let mut inner = v.into_inner();
|
if v.as_rule() == Rule::etc_pattern {
|
||||||
ast_expr!((
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut inner = dbg!(v).into_inner();
|
||||||
|
Some(ast_expr!((
|
||||||
Identifier::try_from(inner.next().unwrap()),
|
Identifier::try_from(inner.next().unwrap()),
|
||||||
inner
|
inner
|
||||||
.next()
|
.next()
|
||||||
.map(|v| Self::try_from(v).map(Box::new).get_map(Box::new))
|
.map(|v| Self::try_from(v).map(Box::new).get_map(Box::new))
|
||||||
.transpose()
|
.transpose()
|
||||||
))
|
)))
|
||||||
|
.transpose()
|
||||||
}),
|
}),
|
||||||
)),
|
)),
|
||||||
|
|
||||||
@@ -90,6 +95,8 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Pattern {
|
|||||||
inner.next().unwrap().try_into()
|
inner.next().unwrap().try_into()
|
||||||
)),
|
)),
|
||||||
|
|
||||||
|
Rule::etc_pattern => Ok(Pattern::Etc),
|
||||||
|
|
||||||
_ => AstError::bug_unimplemented(pair),
|
_ => AstError::bug_unimplemented(pair),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
class Bro {
|
|
||||||
constructor([str*, str*] [idk, bro]) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
test_package::hello();
|
test_package::hello();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user