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

Revamp the PieceWriter API some. #1298

Merged
merged 4 commits into from
Oct 31, 2023
Merged

Revamp the PieceWriter API some. #1298

merged 4 commits into from
Oct 31, 2023

Conversation

munificent
Copy link
Member

@munificent munificent commented Oct 27, 2023

The current API is pretty confusing around when you should use pop(), split(), or both. If you get it wrong, sometimes it silently works but may leave a bug for later, or sometimes it fails it seemingly unrelated ways. Also, it's not clear whether you should call pop() first or split(). (It actually didn't matter.)

I tried to simplify the API some. It's still more subtle and brittle than I would like, but it's not clear to me if the complexity is essential. I'm trying to build Piece trees that don't have unnecessary nodes, so some slippage between AST node boundaries and Piece tree boundaries is essential.

I hope this API is better. The changes are:

  • Make split() implicitly pop() the previous piece. This means you never need to call both split() and pop(). Just call split(). You do still have to know when to prefer pop() instead of split(), but I added some documentation to try to clarify that.

  • Rename the main PieceWriter field from writer to pieces. I think this reads a little better at the callsites and makes it clearer what is being pushed, popped, and split. "Writer" doesn't really mean much.

  • Rename space() to writeSpace(). Then add a helper method in PieceFactory called space(). This way, almost all calls on pieces deal with pieces and calls that recurse into or write pieces of an AST are bare function calls: token(), visit(), and space().

The current API is pretty confusing around when you should use `pop()`,
`split()`, or both. If you get it wrong, sometimes it silently works
but may leave a bug for later, or sometimes it fails it seemingly
unrelated ways. Also, it's not clear whether you should call `pop()`
first or `split()`. (It actually didn't matter.)

I tried to simplify the API some. It's still more subtle and brittle
than I would like, but it's not clear to me if the complexity is
essential. I'm trying to build Piece trees that don't have unnecessary
nodes, so some slippage between AST node boundaries and Piece tree
boundaries is essential.

I hope this API is better. The changes are:

- Make `split()` implicitly `pop()` the previous piece. This means you
  never need to call both `split()` and `pop()`. Just call `split()`.
  You do still have to know when to prefer `pop()` instead of `split()`,
  but I added some documentation to try to clarify that.

- Rename the main PieceWriter field from `writer` to `pieces`. I think
  this reads a little better at the callsites and makes it clearer what
  is being pushed, popped, and split. "Writer" doesn't really mean much.

- Rename `space()` to `writeSpace()`. Then add a helper method in
  PieceFactory called `space()`. This way, almost all calls on `pieces`
  deal with pieces and calls that recurse into or write pieces of an AST
  are bare function calls: `token()`, `visit()`, and `space()`.
lib/src/front_end/piece_factory.dart Outdated Show resolved Hide resolved
lib/src/front_end/piece_writer.dart Outdated Show resolved Hide resolved
lib/src/front_end/piece_writer.dart Outdated Show resolved Hide resolved
@munificent munificent merged commit 19f42a9 into main Oct 31, 2023
7 checks passed
@munificent munificent deleted the revamp-piece-api branch October 31, 2023 00:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants