Skip to content

refactor: Minify duplicated Token-to-char conversion with helper - #48

Merged
thatmagicalcat merged 1 commit into
thatmagicalcat:mainfrom
gavincrawford:refactor/delim-to-char
Jul 21, 2026
Merged

refactor: Minify duplicated Token-to-char conversion with helper#48
thatmagicalcat merged 1 commit into
thatmagicalcat:mainfrom
gavincrawford:refactor/delim-to-char

Conversation

@gavincrawford

Copy link
Copy Markdown
Contributor

The existing parser duplicates the conversion from Token back to corresponding character representation. This commit adds a helper that cleans up excess LOC for the two implementations that existed previously. Saves about nine lines, but more importantly, establishes a single source-of-truth for back-conversion between Token and char.

Ideally, I'd say we write a reversible From<char>, because it is associative and provides us with Into<char> for Token, but I feel this is a larger architectural choice.

Also, as noted with a TODO comment, the unreachable on line 483 isn't really unreachable. This should be addressed, but is outside of the scope of this particular change.

@gavincrawford

Copy link
Copy Markdown
Contributor Author

Unreachable noted in #49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the parser’s delimiter handling by centralizing Tokenchar back-conversion into a single helper on Token, removing duplicated match logic in Parser.

Changes:

  • Added Token::as_char() to convert delimiter tokens (including Escape variants) back into their character form.
  • Updated Parser::parse_left_delimited to use the new helper and removed the old duplicated conversion logic (read_delimiter).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/token.rs Introduces Token::as_char() helper to centralize token-to-character delimiter conversion.
src/parser.rs Refactors parse_left_delimited to use as_char() and deletes duplicated delimiter conversion code.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/parser.rs
Comment on lines +432 to 439
// Peek left and convert it back to it's character representation
let left = self
.peek()
.and_then(|tok| tok.as_char())
.ok_or(ParseError::ExpectedDelimiter { side: "left" })?;

let inner_start = self.pos;
let mut depth = 0usize;
Comment thread src/parser.rs
Comment on lines +474 to 478
// Expect left and right delimiters to match
// TODO: this should probably get it's own strongly-typed error variant. it's not really
// unreachable code, and this branch could reasonably be called in production, resulting in
// a hard panic.
let expected_right = match left {
Comment thread src/token.rs
}

impl<'a> Token<'a> {
pub fn as_char(&self) -> Option<char> {
@thatmagicalcat

Copy link
Copy Markdown
Owner

thanks! I'll merge it for now and hope you'll resolve the unreachable issue

@thatmagicalcat
thatmagicalcat merged commit 4ea825c into thatmagicalcat:main Jul 21, 2026
1 check passed
@gavincrawford
gavincrawford deleted the refactor/delim-to-char branch July 21, 2026 04:13
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