Skip to content

Commit 47dfa2a

Browse files
Fix #98: Implement Rust highlighting support (#99)
Initial Rust highlighting support * Basic support for the language * Line comments (`//`) * Block comments (`/* */`), but no recursion * Chars * Strings (`"..."`) * Raw strings (`r"..."`, `r#"..."#`, up to 5 levels) * Added some Rust syntax test files Co-authored-by: Romain Failliot <[email protected]>
1 parent 122879d commit 47dfa2a

File tree

4 files changed

+245
-0
lines changed

4 files changed

+245
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Rust syntax file for Diffuse
2+
# Copyright (C) 2022 Alexander Lopatin (@alopatindev), Romain Failliot <[email protected]>
3+
4+
# DISCLAIMER: this is a raw support for Rust language, help wanted.
5+
6+
syntax Rust normal text
7+
syntax_files Rust '\.rs$'
8+
9+
# Colours
10+
colour rust_comment 0.2 0.4 0.64
11+
colour rust_fixme 1.0 0.5 0.0
12+
colour rust_import 0.77 0.63 0.0
13+
colour rust_keyword 0.77 0.63 0.0
14+
colour rust_type 0.3 0.6 0.02
15+
colour rust_literal 1.0 0.2 0.8
16+
colour rust_char 0.8 0.0 0.0
17+
colour rust_string 0.8 0.0 0.0
18+
colour rust_escapedchar 0.46 0.31 0.48
19+
colour rust_string_multilinechar 0.46 0.31 0.48
20+
colour rust_punctuation 0.5 0.5 0.5
21+
colour rust_macro 0.01 0.6 0.9
22+
23+
# Whitespace (TODO)
24+
# Doc: https://doc.rust-lang.org/reference/whitespace.html
25+
syntax_pattern normal normal text '[ \t\r\n]+'
26+
27+
# Macro (TODO)
28+
#syntax_pattern normal macro rust_macro '[a-z_]+[a-z0-9_]*!' ignorecase
29+
30+
# Line comment
31+
# Doc: https://doc.rust-lang.org/reference/comments.html
32+
syntax_pattern normal line_comment rust_comment '//'
33+
syntax_pattern line_comment normal rust_comment '(\r\n|\r|\n)$'
34+
syntax_pattern line_comment line_comment rust_comment '\\(\r\n|\r|\n)$'
35+
syntax_pattern line_comment line_comment rust_fixme '\b(TODO|FIXME|XXX|NOTE)\b'
36+
syntax_pattern line_comment line_comment rust_comment '.[^\\TFX\r\n]*'
37+
38+
# Block comment
39+
syntax_pattern normal block_comment rust_comment '/\*'
40+
syntax_pattern block_comment normal rust_comment '\*/'
41+
syntax_pattern block_comment block_comment rust_fixme '\b(TODO|FIXME|XXX|NOTE)\b'
42+
syntax_pattern block_comment block_comment rust_comment '.[^\*TFX]*'
43+
44+
# Keywords (TODO)
45+
# Doc: https://doc.rust-lang.org/reference/keywords.html#weak-keywords
46+
#syntax_pattern normal normal rust_string '\b\'[a-z_]+[a-z0-9_]*\b' ignorecase
47+
#syntax_pattern normal normal rust_string '\b\'[a-z_]+\b' ignorecase
48+
syntax_pattern normal normal rust_string "\b'[a-z_]+\b" ignorecase
49+
50+
# Char
51+
syntax_pattern normal normal rust_char "'.'"
52+
syntax_pattern normal char rust_char "'(?=\\\\)"
53+
syntax_pattern char normal rust_char "'"
54+
syntax_pattern char char rust_escapedchar "\\\\([0-7]{1,3}|x[0-9a-f]{1,2}|u\{[0-9a-f]{1,6}\}|.)"
55+
56+
# String
57+
# Doc: https://doc.rust-lang.org/reference/tokens.html#string-literals
58+
syntax_pattern normal string rust_string '"'
59+
syntax_pattern string normal rust_string '"'
60+
syntax_pattern string string rust_escapedchar '(\\([0-7]{1,3}|x[0-9a-f]{1,2}|u\{[0-9a-f]{1,6}\}|.))+' ignorecase
61+
syntax_pattern string string rust_string_multilinechar '\\$'
62+
syntax_pattern string string rust_string '[^\\"]+'
63+
64+
# Raw quote-string, level 0
65+
# Doc: https://doc.rust-lang.org/reference/tokens.html#raw-string-literals
66+
syntax_pattern normal raw_string_0 rust_string 'r"'
67+
syntax_pattern raw_string_0 normal rust_string '"'
68+
syntax_pattern raw_string_0 raw_string_0 rust_string '[^"$]+'
69+
70+
# Raw sharp-string, level 1
71+
syntax_pattern normal raw_string_1 rust_string 'r#"'
72+
syntax_pattern raw_string_1 normal rust_string '"#'
73+
syntax_pattern raw_string_1 raw_string_1 rust_string '.*?((?="#)|\r?\n)'
74+
75+
# Raw sharp-string, level 2
76+
syntax_pattern normal raw_string_2 rust_string 'r##"'
77+
syntax_pattern raw_string_2 normal rust_string '"##'
78+
syntax_pattern raw_string_2 raw_string_2 rust_string '.*?((?="##)|\r?\n)'
79+
80+
# Raw sharp-string, level 3
81+
syntax_pattern normal raw_string_3 rust_string 'r###"'
82+
syntax_pattern raw_string_3 normal rust_string '"###'
83+
syntax_pattern raw_string_3 raw_string_3 rust_string '.*?((?="###)|\r?\n)'
84+
85+
# Raw sharp-string, level 4
86+
syntax_pattern normal raw_string_4 rust_string 'r####"'
87+
syntax_pattern raw_string_4 normal rust_string '"####'
88+
syntax_pattern raw_string_4 raw_string_4 rust_string '.*?((?="####)|\r?\n)'
89+
90+
# Raw sharp-string, level 5
91+
# Limit is 256 '#', but 5 seems reasonable for now
92+
syntax_pattern normal raw_string_5 rust_string 'r#####"'
93+
syntax_pattern raw_string_5 normal rust_string '"#####'
94+
syntax_pattern raw_string_5 raw_string_5 rust_string '.*?((?="#####)|\r?\n)'
95+
96+
# Suffixes (TODO)
97+
# Doc: https://doc.rust-lang.org/reference/tokens.html#suffixes
98+
syntax_pattern normal normal rust_literal '\b[0-9_]+(isize|usize|char|bool|u8|u16|u32|u64|u128|f32|f64|i8|i16|i32|i64|i128|)\b'
99+
syntax_pattern normal normal rust_literal '\b0b[01_]+(isize|usize|char|bool|u8|u16|u32|u64|u128|f32|f64|i8|i16|i32|i64|i128|)\b'
100+
syntax_pattern normal normal rust_literal '\b0o[0-7_]+(isize|usize|char|bool|u8|u16|u32|u64|u128|f32|f64|i8|i16|i32|i64|i128|)\b'
101+
syntax_pattern normal normal rust_literal '\b0x[0-9a-f_]+(isize|usize|char|bool|u8|u16|u32|u64|u128|f32|f64|i8|i16|i32|i64|i128|)\b'
102+
syntax_pattern normal normal rust_literal '\b[0-9_]+E\+[0-9_]+(f32|f64)\b'
103+
syntax_pattern normal normal rust_literal '\b(true|false)\b'
104+
105+
# import keywords
106+
syntax_pattern normal normal rust_import '\b(use|mod)\b'
107+
108+
# typing keywords
109+
syntax_pattern normal normal rust_type '\b(isize|usize|char|bool|u8|u16|u32|u64|u128|f32|f64|i8|i16|i32|i64|i128|str|Self)\b'
110+
111+
# keywords
112+
syntax_pattern normal normal rust_keyword '\b(as|break|const|continue|crate|else|enum|extern|false|fn|for|if|impl|in|let|loop|match|move|mut|pub|ref|return|self|Self|static|struct|super|trait|true|type|unsafe|where|while|async|await|dyn|abstract|become|box|do|final|macro|override|priv|typeof|unsized|virtual|yield|try|union)\b'
113+
114+
# punctuation
115+
syntax_pattern normal normal rust_punctuation '[!~%\|\&\^\(\)\<\>\*\-\+=\{\}\[\]:;,\?$]+'
116+
syntax_pattern normal normal rust_punctuation '[/\.]'
117+
118+
# parsing optimization
119+
syntax_pattern normal normal text '[a-z_][a-z_0-9]*' ignorecase

tests/syntax/rust/untested.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#[derive(Clone)]
2+
struct A<'a_bж, B> {
3+
x: &'a_bж B,
4+
z: &'static str,
5+
}
6+
7+
trait Tr {}
8+
9+
async fn f(x: Result<(), ()>) -> Result<(), ()> {
10+
let _ = x?;
11+
Ok(())
12+
}
13+
14+
async fn g() {
15+
f(Ok(())).await.unwrap()
16+
}
17+
18+
pub fn main() -> () {
19+
println!("\x61\x62");
20+
let mut x: i32 = 1;
21+
let s: char = 's';
22+
let s: char = '\123';
23+
let x: i32 = -1_322_i32;
24+
let x: Box<dyn Tr> = todo!();
25+
let zsd: usize = 123456usize;
26+
let фыва_1 = "asdf";
27+
let abra_cadabra_2 = r#"abcd"#;
28+
let x = 1;
29+
let asd = r###"sdasd sdf"###;
30+
let abra_cadabra_2 = r#"multiline
31+
string
32+
"#;
33+
let asd = r###"multiline string
34+
any characters '"_\/%
35+
abcd"###;
36+
return ();
37+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
pub fn comments() -> () {
2+
// line comment
3+
4+
// line comment
5+
// line comment
6+
7+
//
8+
9+
//
10+
11+
//
12+
//
13+
14+
// line comment // test
15+
16+
/* line comment */
17+
18+
/* line comment */
19+
/* line comment */
20+
21+
/* block
22+
comment
23+
*/
24+
25+
/*
26+
block
27+
comment
28+
*/
29+
30+
/**
31+
block
32+
comment
33+
*/
34+
35+
/**/
36+
37+
/* */
38+
39+
/***/
40+
41+
// Not supported yet:
42+
//
43+
// Recursive block comment:
44+
// - This is valid: /* line /* test */ comment */
45+
// - This is invalid: /* line /* test comment */
46+
}

tests/syntax/rust/valid_strings.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
pub fn strings() -> () {
2+
let s = "foobar";
3+
let s = "foo\
4+
bar";
5+
let s = "foo\
6+
7+
bar";
8+
9+
let s = "\
10+
bar";
11+
12+
let escapedchars = "\\;\";\r\n\t\0";
13+
let octalchars = "\12;\123";
14+
let hexchars = "\x61;\x2B;\u{F7};\u{00F7};\u{0000F7}";
15+
let unicodechars = "\u{F7};\u{00F7};\u{0000F7}";
16+
17+
let rs0 = r"";
18+
let rs1 = r#""#;
19+
let rs2 = r##""##;
20+
let rs3 = r###""###;
21+
let rs4 = r####""####;
22+
let rs5 = r#####""#####;
23+
24+
let rs0 = r"foo bar";
25+
let rs1 = r#""foo bar""#;
26+
let rs2 = r##"foo "# bar"##;
27+
let rs3 = r###"foo "## bar"###;
28+
let rs4 = r####"foo "### bar"####;
29+
let rs5 = r#####"foo "#### bar"#####;
30+
31+
let rs0 = r"foo
32+
bar";
33+
let rs1 = r#""foo
34+
bar""#;
35+
let rs2 = r##"foo
36+
bar"##;
37+
let rs3 = r###"foo
38+
bar"###;
39+
let rs4 = r####"foo
40+
bar"####;
41+
let rs5 = r#####"foo
42+
bar"#####;
43+
}

0 commit comments

Comments
 (0)