cmark_syntax/languages/
rust.rs1use crate::{Highlight, Kind};
2use logos::Logos;
3
4#[derive(Logos, PartialEq, Eq, Clone, Copy, Debug)]
5pub enum Rust {
6 #[regex("[a-z_$][a-zA-Z0-9_]*!?")]
7 #[regex("[A-Z_]*!?", priority = 3)]
8 Identifier,
9
10 #[regex("[A-Z][a-zA-Z0-9_]*!?")]
11 StrongIdentifier,
12
13 #[regex("\"([^\"\\n]|\\\\[\"\\n])*\"")]
14 #[regex("'([^']|\\\\[^'\\n \\t]*)'")]
15 #[regex("r\"[^\"]*\"")]
16 #[regex("r#\"#?([^#]|[^\"]#)*\"#")]
17 #[regex("b\"([^\"\\n]|\\\\[\"\\n])*\"")]
18 #[regex("b'([^']|\\\\[^'\\n \\t]*)'")]
19 #[regex("br\"[^\"]*\"")]
20 #[regex("br#\"#?([^#]|[^\"]#)*\"#")]
21 #[regex("[0-9][0-9_]*")]
22 #[regex("0[xX][0-9a-fA-F_]+")]
23 #[regex("0[oO][0-7_]+")]
24 #[regex("0[bB][01_]+")]
25 Literal,
26
27 #[regex(r#"\?|!|\^|-|\+|\*|&|/|\\|<|>|=|=>|->|_|#\[[^\]]*\]|&"#)]
28 Glyph,
29
30 #[regex("\\.|:", priority = 3)]
31 GlyphCtx,
32
33 #[regex("\\{|\\}|\\[|\\]|\\(|\\)")]
34 Bracket,
35
36 #[regex("'[a-zA-Z_][a-zA-Z0-9_]*")]
37 Lifetime,
38
39 #[regex("as|break|const|continue|crate|dyn|else|extern")]
40 #[regex("false|for|if|impl|in|let|loop|match|mod|move|mut")]
41 #[regex("pub|ref|return|self|Self|static|super")]
42 #[regex("true|unsafe|use|where|while")]
43 #[regex("abstract|async|await|become|box|do|final|macro")]
44 #[regex("override|priv|try|typeof|unsized|virtual|yield")]
45 Keyword,
46
47 #[regex("fn|enum|struct|type|trait")]
48 KeywordCtx,
49
50 #[regex("str|bool|[ui](8|16|32|64|size)|f32|f64")]
51 Special,
52
53 #[regex("//[^\n]*")]
54 #[regex("/\\*([^/]|[^*]/)*\\*/")]
55 Comment,
56
57 None,
58}
59
60impl Highlight for Rust {
61 const LANG: &'static str = "rust";
62 const START: Self = Self::None;
63
64 fn kind(tokens: &[Self; 2]) -> Kind {
65 use Rust::*;
66
67 match tokens {
68 [KeywordCtx, StrongIdentifier]
69 | [KeywordCtx, Identifier]
70 | [GlyphCtx, Identifier]
71 | [_, Lifetime] => Kind::SpecialIdentifier,
72 [_, Identifier] => Kind::Identifier,
73 [_, StrongIdentifier] | [_, Special] => Kind::StrongIdentifier,
74 [_, Literal] => Kind::Literal,
75 [_, Glyph] | [_, GlyphCtx] => Kind::Glyph,
76 [_, Keyword] | [_, KeywordCtx] => Kind::Keyword,
77 [_, Comment] => Kind::Comment,
78 _ => Kind::None,
79 }
80 }
81}