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