ruleset/lib.rs
1mod digest;
2pub(self) use self::digest::*;
3
4mod matcher;
5pub use self::matcher::*;
6
7mod rule;
8pub use self::rule::*;
9
10mod subrule;
11pub use self::subrule::*;
12
13mod consequence;
14pub use self::consequence::*;
15
16mod consequent;
17pub use self::consequent::*;
18
19mod reducible;
20pub use reducible::*;
21
22mod reducer;
23pub use reducer::*;
24
25mod reductum;
26pub use self::reductum::*;
27
28mod ruleset;
29pub use self::ruleset::*;
30
31mod chart;
32pub use self::chart::*;
33
34mod reduction;
35pub use reduction::*;
36
37mod input;
38pub use input::*;
39
40mod conclusion;
41pub use self::conclusion::*;
42
43mod item;
44pub use item::*;
45
46mod addressable;
47pub(self) use addressable::*;
48
49mod child;
50pub(self) use child::*;
51
52mod child_maybe;
53pub(self) use child_maybe::*;
54
55mod r#match;
56pub use r#match::*;
57
58mod any_error;
59pub use any_error::*;
60
61mod parser;
62pub use self::parser::*;
63
64mod syntactic_category;
65pub use self::syntactic_category::*;
66
67mod tree;
68pub use self::tree::*;
69
70mod iterator_reducer_ext;
71pub use self::iterator_reducer_ext::*;
72
73mod by_matches;
74
75pub mod prelude;
76
77#[allow(rustdoc::invalid_rust_codeblocks)]
78/// A macro for defining a ruleset.
79///
80/// A single ruleset is to model a [language construct](https://en.wikipedia.org/wiki/Language_construct) which is typically expressed as disjunction of some alternatives.
81///
82/// This macro currently doesn't support disjunctions whose alternatives have different lengths, so if you want to express a Kleene star or plus, you have to write at least two rules.
83///
84/// # Usage
85///
86/// The basic syntax of the macro body is a sequence of `r` rules, each being terminated by a semicolon:
87///
88/// ```rust
89/// ruleset::ruleset! {
90/// <rule 0>;
91/// ⋮
92/// <rule r-1>;
93/// }
94/// ```
95///
96/// Each `<rule i>` consists of a sequence of `a` antecedents, a `=>`, and `c` consequents in this order, with the restriction that either `c == 1` (i.e. context-free rules) or `a == c` (i.e. context-sensitive rules) must hold:
97///
98/// ```rust
99/// <antecedent 0>, …, <antecedent a-1> => <consequent 0>, …, <consequent c-1>
100/// ```
101///
102/// Each `<antecedent i>` is a pattern, and each `<consequent i>` is an expression, in the sense of Rust's syntax.
103///
104/// In the following sections, we premise that the symbol type is defined as:
105///
106/// ```rust
107/// enum Symbol {
108/// Str(&'static str),
109/// String(String),
110/// BoxString(Box<String>),
111/// Usize(usize),
112/// BoxUsize(Box<usize>),
113/// }
114/// ```
115///
116/// ## Miscellaneous Tips
117///
118/// ### Underscores
119///
120/// You can use underscores not only in antecedents but also in consequents in context-sensitive rules:
121///
122/// ```rust
123/// ruleset::ruleset! {
124/// Str("foo"), Str("bar") => _, Str("baz");
125/// Str("foo"), Str("bar") => Str("foo"), Str("baz");
126/// }
127/// ```
128///
129/// The above two rules are theoretically equivalent, but the former is rather efficient when it comes to computing.
130///
131/// ### Underscore-Dots
132///
133/// Context-free rules are actually a special case of context-sensitive rules where all consequents except the last one are underscore-dots:
134///
135/// ```rust
136/// ruleset::ruleset! {
137/// Str("foo"), Str("bar"), Str("baz") => Str("qux");
138/// Str("foo"), Str("bar"), Str("baz") => _. _. Str("qux");
139/// }
140/// ```
141///
142/// The former is the shorthand for the latter. An underscore-dot means that the corresponding antecedent will belong to the next non-underscore-dot consequent.
143///
144/// Use of underscore-dots is sometimes necessary to avoid ambiguity of context-sensitive rules:
145///
146/// ```rust
147/// ruleset::ruleset! {
148/// // Str("foo"), Str("bar"), Str("baz") => Str("qux"), Str("quux");
149/// Str("foo"), Str("bar"), Str("baz") => Str("qux"), _. Str("quux");
150/// Str("foo"), Str("bar"), Str("baz") => _. Str("qux"), Str("quux");
151/// }
152/// ```
153///
154/// The first rule (commented out) is ambiguous because it's unclear whether the second antecedent corresponds to the first consequent or the second one, thus results in an error. One of the latter two rules must be used instead.
155///
156/// ### Early Return
157///
158/// Each rule is internally implemented as a closure whose return type is fallible (i.e. implements `FromResidual<Option<Infallible>>` and `FromResidual<Result<Infallible, E>>`), so you can use the `?` operator inside guards and consequent expressions:
159///
160/// ```rust
161/// ruleset::ruleset! {
162/// Str(str) if let n = str.parse()? => Usize(n);
163/// Str(str) => Usize(str.parse()?);
164/// }
165/// ```
166///
167/// Above two rules are effectively equivalent, but it's a good habit to bail out of pattern matching as soon as possible; consider the following example:
168///
169/// ```rust
170/// ruleset::ruleset! {
171/// Str(str0), Str(str1), Str(str2) => Usize(str0.parse()? * str1.parse()? * str2.parse()?);
172/// }
173/// ```
174///
175/// In this case, validation of the first input is deferred until all the three inputs has been matched and captured. This will obviously lead to wasted computation. However, most likely you'd want to integrate error reporting mechanisms to the parser, and there may be cases where validations can't be performed until all informations to report have been collected from the inputs, so you should consider what suits your case.
176///
177/// ### Error Reporting
178///
179/// As stated in the previous section, you can return `Err(content)` from within a rule. Any value of types that implement `ruleset::AnyError` trait can be the content of an error. The returned errors are just discarded for now. The API surface around this is under consideration.
180///
181/// `ruleset::AnyError` is blanket-implemented when a type implements `std::any::Any + std::error::Error`.
182///
183/// ## Extensions to Pattern Matching Syntax and Semantics
184///
185/// Basically, the syntax of rules is designed to be similar to stable Rust's pattern matching syntax, but there are some extensions mimicking nightly features and proposals for improved ergonomics. These are all handled by the macro, so you can use them in stable Rust.
186///
187/// ### `if_let_guard`
188///
189/// You can use `if let` guards in antecedents, which is behind the [`if_let_guard`](https://github.com/rust-lang/rust/issues/51114) feature gate at the time of writing. Like in antecedent patterns, captured portions of guard patterns are also available for later use:
190///
191/// ```rust
192/// ruleset::ruleset! {
193/// Str(s) if let Ok(n) = s.parse() => Usize(n);
194/// }
195/// ```
196///
197/// The macro (ab)uses the `if let` syntax not only for guards but also for arbitrary local variable bindings that can be used for later guards or consequent values, thus `#[allow(irrefutable_let_patterns)]` is always annotated internally to suppress the warnings for trivial bindings like `if let a = 42`.
198///
199/// ### `let_chains`
200///
201/// You can conjoint or disjoint `let` bindings and normal boolean expressions in guards, which is behind the [`let_chains`](https://github.com/rust-lang/rust/issues/53667) feature gate at the time of writing:
202///
203/// ```rust
204/// ruleset::ruleset! {
205/// Str(s) if let Ok(n) = s.parse() && n % 42 == 0 => Usize(n);
206/// }
207/// ```
208///
209/// ### Deref Patterns
210///
211/// For ease of writing destructuring patterns against your nested `Deref` types, this macro supports [deref patterns](https://github.com/rust-lang/rfcs/issues/2099), so you can use `&` to dereference to the target type amidst patterns:
212///
213/// ```rust
214/// ruleset::ruleset! {
215/// String(&"hello") => String("world".into());
216/// BoxString(&&"hello") => BoxString(Box::new("world".into()));
217/// }
218/// ```
219///
220/// This allows you to avoid using guards in certain cases, for example:
221///
222/// - `String(ref s) if s == "hello"` can be replaced with `String(&"hello")`
223/// - `BoxUsize(ref n) if (0..42).contains(&**n)` can be replaced with `BoxUsize(&(0..42))`
224///
225/// ### Equal Constraint
226///
227/// It's sometimes the case that multiple fields in patterns are required to be equal, but writing such trivial constraints using guards looks verbose. You can instead repeat the same identifier for such fields:
228///
229/// ```rust
230/// ruleset::ruleset! {
231/// Usize(n), Usize(n) => Str("same");
232/// Usize(n), Usize(m) if n == m => Str("same");
233/// }
234/// ```
235///
236/// Above two rules are equivalent; it internally uses the `PartialEq` equality.
237///
238/// This semantics won't be incorporated into the compiler at least in the near future due to [several difficulties](https://internals.rust-lang.org/t/vibe-check-equality-patterns/21775), but for us it's perfectly fine as we don't care for exhaustiveness and mutability of antecedents.
239pub use ruleset_macros::ruleset;
240
241pub(crate) use ruleset_macros::*;
242
243#[doc(hidden)]
244pub use stable_try_trait_v2::*;