From 5ba4f949c23dcf53f34995c90b7c01e6c641b1f0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 6 Nov 2020 22:21:56 +0100 Subject: Kill RAW_ literals Syntactically, they are indistinguishable from non-raw versions, so it doesn't make sense to separate then *at the syntax* level. --- crates/syntax/src/ast/token_ext.rs | 52 +++++++++++++++----------------------- 1 file changed, 21 insertions(+), 31 deletions(-) (limited to 'crates/syntax/src/ast/token_ext.rs') diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs index 8d3fad5a6..6cd20b6a6 100644 --- a/crates/syntax/src/ast/token_ext.rs +++ b/crates/syntax/src/ast/token_ext.rs @@ -139,14 +139,31 @@ pub trait HasQuotes: AstToken { } impl HasQuotes for ast::String {} -impl HasQuotes for ast::RawString {} pub trait HasStringValue: HasQuotes { fn value(&self) -> Option>; } +impl ast::String { + pub fn is_raw(&self) -> bool { + self.text().starts_with('r') + } + pub fn map_range_up(&self, range: TextRange) -> Option { + let contents_range = self.text_range_between_quotes()?; + assert!(TextRange::up_to(contents_range.len()).contains_range(range)); + Some(range + contents_range.start()) + } +} + impl HasStringValue for ast::String { fn value(&self) -> Option> { + if self.is_raw() { + let text = self.text().as_str(); + let text = + &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; + return Some(Cow::Borrowed(text)); + } + let text = self.text().as_str(); let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; @@ -166,20 +183,9 @@ impl HasStringValue for ast::String { } } -// FIXME: merge `ast::RawString` and `ast::String`. -impl HasStringValue for ast::RawString { - fn value(&self) -> Option> { - let text = self.text().as_str(); - let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; - Some(Cow::Borrowed(text)) - } -} - -impl ast::RawString { - pub fn map_range_up(&self, range: TextRange) -> Option { - let contents_range = self.text_range_between_quotes()?; - assert!(TextRange::up_to(contents_range.len()).contains_range(range)); - Some(range + contents_range.start()) +impl ast::ByteString { + pub fn is_raw(&self) -> bool { + self.text().starts_with("br") } } @@ -522,22 +528,6 @@ impl HasFormatSpecifier for ast::String { } } -impl HasFormatSpecifier for ast::RawString { - fn char_ranges( - &self, - ) -> Option)>> { - let text = self.text().as_str(); - let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; - let offset = self.text_range_between_quotes()?.start() - self.syntax().text_range().start(); - - let mut res = Vec::with_capacity(text.len()); - for (idx, c) in text.char_indices() { - res.push((TextRange::at(idx.try_into().unwrap(), TextSize::of(c)) + offset, Ok(c))); - } - Some(res) - } -} - impl ast::IntNumber { #[rustfmt::skip] pub(crate) const SUFFIXES: &'static [&'static str] = &[ -- cgit v1.2.3 From 6158304f8b64ef7cdf58b14bc675baf33a27a853 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 6 Nov 2020 22:30:58 +0100 Subject: Simplify --- crates/syntax/src/ast/token_ext.rs | 54 +++++++++++++++----------------------- 1 file changed, 21 insertions(+), 33 deletions(-) (limited to 'crates/syntax/src/ast/token_ext.rs') diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs index 6cd20b6a6..bf0035986 100644 --- a/crates/syntax/src/ast/token_ext.rs +++ b/crates/syntax/src/ast/token_ext.rs @@ -114,36 +114,6 @@ impl QuoteOffsets { } } -pub trait HasQuotes: AstToken { - fn quote_offsets(&self) -> Option { - let text = self.text().as_str(); - let offsets = QuoteOffsets::new(text)?; - let o = self.syntax().text_range().start(); - let offsets = QuoteOffsets { - quotes: (offsets.quotes.0 + o, offsets.quotes.1 + o), - contents: offsets.contents + o, - }; - Some(offsets) - } - fn open_quote_text_range(&self) -> Option { - self.quote_offsets().map(|it| it.quotes.0) - } - - fn close_quote_text_range(&self) -> Option { - self.quote_offsets().map(|it| it.quotes.1) - } - - fn text_range_between_quotes(&self) -> Option { - self.quote_offsets().map(|it| it.contents) - } -} - -impl HasQuotes for ast::String {} - -pub trait HasStringValue: HasQuotes { - fn value(&self) -> Option>; -} - impl ast::String { pub fn is_raw(&self) -> bool { self.text().starts_with('r') @@ -153,10 +123,8 @@ impl ast::String { assert!(TextRange::up_to(contents_range.len()).contains_range(range)); Some(range + contents_range.start()) } -} -impl HasStringValue for ast::String { - fn value(&self) -> Option> { + pub fn value(&self) -> Option> { if self.is_raw() { let text = self.text().as_str(); let text = @@ -181,6 +149,26 @@ impl HasStringValue for ast::String { let res = if buf == text { Cow::Borrowed(text) } else { Cow::Owned(buf) }; Some(res) } + + pub fn quote_offsets(&self) -> Option { + let text = self.text().as_str(); + let offsets = QuoteOffsets::new(text)?; + let o = self.syntax().text_range().start(); + let offsets = QuoteOffsets { + quotes: (offsets.quotes.0 + o, offsets.quotes.1 + o), + contents: offsets.contents + o, + }; + Some(offsets) + } + pub fn text_range_between_quotes(&self) -> Option { + self.quote_offsets().map(|it| it.contents) + } + pub fn open_quote_text_range(&self) -> Option { + self.quote_offsets().map(|it| it.quotes.0) + } + pub fn close_quote_text_range(&self) -> Option { + self.quote_offsets().map(|it| it.quotes.1) + } } impl ast::ByteString { -- cgit v1.2.3