nash-parse 0.7.0

Parser for the nash programming language
Documentation
//! Tuple, unit, and parenthesized expression parsing for Nash.
//!
//! Ported from Elm's tuple parsing in `Parse/Expression.hs`.
//!
//! Handles:
//! - `()` → Unit
//! - `(expr)` → parenthesized expression (returned unwrapped)
//! - `(a, b)`, `(a, b, c, ...)` → Tuple

use bumpalo::collections::Vec as BumpVec;
use nash_region::{Located, Position};
use nash_source::Expr;

use crate::Parser;
use crate::error::{self, Tuple};

impl<'a> Parser<'a> {
    /// Parse a tuple, unit, or parenthesized expression.
    ///
    /// Mirrors Elm's `tuple`:
    /// ```haskell
    /// tuple start@(A.Position row col) =
    ///   inContext E.Tuple (word1 0x28 {-(-} E.Start) $
    ///     do  ...
    /// ```
    pub(crate) fn tuple(
        &mut self,
        start: Position,
    ) -> Result<&'a Located<Expr<'a>>, error::Expr<'a>> {
        self.in_context(
            // Wrap Tuple errors with Expr::Tuple context
            |bump, tuple_err, row, col| error::Expr::Tuple(bump.alloc(tuple_err), row, col),
            // Start parser: parse '('
            |p| p.word1(0x28, error::Expr::Start),
            // Body parser: parse tuple contents
            |p| p.tuple_body(start),
        )
    }

    /// Parse the body of a tuple after the opening '('.
    ///
    /// Returns `Tuple` errors which get wrapped by `in_context`.
    ///
    /// Handles:
    /// - `()` → Unit
    /// - `(expr)` → parenthesized expression
    /// - `(a, b, ...)` → Tuple
    /// - `(+)` → Operator section (Op)
    /// - `(-expr)` → Parenthesized negation (parsed as expression)
    fn tuple_body(&mut self, start: Position) -> Result<&'a Located<Expr<'a>>, Tuple<'a>> {
        self.chomp_and_check_indent(Tuple::Space, Tuple::IndentExpr1)?;
        self.one_of(
            Tuple::IndentExpr1,
            vec![
                Box::new(|p: &mut Parser<'a>| {
                    let op = p.operator(Tuple::IndentExpr1, Tuple::OperatorReserved)?;
                    if p.peek() == Some(b')') {
                        p.advance();
                        return Ok(p.add_end(start, Expr::Op(op)));
                    }

                    if op == "-" {
                        p.chomp_and_check_indent(Tuple::Space, Tuple::IndentExpr1)?;
                        let inner = p.specialize(
                            |bump, error, row, col| Tuple::Expr(bump.alloc(error), row, col),
                            |p| p.term(),
                        )?;
                        let neg_end = p.get_position();
                        let neg_start = Position::new(start.line, start.column + 1);
                        let negated = p.alloc(Located::at(
                            nash_region::Region::new(neg_start, neg_end),
                            Expr::Negate(inner),
                        ));
                        p.chomp(Tuple::Space)?;
                        let (full, end) = p.specialize(
                            |bump, error, row, col| Tuple::Expr(bump.alloc(error), row, col),
                            |p| p.chomp_expr_end(start, negated, vec![], neg_end),
                        )?;
                        p.check_indent(end.line, end.column, Tuple::IndentEnd)?;
                        return p.chomp_tuple_end(start, full);
                    }

                    p.chomp_and_check_indent(Tuple::Space, Tuple::IndentExpr1)?;
                    let (right, end) = p.tuple_expr()?;
                    p.check_indent(end.line, end.column, Tuple::IndentEnd)?;
                    p.chomp(Tuple::Space)?;
                    p.word1(b')', Tuple::OperatorClose)?;
                    Ok(p.add_end(
                        start,
                        Expr::RightSection {
                            operator: op,
                            right,
                        },
                    ))
                }),
                Box::new(|p: &mut Parser<'a>| {
                    p.word1(b')', Tuple::IndentExpr1)?;
                    Ok(p.add_end(start, Expr::Unit))
                }),
                Box::new(|p: &mut Parser<'a>| {
                    let (first, end) = p.tuple_expr()?;
                    p.check_indent(end.line, end.column, Tuple::IndentEnd)?;
                    p.chomp_tuple_end(start, first)
                }),
            ],
        )
    }

    /// Parse a tuple entry expression.
    ///
    /// In Elm this uses `specialize E.TupleExpr expression`.
    /// Returns both the expression and its end position for indent checking.
    fn tuple_expr(&mut self) -> Result<(&'a Located<Expr<'a>>, Position), Tuple<'a>> {
        self.specialize(
            |bump, expr_err, row, col| Tuple::Expr(bump.alloc(expr_err), row, col),
            |p| p.expression(),
        )
    }

    /// Parse the rest of a tuple after the first expression.
    ///
    /// Mirrors Elm's `chompTupleEnd`:
    /// ```haskell
    /// chompTupleEnd start firstExpr revExprs =
    ///   oneOf E.TupleEnd
    ///     [ do  word1 0x2C {-,-} E.TupleEnd
    ///           ...
    ///           chompTupleEnd start firstExpr (entry : revExprs)
    ///     , do  word1 0x29 {-)-} E.TupleEnd
    ///           case reverse revExprs of
    ///             [] -> return firstExpr  -- parenthesized
    ///             secondExpr : otherExprs ->
    ///               addEnd start (Src.Tuple firstExpr secondExpr otherExprs)
    ///     ]
    /// ```
    fn chomp_tuple_end(
        &mut self,
        start: Position,
        first: &'a Located<Expr<'a>>,
    ) -> Result<&'a Located<Expr<'a>>, Tuple<'a>> {
        let mut rest: BumpVec<'a, &'a Located<Expr<'a>>> = BumpVec::new_in(self.bump);

        loop {
            // Chomp whitespace
            self.chomp(Tuple::Space)?;

            let saved = self.save_state();
            match self.operator(Tuple::End, Tuple::OperatorReserved) {
                Ok(operator) => {
                    self.chomp(Tuple::Space)?;
                    if self.word1(b')', Tuple::OperatorClose).is_ok() {
                        return Ok(self.add_end(
                            start,
                            Expr::LeftSection {
                                left: first,
                                operator,
                            },
                        ));
                    }
                    self.restore_state(saved);
                }
                Err(error) => {
                    if self.pos != saved.pos {
                        return Err(error);
                    }
                    self.restore_state(saved);
                }
            }

            // Expect comma or closing paren
            let done = self.one_of(
                Tuple::End,
                vec![
                    // Comma - parse another expression
                    Box::new(|p: &mut Parser<'a>| {
                        p.word1(0x2C, Tuple::End)?;

                        // Chomp whitespace and check indent after comma
                        p.chomp_and_check_indent(Tuple::Space, Tuple::IndentExprN)?;

                        // Parse the expression
                        let (elem, end) = p.tuple_expr()?;
                        rest.push(elem);

                        // Check indent using expression's end position
                        p.check_indent(end.line, end.column, Tuple::IndentEnd)?;

                        Ok(false) // Not done, continue loop
                    }),
                    // Closing paren - done
                    Box::new(|p: &mut Parser<'a>| {
                        p.word1(0x29, Tuple::End)?;
                        Ok(true) // Done
                    }),
                ],
            )?;

            if done {
                break;
            }
        }

        // Determine what we parsed
        if rest.is_empty() {
            // Record grouping distinguishes a positional constructor argument from
            // construction by label. Other parenthesized expressions are transparent.
            if let Expr::Record { fields, .. } = &first.value {
                Ok(self.add_end(
                    start,
                    Expr::Record {
                        fields,
                        grouped: true,
                    },
                ))
            } else {
                Ok(first)
            }
        } else {
            // Tuple: need at least 2 elements
            let second = rest.remove(0);
            let others = rest.into_bump_slice();
            Ok(self.add_end(
                start,
                Expr::Tuple {
                    first,
                    second,
                    rest: others,
                },
            ))
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::expression::{
        assert_expr_error_snapshot, assert_expr_snapshot, assert_expression_snapshot,
        assert_indented_expr_snapshot,
    };

    #[test]
    fn unit() {
        assert_expr_snapshot!("()");
    }

    #[test]
    fn parenthesized() {
        assert_expr_snapshot!("(42)");
    }

    #[test]
    fn pair() {
        assert_expr_snapshot!("(1, 2)");
    }

    #[test]
    fn triple() {
        assert_expr_snapshot!("(1, 2, 3)");
    }

    #[test]
    fn with_whitespace() {
        assert_expr_snapshot!("( 1 , 2 , 3 )");
    }

    #[test]
    fn nested() {
        assert_expr_snapshot!("((1, 2), 3)");
    }

    #[test]
    fn nested_list() {
        assert_expr_snapshot!("([1, 2], [3, 4])");
    }

    #[test]
    fn multiline() {
        assert_indented_expr_snapshot!(
            "(
                1,
                2,
                3
            )"
        );
    }

    #[test]
    fn error_unclosed() {
        assert_expr_error_snapshot!("(1, 2");
    }

    #[test]
    fn error_trailing_comma() {
        assert_expr_error_snapshot!("(1, 2,)");
    }

    #[test]
    fn error_empty_comma() {
        assert_expr_error_snapshot!("(,)");
    }

    #[test]
    fn right_section() {
        assert_expr_snapshot!("(> 5)");
    }
    #[test]
    fn left_section() {
        assert_expr_snapshot!("(5 >)");
    }
    #[test]
    fn call_left_section() {
        assert_expr_snapshot!("(f x |> )");
    }
    #[test]
    fn operator_application() {
        assert_expr_snapshot!("((+) 1)");
    }
    #[test]
    fn minus_operator() {
        assert_expr_snapshot!("(-)");
    }
    #[test]
    fn parenthesized_negative() {
        assert_expr_snapshot!("(-1)");
    }
    #[test]
    fn spaced_parenthesized_negative() {
        assert_expr_snapshot!("(- 1)");
    }
    #[test]
    fn minus_left_section() {
        assert_expr_snapshot!("(1 -)");
    }
    #[test]
    fn applied_section() {
        assert_expression_snapshot!("(> 5) 10");
    }
    #[test]
    fn error_section_unclosed() {
        assert_expr_error_snapshot!("(> 5");
    }
    #[test]
    fn error_reserved_section_operator() {
        assert_expr_error_snapshot!("(=> 5)");
    }
    #[test]
    fn error_section_missing_operand() {
        assert_expr_error_snapshot!("(> ,)");
    }
}