Skip to content

Commit

Permalink
Add union to syntax. (rust-lang#260)
Browse files Browse the repository at this point in the history
* Add union to syntax.

* Add test to verify that `union` is not a keyword.
  • Loading branch information
ehuss authored and jasonwilliams committed Apr 22, 2018
1 parent 65a5c32 commit 761796f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 14 deletions.
65 changes: 51 additions & 14 deletions RustEnhanced.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ contexts:
2: storage.type.struct.rust
push: struct-identifier

- match: '\b(?:(pub)\s+)?(union)\s+'
scope: meta.union.rust
captures:
1: storage.modifier.rust
2: storage.type.union.rust
push: union-identifier

- match: '\b(?:(pub)\s+)?(type)\s+({{identifier}})\b'
captures:
1: storage.modifier.rust
Expand Down Expand Up @@ -547,23 +554,53 @@ contexts:
pop: true
- match: '\{'
scope: punctuation.definition.block.begin.rust
push: struct-classic-body

struct-classic-body:
- meta_scope: meta.block.rust
- match: '(?=\})'
pop: true
- include: comments
- include: attribute
- match: \bpub\b
scope: storage.modifier.rust
- match: '{{identifier}}(?=\s*:)'
scope: variable.other.member.rust
push:
- meta_scope: meta.block.rust
- match: '(?=\})'
- match: ',|(?=\})'
pop: true
- include: comments
- include: attribute
- match: \bpub\b
scope: storage.modifier.rust
- match: '{{identifier}}(?=\s*:)'
scope: variable.other.member.rust
push:
- match: ',|(?=\})'
pop: true
- include: comments
- match: ':'
scope: punctuation.separator.rust
- include: type-any-identifier
- match: ':'
scope: punctuation.separator.rust
- include: type-any-identifier


union-identifier:
- match: '{{identifier}}(?=<)'
scope: entity.name.union.rust
set:
- meta_scope: meta.union.rust meta.generic.rust
- match: '(?=<)'
push: generic-angles
- match: ''
set: union-body
- match: '{{identifier}}'
scope: entity.name.union.rust
set: union-body

union-body:
- meta_scope: meta.union.rust
- include: comments
- match: '(?=\bwhere\b)'
push: impl-where
- match: '\{'
scope: punctuation.definition.block.begin.rust
push: struct-classic-body
- match: '\}'
scope: meta.block.rust punctuation.definition.block.end.rust
pop: true
- match: '(?=;)'
pop: true

macro-block:
- meta_scope: meta.macro.rust
Expand Down
36 changes: 36 additions & 0 deletions syntax_test_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,3 +1167,39 @@ fn main() {
}.collect::<Vec<_>>();
println!("{:?}", l);
}

union Union {
//^^^ meta.union storage.type.union
//^^^^^^^^^^^ meta.union
// ^^^^^ entity.name.union
// ^ meta.block punctuation.definition.block.begin
f: u32,
// ^ meta.union meta.block variable.other.member
// ^ meta.union meta.block punctuation.separator
// ^^^ meta.union meta.block storage.type
}
// <- meta.union meta.block punctuation.definition.block.end

pub union Foo<'a, Y: Baz>
// <- storage.modifier
//^^^^^^^^^^^^^^^^^^^^^^^ meta.union
// ^^^^^ meta.union storage.type.union
// ^^^ meta.union meta.generic entity.name.union
// ^ meta.union meta.generic meta.generic punctuation.definition.generic.begin
// ^^ meta.union meta.generic meta.generic storage.modifier.lifetime
where X: Whatever,
// ^^^^^ meta.union meta.where keyword.other
// ^ meta.union meta.where
// ^ meta.union meta.where punctuation.separator
// ^^^^^^^^^^ meta.union meta.where
{
// <- meta.union meta.block punctuation.definition.block.begin
f: SomeType, // Comment beside a field
// ^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.union meta.block comment.line.double-slash
}
// <- meta.union meta.block punctuation.definition.block.end

// Union was implemented in such a way that `union` is not a keyword. Verify
// that we don't accidentally interpret it as a keyword.
fn union() {}
// ^^^^^ meta.function entity.name.function

0 comments on commit 761796f

Please sign in to comment.