Skip to content

Commit 1557cca

Browse files
Add grammar documentation.
1 parent 569a930 commit 1557cca

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

fuzzcheck/src/mutators/grammar/grammar.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,26 @@ pub enum Grammar {
2323
pub fn regex(s: &str) -> Rc<Grammar> {
2424
grammar_from_regex(s)
2525
}
26+
2627
#[no_coverage]
28+
/// Creates an [`Rc<Grammar>`] which outputs characters in the given range.
29+
///
30+
/// For example, to generate characters in the range 'a' to 'z' (inclusive), one
31+
/// could use this code
32+
///
33+
/// ```
34+
/// let a_to_z = literal_ranges('a'..='z');
35+
/// ```
2736
pub fn literal_ranges(ranges: Vec<RangeInclusive<char>>) -> Rc<Grammar> {
2837
Rc::new(Grammar::Literal(ranges))
2938
}
39+
3040
#[no_coverage]
41+
/// Creates an [`Rc<Grammar>`] which matches a single character literal.
42+
///
43+
/// ```
44+
/// let l = literal('l');
45+
/// ```
3146
pub fn literal(l: char) -> Rc<Grammar> {
3247
Rc::new(Grammar::Literal(vec![l..=l]))
3348
}
@@ -48,15 +63,31 @@ where
4863
};
4964
Rc::new(Grammar::Literal(vec![start..=end]))
5065
}
66+
67+
/// Produces a grammar which will choose between the provided grammars.
5168
#[no_coverage]
5269
pub fn alternation(gs: impl IntoIterator<Item = Rc<Grammar>>) -> Rc<Grammar> {
5370
Rc::new(Grammar::Alternation(gs.into_iter().collect()))
5471
}
72+
73+
/// Produces a grammar which will concatenate the output of all the provided
74+
/// grammars together, in order.
75+
///
76+
/// For example, the grammar
77+
/// ```
78+
/// concatenation([
79+
/// regex("fuzz"),
80+
/// regex("check")
81+
/// ])
82+
/// ```
83+
/// would output "fuzzcheck".
5584
#[no_coverage]
5685
pub fn concatenation(gs: impl IntoIterator<Item = Rc<Grammar>>) -> Rc<Grammar> {
5786
Rc::new(Grammar::Concatenation(gs.into_iter().collect()))
5887
}
88+
5989
#[no_coverage]
90+
/// Repeats the provided grammar some number of times in the given range.
6091
pub fn repetition<R>(gs: Rc<Grammar>, range: R) -> Rc<Grammar>
6192
where
6293
R: RangeBounds<usize>,
@@ -75,10 +106,19 @@ where
75106
}
76107

77108
#[no_coverage]
109+
/// Used to indicate a point of recursion to Fuzzcheck. Should be combined with
110+
/// [`recursive`].
111+
///
112+
/// See the module documentation ([`super`]) for an example on how to use it.
78113
pub fn recurse(g: &Weak<Grammar>) -> Rc<Grammar> {
79114
Rc::new(Grammar::Recurse(g.clone()))
80115
}
116+
81117
#[no_coverage]
118+
/// Creates a recursive grammar. This function should be combined with
119+
/// [`recurse`] to make recursive calls.
120+
///
121+
/// See the module documentation ([`super`]) for an example on how to use it.
82122
pub fn recursive(data_fn: impl Fn(&Weak<Grammar>) -> Rc<Grammar>) -> Rc<Grammar> {
83123
Rc::new(Grammar::Recursive(Rc::new_cyclic(|g| {
84124
Rc::try_unwrap(data_fn(g)).unwrap()

0 commit comments

Comments
 (0)