Pattern AST and grammar
This commit is contained in:
+2
-1
@@ -94,8 +94,9 @@ pub enum TopLevelKind {
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum Pattern {
|
||||
Tuple(Path, Vec<Identifier>),
|
||||
NamedTuple(Path, Vec<Identifier>),
|
||||
Struct(Path, Vec<Identifier>),
|
||||
Tuple(Vec<Identifier>),
|
||||
Path(Path),
|
||||
Id(Identifier),
|
||||
}
|
||||
|
||||
@@ -13,9 +13,10 @@ inner_str = @{ (!"\"" ~ ("\\\"" | ANY))* }
|
||||
static_path = { identifier ~ ("::" ~ identifier)* }
|
||||
|
||||
// pattern
|
||||
tuple_pattern = { static_path ~ "(" ~ (identifier ~ ("," ~ identifier)*)? ~ ")" }
|
||||
struct_pattern = { static_path ~ "{" ~ (identifier ~ ("," ~ identifier)*)? ~ "}" }
|
||||
pattern = _{ tuple_pattern | struct_pattern | 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 }
|
||||
|
||||
export = { "public" }
|
||||
mutable = { "mut" }
|
||||
|
||||
@@ -518,6 +518,33 @@ impl From<pest::iterators::Pair<'_, Rule>> for VarDeclStmt {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for Pattern {
|
||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||
let rule = pair.as_rule();
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
match rule {
|
||||
Rule::tuple_pattern => Pattern::Tuple(inner.map(Identifier::from).collect()),
|
||||
|
||||
Rule::named_tuple_pattern => Pattern::NamedTuple(
|
||||
Path::from(inner.next().unwrap()),
|
||||
inner.map(Identifier::from).collect(),
|
||||
),
|
||||
|
||||
Rule::struct_pattern => Pattern::Struct(
|
||||
Path::from(inner.next().unwrap()),
|
||||
inner.map(Identifier::from).collect(),
|
||||
),
|
||||
|
||||
Rule::identifier => Pattern::Id(Identifier::from(inner.next().unwrap())),
|
||||
|
||||
Rule::static_path => Pattern::Path(Path::from(inner.next().unwrap())),
|
||||
|
||||
_ => unimplemented!("{rule:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for VarDecl {
|
||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||
match pair.as_rule() {
|
||||
|
||||
+2
-1
@@ -4,5 +4,6 @@ use <my_class::MyClass>;
|
||||
void main() {
|
||||
var my_class = new MyClass("World");
|
||||
my_class.greet();
|
||||
var x = 1 + 2 + 3;
|
||||
|
||||
var (x, y) = (10, 20);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user