Extend parser for 'test' keyword.
This commit is contained in:
parent
a65b4aa471
commit
ea48747825
|
@ -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<Annotation>,
|
||||
|
@ -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))));
|
||||
|
||||
|
|
|
@ -74,6 +74,7 @@ fn module_parser() -> impl Parser<Token, Vec<UntypedDefinition>, Error = ParseEr
|
|||
data_parser(),
|
||||
type_alias_parser(),
|
||||
fn_parser(),
|
||||
test_parser(),
|
||||
constant_parser(),
|
||||
))
|
||||
.repeated()
|
||||
|
@ -266,6 +267,36 @@ pub fn fn_parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseEr
|
|||
)
|
||||
}
|
||||
|
||||
pub fn test_parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError> {
|
||||
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<Token, ast::UntypedDefinition, Error = ParseError> {
|
||||
pub_parser()
|
||||
.or_not()
|
||||
|
|
Loading…
Reference in New Issue