Match patterns

This commit is contained in:
2026-05-07 23:33:27 +02:00
parent bd066e05d1
commit c7ada72029
5 changed files with 16 additions and 6 deletions
+1
View File
@@ -97,6 +97,7 @@ pub enum Pattern {
NamedTuple(Path, Vec<Identifier>), NamedTuple(Path, Vec<Identifier>),
Struct(Path, Vec<Identifier>), Struct(Path, Vec<Identifier>),
Tuple(Vec<Identifier>), Tuple(Vec<Identifier>),
Literal(Literal),
Path(Path), Path(Path),
Id(Identifier), Id(Identifier),
} }
+7 -3
View File
@@ -16,7 +16,7 @@ static_path = { identifier ~ ("::" ~ identifier)* }
tuple_pattern = { "(" ~ (identifier ~ ("," ~ identifier)*)? ~ ")" } tuple_pattern = { "(" ~ (identifier ~ ("," ~ identifier)*)? ~ ")" }
named_tuple_pattern = { static_path ~ "(" ~ (identifier ~ ("," ~ identifier)*)? ~ ")" } named_tuple_pattern = { static_path ~ "(" ~ (identifier ~ ("," ~ identifier)*)? ~ ")" }
struct_pattern = { static_path ~ "{" ~ (identifier ~ ("," ~ identifier)*)? ~ "}" } struct_pattern = { static_path ~ "{" ~ (identifier ~ ("," ~ identifier)*)? ~ "}" }
pattern = _{ named_tuple_pattern | tuple_pattern | struct_pattern | identifier } pattern = _{ named_tuple_pattern | tuple_pattern | struct_pattern | literal | static_path }
export = { "public" } export = { "public" }
mutable = { "mut" } mutable = { "mut" }
@@ -64,7 +64,7 @@ mod_attribute = {
} }
meta = { meta = {
static_path ~ ("=" ~ primary | "(" ~ meta_list? ~ ")")? static_path ~ ("=" ~ literal | "(" ~ meta_list? ~ ")")?
} }
meta_list = { meta_list = {
@@ -164,11 +164,15 @@ macro_inner_v = {
tuple = { "(" ~ (expr ~ ("," ~ expr)*)? ~ ")" } tuple = { "(" ~ (expr ~ ("," ~ expr)*)? ~ ")" }
primary = { literal = {
float float
| integer | integer
| boolean | boolean
| string_lit | string_lit
}
primary = {
literal
| static_path | static_path
| tuple | tuple
} }
+4 -3
View File
@@ -400,6 +400,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for Literal {
match rule { match rule {
Rule::primary => Self::from(inner.next().unwrap()), Rule::primary => Self::from(inner.next().unwrap()),
Rule::literal => Self::from(inner.next().unwrap()),
Rule::integer => Literal::Int(pair.as_str().parse::<i64>().unwrap()), Rule::integer => Literal::Int(pair.as_str().parse::<i64>().unwrap()),
Rule::float => Literal::Float(pair.as_str().parse::<f64>().unwrap()), Rule::float => Literal::Float(pair.as_str().parse::<f64>().unwrap()),
Rule::boolean => Literal::Bool(pair.as_str().parse::<bool>().unwrap()), Rule::boolean => Literal::Bool(pair.as_str().parse::<bool>().unwrap()),
@@ -436,9 +437,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for Expression {
} }
Rule::primary => Expression::from(inner.next().unwrap()), Rule::primary => Expression::from(inner.next().unwrap()),
Rule::static_path => Expression::Path(Path::from(pair)), Rule::static_path => Expression::Path(Path::from(pair)),
Rule::integer | Rule::float | Rule::boolean | Rule::string_lit | Rule::tuple => { Rule::literal => Expression::Literal(Literal::from(pair)),
Expression::Literal(Literal::from(pair))
}
_ => unimplemented!("{rule:#?}"), _ => unimplemented!("{rule:#?}"),
} }
} }
@@ -549,6 +548,8 @@ impl From<pest::iterators::Pair<'_, Rule>> for Pattern {
inner.map(Identifier::from).collect(), inner.map(Identifier::from).collect(),
), ),
Rule::literal => Pattern::Literal(Literal::from(pair)),
Rule::identifier => Pattern::Id(Identifier::from(pair)), Rule::identifier => Pattern::Id(Identifier::from(pair)),
Rule::static_path => Pattern::Path(Path::from(pair)), Rule::static_path => Pattern::Path(Path::from(pair)),
+1
View File
@@ -666,6 +666,7 @@ impl GetRust for Pattern {
match self { match self {
Self::Id(id) => id.get_rust(), Self::Id(id) => id.get_rust(),
Self::Path(path) => path.get_rust(), Self::Path(path) => path.get_rust(),
Self::Literal(lit) => lit.get_rust(),
Self::Struct(path, ids) => format!( Self::Struct(path, ids) => format!(
"{} {{{}}}", "{} {{{}}}",
path.get_rust(), path.get_rust(),
+3
View File
@@ -9,6 +9,9 @@ void main() {
match (MyEnum::Tuple(10, String::new())) { match (MyEnum::Tuple(10, String::new())) {
MyEnum::Tuple(i, _) => { MyEnum::Tuple(i, _) => {
println!("{i}"); println!("{i}");
}
MyEnum::Named => {
} }
_ => {} _ => {}
} }