Skip to content

Commit ba7b3c2

Browse files
bors[bot]Plasticcaz
andcommitted
Merge #59
59: Moved TokenSet into it's own file. r=matklad a=Plasticcaz As discussed in Issue #11, the only thing left in that issue that hasn't been fixed appears to be that TokenSet is not in it's own file. This pull request pulls TokenSet, it's macros and it's test into it's own file. Co-authored-by: Zac Winter <[email protected]>
2 parents 751562d + 518cc87 commit ba7b3c2

File tree

4 files changed

+41
-37
lines changed

4 files changed

+41
-37
lines changed

crates/libsyntax2/src/grammar/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ mod type_params;
3232
mod types;
3333

3434
use {
35-
parser_api::{Marker, CompletedMarker, Parser, TokenSet},
35+
token_set::TokenSet,
36+
parser_api::{Marker, CompletedMarker, Parser},
3637
SyntaxKind::{self, *},
3738
};
3839
pub(crate) use self::{

crates/libsyntax2/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub mod algo;
3131
pub mod ast;
3232
mod lexer;
3333
#[macro_use]
34+
mod token_set;
3435
mod parser_api;
3536
mod grammar;
3637
mod parser_impl;

crates/libsyntax2/src/parser_api.rs

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,10 @@
11
use {
2+
token_set::TokenSet,
23
parser_impl::ParserImpl,
34
SyntaxKind::{self, ERROR},
45
drop_bomb::DropBomb,
56
};
67

7-
#[derive(Clone, Copy)]
8-
pub(crate) struct TokenSet(pub(crate) u128);
9-
10-
fn mask(kind: SyntaxKind) -> u128 {
11-
1u128 << (kind as usize)
12-
}
13-
14-
impl TokenSet {
15-
pub const EMPTY: TokenSet = TokenSet(0);
16-
17-
pub fn contains(&self, kind: SyntaxKind) -> bool {
18-
self.0 & mask(kind) != 0
19-
}
20-
}
21-
22-
#[macro_export]
23-
macro_rules! token_set {
24-
($($t:ident),*) => { TokenSet($(1u128 << ($t as usize))|*) };
25-
($($t:ident),* ,) => { token_set!($($t),*) };
26-
}
27-
28-
#[macro_export]
29-
macro_rules! token_set_union {
30-
($($ts:expr),*) => { TokenSet($($ts.0)|*) };
31-
($($ts:expr),* ,) => { token_set_union!($($ts),*) };
32-
}
33-
34-
#[test]
35-
fn token_set_works_for_tokens() {
36-
use SyntaxKind::*;
37-
let ts = token_set! { EOF, SHEBANG };
38-
assert!(ts.contains(EOF));
39-
assert!(ts.contains(SHEBANG));
40-
assert!(!ts.contains(PLUS));
41-
}
42-
438
/// `Parser` struct provides the low-level API for
449
/// navigating through the stream of tokens and
4510
/// constructing the parse tree. The actual parsing

crates/libsyntax2/src/token_set.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use SyntaxKind;
2+
3+
#[derive(Clone, Copy)]
4+
pub(crate) struct TokenSet(pub(crate) u128);
5+
6+
fn mask(kind: SyntaxKind) -> u128 {
7+
1u128 << (kind as usize)
8+
}
9+
10+
impl TokenSet {
11+
pub const EMPTY: TokenSet = TokenSet(0);
12+
13+
pub fn contains(&self, kind: SyntaxKind) -> bool {
14+
self.0 & mask(kind) != 0
15+
}
16+
}
17+
18+
#[macro_export]
19+
macro_rules! token_set {
20+
($($t:ident),*) => { TokenSet($(1u128 << ($t as usize))|*) };
21+
($($t:ident),* ,) => { token_set!($($t),*) };
22+
}
23+
24+
#[macro_export]
25+
macro_rules! token_set_union {
26+
($($ts:expr),*) => { TokenSet($($ts.0)|*) };
27+
($($ts:expr),* ,) => { token_set_union!($($ts),*) };
28+
}
29+
30+
#[test]
31+
fn token_set_works_for_tokens() {
32+
use SyntaxKind::*;
33+
let ts = token_set! { EOF, SHEBANG };
34+
assert!(ts.contains(EOF));
35+
assert!(ts.contains(SHEBANG));
36+
assert!(!ts.contains(PLUS));
37+
}

0 commit comments

Comments
 (0)