Update: Decleration Helpers, parser #65

This commit is contained in:
2026-06-05 17:52:04 +02:00
parent 56c22eb1ed
commit ac9ee5a4b6
4 changed files with 25 additions and 30 deletions
+2 -2
View File
@@ -192,7 +192,7 @@ attributes = {
// DECLARATION HELPERS // DECLARATION HELPERS
// ====================================================== // ======================================================
var_decl = { var_decl = {
"let" ~ WHITESPACE ~ pattern ~ type_expr "let" ~ WHITESPACE ~ pattern ~ type_expr?
} }
field = { field = {
@@ -200,7 +200,7 @@ field = {
} }
param = { param = {
pattern ~ type_expr pattern ~ type_expr?
} }
field_list = { field_list = {
+13 -18
View File
@@ -2,7 +2,7 @@ use crate::{
Rule, Rule,
ast::*, ast::*,
ast_expr, ast_expr,
error::{AstError, IntoErr}, error::{AstError, IntoErr, collect_recovered},
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt {
@@ -48,25 +48,12 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDecl {
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() { match pair.as_rule() {
Rule::var_decl => { Rule::var_decl | Rule::param => {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
let type_ = inner
.next()
.and_then(|pair| {
if pair.as_rule() == Rule::let_kw {
None
} else {
Some(TypeExpr::try_from(pair))
}
})
.transpose();
let name = Pattern::try_from(inner.next().unwrap());
ast_expr!(VarDecl { ast_expr!(VarDecl {
type_: type_, name: Pattern::try_from(inner.next().unwrap()),
name: name, type_: inner.next().map(TypeExpr::try_from).transpose(),
}) })
} }
@@ -85,8 +72,8 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FieldDecl {
ast_expr!(FieldDecl { ast_expr!(FieldDecl {
visibility: Visibility::try_from(&mut inner), visibility: Visibility::try_from(&mut inner),
type_: TypeExpr::try_from(inner.next().unwrap()),
name: Identifier::try_from(inner.next().unwrap()), name: Identifier::try_from(inner.next().unwrap()),
type_: TypeExpr::try_from(inner.next().unwrap()),
}) })
} }
@@ -94,3 +81,11 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FieldDecl {
} }
} }
} }
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ParamList {
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
Ok(ParamList(collect_recovered(pair.into_inner()).get()?))
}
}
+1 -9
View File
@@ -2,7 +2,7 @@ use crate::{
Rule, Rule,
ast::*, ast::*,
ast_ensure, ast_expr, ast_ensure, ast_expr,
error::{AstError, AstResult, IntoErr, collect_recovered}, error::{AstError, AstResult, IntoErr},
parser::{consume_rule, listen_rule}, parser::{consume_rule, listen_rule},
}; };
@@ -64,11 +64,3 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
}) })
} }
} }
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ParamList {
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
Ok(ParamList(collect_recovered(pair.into_inner()).get()?))
}
}
+8
View File
@@ -1 +1,9 @@
void main() {} void main() {}
struct MyStruct<'a> {
name *'a String
}
i32 add(a i32, b i32) {
a + b
}