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
//! Command codes and their ranges.
//!
//! The numbering follows LuaTeX's `token.commands()`. TeX relies on the
//! order of command codes: expandable commands come after `max_command`,
//! internal quantities lie between `min_internal` and `max_internal`, and
//! commands after `max_non_prefixed_command` are assignments.
pub use crate::cmd_gen::Cmd;
impl Cmd {
/// The largest category code of a character token (`max_char_code`).
pub const MAX_CHAR_CODE: Cmd = Cmd::DelimNum;
/// The smallest command that can follow `\the` (`min_internal`).
pub const MIN_INTERNAL: Cmd = Cmd::CharGiven;
/// The largest command that cannot be prefixed by `\global`.
pub const MAX_NON_PREFIXED: Cmd = Cmd::LastItem;
/// The largest command that can follow `\the` (`max_internal`).
pub const MAX_INTERNAL: Cmd = Cmd::Register;
/// The largest unexpandable command (`max_command`).
pub const MAX_COMMAND: Cmd = Cmd::SetFontId;
/// The command with numeric code `n`.
#[inline]
pub fn from_u8(n: u8) -> Cmd {
Cmd::ALL[n as usize]
}
/// Whether the command is expandable.
#[inline]
pub fn is_expandable(self) -> bool {
self > Cmd::MAX_COMMAND
}
/// Whether the command is a macro call (`call` … `long_outer_call`).
#[inline]
pub fn is_call(self) -> bool {
self >= Cmd::Call && self <= Cmd::LongOuterCall
}
/// Whether the command is an outer macro or a special outer marker
/// (`cur_cmd >= outer_call`).
#[inline]
pub fn is_outer_or_special(self) -> bool {
self >= Cmd::OuterCall
}
/// Whether the command denotes an internal quantity.
#[inline]
pub fn is_internal(self) -> bool {
self >= Cmd::MIN_INTERNAL && self <= Cmd::MAX_INTERNAL
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ranges_are_ordered() {
assert!(Cmd::MAX_CHAR_CODE < Cmd::CharNum);
assert!(Cmd::MIN_INTERNAL <= Cmd::MAX_NON_PREFIXED);
assert!(Cmd::MAX_NON_PREFIXED < Cmd::ToksRegister);
assert!(Cmd::MAX_INTERNAL < Cmd::Advance);
assert!(Cmd::MAX_COMMAND < Cmd::UndefinedCs);
assert!(Cmd::UndefinedCs.is_expandable());
assert!(!Cmd::SetFontId.is_expandable());
assert!(Cmd::LongOuterCall.is_call());
assert_eq!(Cmd::from_u8(147), Cmd::Call);
}
}