cmark_syntax/languages/
javascript.rs

1use crate::{Highlight, Kind};
2use logos::Logos;
3
4#[derive(Logos, PartialEq, Eq, Clone, Copy, Debug)]
5pub enum JavaScript {
6    #[regex("[a-zA-Z_$][a-zA-Z0-9_]*")]
7    Identifier,
8
9    #[regex("\"([^\"\\\\]|\\\\[.\n])*\"")]
10    #[regex("`([^`]|\\\\`)*`")]
11    #[regex("'([^']|\\\\')'")]
12    #[regex("[0-9][0-9]*")]
13    #[regex("0[xX][0-9a-fA-F]+")]
14    #[regex("0[oO][0-7]+")]
15    #[regex("0[bB][01]+")]
16    Literal,
17
18    #[regex(r#"\?|:|!|\^|-|\+|\*|&|/|\|<|>|=|=>|_"#, priority = 3)]
19    Glyph,
20
21    #[regex(r"\.")]
22    GlyphCtx,
23
24    #[regex("arguments|async|await|break|case|catch|const|continue")]
25    #[regex("debugger|default|delete|do|else|enum|eval|export|extends")]
26    #[regex("false|finally|for|if|implements|import|in|instanceof")]
27    #[regex("interface|let|long|native|new|null|package|private")]
28    #[regex("protected|public|return|static|super|switch|this|throw")]
29    #[regex("true|try|typeof|var|void|while|with|yield")]
30    Keyword,
31
32    #[regex("function|class")]
33    KeywordCtx,
34
35    #[regex("undefined|Object|Array|Number|String|NaN|Infinity|Date|Math")]
36    Special,
37
38    #[regex("//[^\n]*")]
39    Comment,
40
41    None,
42}
43
44impl Highlight for JavaScript {
45    const LANG: &'static str = "js";
46    const START: Self = Self::None;
47
48    fn kind(tokens: &[Self; 2]) -> Kind {
49        use JavaScript::*;
50
51        match tokens {
52            [KeywordCtx, Identifier] | [GlyphCtx, Identifier] | [_, Special] => {
53                Kind::SpecialIdentifier
54            }
55            [_, Identifier] => Kind::Identifier,
56            [_, Literal] => Kind::Literal,
57            [_, Glyph] | [_, GlyphCtx] => Kind::Glyph,
58            [_, Keyword] | [_, KeywordCtx] => Kind::Keyword,
59            [_, Comment] => Kind::Comment,
60            _ => Kind::None,
61        }
62    }
63}