@@ -41,7 +41,7 @@ pub fn parse_essence_file_native(
41
41
let mut constraint_vec: Vec < Expression > = Vec :: new ( ) ;
42
42
for constraint in named_children ( & statement) {
43
43
if constraint. kind ( ) != "single_line_comment" {
44
- constraint_vec. push ( parse_constraint ( constraint, & source_code) ) ;
44
+ constraint_vec. push ( parse_constraint ( constraint, & source_code, & statement ) ) ;
45
45
}
46
46
}
47
47
model. as_submodel_mut ( ) . add_constraints ( constraint_vec) ;
@@ -51,6 +51,19 @@ pub fn parse_essence_file_native(
51
51
let letting_vars = parse_letting_statement ( statement, & source_code) ;
52
52
model. as_submodel_mut ( ) . symbols_mut ( ) . extend ( letting_vars) ;
53
53
}
54
+ "dominance_relation" => {
55
+ let inner = statement
56
+ . child ( 1 )
57
+ . expect ( "Expected a sub-expression inside `dominanceRelation`" ) ;
58
+ let expr = parse_constraint ( inner, & source_code, & statement) ;
59
+ let dominance = Expression :: DominanceRelation ( Metadata :: new ( ) , Box :: new ( expr) ) ;
60
+ if model. dominance . is_some ( ) {
61
+ return Err ( EssenceParseError :: ParseError ( Error :: Parse (
62
+ "Duplicate dominance relation" . to_owned ( ) ,
63
+ ) ) ) ;
64
+ }
65
+ model. dominance = Some ( dominance) ;
66
+ }
54
67
_ => {
55
68
let kind = statement. kind ( ) ;
56
69
return Err ( EssenceParseError :: ParseError ( Error :: Parse (
@@ -63,8 +76,9 @@ pub fn parse_essence_file_native(
63
76
}
64
77
65
78
fn get_tree ( path : & str , filename : & str , extension : & str ) -> ( Tree , String ) {
66
- let source_code = fs:: read_to_string ( format ! ( "{path}/{filename}.{extension}" ) )
67
- . expect ( "Failed to read the source code file" ) ;
79
+ let pth = format ! ( "{path}/{filename}.{extension}" ) ;
80
+ let source_code = fs:: read_to_string ( & pth)
81
+ . unwrap_or_else ( |_| panic ! ( "Failed to read the source code file {}" , pth) ) ;
68
82
let mut parser = Parser :: new ( ) ;
69
83
parser. set_language ( & LANGUAGE . into ( ) ) . unwrap ( ) ;
70
84
(
@@ -201,7 +215,7 @@ fn parse_letting_statement(letting_statement_list: Node, source_code: &str) -> S
201
215
for name in temp_symbols {
202
216
symbol_table. insert ( Rc :: new ( Declaration :: new_value_letting (
203
217
Name :: UserName ( String :: from ( name) ) ,
204
- parse_constraint ( expr_or_domain, source_code) ,
218
+ parse_constraint ( expr_or_domain, source_code, & letting_statement_list ) ,
205
219
) ) ) ;
206
220
}
207
221
}
@@ -219,24 +233,24 @@ fn parse_letting_statement(letting_statement_list: Node, source_code: &str) -> S
219
233
symbol_table
220
234
}
221
235
222
- fn parse_constraint ( constraint : Node , source_code : & str ) -> Expression {
236
+ fn parse_constraint ( constraint : Node , source_code : & str , root : & Node ) -> Expression {
223
237
match constraint. kind ( ) {
224
- "constraint" | "expression" => child_expr ( constraint, source_code) ,
238
+ "constraint" | "expression" => child_expr ( constraint, source_code, root ) ,
225
239
"not_expr" => Expression :: Not (
226
240
Metadata :: new ( ) ,
227
- Box :: new ( child_expr ( constraint, source_code) ) ,
241
+ Box :: new ( child_expr ( constraint, source_code, root ) ) ,
228
242
) ,
229
243
"abs_value" => Expression :: Abs (
230
244
Metadata :: new ( ) ,
231
- Box :: new ( child_expr ( constraint, source_code) ) ,
245
+ Box :: new ( child_expr ( constraint, source_code, root ) ) ,
232
246
) ,
233
247
"negative_expr" => Expression :: Neg (
234
248
Metadata :: new ( ) ,
235
- Box :: new ( child_expr ( constraint, source_code) ) ,
249
+ Box :: new ( child_expr ( constraint, source_code, root ) ) ,
236
250
) ,
237
251
"exponent" | "product_expr" | "sum_expr" | "comparison" | "and_expr" | "or_expr"
238
252
| "implication" => {
239
- let expr1 = child_expr ( constraint, source_code) ;
253
+ let expr1 = child_expr ( constraint, source_code, root ) ;
240
254
let op = constraint. child ( 1 ) . unwrap_or_else ( || {
241
255
panic ! (
242
256
"Error: missing node in expression of kind {}" ,
@@ -250,7 +264,7 @@ fn parse_constraint(constraint: Node, source_code: &str) -> Expression {
250
264
constraint. kind( )
251
265
)
252
266
} ) ;
253
- let expr2 = parse_constraint ( expr2_node, source_code) ;
267
+ let expr2 = parse_constraint ( expr2_node, source_code, root ) ;
254
268
255
269
match op_type {
256
270
"**" => Expression :: UnsafePow ( Metadata :: new ( ) , Box :: new ( expr1) , Box :: new ( expr2) ) ,
@@ -280,7 +294,7 @@ fn parse_constraint(constraint: Node, source_code: &str) -> Expression {
280
294
"quantifier_expr" => {
281
295
let mut expr_list = Vec :: new ( ) ;
282
296
for expr in named_children ( & constraint) {
283
- expr_list. push ( parse_constraint ( expr, source_code) ) ;
297
+ expr_list. push ( parse_constraint ( expr, source_code, root ) ) ;
284
298
}
285
299
286
300
let quantifier = constraint. child ( 0 ) . unwrap_or_else ( || {
@@ -331,6 +345,18 @@ fn parse_constraint(constraint: Node, source_code: &str) -> Expression {
331
345
Atom :: Reference ( Name :: UserName ( variable_name) ) ,
332
346
)
333
347
}
348
+ "from_solution" => match root. kind ( ) {
349
+ "dominance_relation" => {
350
+ let inner = child_expr ( constraint, source_code, root) ;
351
+ match inner {
352
+ Expression :: Atomic ( _, _) => {
353
+ Expression :: FromSolution ( Metadata :: new ( ) , Box :: new ( inner) )
354
+ }
355
+ _ => panic ! ( "Expression inside a `fromSolution()` must be a variable name" ) ,
356
+ }
357
+ }
358
+ _ => panic ! ( "`fromSolution()` is only allowed inside dominance relation definitions" ) ,
359
+ } ,
334
360
_ => panic ! ( "{} is not a recognized node kind" , constraint. kind( ) ) ,
335
361
}
336
362
}
@@ -339,9 +365,9 @@ fn named_children<'a>(node: &'a Node<'a>) -> impl Iterator<Item = Node<'a>> + 'a
339
365
( 0 ..node. named_child_count ( ) ) . filter_map ( |i| node. named_child ( i) )
340
366
}
341
367
342
- fn child_expr ( node : Node , source_code : & str ) -> Expression {
368
+ fn child_expr ( node : Node , source_code : & str , root : & Node ) -> Expression {
343
369
let child = node
344
370
. named_child ( 0 )
345
371
. unwrap_or_else ( || panic ! ( "Error: missing node in expression of kind {}" , node. kind( ) ) ) ;
346
- parse_constraint ( child, source_code)
372
+ parse_constraint ( child, source_code, root )
347
373
}
0 commit comments