Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the reverse function call more useful #518

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 46 additions & 11 deletions numbat/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ pub enum ParseErrorKind {
#[error("Expected identifier")]
ExpectedIdentifier,

#[error("Expected identifier or function call after postfix apply (`//`)")]
ExpectedIdentifierOrCallAfterPostfixApply,

#[error("Expected dimension identifier, '1', or opening parenthesis")]
ExpectedDimensionPrimary,

Expand Down Expand Up @@ -880,16 +883,30 @@ impl<'a> Parser<'a> {
let mut expr = self.condition()?;
let mut full_span = expr.full_span();
while self.match_exact(TokenKind::PostfixApply).is_some() {
let identifier = self.identifier()?;
let identifier_span = self.last().unwrap().span;
full_span = full_span.extend(&identifier_span);

expr = Expression::FunctionCall(
identifier_span,
full_span,
Box::new(Expression::Identifier(identifier_span, identifier)),
vec![expr],
);
match self.call()? {
Expression::Identifier(span, ident) => {
full_span = full_span.extend(&span);

expr = Expression::FunctionCall(
span,
full_span,
Box::new(Expression::Identifier(span, ident)),
vec![expr],
);
}
Expression::FunctionCall(call_span, fn_full_span, call, mut params) => {
full_span = full_span.extend(&fn_full_span);

params.push(expr);
expr = Expression::FunctionCall(call_span, full_span, call, params);
}
_other => {
return Err(ParseError::new(
ParseErrorKind::ExpectedIdentifierOrCallAfterPostfixApply,
full_span,
))
}
}
}
Ok(expr)
}
Expand Down Expand Up @@ -1857,7 +1874,7 @@ mod tests {
for input in inputs {
match parse(input, 0) {
Err((_, errors)) => {
assert_eq!(errors[0].kind, error_kind);
assert_eq!(errors[0].kind, error_kind, "Failed on {}", input);
irevoire marked this conversation as resolved.
Show resolved Hide resolved
}
_ => {
panic!();
Expand Down Expand Up @@ -2734,6 +2751,24 @@ mod tests {
vec![binop!(scalar!(1.0), Add, scalar!(1.0))],
),
);
parse_as_expression(
&["1 + 1 // kefir(2)"],
Expression::FunctionCall(
Span::dummy(),
Span::dummy(),
Box::new(identifier!("kefir")),
vec![scalar!(2.0), binop!(scalar!(1.0), Add, scalar!(1.0))],
),
);

should_fail_with(&["1 // print()"], ParseErrorKind::InlineProcedureUsage);

should_fail_with(&["1 // +"], ParseErrorKind::ExpectedPrimary);

should_fail_with(
&["1 // 2", "1 // 1 +"],
ParseErrorKind::ExpectedIdentifierOrCallAfterPostfixApply,
);
}

#[test]
Expand Down
Loading