zen_expression/intellisense/
mod.rs

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use crate::arena::UnsafeArena;
use crate::intellisense::scope::IntelliSenseScope;
use crate::intellisense::types::provider::TypesProvider;
use crate::lexer::Lexer;
use crate::parser::{Node, Parser};
use crate::variable::VariableType;
use serde::Serialize;
use std::cell::RefCell;
use std::rc::Rc;

mod scope;
mod types;

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct IntelliSenseToken {
    pub span: (u32, u32),
    pub kind: Rc<VariableType>,
    pub node_kind: &'static str,
    pub error: Option<String>,
}

pub struct IntelliSense<'arena> {
    arena: UnsafeArena<'arena>,
    lexer: Lexer<'arena>,
}

impl<'arena> IntelliSense<'arena> {
    pub fn new() -> Self {
        Self {
            arena: UnsafeArena::new(),
            lexer: Lexer::new(),
        }
    }

    pub fn type_check(
        &mut self,
        source: &'arena str,
        data: &VariableType,
    ) -> Option<Vec<IntelliSenseToken>> {
        let arena = self.arena.get();

        let tokens = self.lexer.tokenize(source).ok()?;
        let parser = Parser::try_new(tokens, &arena).map(|p| p.standard()).ok()?;

        let parser_result = parser.with_metadata().parse();
        let ast = parser_result.root;
        let metadata = parser_result.metadata?;

        let type_data = TypesProvider::generate(
            ast,
            IntelliSenseScope {
                pointer_data: data,
                root_data: data,
                current_data: data,
            },
        );

        let results = RefCell::new(Vec::new());
        ast.walk(|node| {
            let addr = node as *const Node as usize;
            let mut r = results.borrow_mut();
            let typ = type_data.get_type(node);

            r.push(IntelliSenseToken {
                span: node
                    .span()
                    .or_else(|| metadata.get(&addr).map(|s| s.span))
                    .unwrap_or_default(),
                node_kind: node.into(),
                error: typ.map(|t| t.error.clone()).flatten(),
                kind: typ
                    .map(|t| t.kind.clone())
                    .unwrap_or_else(|| Rc::new(VariableType::Any)),
            });
        });

        self.arena.with_mut(|a| a.reset());
        Some(results.into_inner())
    }

    pub fn type_check_unary(
        &mut self,
        source: &'arena str,
        data: &VariableType,
    ) -> Option<Vec<IntelliSenseToken>> {
        let arena = self.arena.get();

        let tokens = self.lexer.tokenize(source).ok()?;
        let parser = Parser::try_new(tokens, &arena).map(|p| p.unary()).ok()?;

        let parser_result = parser.with_metadata().parse();
        let ast = parser_result.root;
        let metadata = parser_result.metadata?;

        let type_data = TypesProvider::generate(
            ast,
            IntelliSenseScope {
                pointer_data: data,
                root_data: data,
                current_data: data,
            },
        );

        let results = RefCell::new(Vec::new());
        ast.walk(|node| {
            let addr = node as *const Node as usize;
            let mut r = results.borrow_mut();
            let typ = type_data.get_type(node);

            r.push(IntelliSenseToken {
                span: metadata.get(&addr).map(|s| s.span).unwrap_or_default(),
                node_kind: node.into(),
                error: typ.map(|t| t.error.clone()).flatten(),
                kind: typ
                    .map(|t| t.kind.clone())
                    .unwrap_or_else(|| Rc::new(VariableType::Any)),
            });
        });

        self.arena.with_mut(|a| a.reset());
        Some(results.into_inner())
    }
}

#[cfg(test)]
mod tests {
    use crate::intellisense::IntelliSense;
    use crate::variable::VariableType;
    use serde_json::json;

    #[test]
    fn sample_test() {
        let mut is = IntelliSense::new();

        let data = json!({ "customer": { "firstName": "John", "lastName": "Doe", "array": [{"a": 5}, {"a": 6}] } });
        let data_type: VariableType = data.into();

        let typ = is.type_check("customer.array[0]", &data_type);
        println!("{:?}", typ);
    }

    #[test]
    fn sample_test_unary() {
        let mut is = IntelliSense::new();

        let data = json!({ "customer": { "firstName": "John", "lastName": "Doe" }, "$": 10});
        let data_type: VariableType = data.into();

        let typ = is.type_check_unary("> 10", &data_type);
        println!("{typ:?}");
    }
}