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
// todo: remove me
#![allow(missing_docs)]

pub mod command {
    use crate::{ArgShape, CommandSpecItem};

    pub fn define_command(len: u8) -> CommandSpecItem {
        CommandSpecItem::Cmd(crate::CmdShape {
            args: crate::ArgShape::Right {
                pattern: crate::ArgPattern::FixedLenTerm { len },
            },
            alias: None,
        })
    }

    pub fn define_glob_command(reg: &str, alias: &str) -> CommandSpecItem {
        CommandSpecItem::Cmd(crate::CmdShape {
            args: crate::ArgShape::Right {
                pattern: crate::ArgPattern::Glob(reg.into()),
            },
            alias: Some(alias.to_owned()),
        })
    }

    pub fn define_symbol(alias: &str) -> CommandSpecItem {
        CommandSpecItem::Cmd(crate::CmdShape {
            args: crate::ArgShape::Right {
                pattern: crate::ArgPattern::None,
            },
            alias: Some(alias.to_owned()),
        })
    }

    pub fn define_command_with_alias(len: u8, alias: &str) -> CommandSpecItem {
        CommandSpecItem::Cmd(crate::CmdShape {
            args: crate::ArgShape::Right {
                pattern: crate::ArgPattern::FixedLenTerm { len },
            },
            alias: Some(alias.to_owned()),
        })
    }

    pub fn define_greedy_command(alias: &str) -> CommandSpecItem {
        CommandSpecItem::Cmd(crate::CmdShape {
            args: crate::ArgShape::Right {
                pattern: crate::ArgPattern::Greedy,
            },
            alias: Some(alias.to_owned()),
        })
    }

    pub fn define_matrix_env(num: Option<u8>, alias: &str) -> CommandSpecItem {
        CommandSpecItem::Env(crate::EnvShape {
            args: num
                .map(|len| crate::ArgPattern::FixedLenTerm { len })
                .unwrap_or(crate::ArgPattern::None),
            ctx_feature: crate::ContextFeature::IsMatrix,
            alias: Some(alias.to_owned()),
        })
    }

    pub fn define_normal_env(num: Option<u8>, alias: &str) -> CommandSpecItem {
        CommandSpecItem::Env(crate::EnvShape {
            args: num
                .map(|len| crate::ArgPattern::FixedLenTerm { len })
                .unwrap_or(crate::ArgPattern::None),
            ctx_feature: crate::ContextFeature::None,
            alias: Some(alias.to_owned()),
        })
    }
    pub const fn define_const_command(args: ArgShape) -> CommandSpecItem {
        CommandSpecItem::Cmd(crate::CmdShape { args, alias: None })
    }

    pub const TEX_CMD0: CommandSpecItem = define_const_command(crate::ArgShape::Right {
        pattern: crate::ArgPattern::FixedLenTerm { len: 0 },
    });
    pub const TEX_CMD1: CommandSpecItem = define_const_command(crate::ArgShape::Right {
        pattern: crate::ArgPattern::FixedLenTerm { len: 1 },
    });
    pub const TEX_CMD2: CommandSpecItem = define_const_command(crate::ArgShape::Right {
        pattern: crate::ArgPattern::FixedLenTerm { len: 2 },
    });
    pub const TEX_SYMBOL: CommandSpecItem = define_const_command(crate::ArgShape::Right {
        pattern: crate::ArgPattern::None,
    });
    pub const TEX_LEFT1_OPEARTOR: CommandSpecItem = define_const_command(crate::ArgShape::Left1);
    pub const TEX_GREEDY_OPERATOR: CommandSpecItem = define_const_command(crate::ArgShape::Right {
        pattern: crate::ArgPattern::Greedy,
    });
    pub const TEX_INFIX_OPERATOR: CommandSpecItem =
        define_const_command(crate::ArgShape::InfixGreedy);
    pub const TEX_MATRIX_ENV: CommandSpecItem = CommandSpecItem::Env(crate::EnvShape {
        args: crate::ArgPattern::None,
        ctx_feature: crate::ContextFeature::IsMatrix,
        alias: None,
    });
    pub const TEX_NORMAL_ENV: CommandSpecItem = CommandSpecItem::Env(crate::EnvShape {
        args: crate::ArgPattern::None,
        ctx_feature: crate::ContextFeature::None,
        alias: None,
    });

    #[derive(Default)]
    pub struct SpecBuilder {
        commands: fxhash::FxHashMap<String, CommandSpecItem>,
    }

    impl SpecBuilder {
        pub fn add_command(&mut self, name: &str, item: CommandSpecItem) -> &mut Self {
            self.commands.insert(name.to_owned(), item);
            self
        }

        pub fn build(self) -> crate::CommandSpec {
            crate::CommandSpec::new(self.commands)
        }
    }
}