retex-engine 1.0.0-rc.2

The TeX typesetting engine for retex
Documentation
//! 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);
    }
}