add comments.

main
bog 2024-03-01 18:11:50 +01:00
parent a0b8cda00d
commit 35e8cc813e
1 changed files with 29 additions and 0 deletions

View File

@ -231,6 +231,20 @@ impl Lexer {
self.cursor += 1; self.cursor += 1;
} }
} }
fn skip_comments(&mut self) {
while let Some('#') = self.text.chars().nth(self.cursor) {
loop {
if let Some('\n')
= self.text.chars().nth(self.cursor) {
break;
}
self.cursor += 1;
}
self.skip_spaces();
}
}
} }
impl Iterator for Lexer { impl Iterator for Lexer {
@ -238,6 +252,7 @@ impl Iterator for Lexer {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
self.skip_spaces(); self.skip_spaces();
self.skip_comments();
if let Some(token) = self.scan_keyword("true") { if let Some(token) = self.scan_keyword("true") {
self.cursor = token.position; self.cursor = token.position;
@ -388,4 +403,18 @@ mod test {
Ok(()) Ok(())
} }
#[test]
fn test_comments() -> Result<(), error::AstError> {
let mut lex = Lexer::new();
let res = lexer_run(&mut lex,
" # aze \n 4 ")?;
assert_eq!(1, res.len());
assert_eq!(Node::Int(4),
*res.get(0).unwrap());
Ok(())
}
} }