Pattern AST and grammar

This commit is contained in:
2026-05-07 22:10:26 +02:00
parent c51ff01ce8
commit a6fb70eebf
4 changed files with 35 additions and 5 deletions
+2 -1
View File
@@ -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),
}
+3 -2
View File
@@ -13,9 +13,10 @@ inner_str = @{ (!"\"" ~ ("\\\"" | ANY))* }
static_path = { identifier ~ ("::" ~ identifier)* }
// pattern
tuple_pattern = { static_path ~ "(" ~ (identifier ~ ("," ~ identifier)*)? ~ ")" }
tuple_pattern = { "(" ~ (identifier ~ ("," ~ identifier)*)? ~ ")" }
named_tuple_pattern = { static_path ~ "(" ~ (identifier ~ ("," ~ identifier)*)? ~ ")" }
struct_pattern = { static_path ~ "{" ~ (identifier ~ ("," ~ identifier)*)? ~ "}" }
pattern = _{ tuple_pattern | struct_pattern | identifier }
pattern = _{ named_tuple_pattern | tuple_pattern | struct_pattern | identifier }
export = { "public" }
mutable = { "mut" }
+27
View File
@@ -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
View File
@@ -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);
}