Match patterns
This commit is contained in:
@@ -97,6 +97,7 @@ pub enum Pattern {
|
||||
NamedTuple(Path, Vec<Identifier>),
|
||||
Struct(Path, Vec<Identifier>),
|
||||
Tuple(Vec<Identifier>),
|
||||
Literal(Literal),
|
||||
Path(Path),
|
||||
Id(Identifier),
|
||||
}
|
||||
|
||||
@@ -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
@@ -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)),
|
||||
|
||||
@@ -666,6 +666,7 @@ impl GetRust for Pattern {
|
||||
match self {
|
||||
Self::Id(id) => id.get_rust(),
|
||||
Self::Path(path) => path.get_rust(),
|
||||
Self::Literal(lit) => lit.get_rust(),
|
||||
Self::Struct(path, ids) => format!(
|
||||
"{} {{{}}}",
|
||||
path.get_rust(),
|
||||
|
||||
@@ -9,6 +9,9 @@ void main() {
|
||||
match (MyEnum::Tuple(10, String::new())) {
|
||||
MyEnum::Tuple(i, _) => {
|
||||
println!("{i}");
|
||||
}
|
||||
MyEnum::Named => {
|
||||
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user