mitex_spec/
preludes.rs

1// todo: remove me
2#![allow(missing_docs)]
3
4pub mod command {
5    use crate::{ArgShape, CommandSpecItem, ContextFeature};
6
7    pub fn define_command(len: u8) -> CommandSpecItem {
8        CommandSpecItem::Cmd(crate::CmdShape {
9            args: crate::ArgShape::Right {
10                pattern: crate::ArgPattern::FixedLenTerm { len },
11            },
12            alias: None,
13        })
14    }
15
16    pub fn define_glob_command(reg: &str, alias: &str) -> CommandSpecItem {
17        CommandSpecItem::Cmd(crate::CmdShape {
18            args: crate::ArgShape::Right {
19                pattern: crate::ArgPattern::Glob {
20                    pattern: reg.into(),
21                },
22            },
23            alias: Some(alias.to_owned()),
24        })
25    }
26
27    pub fn define_glob_env(reg: &str, alias: &str, ctx_feature: ContextFeature) -> CommandSpecItem {
28        CommandSpecItem::Env(crate::EnvShape {
29            args: crate::ArgPattern::Glob {
30                pattern: reg.into(),
31            },
32            ctx_feature,
33            alias: Some(alias.to_owned()),
34        })
35    }
36
37    pub fn define_symbol(alias: &str) -> CommandSpecItem {
38        CommandSpecItem::Cmd(crate::CmdShape {
39            args: crate::ArgShape::Right {
40                pattern: crate::ArgPattern::None,
41            },
42            alias: Some(alias.to_owned()),
43        })
44    }
45
46    pub fn define_command_with_alias(len: u8, alias: &str) -> CommandSpecItem {
47        CommandSpecItem::Cmd(crate::CmdShape {
48            args: crate::ArgShape::Right {
49                pattern: crate::ArgPattern::FixedLenTerm { len },
50            },
51            alias: Some(alias.to_owned()),
52        })
53    }
54
55    pub fn define_greedy_command(alias: &str) -> CommandSpecItem {
56        CommandSpecItem::Cmd(crate::CmdShape {
57            args: crate::ArgShape::Right {
58                pattern: crate::ArgPattern::Greedy,
59            },
60            alias: Some(alias.to_owned()),
61        })
62    }
63
64    pub fn define_matrix_env(num: Option<u8>, alias: &str) -> CommandSpecItem {
65        CommandSpecItem::Env(crate::EnvShape {
66            args: num
67                .map(|len| crate::ArgPattern::FixedLenTerm { len })
68                .unwrap_or(crate::ArgPattern::None),
69            ctx_feature: crate::ContextFeature::IsMatrix,
70            alias: Some(alias.to_owned()),
71        })
72    }
73
74    pub fn define_normal_env(num: Option<u8>, alias: &str) -> CommandSpecItem {
75        CommandSpecItem::Env(crate::EnvShape {
76            args: num
77                .map(|len| crate::ArgPattern::FixedLenTerm { len })
78                .unwrap_or(crate::ArgPattern::None),
79            ctx_feature: crate::ContextFeature::None,
80            alias: Some(alias.to_owned()),
81        })
82    }
83    pub const fn define_const_command(args: ArgShape) -> CommandSpecItem {
84        CommandSpecItem::Cmd(crate::CmdShape { args, alias: None })
85    }
86
87    pub const TEX_CMD0: CommandSpecItem = define_const_command(crate::ArgShape::Right {
88        pattern: crate::ArgPattern::FixedLenTerm { len: 0 },
89    });
90    pub const TEX_CMD1: CommandSpecItem = define_const_command(crate::ArgShape::Right {
91        pattern: crate::ArgPattern::FixedLenTerm { len: 1 },
92    });
93    pub const TEX_CMD2: CommandSpecItem = define_const_command(crate::ArgShape::Right {
94        pattern: crate::ArgPattern::FixedLenTerm { len: 2 },
95    });
96    pub const TEX_SYMBOL: CommandSpecItem = define_const_command(crate::ArgShape::Right {
97        pattern: crate::ArgPattern::None,
98    });
99    pub const TEX_LEFT1_OPEARTOR: CommandSpecItem = define_const_command(crate::ArgShape::Left1);
100    pub const TEX_GREEDY_OPERATOR: CommandSpecItem = define_const_command(crate::ArgShape::Right {
101        pattern: crate::ArgPattern::Greedy,
102    });
103    pub const TEX_INFIX_OPERATOR: CommandSpecItem =
104        define_const_command(crate::ArgShape::InfixGreedy);
105    pub const TEX_MATRIX_ENV: CommandSpecItem = CommandSpecItem::Env(crate::EnvShape {
106        args: crate::ArgPattern::None,
107        ctx_feature: crate::ContextFeature::IsMatrix,
108        alias: None,
109    });
110    pub const TEX_NORMAL_ENV: CommandSpecItem = CommandSpecItem::Env(crate::EnvShape {
111        args: crate::ArgPattern::None,
112        ctx_feature: crate::ContextFeature::None,
113        alias: None,
114    });
115
116    #[derive(Default)]
117    pub struct SpecBuilder {
118        commands: fxhash::FxHashMap<String, CommandSpecItem>,
119    }
120
121    impl SpecBuilder {
122        pub fn add_command(&mut self, name: &str, item: CommandSpecItem) -> &mut Self {
123            self.commands.insert(name.to_owned(), item);
124            self
125        }
126
127        pub fn build(self) -> crate::CommandSpec {
128            crate::CommandSpec::new(self.commands)
129        }
130    }
131}