diff --git a/crates/aiken-lang/src/parser.rs b/crates/aiken-lang/src/parser.rs index 99d4c633..e9163e21 100644 --- a/crates/aiken-lang/src/parser.rs +++ b/crates/aiken-lang/src/parser.rs @@ -1028,7 +1028,7 @@ pub fn expr_parser( }, ); - let assert_parser = just(Token::Assert) + let assert_parser = just(Token::Expect) .ignore_then(pattern_parser()) .then(just(Token::Colon).ignore_then(type_parser()).or_not()) .then_ignore(just(Token::Equal)) diff --git a/crates/aiken-lang/src/parser/lexer.rs b/crates/aiken-lang/src/parser/lexer.rs index 84aaa4d2..9b390364 100644 --- a/crates/aiken-lang/src/parser/lexer.rs +++ b/crates/aiken-lang/src/parser/lexer.rs @@ -87,7 +87,8 @@ pub fn lexer() -> impl Parser, Error = ParseError> { "trace" => Token::Trace, "error" => Token::ErrorTerm, "as" => Token::As, - "assert" => Token::Assert, + "assert" => Token::Expect, + "expect" => Token::Expect, "const" => Token::Const, "fn" => Token::Fn, "test" => Token::Test, diff --git a/crates/aiken-lang/src/parser/token.rs b/crates/aiken-lang/src/parser/token.rs index 3106326d..e94de71c 100644 --- a/crates/aiken-lang/src/parser/token.rs +++ b/crates/aiken-lang/src/parser/token.rs @@ -60,7 +60,6 @@ pub enum Token { NewLine, // Keywords (alphabetically): As, - Assert, Const, Fn, If, @@ -141,7 +140,6 @@ impl fmt::Display for Token { Token::EmptyLine => "EMPTYLINE", Token::NewLine => "NEWLINE", Token::As => "as", - Token::Assert => "assert", Token::Expect => "expect", Token::When => "when", Token::Is => "is", diff --git a/crates/aiken-lang/src/tests/parser.rs b/crates/aiken-lang/src/tests/parser.rs index 845c9393..e71f882e 100644 --- a/crates/aiken-lang/src/tests/parser.rs +++ b/crates/aiken-lang/src/tests/parser.rs @@ -324,6 +324,77 @@ fn empty_function() { ) } +#[test] +fn expect() { + let code = indoc! {r#" + pub fn run() { + expect Some(x) = something.field + x.other_field + } + "#}; + + assert_definitions( + code, + vec![ast::UntypedDefinition::Fn(Function { + arguments: vec![], + body: expr::UntypedExpr::Sequence { + location: Span::new((), 19..69), + expressions: vec![ + expr::UntypedExpr::Assignment { + location: Span::new((), 19..51), + value: expr::UntypedExpr::FieldAccess { + location: Span::new((), 36..51), + label: "field".to_string(), + container: expr::UntypedExpr::Var { + location: Span::new((), 36..45), + name: "something".to_string(), + } + .into(), + } + .into(), + pattern: ast::Pattern::Constructor { + is_record: false, + location: Span::new((), 26..33), + name: "Some".to_string(), + arguments: vec![ast::CallArg { + label: None, + location: Span::new((), 31..32), + value: ast::Pattern::Var { + location: Span::new((), 31..32), + name: "x".to_string(), + }, + }], + module: None, + constructor: (), + with_spread: false, + tipo: (), + }, + kind: ast::AssignmentKind::Assert, + annotation: None, + }, + expr::UntypedExpr::FieldAccess { + location: Span::new((), 56..69), + label: "other_field".to_string(), + container: expr::UntypedExpr::Var { + location: Span::new((), 56..57), + name: "x".to_string(), + } + .into(), + }, + ], + }, + doc: None, + + location: Span::new((), 0..12), + name: "run".to_string(), + public: true, + return_annotation: None, + return_type: (), + end_position: 70, + })], + ) +} + #[test] fn plus_binop() { let code = indoc! {r#" diff --git a/crates/aiken-lang/src/tipo/infer.rs b/crates/aiken-lang/src/tipo/infer.rs index 54037cfd..771c5d0c 100644 --- a/crates/aiken-lang/src/tipo/infer.rs +++ b/crates/aiken-lang/src/tipo/infer.rs @@ -511,8 +511,11 @@ fn validate_module_name(name: &str) -> Result<(), Error> { fn str_to_keyword(word: &str) -> Option { // Alphabetical keywords: match word { + "assert" => Some(Token::Expect), + "expect" => Some(Token::Expect), + "else" => Some(Token::Else), + "is" => Some(Token::Is), "as" => Some(Token::As), - "assert" => Some(Token::Assert), "when" => Some(Token::When), "const" => Some(Token::Const), "fn" => Some(Token::Fn),