From ea487478250ac2f938cdc41a3b9542ccf0dbde98 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Wed, 7 Dec 2022 16:21:16 +0100 Subject: [PATCH] Extend parser for 'test' keyword. --- crates/lang/src/format.rs | 23 +++++++++++++++++++++-- crates/lang/src/parser.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/crates/lang/src/format.rs b/crates/lang/src/format.rs index 71e76d10..9a60bfca 100644 --- a/crates/lang/src/format.rs +++ b/crates/lang/src/format.rs @@ -215,7 +215,24 @@ impl<'comments> Formatter<'comments> { return_annotation, end_position, .. - }) => self.definition_fn(public, name, args, return_annotation, body, *end_position), + }) => self.definition_fn( + public, + "fn", + name, + args, + return_annotation, + body, + *end_position, + ), + + Definition::Test(Function { + name, + arguments: args, + body, + public, + end_position, + .. + }) => self.definition_fn(public, "test", name, args, &None, body, *end_position), Definition::TypeAlias(TypeAlias { alias, @@ -496,6 +513,7 @@ impl<'comments> Formatter<'comments> { fn definition_fn<'a>( &mut self, public: &'a bool, + keyword: &'a str, name: &'a str, args: &'a [UntypedArg], return_annotation: &'a Option, @@ -504,7 +522,8 @@ impl<'comments> Formatter<'comments> { ) -> Document<'a> { // Fn name and args let head = pub_(*public) - .append("fn ") + .append(keyword) + .append(" ") .append(name) .append(wrap_args(args.iter().map(|e| (self.fn_arg(e), false)))); diff --git a/crates/lang/src/parser.rs b/crates/lang/src/parser.rs index d92e7e31..e7275c6d 100644 --- a/crates/lang/src/parser.rs +++ b/crates/lang/src/parser.rs @@ -74,6 +74,7 @@ fn module_parser() -> impl Parser, Error = ParseEr data_parser(), type_alias_parser(), fn_parser(), + test_parser(), constant_parser(), )) .repeated() @@ -266,6 +267,36 @@ pub fn fn_parser() -> impl Parser impl Parser { + just(Token::Test) + .ignore_then(select! {Token::Name {name} => name}) + .then_ignore(just(Token::LeftParen)) + .then_ignore(just(Token::RightParen)) + .map_with_span(|name, span| (name, span)) + .then( + expr_seq_parser() + .or_not() + .delimited_by(just(Token::LeftBrace), just(Token::RightBrace)), + ) + .map_with_span(|((name, span_end), body), span| { + ast::UntypedDefinition::Fn(ast::Function { + arguments: vec![], + body: body.unwrap_or(expr::UntypedExpr::Todo { + kind: TodoKind::EmptyFunction, + location: span, + label: None, + }), + doc: None, + location: span_end, + end_position: span.end - 1, + name, + public: true, + return_annotation: None, + return_type: (), + }) + }) +} + fn constant_parser() -> impl Parser { pub_parser() .or_not()