conjure_cp_core/lib.rs
1#[doc(hidden)]
2pub extern crate self as conjure_cp_core;
3
4#[doc(hidden)]
5pub use ast::Model;
6
7pub mod ast;
8
9// NOTE: this module defines the bug! macro, which is exported at the crate level, and has no other
10// contents.
11mod bug;
12
13pub mod context;
14pub mod error;
15pub mod instantiate;
16pub mod parse;
17pub mod representation;
18pub mod rule_engine;
19pub mod settings;
20pub mod solver;
21pub mod stats;
22
23// Various internal helper functions
24mod utils;
25
26/// Creates a [`Domain::Int`](ast::Domain::Int).
27///
28/// # Examples
29/// ```
30/// use conjure_cp_core::{domain_int,range,ast::Domain};
31///
32/// let a = 2*10;
33/// assert_eq!(domain_int!(1..5,a+2,), Domain::int(vec![range!(1..5),range!(a+2)]));
34/// assert_eq!(domain_int!(), Domain::int_ground(vec![]))
35/// ```
36#[macro_export]
37macro_rules! domain_int {
38 () => {$crate::ast::Domain::int_ground(vec![])};
39
40 // when parsing expressions, rust groups 1..2 into a single token tree, (1..2)
41 // however, we want it to be three seperate token trees [1,..,2] for parsing.
42 // use defile to turn it back into 3 token trees
43 ($($e:expr),+ $(,)?) => {::defile::defile! { $crate::ast::Domain::int(vec![$($crate::range!(@$e)),+]) } };
44}
45
46/// Creates a [`GroundDomain::Int`]
47#[macro_export]
48macro_rules! domain_int_ground {
49 () => {$crate::ast::GroundDomain::Int(vec![]).into()};
50
51 // when parsing expressions, rust groups 1..2 into a single token tree, (1..2)
52 // however, we want it to be three seperate token trees [1,..,2] for parsing.
53 // use defile to turn it back into 3 token trees
54 ($($e:expr),+ $(,)?) => {::defile::defile! { $crate::ast::GroundDomain::Int(vec![$($crate::range!(@$e)),+]).into() } };
55}
56
57/// Creates a [`Range`](ast::Range).
58///
59/// # Examples
60///
61/// ```
62/// use conjure_cp_core::{range,ast::Range};
63///
64/// let a = 2*10;
65/// assert_eq!(range!(..a),Range::UnboundedL(a));
66/// assert_eq!(range!(2*5..),Range::UnboundedR(10));
67/// assert_eq!(range!(..10),Range::UnboundedL(10));
68/// assert_eq!(range!(1..5),Range::Bounded(1,5));
69/// assert_eq!(range!(Some(10).unwrap()),Range::Single(10));
70/// ```
71#[macro_export]
72macro_rules! range {
73 // decl macros have no lookahead, hence this nonsense with pushdown automata.
74
75 // @hasLowerbound: have atleast one token of the lower bound on the stack
76 (@hasLowerBound [$($lower:tt)+] -> ..) => {$crate::ast::Range::UnboundedR($crate::as_expr!($($lower)+))};
77 (@hasLowerBound [$($lower:tt)+] -> .. $($tail:tt)+) => {$crate::ast::Range::Bounded($crate::as_expr!($($lower)+),$crate::as_expr!($($tail)+))};
78 (@hasLowerBound [$($lower:tt)+] -> $b:tt $($tail:tt)*) => {range!(@hasLowerBound [$($lower)+ $b] -> $($tail)*)};
79 (@hasLowerBound [$($lower:tt)+] ->) => {$crate::ast::Range::Single($crate::as_expr!($($lower)+))};
80
81 // initial tokens
82 (.. $($a:tt)+) => {$crate::ast::Range::UnboundedL($crate::as_expr!($($a)+))};
83
84 ($a:tt $($tail:tt)*) => {range!(@hasLowerBound [$a] -> $($tail)*)};
85
86}
87
88// coorce a tt fragment into a expr fragment
89// https://lukaswirth.dev/tlborm/decl-macros/building-blocks/ast-coercion.html
90#[macro_export]
91#[doc(hidden)]
92macro_rules! as_expr {
93 ($e:expr) => {
94 $e
95 };
96}