Improve built-in function call parsing

This commit is contained in:
Jeff 2024-05-18 17:55:58 -04:00
parent 12210fd3ec
commit 8fb8a456cd

View File

@ -261,30 +261,40 @@ pub fn parser<'src>(
); );
let built_in_function_call = choice(( let built_in_function_call = choice((
just(Token::Keyword(Keyword::ReadLine)) just(Token::Keyword(Keyword::ReadLine)).to(Keyword::ReadLine),
.ignore_then(just(Token::Control(Control::ParenOpen))) just(Token::Keyword(Keyword::Sleep)).to(Keyword::Sleep),
.to(BuiltInFunctionCall::ReadLine), just(Token::Keyword(Keyword::WriteLine)).to(Keyword::WriteLine),
just(Token::Keyword(Keyword::Sleep))
.ignore_then(just(Token::Control(Control::ParenOpen)))
.ignore_then(expression.clone())
.map(|expression| BuiltInFunctionCall::Sleep(expression)),
just(Token::Keyword(Keyword::WriteLine))
.ignore_then(just(Token::Control(Control::ParenOpen)))
.ignore_then(expression.clone())
.map(|expression| BuiltInFunctionCall::WriteLine(expression)),
)) ))
.then_ignore(just(Token::Control(Control::ParenClose))) .then(
.try_map_with(move |built_in_function_call, state| { expression
if allow_built_ins { .clone()
Ok(Expression::BuiltInFunctionCall( .delimited_by(
Box::new(built_in_function_call).with_position(state.span()), just(Token::Control(Control::ParenOpen)),
)) just(Token::Control(Control::ParenClose)),
} else { )
Err(Rich::custom( .separated_by(Token::Control(Control::Comma)),
)
.map_with(|(keyword, arguments), state| {
if !allow_built_ins {
return Err(Rich::custom(
state.span(), state.span(),
"Built-in function calls can only be used by the standard library.", "Built-in function calls can only be used by the standard library.",
)) ));
} }
let call = match keyword {
Keyword::ReadLine => Expression::BuiltInFunctionCall(
Box::new(BuiltInFunctionCall::ReadLine).with_position(state.span()),
),
_ => {
return Err(Rich::custom(
state.span(),
"Could not parse this built-in function call.",
))
}
};
Ok(call)
}); });
let structure_field = identifier let structure_field = identifier