diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index e3d0658..97e1613 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -192,7 +192,7 @@ attributes = { // DECLARATION HELPERS // ====================================================== var_decl = { - "let" ~ WHITESPACE ~ pattern ~ type_expr + "let" ~ WHITESPACE ~ pattern ~ type_expr? } field = { @@ -200,7 +200,7 @@ field = { } param = { - pattern ~ type_expr + pattern ~ type_expr? } field_list = { diff --git a/crates/parser/src/parser/common/decl.rs b/crates/parser/src/parser/common/decl.rs index b0af0e0..7a07fb9 100644 --- a/crates/parser/src/parser/common/decl.rs +++ b/crates/parser/src/parser/common/decl.rs @@ -2,7 +2,7 @@ use crate::{ Rule, ast::*, ast_expr, - error::{AstError, IntoErr}, + error::{AstError, IntoErr, collect_recovered}, }; impl<'a> TryFrom> for VarDeclStmt { @@ -48,25 +48,12 @@ impl<'a> TryFrom> for VarDecl { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { match pair.as_rule() { - Rule::var_decl => { + Rule::var_decl | Rule::param => { 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 { - type_: type_, - name: name, + name: Pattern::try_from(inner.next().unwrap()), + type_: inner.next().map(TypeExpr::try_from).transpose(), }) } @@ -85,8 +72,8 @@ impl<'a> TryFrom> for FieldDecl { ast_expr!(FieldDecl { visibility: Visibility::try_from(&mut inner), - type_: TypeExpr::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> for FieldDecl { } } } + +impl<'a> TryFrom> for ParamList { + type Error = AstError<'a, Self>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { + Ok(ParamList(collect_recovered(pair.into_inner()).get()?)) + } +} diff --git a/crates/parser/src/parser/items/function.rs b/crates/parser/src/parser/items/function.rs index 248ef3a..d44d00d 100644 --- a/crates/parser/src/parser/items/function.rs +++ b/crates/parser/src/parser/items/function.rs @@ -2,7 +2,7 @@ use crate::{ Rule, ast::*, ast_ensure, ast_expr, - error::{AstError, AstResult, IntoErr, collect_recovered}, + error::{AstError, AstResult, IntoErr}, parser::{consume_rule, listen_rule}, }; @@ -64,11 +64,3 @@ impl<'a> TryFrom> for FunctionDecl { }) } } - -impl<'a> TryFrom> for ParamList { - type Error = AstError<'a, Self>; - - fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { - Ok(ParamList(collect_recovered(pair.into_inner()).get()?)) - } -} diff --git a/mist-test/src/main.mist b/mist-test/src/main.mist index 05a37e9..0e319b9 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -1 +1,9 @@ -void main() {} \ No newline at end of file +void main() {} + +struct MyStruct<'a> { + name *'a String +} + +i32 add(a i32, b i32) { + a + b +}