1
use std::collections::BTreeMap;
2
use std::sync::{Mutex, OnceLock};
3

            
4
use tree_sitter::{Node, Parser, Tree};
5
use tree_sitter_essence::LANGUAGE;
6

            
7
use super::traversal::WalkDFS;
8
use crate::diagnostics::diagnostics_api::SymbolKind;
9
use crate::diagnostics::source_map::{HoverInfo, SourceMap, SpanId, span_with_hover};
10
use crate::errors::RecoverableParseError;
11
use conjure_cp_core::ast::{Name, SymbolTablePtr};
12

            
13
/// Context for parsing, containing shared state passed through parser functions.
14
pub struct ParseContext<'a> {
15
    pub source_code: &'a str,
16
    pub root: &'a Node<'a>,
17
    pub symbols: Option<SymbolTablePtr>,
18
    pub errors: &'a mut Vec<RecoverableParseError>,
19
    pub source_map: &'a mut SourceMap,
20
    pub decl_spans: &'a mut BTreeMap<Name, SpanId>,
21
    /// What type the current expression/literal itself should be
22
    pub typechecking_context: TypecheckingContext,
23
    /// What type the elements within a collection should be
24
    pub inner_typechecking_context: TypecheckingContext,
25
}
26

            
27
impl<'a> ParseContext<'a> {
28
19320
    pub fn new(
29
19320
        source_code: &'a str,
30
19320
        root: &'a Node<'a>,
31
19320
        symbols: Option<SymbolTablePtr>,
32
19320
        errors: &'a mut Vec<RecoverableParseError>,
33
19320
        source_map: &'a mut SourceMap,
34
19320
        decl_spans: &'a mut BTreeMap<Name, SpanId>,
35
19320
    ) -> Self {
36
19320
        Self {
37
19320
            source_code,
38
19320
            root,
39
19320
            symbols,
40
19320
            errors,
41
19320
            source_map,
42
19320
            decl_spans,
43
19320
            typechecking_context: TypecheckingContext::Unknown,
44
19320
            inner_typechecking_context: TypecheckingContext::Unknown,
45
19320
        }
46
19320
    }
47

            
48
703
    pub fn record_error(&mut self, error: RecoverableParseError) {
49
703
        self.errors.push(error);
50
703
    }
51

            
52
    /// Create a new ParseContext with different symbols but sharing source_code, root, errors, and source_map.
53
3974
    pub fn with_new_symbols(&mut self, symbols: Option<SymbolTablePtr>) -> ParseContext<'_> {
54
3974
        ParseContext {
55
3974
            source_code: self.source_code,
56
3974
            root: self.root,
57
3974
            symbols,
58
3974
            errors: self.errors,
59
3974
            source_map: self.source_map,
60
3974
            decl_spans: self.decl_spans,
61
3974
            typechecking_context: self.typechecking_context,
62
3974
            inner_typechecking_context: self.inner_typechecking_context,
63
3974
        }
64
3974
    }
65

            
66
47690
    pub fn save_decl_span(&mut self, name: Name, span_id: SpanId) {
67
47690
        self.decl_spans.insert(name, span_id);
68
47690
    }
69

            
70
146654
    pub fn lookup_decl_span(&self, name: &Name) -> Option<SpanId> {
71
146654
        self.decl_spans.get(name).copied()
72
146654
    }
73

            
74
65
    pub fn lookup_decl_line(&self, name: &Name) -> Option<u32> {
75
65
        let span_id = self.lookup_decl_span(name)?;
76
65
        let span = self.source_map.spans.get(span_id as usize)?;
77
65
        Some(span.start_point.line + 1)
78
65
    }
79

            
80
    /// Helper to add to span and documentation hover info into the source map
81
205153
    pub fn add_span_and_doc_hover(
82
205153
        &mut self,
83
205153
        node: &tree_sitter::Node,
84
205153
        doc_key: &str, // name of the documentation file in Bits
85
205153
        kind: SymbolKind,
86
205153
        ty: Option<String>,
87
205153
        decl_span: Option<u32>,
88
205153
    ) {
89
205153
        let hover = HoverInfo {
90
205153
            description: String::new(),
91
205153
            doc_key: Some(normalise_documentation_key(doc_key)),
92
205153
            kind: Some(kind),
93
205153
            ty,
94
205153
            decl_span,
95
205153
        };
96
205153
        span_with_hover(node, self.source_code, self.source_map, hover);
97
205153
    }
98
}
99

            
100
// Used to detect type mismatches during parsing.
101
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
102
pub enum TypecheckingContext {
103
    Boolean,
104
    Arithmetic,
105
    Set,
106
    SetOrMatrix,
107
    MSet,
108
    Matrix,
109
    Tuple,
110
    Record,
111
    Partition,
112
    Sequence,
113
    /// Context is unknown or flexible
114
    Unknown,
115
}
116

            
117
/// Parse the given source code into a syntax tree using tree-sitter.
118
///
119
/// If successful, returns a tuple containing the syntax tree and the raw source code.
120
/// If the source code is not valid Essence, returns None.
121
19089
pub fn get_tree(src: &str) -> Option<(Tree, String)> {
122
19089
    let mut parser = Parser::new();
123
19089
    parser.set_language(&LANGUAGE.into()).unwrap();
124

            
125
19089
    parser.parse(src, None).and_then(|tree| {
126
19089
        let root = tree.root_node();
127
19089
        if root.is_error() {
128
            return None;
129
19089
        }
130
19089
        Some((tree, src.to_string()))
131
19089
    })
132
19089
}
133

            
134
/// Parse an expression fragment, allowing a dummy prefix for error recovery.
135
///
136
/// NOTE: The new source code may be different from the original source code.
137
///       See implementation for details.
138
472
pub fn get_expr_tree(src: &str) -> Option<(Tree, String)> {
139
472
    let mut parser = Parser::new();
140
472
    parser.set_language(&LANGUAGE.into()).unwrap();
141

            
142
472
    parser.parse(src, None).and_then(|tree| {
143
472
        let root = tree.root_node();
144
472
        if root.is_error() {
145
            return None;
146
472
        }
147

            
148
472
        let children: Vec<_> = named_children(&root).collect();
149
472
        let first_child = children.first()?;
150

            
151
        // HACK: Tree-sitter can only parse a complete program from top to bottom, not an individual bit of syntax.
152
        // See: https://github.com/tree-sitter/tree-sitter/issues/711 and linked issues.
153
        // However, we can use a dummy _FRAGMENT_EXPRESSION prefix (which we insert as necessary)
154
        // to trick the parser into accepting an isolated expression.
155
        // This way we can parse an isolated expression and it is only slightly cursed :)
156
472
        if first_child.is_error() {
157
236
            if src.starts_with("_FRAGMENT_EXPRESSION") {
158
                None
159
            } else {
160
236
                get_expr_tree(&format!("_FRAGMENT_EXPRESSION {src}"))
161
            }
162
        } else {
163
236
            Some((tree, src.to_string()))
164
        }
165
472
    })
166
472
}
167

            
168
/// Get the named children of a node
169
123792
pub fn named_children<'a>(node: &'a Node<'a>) -> impl Iterator<Item = Node<'a>> + 'a {
170
123792
    (0..node.named_child_count())
171
150049
        .filter_map(|i| u32::try_from(i).ok().and_then(|i| node.named_child(i)))
172
123792
}
173

            
174
1181
pub fn node_is_expression(node: &Node) -> bool {
175
474
    matches!(
176
1181
        node.kind(),
177
1181
        "bool_expr" | "arithmetic_expr" | "comparison_expr" | "atom"
178
    )
179
1181
}
180

            
181
/// Get all top-level nodes that match the given predicate
182
236
pub fn query_toplevel<'a>(
183
236
    node: &'a Node<'a>,
184
236
    predicate: &'a dyn Fn(&Node<'a>) -> bool,
185
236
) -> impl Iterator<Item = Node<'a>> + 'a {
186
718
    WalkDFS::with_retract(node, predicate).filter(|n| n.is_named() && predicate(n))
187
236
}
188

            
189
/// Get all meta-variable names in a node
190
1
pub fn get_metavars<'a>(node: &'a Node<'a>, src: &'a str) -> impl Iterator<Item = String> + 'a {
191
16
    query_toplevel(node, &|n| n.kind() == "metavar").filter_map(|child| {
192
1
        child
193
1
            .named_child(0)
194
1
            .map(|name| src[name.start_byte()..name.end_byte()].to_string())
195
1
    })
196
1
}
197

            
198
/// Fetch Essence syntax documentation from Conjure's `docs/bits/` folder on GitHub.
199
///
200
/// `name` is the name of the documentation file (without .md suffix). If the file is not found or an error occurs, returns None.
201
pub fn get_documentation(name: &str) -> Option<String> {
202
    static DOCUMENTATION_CACHE: OnceLock<Mutex<BTreeMap<String, Option<String>>>> = OnceLock::new();
203

            
204
    let base = normalise_documentation_key(name);
205
    let cache = DOCUMENTATION_CACHE.get_or_init(|| Mutex::new(BTreeMap::new()));
206

            
207
    if let Some(cached) = cache.lock().ok()?.get(&base).cloned() {
208
        return cached;
209
    }
210

            
211
    // This url is for raw Markdown bytes
212
    let url =
213
        format!("https://raw.githubusercontent.com/conjure-cp/conjure/main/docs/bits/{base}.md");
214

            
215
    let output = std::process::Command::new("curl")
216
        .args(["-fsSL", &url])
217
        .output()
218
        .ok();
219

            
220
    let documentation = output
221
        .filter(|output| output.status.success())
222
        .and_then(|output| String::from_utf8(output.stdout).ok());
223

            
224
    if let Ok(mut cache) = cache.lock() {
225
        cache.insert(base, documentation.clone());
226
    }
227

            
228
    documentation
229
}
230

            
231
205153
fn normalise_documentation_key(name: &str) -> String {
232
205153
    name.strip_suffix(".md").unwrap_or(name).to_string()
233
205153
}
234

            
235
mod test {
236
    #[allow(unused)]
237
    use super::*;
238

            
239
    #[test]
240
1
    fn test_get_metavars() {
241
1
        let src = "such that &x = y";
242
1
        let (tree, _) = get_tree(src).unwrap();
243
1
        let root = tree.root_node();
244
1
        let metavars = get_metavars(&root, src).collect::<Vec<_>>();
245
1
        assert_eq!(metavars, vec!["x"]);
246
1
    }
247
}