From 359e70d1b20402ca9cc8731909daecfab598e55d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 27 Dec 2018 15:03:18 +0300 Subject: support literal suffixes --- crates/ra_syntax/src/string_lexing/parser.rs | 10 ++++++++++ crates/ra_syntax/src/string_lexing/string.rs | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) (limited to 'crates/ra_syntax/src/string_lexing') diff --git a/crates/ra_syntax/src/string_lexing/parser.rs b/crates/ra_syntax/src/string_lexing/parser.rs index 13f3db889..14c6015c2 100644 --- a/crates/ra_syntax/src/string_lexing/parser.rs +++ b/crates/ra_syntax/src/string_lexing/parser.rs @@ -139,6 +139,16 @@ impl<'a> Parser<'a> { )) } } + + pub fn parse_suffix(&mut self) -> Option { + let start = self.get_pos(); + let _ = self.peek()?; + while let Some(_) = self.peek() { + self.advance(); + } + let end = self.get_pos(); + Some(TextRange::from_to(start, end)) + } } #[derive(Debug, Eq, PartialEq, Clone)] diff --git a/crates/ra_syntax/src/string_lexing/string.rs b/crates/ra_syntax/src/string_lexing/string.rs index 7476fea13..064f08544 100644 --- a/crates/ra_syntax/src/string_lexing/string.rs +++ b/crates/ra_syntax/src/string_lexing/string.rs @@ -1,12 +1,15 @@ -use crate::string_lexing::{ +use crate::{ + TextRange, + string_lexing::{ parser::Parser, StringComponent, -}; +}}; pub fn parse_string_literal(src: &str) -> StringComponentIterator { StringComponentIterator { parser: Parser::new(src, b'"'), has_closing_quote: false, + suffix: None, prefix: None, quote: b'"', } @@ -16,6 +19,7 @@ pub fn parse_byte_string_literal(src: &str) -> StringComponentIterator { StringComponentIterator { parser: Parser::new(src, b'"'), has_closing_quote: false, + suffix: None, prefix: Some(b'b'), quote: b'"', } @@ -25,6 +29,7 @@ pub fn parse_char_literal(src: &str) -> StringComponentIterator { StringComponentIterator { parser: Parser::new(src, b'\''), has_closing_quote: false, + suffix: None, prefix: None, quote: b'\'', } @@ -34,6 +39,7 @@ pub fn parse_byte_literal(src: &str) -> StringComponentIterator { StringComponentIterator { parser: Parser::new(src, b'\''), has_closing_quote: false, + suffix: None, prefix: Some(b'b'), quote: b'\'', } @@ -42,6 +48,7 @@ pub fn parse_byte_literal(src: &str) -> StringComponentIterator { pub struct StringComponentIterator<'a> { parser: Parser<'a>, pub has_closing_quote: bool, + pub suffix: Option, prefix: Option, quote: u8, } @@ -72,6 +79,9 @@ impl<'a> Iterator for StringComponentIterator<'a> { if self.parser.peek() == Some(self.quote as char) { self.parser.advance(); self.has_closing_quote = true; + if let Some(range) = self.parser.parse_suffix() { + self.suffix = Some(range); + } } assert!( -- cgit v1.2.3