Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,9 @@ enum CharClassesStatus {
Normal,
LitString,
LitStringEscape,
LitRawString(u32),
RawStringPrefix(u32),
RawStringSuffix(u32),
LitChar,
LitCharEscape,
// The u32 is the nesting deepness of the comment
Expand Down Expand Up @@ -858,6 +861,20 @@ where
}
}

fn is_raw_string_suffix<T>(iter: &mut MultiPeek<T>, count: u32) -> bool
where
T: Iterator,
T::Item: RichChar,
{
for _ in 0..count {
match iter.peek() {
Some(c) if c.get_char() == '#' => continue,
_ => return false,
}
}
true
}

impl<T> Iterator for CharClasses<T>
where
T: Iterator,
Expand All @@ -870,6 +887,43 @@ where
let chr = item.get_char();
let mut char_kind = FullCodeCharKind::Normal;
self.status = match self.status {
CharClassesStatus::LitRawString(sharps) => {
char_kind = FullCodeCharKind::InString;
match chr {
'"' => {
if sharps == 0 {
char_kind = FullCodeCharKind::Normal;
CharClassesStatus::Normal
} else if is_raw_string_suffix(&mut self.base, sharps) {
CharClassesStatus::RawStringSuffix(sharps)
} else {
CharClassesStatus::LitRawString(sharps)
}
}
_ => CharClassesStatus::LitRawString(sharps),
}
}
CharClassesStatus::RawStringPrefix(sharps) => {
char_kind = FullCodeCharKind::InString;
match chr {
'#' => CharClassesStatus::RawStringPrefix(sharps + 1),
'"' => CharClassesStatus::LitRawString(sharps),
_ => CharClassesStatus::Normal, // Unreachable.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below, could we use unreachable!?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I am not sure... That part should be unreachable in normal code, but could be reachable in macro call.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I think it would be good to clarify that in the comment, but no need to block the PR on it.

}
}
CharClassesStatus::RawStringSuffix(sharps) => {
match chr {
'#' => {
if sharps == 1 {
CharClassesStatus::Normal
} else {
char_kind = FullCodeCharKind::InString;
CharClassesStatus::RawStringSuffix(sharps - 1)
}
}
_ => CharClassesStatus::Normal, // Unreachable
}
}
CharClassesStatus::LitString => match chr {
'"' => CharClassesStatus::Normal,
'\\' => {
Expand All @@ -892,6 +946,13 @@ where
},
CharClassesStatus::LitCharEscape => CharClassesStatus::LitChar,
CharClassesStatus::Normal => match chr {
'r' => match self.base.peek().map(|c| c.get_char()) {
Some('#') | Some('"') => {
char_kind = FullCodeCharKind::InString;
CharClassesStatus::RawStringPrefix(0)
}
_ => CharClassesStatus::Normal,
},
'"' => {
char_kind = FullCodeCharKind::InString;
CharClassesStatus::LitString
Expand Down
20 changes: 20 additions & 0 deletions tests/source/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,23 @@ fn foo() {
}
}
}

// #2642
macro_rules! template {
($name: expr) => {
format_args!(r##"
"http://example.com"

# test
"##, $name)
}
}

macro_rules! template {
() => {
format_args!(r"
//

")
}
}
25 changes: 25 additions & 0 deletions tests/target/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,28 @@ fn foo() {
}
}
}

// #2642
macro_rules! template {
($name:expr) => {
format_args!(
r##"
"http://example.com"

# test
"##,
$name
)
};
}

macro_rules! template {
() => {
format_args!(
r"
//

"
)
};
}