浏览代码

Make the reverse function call operator more powerful

Tamo 1 年之前
父节点
当前提交
2929d8695b
共有 1 个文件被更改,包括 31 次插入10 次删除
  1. 31 10
      numbat/src/parser.rs

+ 31 - 10
numbat/src/parser.rs

@@ -108,6 +108,11 @@ pub enum ParseErrorKind {
     #[error("Expected identifier")]
     ExpectedIdentifier,
 
+    #[error(
+        "Expected identifier or function call after postfix apply (`//`) but instead got: `{0}`"
+    )]
+    ExpectedIdentifierOrCallAfterPostfixApply(String),
+
     #[error("Expected dimension identifier, '1', or opening parenthesis")]
     ExpectedDimensionPrimary,
 
@@ -880,16 +885,32 @@ 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(format!(
+                            "{other:?}"
+                        )),
+                        full_span,
+                    ))
+                }
+            }
         }
         Ok(expr)
     }