Move raw_delimiter_count to syntax crate

This commit is contained in:
Victor Song 2024-01-30 01:13:17 -06:00
parent e8c9ca2a2f
commit 6cd458f3d0
2 changed files with 13 additions and 10 deletions

View file

@ -59,21 +59,21 @@ fn literal_to_external(literal: ast::LiteralKind) -> Option<proc_macro::bridge::
Some(match lit.kind() {
ast::LiteralKind::String(data) => {
if data.is_raw() {
bridge::LitKind::StrRaw(raw_delimiter_count(data)?)
bridge::LitKind::StrRaw(data.raw_delimiter_count()?)
} else {
bridge::LitKind::Str
}
}
ast::LiteralKind::ByteString(data) => {
if data.is_raw() {
bridge::LitKind::ByteStrRaw(raw_delimiter_count(data)?)
bridge::LitKind::ByteStrRaw(data.raw_delimiter_count()?)
} else {
bridge::LitKind::ByteStr
}
}
ast::LiteralKind::CString(data) => {
if data.is_raw() {
bridge::LitKind::CStrRaw(raw_delimiter_count(data)?)
bridge::LitKind::CStrRaw(data.raw_delimiter_count()?)
} else {
bridge::LitKind::CStr
}
@ -86,13 +86,6 @@ fn literal_to_external(literal: ast::LiteralKind) -> Option<proc_macro::bridge::
})
}
fn raw_delimiter_count<S: IsString>(s: S) -> Option<u8> {
let text = s.text();
let quote_range = s.text_range_between_quotes()?;
let range_start = s.syntax().text_range().start();
text[TextRange::up_to((quote_range - range_start).start())].matches('#').count().try_into().ok()
}
fn str_to_lit_node(input: &str) -> Option<ast::Literal> {
let input = input.trim();
let source_code = format!("fn f() {{ let _ = {input}; }}");

View file

@ -204,6 +204,16 @@ pub trait IsString: AstToken {
assert!(TextRange::up_to(contents_range.len()).contains_range(range));
Some(range + contents_range.start())
}
fn raw_delimiter_count(&self) -> Option<u8> {
let text = self.text();
let quote_range = self.text_range_between_quotes()?;
let range_start = self.syntax().text_range().start();
text[TextRange::up_to((quote_range - range_start).start())]
.matches('#')
.count()
.try_into()
.ok()
}
}
impl IsString for ast::String {