Skip to content

Commit 3f819f7

Browse files
committed
Syntax: Rework enums.
Enums weren't supporting type parameters, among other things.
1 parent 24e5f8e commit 3f819f7

File tree

3 files changed

+110
-20
lines changed

3 files changed

+110
-20
lines changed

RustEnhanced.sublime-syntax

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ file_extensions:
77
scope: source.rust
88
variables:
99
identifier: '(?:[[:alpha:]][_[:alnum:]]*|_[_[:alnum:]]+)'
10+
camel_ident: '\b_*[A-Z][a-zA-Z0-9_]*[a-z][a-zA-Z0-9_]*\b'
1011
escaped_byte: '\\([nrt0\"''\\]|x\h{2})'
1112
escaped_char: '\\([nrt0\"''\\]|x[0-7]\h|u\{(?:\h_*){1,6}\})'
1213
int_suffixes: '[iu](?:8|16|32|64|128|size)'
@@ -90,13 +91,9 @@ contexts:
9091
scope: storage.type.impl.rust
9192
push: impl-definition
9293

93-
- match: '\b(enum)\s+({{identifier}})\b'
94-
captures:
95-
1: storage.type.enum.rust
96-
2: entity.name.enum.rust
97-
push:
98-
- meta_scope: meta.enum.rust
99-
- include: statements-block
94+
- match: '\benum\b'
95+
scope: storage.type.enum.rust
96+
push: enum-identifier
10097

10198
- match: \b(let|const|static)\b
10299
scope: storage.type.rust
@@ -107,15 +104,6 @@ contexts:
107104
- match: \bmod\b
108105
scope: storage.type.module.rust
109106

110-
- match: \bstruct\b
111-
scope: storage.type.struct.rust
112-
113-
- match: \bimpl\b
114-
scope: storage.type.impl.rust
115-
116-
- match: \benum\b
117-
scope: storage.type.enum.rust
118-
119107
- match: \btype\b
120108
scope: storage.type.type.rust
121109

@@ -634,6 +622,67 @@ contexts:
634622
- match: '(?=;)'
635623
pop: true
636624

625+
enum-identifier:
626+
- meta_scope: meta.enum.rust
627+
- include: comments
628+
- match: '{{identifier}}(?=<)'
629+
scope: entity.name.enum.rust
630+
set:
631+
- meta_scope: meta.enum.rust meta.generic.rust
632+
- match: '(?=<)'
633+
push: generic-angles
634+
- match: ''
635+
set: enum-maybe-where
636+
- match: '{{identifier}}'
637+
scope: entity.name.enum.rust
638+
set: enum-maybe-where
639+
640+
enum-maybe-where:
641+
- meta_scope: meta.enum.rust
642+
- include: comments
643+
- match: '(?=\bwhere\b)'
644+
push: impl-where
645+
- match: '\{'
646+
scope: punctuation.definition.block.begin.rust
647+
set: enum-body
648+
649+
enum-body:
650+
- meta_scope: meta.enum.rust
651+
- include: comments
652+
- include: attribute
653+
- match: '\}'
654+
scope: punctuation.definition.block.end.rust
655+
pop: true
656+
- match: '\b[[:upper:]_][[:upper:][:digit:]_]*\b'
657+
scope: constant.other.rust
658+
push: enum-variant-type
659+
- match: '{{camel_ident}}'
660+
scope: storage.type.source.rust
661+
push: enum-variant-type
662+
- match: '{{identifier}}'
663+
push: enum-variant-type
664+
665+
enum-variant-type:
666+
- include: comments
667+
- match: '(?=\})'
668+
pop: true
669+
- match: ','
670+
pop: true
671+
- match: '='
672+
set: enum-discriminant
673+
- match: '(?=\()'
674+
push: struct-tuple
675+
- match: '(?=\{)'
676+
push: struct-classic
677+
678+
enum-discriminant:
679+
- match: ','
680+
pop: true
681+
- match: '(?=\})'
682+
pop: true
683+
# This is just constant-expression, but we don't have that defined.
684+
- include: statements
685+
637686
macro-block:
638687
- meta_scope: meta.macro.rust
639688
- match: '\}'

tests/syntax-rust/syntax_test_attributes.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,13 @@ pub struct Claim {
4747
pub date: String,
4848
}
4949

50+
enum E {
51+
#[allow(dead_code)]
52+
// ^^^^^^^^^^^^^^^^^^^ meta.enum meta.annotation
53+
// ^^^^^ support.function
54+
A(i32),
55+
// ^^^ meta.enum meta.struct meta.group storage.type
56+
}
57+
5058
// TODO: FIXME - Generic parameters.
5159
// unsafe impl<#[may_dangle] T: ?Sized> Drop for Box<T> { }

tests/syntax-rust/syntax_test_enum.rs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ enum OperatingSystem
55
// ^^^^^^^^^^^^^^^^^ meta.enum
66
// ^^^^^^^^^^^^^^^ entity.name.enum
77
{
8-
// <- meta.enum meta.block punctuation.definition.block.begin
8+
// <- meta.enum punctuation.definition.block.begin
99
Osx,
10+
// ^^^ meta.enum storage.type.source
1011
Windows,
1112
Linux,
1213
Bsd(String),
1314
// ^^^^^^ support.type
1415
Info { field: i32, value: str }
15-
// ^ meta.block meta.block punctuation.definition.block.begin
16+
// ^ punctuation.definition.block.begin
1617
// ^^^ storage.type
1718
// ^^^ storage.type
18-
// ^ meta.block meta.block punctuation.definition.block.end
19+
// ^ meta.block punctuation.definition.block.end
1920
}
20-
// <- meta.enum meta.block punctuation.definition.block.end
21+
// <- meta.enum punctuation.definition.block.end
2122

2223
let q = Message::Quit;
2324
// ^^^^^^^ storage.type.source
@@ -33,3 +34,35 @@ let m = Message::Move { x: 50, y: 200 };
3334
// ^^^^^^^^^^^^^^^^^ meta.block
3435
// ^^ constant.numeric.integer.decimal
3536
// ^^^ constant.numeric.integer.decimal
37+
38+
enum Discriminant {
39+
A = 1,
40+
// ^ meta.enum constant.other
41+
// ^ meta.enum constant.numeric.integer.decimal
42+
V1 = 0xABC,
43+
// ^^ meta.enum constant.other
44+
// ^^^^^ meta.enum constant.numeric.integer.hexadecimal
45+
V2,
46+
// ^^ meta.enum constant.other
47+
SomeValue = 123,
48+
// ^^^^^^^^^ meta.enum storage.type.source
49+
// ^^^ meta.enum constant.numeric.integer.decimal
50+
V3 = (1<<4),
51+
// ^^ meta.enum constant.other
52+
// ^^^^^^ meta.enum meta.group
53+
// ^ constant.numeric.integer.decimal
54+
// ^^ keyword.operator
55+
// ^ constant.numeric.integer.decimal
56+
lowercase,
57+
// ^^^^^^^^^^^ meta.enum
58+
}
59+
60+
// Enum type parameters.
61+
enum E<'asdf> {}
62+
// ^^^^^^^ meta.enum meta.generic
63+
// ^^^^^storage.modifier.lifetime
64+
enum C<T> where T: Copy {}
65+
// ^^^ meta.enum meta.generic
66+
// ^^^^^^^^^^^^^ meta.enum meta.where
67+
// ^^^^^ keyword.other
68+
// ^^^^ support.type

0 commit comments

Comments
 (0)