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>),
Struct(Path, Vec<Identifier>),
Tuple(Vec<Identifier>),
Literal(Literal),
Path(Path),
Id(Identifier),
}
+7 -3
View File
@@ -16,7 +16,7 @@ static_path = { identifier ~ ("::" ~ identifier)* }
tuple_pattern = { "(" ~ (identifier ~ ("," ~ identifier)*)? ~ ")" }
named_tuple_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" }
mutable = { "mut" }
@@ -64,7 +64,7 @@ mod_attribute = {
}
meta = {
static_path ~ ("=" ~ primary | "(" ~ meta_list? ~ ")")?
static_path ~ ("=" ~ literal | "(" ~ meta_list? ~ ")")?
}
meta_list = {
@@ -164,11 +164,15 @@ macro_inner_v = {
tuple = { "(" ~ (expr ~ ("," ~ expr)*)? ~ ")" }
primary = {
literal = {
float
| integer
| boolean
| string_lit
}
primary = {
literal
| static_path
| tuple
}
+4 -3
View File
@@ -400,6 +400,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for Literal {
match rule {
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::float => Literal::Float(pair.as_str().parse::<f64>().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::static_path => Expression::Path(Path::from(pair)),
Rule::integer | Rule::float | Rule::boolean | Rule::string_lit | Rule::tuple => {
Expression::Literal(Literal::from(pair))
}
Rule::literal => Expression::Literal(Literal::from(pair)),
_ => unimplemented!("{rule:#?}"),
}
}
@@ -549,6 +548,8 @@ impl From<pest::iterators::Pair<'_, Rule>> for Pattern {
inner.map(Identifier::from).collect(),
),
Rule::literal => Pattern::Literal(Literal::from(pair)),
Rule::identifier => Pattern::Id(Identifier::from(pair)),
Rule::static_path => Pattern::Path(Path::from(pair)),