aboutsummaryrefslogtreecommitdiff
path: root/code/src/syntax_highlight/grammar.rs
blob: 0cecac9503bb0cd33a78c954797023e58d6e0c06 (plain)
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
const CONST: &str = "[\\. \\(,=^]([A-Z][A-Z0-9_]*)[\\. ,\\)=$]";
const TYPE: &str = "[\\. \\(,=^]([A-Z][a-zA-Z0-9_]*)[\\. ,\\)\\(=$]";
const IDENT: &str = "[\\. \\(,=^]([a-z][a-zA-Z0-9_]*)[\\. ,\\)=$]";
const FUNC: &str = "[\\. \\(,=^]([a-z][A-Z0-9_]*)\\(";

pub fn grammar_for(syntax: &str) -> &'static [(&'static str, &'static [&'static str])] {
    match syntax {
        "rs" | "rust" => &[
            (
                "keyword",
                &[
                    "fn", "pub", "async", "return", "if", "else", "let", "for", "in", "while",
                    "loop", "impl", "for", "trait", "struct", "enum", "dyn",
                ],
            ),
            (
                "type",
                &[
                    TYPE, "bool", "usize", "u8", "u16", "u32", "u64", "u128", "i8", "i16", "i32",
                    "i64", "i128", "isize", "f32", "f64",
                ],
            ),
            ("constant", &[CONST]),
            ("comment", &["(?m)(//.*)$", "(?ms)/\\*.*?\\*/"]),
            ("macro", &["[\\. ,=^]([a-z_][A-Za-z0-9_]*!)[\\. ,=$]"]),
            ("identifier", &[IDENT]),
            ("function", &[FUNC]),
            (
                "literal",
                &["\".*?\"", "(0x|0o|0b)[0-9a-f]+", "\\d+", "true", "false"],
            ),
        ],
        "py" | "python" => &[
            ("type", &["bytes", "bool", "int", "str", "float"]),
            (
                "keyword",
                &[
                    "from", "for", "def", "if", "else", "elif", "while", "with", "in", "assert",
                    "global", "nonlocal", "as", "import",
                ],
            ),
            ("comment", &["(?m)(#.*)$"]),
            ("constant", &[CONST]),
            ("identifier", &[IDENT]),
            (
                "literal",
                &[
                    "b?f?\".*?\"",
                    "b?f?'.*?'",
                    "(0x|0o|0b)[0-9a-f]+",
                    "\\d+",
                    "true",
                    "false",
                ],
            ),
        ],
        "tree" => &[("keyword", &["[├─└│]+"])],
        // makefile doesnt really match the token-kinds, i'll just use something that looks goo
        "makefile" | "mk" => &[
            ("comment", &["(?m)(#.*)$"]),
            ("literal", &[".+: "]),
            ("macro", &["\\$\\(\\w+\\)"]),
            ("type", &["\\$@", "\\$<"]),
        ],
        _ => &[],
    }
}