Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
suaviloquence committed Aug 5, 2024
1 parent 4f93420 commit 3397951
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/frontend/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl<'a> Parser<'a> {
}

fn parse_selector_list(&mut self) -> Result<Option<AstRef<'a, SelectorList<'a>>>> {
let mut item = self.scanner.peek_token();
let mut item = self.scanner.peek_non_comment();
if item.1.token == Token::Whitespace {
self.scanner.eat_token();
let next = self.scanner.peek_non_whitespace();
Expand Down
49 changes: 48 additions & 1 deletion src/frontend/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ pub enum Token {
Semi,
/// a less than sign `<` to indicate the start of an inline expansion
Less,
/// A single-line comment that begins with two forward slashes '//' and
/// spans the rest of the line
Comment,
/// special token to indicate the end of the file
Eof,
/// special token to indicate unknown token
Expand Down Expand Up @@ -100,6 +103,7 @@ mod statics {
Colon <- ":"
Semi <- ";"
Less <- "<"
Comment <- r"//[^\n]*"
};
}
}
Expand Down Expand Up @@ -189,14 +193,32 @@ impl<'a> Scanner<'a> {
(span, lexeme)
}

/// Looks ahead for the next non-`Comment` [`Lexeme`]
/// and returns it without [eating](Self::eat_token) it.
pub fn peek_non_comment(&mut self) -> (Span, Lexeme<'a>) {
while let (
_,
Lexeme {
token: Token::Comment,
..
},
) = self.peek_token()
{
self.eat_token();
}
self.peek_token()
}

/// Looks ahead for the first non-whitespace, non-comment [`Lexeme`]
/// and returns it without [eating](Self::eat_token) it.
pub fn peek_non_whitespace(&mut self) -> (Span, Lexeme<'a>) {
while let (
_,
Lexeme {
token: Token::Whitespace,
..
},
) = self.peek_token()
) = self.peek_non_comment()
{
self.eat_token();
}
Expand Down Expand Up @@ -312,6 +334,31 @@ mod tests {
assert_eq!(sc.eat_token().1, lx!(BraceClose, "}"));
}

#[test]
fn test_comments() {
let mut sc = Scanner::new(
r"// Hello! This is a comment!
b: a // and another! {
{
// } don't be fooled!
}",
);

assert_eq!(sc.peek_non_whitespace().1, lx!(Id, "b"));
sc.eat_token();
assert_eq!(sc.peek_non_whitespace().1, lx!(Colon, ":"));
sc.eat_token();
assert_eq!(sc.peek_non_whitespace().1, lx!(Id, "a"));
sc.eat_token();
assert_eq!(sc.peek_non_whitespace().1, lx!(BraceOpen, "{"));
sc.eat_token();
assert_eq!(sc.eat_token().1.token, Token::Whitespace);
assert_eq!(sc.eat_token().1, lx!(Comment, "// } don't be fooled!"));
assert_eq!(sc.peek_non_whitespace().1, lx!(BraceClose, "}"));
sc.eat_token();
assert_eq!(sc.eat_token().1.token, Token::Eof);
}

#[test]
fn all_regex_is_valid() {
let _ = &*REGEX_SET;
Expand Down

0 comments on commit 3397951

Please sign in to comment.