1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use crate::functions::is_space::is_space;
use crate::records::comment::Comment;
use crate::records::lexeme::Type;
use crate::records::parser::Parser;
impl Parser {
pub fn next_lexeme(&mut self) {
let mut r#type = self.lexer.next_with(false, true).r#type;
while r#type == Type::BrokenComment
|| r#type == Type::Comment
|| r#type == Type::BlockComment
{
let lexeme = *self.lexer.current();
if self.options.capture_comments {
self.comment_locations.push(Comment {
r#type: lexeme.r#type,
location: lexeme.location,
});
}
// Subtlety: Broken comments are weird because we record them as comments AND pass them to the parser as a lexeme.
// The parser will turn this into a proper syntax error.
if lexeme.r#type == Type::BrokenComment {
return;
}
// Comments starting with ! are called "hot comments" and contain directives for type checking / linting / compiling
if lexeme.r#type == Type::Comment && lexeme.get_length() > 0 {
unsafe {
let text_ptr = lexeme.data.data as *const u8;
if *text_ptr == b'!' {
let mut end = lexeme.get_length();
while end > 0 && is_space(*text_ptr.add(end as usize - 1) as char) {
end -= 1;
}
let content = core::str::from_utf8_unchecked(core::slice::from_raw_parts(
text_ptr.add(1),
(end - 1) as usize,
));
self.hotcomments
.push(crate::records::hot_comment::HotComment {
header: self.hotcomment_header,
location: lexeme.location,
content: alloc::string::String::from(content),
});
}
}
}
r#type = self.lexer.next_with(false, false).r#type;
}
}
}