Skip to content

Commit

Permalink
hex-literal: improve comments filtration (#666)
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Nov 11, 2021
1 parent 04a1550 commit 59ab43f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 83 deletions.
1 change: 1 addition & 0 deletions hex-literal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [UNRELEASED]
### Changed
- Provide more info in `panic!` messages
- Minor changes in the comments filtration code

### Added
- More tests for `hex!()` macro
Expand Down
108 changes: 25 additions & 83 deletions hex-literal/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ impl<I: Iterator<Item = u8>> Iterator for ExcludingComments<I> {
let next_byte = self.next_byte();
if next_byte.is_none() {
match self.state {
State::BlockComment | State::PotentiallyLeavingBlockComment => {
State::BlockComment | State::PotentialBlockCommentEnd => {
panic!("block comment not terminated with */")
}
// No more bytes after a single '/'
State::PotentialComment { .. } => panic!("encountered invalid character: `/`"),
State::PotentialComment { .. } => panic!("encountered isolated `/`"),
_ => {}
}
}
Expand All @@ -45,16 +44,17 @@ impl<I: Iterator<Item = u8>> Iterator for ExcludingComments<I> {
/// '/' '*'
/// LineComment BlockComment
/// '\n' '*'
/// Normal PotentiallyLeavingBlockComment
/// Normal PotentialBlockCommentEnd
/// '/' '_'
/// Normal BlockComment
/// </pre>
/// </pre>
#[derive(Copy, Clone)]
enum State {
Normal,
PotentialComment { previous: u8 },
PotentialComment,
LineComment,
BlockComment,
PotentiallyLeavingBlockComment,
PotentialBlockCommentEnd,
}

impl<I: Iterator<Item = u8>> ExcludingComments<I> {
Expand All @@ -67,81 +67,22 @@ impl<I: Iterator<Item = u8>> ExcludingComments<I> {

fn next_byte(&mut self) -> Option<u8> {
loop {
return match self.state {
State::Normal => {
let next = self.iter.next()?;
match next {
b'/' => {
self.state = State::PotentialComment { previous: next };
continue;
}
_ => Some(next),
}
}
State::PotentialComment { previous } => {
let peeked_next = self.iter.peek()?;
match peeked_next {
b'/' => {
// second /, enter line comment and consume
self.iter.next();
self.state = State::LineComment;
continue;
}
b'*' => {
/* entering a block comment consume '*' */
self.iter.next();
self.state = State::BlockComment;
continue;
}
_ => {
// here we need to emit the previous character (the first '/')
// and do not consume the current character
self.state = State::Normal;
return Some(previous);
}
}
}
State::LineComment => {
let next = self.iter.next()?;
match next {
b'\n' => {
self.state = State::Normal;
return Some(next);
}
_ => {
// ignore all other characters while in the line comment
continue;
}
}
}
State::BlockComment => {
let next = self.iter.next()?;
match next {
b'*' => {
self.state = State::PotentiallyLeavingBlockComment;
continue;
}
_ => {
/* ignore all other characters while in the block comment */
continue;
}
}
}
State::PotentiallyLeavingBlockComment => {
let next = self.iter.next()?;
match next {
b'/' => {
/* Left the block comment */
self.state = State::Normal;
continue;
}
_ => {
/* we're still in the block comment */
self.state = State::BlockComment;
continue;
}
}
let next = self.iter.next()?;
self.state = match (self.state, next) {
(State::Normal, b'/') => State::PotentialComment,
(State::Normal, _) => return Some(next),
(State::PotentialComment, b'/') => State::LineComment,
(State::PotentialComment, b'*') => State::BlockComment,
(State::PotentialComment, _) => panic!("encountered isolated `/`"),
(State::LineComment, b'\n') => {
self.state = State::Normal;
return Some(b'\n');
}
(State::LineComment, _) => continue,
(State::BlockComment, b'*') => State::PotentialBlockCommentEnd,
(State::BlockComment, _) => continue,
(State::PotentialBlockCommentEnd, b'/') => State::Normal,
(State::PotentialBlockCommentEnd, _) => State::BlockComment,
};
}
}
Expand Down Expand Up @@ -195,8 +136,9 @@ mod tests {
}

#[test]
fn single_slash_is_not_excluded() {
assert_eq!(exclude_comments("ab/cd"), "ab/cd");
#[should_panic]
fn panic_on_single_slash() {
exclude_comments("ab/cd");
}

#[test]
Expand Down

0 comments on commit 59ab43f

Please sign in to comment.