From 30cd4d5acb7dfd40cea264a926d1c89f0c3522c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20Ochagav=C3=ADa?= Date: Sun, 11 Nov 2018 20:41:43 +0100 Subject: Validate byte string literals --- crates/ra_syntax/src/string_lexing.rs | 53 +++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) (limited to 'crates/ra_syntax/src/string_lexing.rs') diff --git a/crates/ra_syntax/src/string_lexing.rs b/crates/ra_syntax/src/string_lexing.rs index 4e8c3a91c..d253c97e7 100644 --- a/crates/ra_syntax/src/string_lexing.rs +++ b/crates/ra_syntax/src/string_lexing.rs @@ -1,6 +1,55 @@ use self::CharComponentKind::*; use rowan::{TextRange, TextUnit}; +pub fn parse_byte_string_literal(src: &str) -> ByteStringComponentIterator { + ByteStringComponentIterator { + parser: Parser::new(src), + has_closing_quote: false, + } +} + +pub struct ByteStringComponentIterator<'a> { + parser: Parser<'a>, + pub has_closing_quote: bool, +} + +impl<'a> Iterator for ByteStringComponentIterator<'a> { + type Item = StringComponent; + fn next(&mut self) -> Option { + if self.parser.pos == 0 { + assert!( + self.parser.advance() == 'b', + "byte string literal should start with a `b`" + ); + + assert!( + self.parser.advance() == '"', + "byte string literal should start with a `b`, followed by 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, + "byte string literal should leave no unparsed input: src = {}, pos = {}, length = {}", + self.parser.src, + self.parser.pos, + self.parser.src.len() + ); + + None + } +} + pub fn parse_string_literal(src: &str) -> StringComponentIterator { StringComponentIterator { parser: Parser::new(src), @@ -81,12 +130,12 @@ impl<'a> Iterator for ByteComponentIterator<'a> { if self.parser.pos == 0 { assert!( self.parser.advance() == 'b', - "Byte literal should start with a b" + "Byte literal should start with a `b`" ); assert!( self.parser.advance() == '\'', - "Byte literal should start with a b, followed by a quote" + "Byte literal should start with a `b`, followed by a quote" ); } -- cgit v1.2.3