1
use std::sync::{Arc, RwLock};
2
use std::{fs, vec};
3

            
4
use conjure_cp_core::Model;
5
use conjure_cp_core::ast::assertions::debug_assert_model_well_formed;
6
use conjure_cp_core::ast::{DeclarationPtr, Expression, Metadata, Moo};
7
use conjure_cp_core::context::Context;
8
#[allow(unused)]
9
use uniplate::Uniplate;
10

            
11
use super::ParseContext;
12
use super::find::parse_find_statement;
13
use super::letting::parse_letting_statement;
14
use super::util::{TypecheckingContext, get_tree};
15
use crate::diagnostics::diagnostics_api::SymbolKind;
16
use crate::diagnostics::source_map::{HoverInfo, SourceMap, span_with_hover};
17
use crate::errors::{FatalParseError, ParseErrorCollection, RecoverableParseError};
18
use crate::expression::parse_expression;
19
use crate::field;
20
use crate::syntax_errors::detect_syntactic_errors;
21

            
22
/// Parse an Essence file into a Model using the tree-sitter parser.
23
575
pub fn parse_essence_file_native(
24
575
    path: &str,
25
575
    context: Arc<RwLock<Context<'static>>>,
26
575
) -> Result<Model, Box<ParseErrorCollection>> {
27
575
    let source_code = fs::read_to_string(path)
28
575
        .unwrap_or_else(|_| panic!("Failed to read the source code file {path}"));
29

            
30
575
    let mut errors = vec![];
31
575
    let model = parse_essence_with_context(&source_code, context, &mut errors);
32

            
33
575
    match model {
34
318
        Ok(Some(m)) => {
35
318
            debug_assert_model_well_formed(&m, "tree-sitter");
36
318
            Ok(m)
37
        }
38
        Ok(None) => {
39
            // Recoverable errors were found, return them as a ParseErrorCollection
40
257
            Err(Box::new(ParseErrorCollection::multiple(
41
257
                errors,
42
257
                Some(source_code),
43
257
                Some(path.to_string()),
44
257
            )))
45
        }
46
        Err(fatal) => {
47
            // Fatal error - wrap in ParseErrorCollection::Fatal
48
            Err(Box::new(ParseErrorCollection::fatal(fatal)))
49
        }
50
    }
51
575
}
52

            
53
960
pub fn parse_essence_with_context(
54
960
    src: &str,
55
960
    context: Arc<RwLock<Context<'static>>>,
56
960
    errors: &mut Vec<RecoverableParseError>,
57
960
) -> Result<Option<Model>, FatalParseError> {
58
960
    match parse_essence_with_context_and_map(src, context, errors)? {
59
329
        Some((model, _source_map)) => Ok(Some(model)),
60
631
        None => Ok(None),
61
    }
62
960
}
63

            
64
962
pub fn parse_essence_with_context_and_map(
65
962
    src: &str,
66
962
    context: Arc<RwLock<Context<'static>>>,
67
962
    errors: &mut Vec<RecoverableParseError>,
68
962
) -> Result<Option<(Model, SourceMap)>, FatalParseError> {
69
962
    let (tree, source_code) = match get_tree(src) {
70
962
        Some(tree) => tree,
71
        None => {
72
            return Err(FatalParseError::TreeSitterError(
73
                "Failed to parse source code".to_string(),
74
            ));
75
        }
76
    };
77

            
78
962
    if tree.root_node().has_error() {
79
367
        detect_syntactic_errors(src, &tree, errors);
80
367
        return Ok(None);
81
595
    }
82

            
83
595
    let mut model = Model::new(context);
84
595
    let mut source_map = SourceMap::default();
85
595
    let root_node = tree.root_node();
86

            
87
    // Create a ParseContext
88
595
    let mut ctx = ParseContext::new(
89
595
        &source_code,
90
595
        &root_node,
91
595
        Some(model.symbols_ptr_unchecked().clone()),
92
595
        errors,
93
595
        &mut source_map,
94
    );
95

            
96
595
    let mut cursor = root_node.walk();
97
3334
    for statement in root_node.children(&mut cursor) {
98
        /*
99
           since find and letting are unnamed children
100
           hover info is added here.
101
           other unnamed children will be skipped.
102
        */
103
3334
        if statement.kind() == "find" {
104
694
            span_with_hover(
105
694
                &statement,
106
694
                ctx.source_code,
107
694
                ctx.source_map,
108
694
                HoverInfo {
109
694
                    description: "Find keyword".to_string(),
110
694
                    kind: Some(SymbolKind::Find),
111
694
                    ty: None,
112
694
                    decl_span: None,
113
694
                },
114
694
            );
115
2640
        } else if statement.kind() == "letting" {
116
397
            span_with_hover(
117
397
                &statement,
118
397
                ctx.source_code,
119
397
                ctx.source_map,
120
397
                HoverInfo {
121
397
                    description: "Letting keyword".to_string(),
122
397
                    kind: Some(SymbolKind::Letting),
123
397
                    ty: None,
124
397
                    decl_span: None,
125
397
                },
126
397
            );
127
2243
        }
128

            
129
3334
        if !statement.is_named() {
130
1667
            continue;
131
1667
        }
132

            
133
1667
        match statement.kind() {
134
1667
            "single_line_comment" => {}
135
1667
            "language_declaration" => {}
136
1667
            "find_statement" => {
137
694
                let var_hashmap = parse_find_statement(&mut ctx, statement)?;
138
707
                for (name, domain) in var_hashmap {
139
707
                    model
140
707
                        .symbols_mut()
141
707
                        .insert(DeclarationPtr::new_find(name, domain));
142
707
                }
143
            }
144
973
            "bool_expr" | "atom" | "comparison_expr" => {
145
576
                ctx.typechecking_context = TypecheckingContext::Boolean;
146
576
                let Some(expr) = parse_expression(&mut ctx, statement)? else {
147
154
                    continue;
148
                };
149
422
                model.add_constraint(expr);
150
            }
151
397
            "language_label" => {}
152
397
            "letting_statement" => {
153
397
                let Some(letting_vars) = parse_letting_statement(&mut ctx, statement)? else {
154
                    continue;
155
                };
156
397
                model.symbols_mut().extend(letting_vars);
157
            }
158
            "dominance_relation" => {
159
                let inner = field!(statement, "expression");
160
                let Some(expr) = parse_expression(&mut ctx, inner)? else {
161
                    continue;
162
                };
163
                let dominance = Expression::DominanceRelation(Metadata::new(), Moo::new(expr));
164
                if model.dominance.is_some() {
165
                    ctx.record_error(RecoverableParseError::new(
166
                        "Duplicate dominance relation".to_string(),
167
                        None,
168
                    ));
169
                    continue;
170
                }
171
                model.dominance = Some(dominance);
172
            }
173
            _ => {
174
                return Err(FatalParseError::internal_error(
175
                    format!("Unexpected top-level statement: {}", statement.kind()),
176
                    Some(statement.range()),
177
                ));
178
            }
179
        }
180
    }
181

            
182
    // check for errors (keyword as identifier)
183
595
    keyword_as_identifier(&mut ctx);
184

            
185
    // Check if there were any recoverable errors
186
595
    if !errors.is_empty() {
187
264
        return Ok(None);
188
331
    }
189
    // otherwise return the model
190
331
    Ok(Some((model, source_map)))
191
962
}
192

            
193
const KEYWORDS: [&str; 21] = [
194
    "forall", "exists", "such", "that", "letting", "find", "minimise", "maximise", "subject", "to",
195
    "where", "and", "or", "not", "if", "then", "else", "in", "sum", "product", "bool",
196
];
197

            
198
595
fn keyword_as_identifier(ctx: &mut ParseContext) {
199
595
    let mut stack = vec![*ctx.root];
200
30772
    while let Some(node) = stack.pop() {
201
30177
        if (node.kind() == "variable" || node.kind() == "identifier" || node.kind() == "parameter")
202
2289
            && let Ok(text) = node.utf8_text(ctx.source_code.as_bytes())
203
        {
204
2289
            let ident = text.trim();
205
2289
            if KEYWORDS.contains(&ident) {
206
55
                let start_point = node.start_position();
207
55
                let end_point = node.end_position();
208
55
                ctx.errors.push(RecoverableParseError::new(
209
55
                    format!("Keyword '{ident}' used as identifier"),
210
55
                    Some(tree_sitter::Range {
211
55
                        start_byte: node.start_byte(),
212
55
                        end_byte: node.end_byte(),
213
55
                        start_point,
214
55
                        end_point,
215
55
                    }),
216
55
                ));
217
2234
            }
218
27888
        }
219

            
220
        // push children onto stack
221
30177
        for i in 0..node.child_count() {
222
29582
            if let Some(child) = u32::try_from(i).ok().and_then(|i| node.child(i)) {
223
29582
                stack.push(child);
224
29582
            }
225
        }
226
    }
227
595
}
228

            
229
2
pub fn parse_essence(src: &str) -> Result<(Model, SourceMap), Box<ParseErrorCollection>> {
230
2
    let context = Arc::new(RwLock::new(Context::default()));
231
2
    let mut errors = vec![];
232
2
    match parse_essence_with_context_and_map(src, context, &mut errors) {
233
2
        Ok(Some((model, source_map))) => {
234
2
            debug_assert_model_well_formed(&model, "tree-sitter");
235
2
            Ok((model, source_map))
236
        }
237
        Ok(None) => {
238
            // Recoverable errors were found, return them as a ParseErrorCollection
239
            Err(Box::new(ParseErrorCollection::multiple(
240
                errors,
241
                Some(src.to_string()),
242
                None,
243
            )))
244
        }
245
        Err(fatal) => Err(Box::new(ParseErrorCollection::fatal(fatal))),
246
    }
247
2
}
248

            
249
mod test {
250
    #[allow(unused_imports)]
251
    use crate::parse_essence;
252
    #[allow(unused_imports)]
253
    use conjure_cp_core::ast::{Atom, Expression, Metadata, Moo, Name};
254
    #[allow(unused_imports)]
255
    use conjure_cp_core::{domain_int, matrix_expr, range};
256
    #[allow(unused_imports)]
257
    use std::ops::Deref;
258

            
259
    #[test]
260
1
    pub fn test_parse_xyz() {
261
1
        let src = "
262
1
        find x, y, z : int(1..4)
263
1
        such that x + y + z = 4
264
1
        such that x >= y
265
1
        ";
266

            
267
1
        let (model, _source_map) = parse_essence(src).unwrap();
268

            
269
1
        let st = model.symbols();
270
1
        let x = st.lookup(&Name::user("x")).unwrap();
271
1
        let y = st.lookup(&Name::user("y")).unwrap();
272
1
        let z = st.lookup(&Name::user("z")).unwrap();
273
1
        assert_eq!(x.domain(), Some(domain_int!(1..4)));
274
1
        assert_eq!(y.domain(), Some(domain_int!(1..4)));
275
1
        assert_eq!(z.domain(), Some(domain_int!(1..4)));
276

            
277
1
        let constraints = model.constraints();
278
1
        assert_eq!(constraints.len(), 2);
279

            
280
1
        let c1 = constraints[0].clone();
281
1
        let x_e = Expression::Atomic(Metadata::new(), Atom::new_ref(x));
282
1
        let y_e = Expression::Atomic(Metadata::new(), Atom::new_ref(y));
283
1
        let z_e = Expression::Atomic(Metadata::new(), Atom::new_ref(z));
284
1
        assert_eq!(
285
            c1,
286
1
            Expression::Eq(
287
1
                Metadata::new(),
288
1
                Moo::new(Expression::Sum(
289
1
                    Metadata::new(),
290
1
                    Moo::new(matrix_expr!(
291
1
                        Expression::Sum(
292
1
                            Metadata::new(),
293
1
                            Moo::new(matrix_expr!(x_e.clone(), y_e.clone()))
294
1
                        ),
295
1
                        z_e
296
1
                    ))
297
1
                )),
298
1
                Moo::new(Expression::Atomic(Metadata::new(), 4.into()))
299
1
            )
300
        );
301

            
302
1
        let c2 = constraints[1].clone();
303
1
        assert_eq!(
304
            c2,
305
1
            Expression::Geq(Metadata::new(), Moo::new(x_e), Moo::new(y_e))
306
        );
307
1
    }
308

            
309
    #[test]
310
1
    pub fn test_parse_letting_index() {
311
1
        let src = "
312
1
        letting a be [ [ 1,2,3 ; int(1,2,4) ], [ 1,3,2 ; int(1,2,4) ], [ 3,2,1 ; int(1,2,4) ] ; int(-2..0) ]
313
1
        find b: int(1..5)
314
1
        such that
315
1
        b < a[-2,2],
316
1
        allDiff(a[-2,..])
317
1
        ";
318

            
319
1
        let (model, _source_map) = parse_essence(src).unwrap();
320
1
        let st = model.symbols();
321
1
        let a_decl = st.lookup(&Name::user("a")).unwrap();
322
1
        let a = a_decl.as_value_letting().unwrap().deref().clone();
323
1
        assert_eq!(
324
            a,
325
1
            matrix_expr!(
326
1
                matrix_expr!(1.into(), 2.into(), 3.into() ; domain_int!(1, 2, 4)),
327
1
                matrix_expr!(1.into(), 3.into(), 2.into() ; domain_int!(1, 2, 4)),
328
1
                matrix_expr!(3.into(), 2.into(), 1.into() ; domain_int!(1, 2, 4));
329
1
                domain_int!(-2..0)
330
            )
331
        )
332
1
    }
333
}