Skip to content

Refactor parser duplication in cst.rs #1411

@milkyskies

Description

@milkyskies

Found while reviewing #1407. Three concrete duplications in crates/floe-core/src/cst.rs and cst/exprs.rs:

1. expect and expect_kind are byte-for-byte identical

crates/floe-core/src/cst.rs:574-594 — both functions have the same signature, the same body, the same error message. One must be dead code, or they diverged historically and silently re-converged.

Fix: delete one, update call sites of the deleted one to use the surviving name. (Quick check shows ~5-10 call sites in cst/types.rs and cst/items.rs.)

2. Seven binary-expression parsers follow an identical template

crates/floe-core/src/cst/exprs.rsparse_or_expr, parse_and_expr, parse_equality_expr, parse_comparison_expr, parse_additive_expr, parse_multiplicative_expr (and partly parse_pipe_expr) all do:

fn parse_X_expr(&mut self) {
    let checkpoint = self.builder.checkpoint();
    self.parse_NEXT();
    while self.at(<operators>) {
        self.builder.start_node_at(checkpoint, SyntaxKind::BINARY_EXPR.into());
        self.bump();
        self.eat_trivia();
        self.parse_NEXT();
        self.builder.finish_node();
    }
}

Fix: one generic parse_left_assoc(&mut self, ops: &[TokenKind], next: fn(&mut Self)) helper. Pipe stays separate (it uses PIPE_EXPR and has the match special case).

3. Repeated self.at(&X) || self.at(&Y) || self.at(&Z) chains

Dozens of these across cst/exprs.rs, cst/items.rs, cst/types.rs. Replace with fn at_any(&self, kinds: &[TokenKind]) -> bool.

Tests

Existing parser tests cover all this. After the refactor:

  • cargo clippy --workspace zero errors
  • cargo test --workspace all pass
  • All tests/cst/* snapshots unchanged

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions