-
Notifications
You must be signed in to change notification settings - Fork 227
gccrs: Implement C-style string literals #4565
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6a00942
gccrs: Implement C-style string literals
Polygonalr b448e1c
gccrs: Add new compiler flag for parsing & compiling C-style string l…
Polygonalr 3f52afd
gccrs: Introduce CStr language item
Polygonalr 116673c
gccrs: Fix field access on DST fat pointers
Polygonalr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ struct Literal | |
| STRING, | ||
| BYTE, | ||
| BYTE_STRING, | ||
| C_STRING, | ||
| INT, | ||
| FLOAT, | ||
| BOOL | ||
|
|
||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1063,6 +1063,10 @@ Lexer::build_token () | |
| return parse_raw_byte_string (loc); | ||
| } | ||
|
|
||
| // C-style strings | ||
| else if (current_char == 'c' && peek_input () == '"') | ||
| return parse_c_string (loc); | ||
|
Comment on lines
+1066
to
+1068
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that in a later PR you can also add C-style raw strings the same way, but I don't know if the kernel makes use of those |
||
|
|
||
| // raw identifiers and raw strings | ||
| if (current_char == 'r') | ||
| { | ||
|
|
@@ -1749,6 +1753,82 @@ Lexer::parse_byte_string (location_t loc) | |
| return Token::make_byte_string (loc, std::move (str)); | ||
| } | ||
|
|
||
| // Parses a C-style string. | ||
| TokenPtr | ||
| Lexer::parse_c_string (location_t loc) | ||
| { | ||
| skip_input (); | ||
| current_column++; | ||
|
|
||
| // Mostly same code copied from parse_string... | ||
|
|
||
| std::string str; | ||
| str.reserve (16); // some sensible default | ||
|
|
||
| current_char = peek_input (); | ||
|
|
||
| const location_t string_begin_locus = get_current_location (); | ||
|
|
||
| while (current_char.value != '"' && !current_char.is_eof ()) | ||
| { | ||
| if (current_char.value == '\\') | ||
| { | ||
| int length = 1; | ||
|
|
||
| auto escape_pair = parse_escape ('"'); | ||
| current_char = std::get<0> (escape_pair); | ||
|
|
||
| if (current_char == Codepoint (0) && std::get<2> (escape_pair)) | ||
| length = std::get<1> (escape_pair) - 1; | ||
| else | ||
| length += std::get<1> (escape_pair); | ||
|
|
||
| if (current_char != Codepoint (0) || !std::get<2> (escape_pair)) | ||
| str += current_char.as_string (); | ||
|
|
||
| current_column += length; | ||
|
|
||
| // FIXME: parse_escape does not update current_char correctly. | ||
| current_char = peek_input (); | ||
| continue; | ||
| } | ||
|
|
||
| current_column++; | ||
| if (current_char.value == '\n') | ||
| { | ||
| current_line++; | ||
| current_column = 1; | ||
| // tell line_table that new line starts | ||
| start_line (current_line, max_column_hint); | ||
| } | ||
|
Polygonalr marked this conversation as resolved.
|
||
|
|
||
| str += current_char; | ||
| skip_input (); | ||
| current_char = peek_input (); | ||
| } | ||
|
|
||
| if (current_char.value == '"') | ||
| { | ||
| current_column++; | ||
|
|
||
| skip_input (); | ||
| current_char = peek_input (); | ||
| } | ||
| else if (current_char.is_eof ()) | ||
| { | ||
| rust_error_at (string_begin_locus, "unended C string literal"); | ||
| return Token::make (END_OF_FILE, get_current_location ()); | ||
| } | ||
| else | ||
| { | ||
| rust_unreachable (); | ||
| } | ||
|
|
||
| str.shrink_to_fit (); | ||
|
|
||
| return Token::make_c_string (loc, std::move (str)); | ||
| } | ||
|
|
||
| // Parses a raw byte string. | ||
| TokenPtr | ||
| Lexer::parse_raw_byte_string (location_t loc) | ||
|
|
||
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is correct, but don't we also need to add an extra zero byte at the end?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it taken care of by the backend already?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C++11 strings already come with a null terminator, so I don't think we need to handle it.