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
use crate::{Highlight, Kind};
use logos::Logos;

#[derive(Logos, PartialEq, Eq, Clone, Copy, Debug)]
pub enum Sh {
    Start,

    #[regex(r"[^\s=$\(\)\{\}]*")]
    Word,

    #[token("$")]
    #[regex(r"\$[^{(\s][^\s]*")]
    #[regex(r"\$\{[^}]*\}")]
    Parameter,

    #[regex(r#""([^"\n]|\\["\n])*""#)]
    #[regex("'[^']*'")]
    Literal,

    #[token("|")]
    #[token("&")]
    #[token("||")]
    #[token("&&")]
    #[token(";")]
    #[token("(")]
    #[token(")")]
    #[token("<")]
    #[token(">")]
    Glyph,

    #[token("\n")]
    Newline,

    #[token("}")]
    #[token("]]")]
    #[regex(r"esac|for|in|done|fi")]
    Keyword,

    #[token("{")]
    #[token("[[")]
    #[regex(r"!|do|then|elif|until|case|else|if|while|function|select")]
    KeywordCtx,

    #[regex("#[^\n]*")]
    Comment,
}

impl Highlight for Sh {
    const LANG: &'static str = "sh";
    const START: Sh = Self::Start;

    fn kind(tokens: &[Self; 2]) -> Kind {
        use Sh::*;

        match tokens {
            [KeywordCtx, Word] | [Glyph, Word] | [Newline, Word] | [Start, Word] => {
                Kind::StrongIdentifier
            }
            [_, Parameter] => Kind::Identifier,
            [_, Literal] => Kind::Literal,
            [_, Glyph] => Kind::Glyph,
            [_, Keyword] | [_, KeywordCtx] => Kind::Keyword,
            [_, Comment] => Kind::Comment,
            _ => Kind::None,
        }
    }
}