aiken_lang/parser/
extra.rs

1use crate::ast::Span;
2use std::iter::Peekable;
3
4#[derive(Debug, PartialEq, Eq, Default, Clone, serde::Serialize, serde::Deserialize)]
5pub struct ModuleExtra {
6    pub module_comments: Vec<Span>,
7    pub doc_comments: Vec<Span>,
8    pub comments: Vec<Span>,
9    pub empty_lines: Vec<usize>,
10}
11
12impl ModuleExtra {
13    pub fn new() -> Self {
14        Default::default()
15    }
16}
17
18#[derive(Debug, PartialEq, Eq)]
19pub struct Comment<'a> {
20    pub start: usize,
21    pub content: &'a str,
22}
23
24impl<'a> From<(&Span, &'a str)> for Comment<'a> {
25    fn from(src: (&Span, &'a str)) -> Comment<'a> {
26        let start = src.0.start;
27        let end = src.0.end;
28        Comment {
29            start,
30            content: std::str::from_utf8(src.1.as_bytes()[start..end].as_ref())
31                .expect("From span to comment"),
32        }
33    }
34}
35
36pub fn comments_before<'a>(
37    comment_spans: &mut Peekable<impl Iterator<Item = &'a Span>>,
38    byte: usize,
39    src: &'a str,
40) -> Vec<&'a str> {
41    let mut comments = vec![];
42    while let Some(Span { start, .. }) = comment_spans.peek() {
43        if start <= &byte {
44            let comment = comment_spans
45                .next()
46                .expect("Comment before accessing next span");
47            comments.push(Comment::from((comment, src)).content)
48        } else {
49            break;
50        }
51    }
52    comments
53}