-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Validate string literals #222
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,68 @@ | ||
| use self::CharComponentKind::*; | ||
| use rowan::{TextRange, TextUnit}; | ||
|
|
||
| pub fn parse_string_literal(src: &str) -> StringComponentIterator { | ||
| StringComponentIterator { | ||
| parser: Parser::new(src), | ||
| has_closing_quote: false, | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Eq, PartialEq, Clone)] | ||
| pub struct StringComponent { | ||
| pub range: TextRange, | ||
| pub kind: StringComponentKind, | ||
| } | ||
|
|
||
| impl StringComponent { | ||
| fn new(range: TextRange, kind: StringComponentKind) -> StringComponent { | ||
| StringComponent { range, kind } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Eq, PartialEq, Clone)] | ||
| pub enum StringComponentKind { | ||
| IgnoreNewline, | ||
| Char(CharComponentKind), | ||
| } | ||
|
|
||
| pub struct StringComponentIterator<'a> { | ||
| parser: Parser<'a>, | ||
| pub has_closing_quote: bool, | ||
| } | ||
|
|
||
| impl<'a> Iterator for StringComponentIterator<'a> { | ||
| type Item = StringComponent; | ||
| fn next(&mut self) -> Option<StringComponent> { | ||
| if self.parser.pos == 0 { | ||
| assert!( | ||
| self.parser.advance() == '"', | ||
| "string literal should start with double quotes" | ||
| ); | ||
| } | ||
|
|
||
| if let Some(component) = self.parser.parse_string_component() { | ||
| return Some(component); | ||
| } | ||
|
|
||
| // We get here when there are no char components left to parse | ||
| if self.parser.peek() == Some('"') { | ||
| self.parser.advance(); | ||
| self.has_closing_quote = true; | ||
| } | ||
|
|
||
| assert!( | ||
| self.parser.peek() == None, | ||
| "string literal should leave no unparsed input: src = {}, pos = {}, length = {}", | ||
| self.parser.src, | ||
| self.parser.pos, | ||
| self.parser.src.len() | ||
| ); | ||
|
|
||
| None | ||
| } | ||
| } | ||
|
|
||
| pub fn parse_char_literal(src: &str) -> CharComponentIterator { | ||
| CharComponentIterator { | ||
| parser: Parser::new(src), | ||
|
|
@@ -93,6 +155,12 @@ impl<'a> Parser<'a> { | |
| next | ||
| } | ||
|
|
||
| pub fn skip_whitespace(&mut self) { | ||
| while self.peek().map(|c| c.is_whitespace()) == Some(true) { | ||
| self.advance(); | ||
| } | ||
| } | ||
|
|
||
| pub fn get_pos(&self) -> TextUnit { | ||
| (self.pos as u32).into() | ||
| } | ||
|
|
@@ -172,6 +240,51 @@ impl<'a> Parser<'a> { | |
| )) | ||
| } | ||
| } | ||
|
|
||
| pub fn parse_ignore_newline(&mut self, start: TextUnit) -> Option<StringComponent> { | ||
| // In string literals, when a `\` occurs immediately before the newline, the `\`, | ||
| // the newline, and all whitespace at the beginning of the next line are ignored | ||
| match self.peek() { | ||
| Some('\n') | Some('\r') => { | ||
| self.skip_whitespace(); | ||
|
Contributor
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. Am I right in assuming that this will eat the sequence: "\r\n" for windows?
Contributor
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.
Contributor
Author
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. It does
Contributor
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. Should this eat a string of the form "\
<all whitespace>
more text"where there is an entirely whitespace line. See that The current behaviour appears to be that, unless I'm mistaken.
Contributor
Author
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. It will indeed eat all the whitespace, including new lines, as does the Rust compiler. See this playground link |
||
| Some(StringComponent::new( | ||
| TextRange::from_to(start, self.get_pos()), | ||
| StringComponentKind::IgnoreNewline, | ||
| )) | ||
| } | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| pub fn parse_string_component(&mut self) -> Option<StringComponent> { | ||
| let next = self.peek()?; | ||
|
|
||
| // Ignore string close | ||
| if next == '"' { | ||
| return None; | ||
| } | ||
|
|
||
| let start = self.get_pos(); | ||
| self.advance(); | ||
|
|
||
| if next == '\\' { | ||
| // Strings can use `\` to ignore newlines, so we first try to parse one of those | ||
| // before falling back to parsing char escapes | ||
| self.parse_ignore_newline(start).or_else(|| { | ||
| let char_component = self.parse_escape(start); | ||
| Some(StringComponent::new( | ||
| char_component.range, | ||
| StringComponentKind::Char(char_component.kind), | ||
| )) | ||
| }) | ||
| } else { | ||
| let end = self.get_pos(); | ||
| Some(StringComponent::new( | ||
| TextRange::from_to(start, end), | ||
| StringComponentKind::Char(CodePoint), | ||
| )) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
||
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 there any way we can avoid the name
Stringhere (bikeshed: StringLit, StringExpr etc.)?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.
Not sure... The token is called
STRINGand as far as I know the generator requires that you use the token name in CamelCase. Maybe @matklad knows?