tor_netdoc/parse/rules.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
//! Keywords for interpreting items and rules for validating them.
use crate::parse::keyword::Keyword;
use crate::parse::tokenize::Item;
use crate::{NetdocErrorKind as EK, Result};
/// May an Item take an object?
#[derive(Copy, Clone)]
enum ObjKind {
/// No object is allowed.
NoObj,
/// An object is required.
RequireObj,
/// An object is optional.
ObjOk,
}
/// A set of restrictions to place on Items for a single keyword.
///
/// These are built by the TokenFmtBuilder API.
#[derive(Clone)]
pub(crate) struct TokenFmt<T: Keyword> {
/// Which keyword is being restricted?
kwd: T,
/// If present, a lower bound on how many arguments may be present.
min_args: Option<usize>,
/// If present, an upper bound on how many arguments may be present.
max_args: Option<usize>,
/// If true, then at least one of this Item must appear.
required: bool,
/// If false, then no more than one this Item may appear.
may_repeat: bool,
/// May this Item have an object? Must it?
obj: ObjKind,
}
impl<T: Keyword> TokenFmt<T> {
/// Return the keyword that this rule restricts.
pub(crate) fn kwd(&self) -> T {
self.kwd
}
/// Check whether a single Item matches this TokenFmt rule, with respect
/// to its number of arguments.
fn item_matches_args(&self, item: &Item<'_, T>) -> Result<()> {
let n_args = item.n_args();
if let Some(max) = self.max_args {
if n_args > max {
return Err(EK::TooManyArguments
.with_msg(self.kwd.to_str())
.at_pos(item.pos()));
}
}
if let Some(min) = self.min_args {
if n_args < min {
return Err(EK::TooFewArguments
.with_msg(self.kwd.to_str())
.at_pos(item.pos()));
}
}
Ok(())
}
/// Check whether a single Item matches a TokenFmt rule, with respect
/// to its object's presence and type.
fn item_matches_obj(&self, item: &Item<'_, T>) -> Result<()> {
match (&self.obj, item.has_obj()) {
(ObjKind::NoObj, true) => Err(EK::UnexpectedObject
.with_msg(self.kwd.to_str())
.at_pos(item.pos())),
(ObjKind::RequireObj, false) => Err(EK::MissingObject
.with_msg(self.kwd.to_str())
.at_pos(item.pos())),
(_, _) => Ok(()),
}
}
/// Check whether a single item has the right number of arguments
/// and object.
pub(crate) fn check_item(&self, item: &Item<'_, T>) -> Result<()> {
self.item_matches_args(item)?;
self.item_matches_obj(item)
}
/// Check whether this kind of item may appear this many times.
pub(crate) fn check_multiplicity(&self, items: &[Item<'_, T>]) -> Result<()> {
match items.len() {
0 => {
if self.required {
return Err(EK::MissingToken.with_msg(self.kwd.to_str()));
}
}
1 => (),
_ => {
if !self.may_repeat {
return Err(EK::DuplicateToken
.with_msg(self.kwd.to_str())
.at_pos(items[1].pos()));
}
}
}
Ok(())
}
}
/// Represents a TokenFmt under construction.
///
/// To construct a rule, create this type with Keyword::rule(), then
/// call method on it to set its fields, and then pass it to
/// SectionRules::add().
///
/// # Example
///
/// ```ignore
/// // There must be exactly one "ROUTER" entry, with 5 or more arguments.
/// section_rules.add(D.rule().required().args(5..));
/// ```
///
/// TODO: I'd rather have this be pub(crate), but I haven't figured out
/// how to make that work. There are complicated cascading side-effects.
pub struct TokenFmtBuilder<T: Keyword>(TokenFmt<T>);
impl<T: Keyword> From<TokenFmtBuilder<T>> for TokenFmt<T> {
fn from(builder: TokenFmtBuilder<T>) -> Self {
builder.0
}
}
impl<T: Keyword> TokenFmtBuilder<T> {
/// Make a new TokenFmtBuilder with default behavior.
///
/// (By default, all arguments are allowed, the Item may appear 0
/// or 1 times, and it may not take an object.)
pub(crate) fn new(t: T) -> Self {
Self(TokenFmt {
kwd: t,
min_args: None,
max_args: None,
required: false,
may_repeat: false,
obj: ObjKind::NoObj,
})
}
/// Indicate that this Item is required.
///
/// By default, no item is required.
pub(crate) fn required(self) -> Self {
Self(TokenFmt {
required: true,
..self.0
})
}
/// Indicate that this Item is required.
///
/// By default, items may not repeat.
pub(crate) fn may_repeat(self) -> Self {
Self(TokenFmt {
may_repeat: true,
..self.0
})
}
/// Indicate that this Item takes no arguments.
///
/// By default, items may take any number of arguments.
pub(crate) fn no_args(self) -> Self {
Self(TokenFmt {
max_args: Some(0),
..self.0
})
}
/// Indicate that this item takes a certain number of arguments.
///
/// The number of arguments is provided as a range, like `5..`.
pub(crate) fn args<R>(self, r: R) -> Self
where
R: std::ops::RangeBounds<usize>,
{
use std::ops::Bound::*;
let min_args = match r.start_bound() {
Included(x) => Some(*x),
Excluded(x) => Some(*x + 1),
Unbounded => None,
};
let max_args = match r.end_bound() {
Included(x) => Some(*x),
Excluded(x) => Some(*x - 1),
Unbounded => None,
};
Self(TokenFmt {
min_args,
max_args,
..self.0
})
}
/// Indicate that this token takes an optional object.
///
/// By default, objects are not allowed.
pub(crate) fn obj_optional(self) -> Self {
Self(TokenFmt {
obj: ObjKind::ObjOk,
..self.0
})
}
/// Indicate that this token takes an required object.
///
/// By default, objects are not allowed.
pub(crate) fn obj_required(self) -> Self {
Self(TokenFmt {
obj: ObjKind::RequireObj,
..self.0
})
}
}