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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use core::num;

use crate::{
    ast::AstToken,
    SyntaxKind::{self, *},
    SyntaxToken,
};

macro_rules! token {
    (
        #[from($kind:ident)]
        $(#[$meta:meta])*
        struct $name:ident;
    ) => {
        #[derive(Debug, Clone, PartialEq, Eq, Hash)]
        $(#[$meta])*
        pub struct $name(pub(super) SyntaxToken);

        impl std::fmt::Display for $name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                std::fmt::Display::fmt(self.syntax(), f)
            }
        }

        impl AstToken for $name {
            fn can_cast(kind: SyntaxKind) -> bool {
                $kind == kind
            }

            fn cast(from: SyntaxToken) -> Option<Self> {
                if from.kind() == $kind {
                    Some(Self(from))
                } else {
                    None
                }
            }

            fn syntax(&self) -> &SyntaxToken {
                &self.0
            }
        }
    };
}

token! { #[from(TOKEN_WHITESPACE)] struct Whitespace; }

token! { #[from(TOKEN_COMMENT)] struct Comment; }

impl Comment {
    pub fn text(&self) -> &str {
        let text = self.syntax().text();
        // Handle both "#..." and "/*...*/" comments.
        match text.strip_prefix("#") {
            Some(s) => s,
            None => text.strip_prefix(r#"/*"#).unwrap().strip_suffix(r#"*/"#).unwrap(),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::ast;
    use crate::match_ast;
    use crate::Root;

    use super::*;
    use rowan::ast::AstNode;

    #[test]
    fn comment() {
        let s = "# comment bruh
/* this is a multiline comment wow
asdfasdf
asdfasdf */
1 + 1
/* last one */
";
        let comments: Vec<String> = Root::parse(s)
            .ok()
            .unwrap()
            .syntax()
            .children_with_tokens()
            .filter_map(|e| match e {
                rowan::NodeOrToken::Token(token) => match_ast! { match token {
                    ast::Comment(it) => Some(it.text().to_string()),
                    _ => None,
                }},
                rowan::NodeOrToken::Node(_) => None,
            })
            .collect();
        let expected = vec![
            " comment bruh",
            " this is a multiline comment wow\nasdfasdf\nasdfasdf ",
            " last one ",
        ];

        assert_eq!(comments, expected);
    }
}

token! { #[from(TOKEN_FLOAT)] struct Float; }

impl Float {
    pub fn value(&self) -> Result<f64, num::ParseFloatError> {
        self.syntax().text().parse()
    }
}

token! { #[from(TOKEN_INTEGER)] struct Integer; }

impl Integer {
    pub fn value(&self) -> Result<i64, num::ParseIntError> {
        self.syntax().text().parse()
    }
}

token! { #[from(TOKEN_PATH)] struct PathContent; }

token! { #[from(TOKEN_STRING_CONTENT)] struct StrContent; }

token! { #[from(TOKEN_URI)] struct Uri; }