feat: add Root::parse_bytes for non-UTF8 input#179
Draft
philiptaron wants to merge 3 commits intomasterfrom
Draft
feat: add Root::parse_bytes for non-UTF8 input#179philiptaron wants to merge 3 commits intomasterfrom
philiptaron wants to merge 3 commits intomasterfrom
Conversation
nix (C++) can parse files with non-UTF8 bytes, but rnix currently requires valid UTF-8 because Root::parse takes &str.
Add a new `parse_bytes(&[u8])` method that handles non-UTF8 input by doing lossy UTF-8 conversion. Invalid byte sequences are replaced with U+FFFD (replacement character), matching the behavior of the C++ Nix parser. This allows parsing `.nix` files that contain non-UTF8 bytes, which was previously impossible since `Root::parse` requires `&str`. Closes #173
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.
Summary
Root::parse_bytes(&[u8])method that handles non-UTF8 inputBackground
The C++ Nix parser uses
%option 8bitin its flex lexer, which allows it to handle arbitrary byte values without UTF-8 validation. The raw bytes are preserved in string literals.rnix uses Rowan for its syntax tree, which requires valid UTF-8. This means we cannot preserve arbitrary bytes exactly. Instead,
parse_bytesdoes lossy UTF-8 conversion - invalid sequences become U+FFFD (�).Behavior comparison:
{ x = "\xff"; }{ x = "\xff"; }(raw 0xFF preserved){ x = "�"; }(U+FFFD replacement)This is sufficient for most use cases (linting, formatting, analysis) where exact byte preservation isn't required.
Example
Test plan
non_utf8_can_be_parsed_with_parse_bytes_issue173Closes #173