Fix lexing for unterminated strings/heredocs etc.#3924
Merged
Conversation
When we hit EOF and still have lex modes left, it means some content was unterminated. Heredocs specifically have logic that needs to happen when the body finished lexing. If we don't reset the mode back to how it was before, it will not continue lexing at the correct place. Followup to ruby#3918. We can't call into `parser_lex` since it resets token locations.
Earlopain
commented
Feb 13, 2026
| pm_statements_node_t *statements = NULL; | ||
|
|
||
| if (!match1(parser, PM_TOKEN_EMBEXPR_END)) { | ||
| if (!match3(parser, PM_TOKEN_EMBEXPR_END, PM_TOKEN_HEREDOC_END, PM_TOKEN_EOF)) { |
Collaborator
Author
There was a problem hiding this comment.
#{ currently gives the interpolation a statement with MissingNode. That's not correct, the missing end token leads to syntax error. It also messes up the locations when it finds the synthetic heredoc end token. I don't think there are any other tokens to consider here, should be all hopefully.
# Before
Prism.parse("\"\#{").value.statements.body[0]
=>
@ InterpolatedStringNode (location: (1,0)-(1,3))
├── flags: newline
├── opening_loc: (1,0)-(1,1) = "\""
├── parts: (length: 1)
│ └── @ EmbeddedStatementsNode (location: (1,1)-(1,3))
│ ├── flags: ∅
│ ├── opening_loc: (1,1)-(1,3) = "\#{"
│ ├── statements:
│ │ @ StatementsNode (location: (1,1)-(1,3))
│ │ ├── flags: ∅
│ │ └── body: (length: 1)
│ │ └── @ MissingNode (location: (1,1)-(1,3))
│ │ └── flags: ∅
│ └── closing_loc: (1,3)-(1,3) = ""
└── closing_loc: ∅
# After
Prism.parse("\"\#{").value.statements.body[0]
=>
@ InterpolatedStringNode (location: (1,0)-(1,3))
├── flags: newline
├── opening_loc: (1,0)-(1,1) = "\""
├── parts: (length: 1)
│ └── @ EmbeddedStatementsNode (location: (1,1)-(1,3))
│ ├── flags: ∅
│ ├── opening_loc: (1,1)-(1,3) = "\#{"
│ ├── statements: ∅
│ └── closing_loc: (1,3)-(1,3) = ""
└── closing_loc: ∅
kddnewton
approved these changes
Feb 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When we hit EOF and still have lex modes left, it means some content was unterminated. Heredocs specifically have logic that needs to happen when the body finished lexing. If we don't reset the mode back to how it was before, it will not continue lexing at the correct place.
Followup to #3918. We can't call into
parser_lexsince it resets token locations. So I went back togoto.Closes #3911