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