Skip to content

Commit 0a94fce

Browse files
committed
Merge #41
41: G: unsafe impl & trait r=matklad a=matklad bors r+
2 parents 5e5313a + 1942da2 commit 0a94fce

File tree

9 files changed

+75
-6
lines changed

9 files changed

+75
-6
lines changed

grammar.ron

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ Grammar(
9191
"USE_ITEM",
9292
"STATIC_ITEM",
9393
"CONST_ITEM",
94+
"TRAIT_ITEM",
95+
"IMPL_ITEM",
9496

9597
"EXTERN_BLOCK",
9698
"ENUM_VARIANT",

src/parser/event_parser/grammar/items/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::*;
33
mod structs;
44
mod use_item;
55
mod consts;
6+
mod traits;
67

78
pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
89
attributes::inner_attributes(p);
@@ -80,6 +81,22 @@ fn item(p: &mut Parser) {
8081
CONST_ITEM
8182
}
8283
},
84+
// TODO: auto trait
85+
// test unsafe_trait
86+
// unsafe trait T {}
87+
UNSAFE_KW if la == TRAIT_KW => {
88+
p.bump();
89+
traits::trait_item(p);
90+
TRAIT_ITEM
91+
}
92+
// TODO: default impl
93+
// test unsafe_impl
94+
// unsafe impl Foo {}
95+
UNSAFE_KW if la == IMPL_KW => {
96+
p.bump();
97+
traits::impl_item(p);
98+
IMPL_ITEM
99+
}
83100
MOD_KW => {
84101
mod_item(p);
85102
MOD_ITEM
@@ -131,6 +148,7 @@ fn extern_block(p: &mut Parser) {
131148
p.bump();
132149
p.expect(R_CURLY);
133150
}
151+
134152
fn mod_item(p: &mut Parser) {
135153
assert!(p.at(MOD_KW));
136154
p.bump();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use super::*;
2+
3+
pub(super) fn trait_item(p: &mut Parser) {
4+
assert!(p.at(TRAIT_KW));
5+
p.bump();
6+
p.expect(IDENT);
7+
p.expect(L_CURLY);
8+
p.expect(R_CURLY);
9+
}
10+
11+
pub(super) fn impl_item(p: &mut Parser) {
12+
assert!(p.at(IMPL_KW));
13+
p.bump();
14+
p.expect(IDENT);
15+
p.expect(L_CURLY);
16+
p.expect(R_CURLY);
17+
}

src/syntax_kinds.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ pub enum SyntaxKind {
9292
USE_ITEM,
9393
STATIC_ITEM,
9494
CONST_ITEM,
95+
TRAIT_ITEM,
96+
IMPL_ITEM,
9597
EXTERN_BLOCK,
9698
ENUM_VARIANT,
9799
NAMED_FIELD,
@@ -207,6 +209,8 @@ impl SyntaxKind {
207209
USE_ITEM => &SyntaxInfo { name: "USE_ITEM" },
208210
STATIC_ITEM => &SyntaxInfo { name: "STATIC_ITEM" },
209211
CONST_ITEM => &SyntaxInfo { name: "CONST_ITEM" },
212+
TRAIT_ITEM => &SyntaxInfo { name: "TRAIT_ITEM" },
213+
IMPL_ITEM => &SyntaxInfo { name: "IMPL_ITEM" },
210214
EXTERN_BLOCK => &SyntaxInfo { name: "EXTERN_BLOCK" },
211215
ENUM_VARIANT => &SyntaxInfo { name: "ENUM_VARIANT" },
212216
NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" },
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
unsafe trait T {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FILE@[0; 18)
2+
TRAIT_ITEM@[0; 18)
3+
UNSAFE_KW@[0; 6)
4+
WHITESPACE@[6; 7)
5+
TRAIT_KW@[7; 12)
6+
WHITESPACE@[12; 13)
7+
IDENT@[13; 14) "T"
8+
WHITESPACE@[14; 15)
9+
L_CURLY@[15; 16)
10+
R_CURLY@[16; 17)
11+
WHITESPACE@[17; 18)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
unsafe impl Foo {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FILE@[0; 19)
2+
IMPL_ITEM@[0; 19)
3+
UNSAFE_KW@[0; 6)
4+
WHITESPACE@[6; 7)
5+
IMPL_KW@[7; 11)
6+
WHITESPACE@[11; 12)
7+
IDENT@[12; 15) "Foo"
8+
WHITESPACE@[15; 16)
9+
L_CURLY@[16; 17)
10+
R_CURLY@[17; 18)
11+
WHITESPACE@[18; 19)

tools/src/bin/collect-tests.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,20 @@ fn collect_tests(s: &str) -> Vec<Test> {
7979
.map(str::trim_left)
8080
.group_by(|line| line.starts_with(prefix));
8181

82-
for (is_comment, block) in comment_blocks.into_iter() {
82+
'outer: for (is_comment, block) in comment_blocks.into_iter() {
8383
if !is_comment {
8484
continue;
8585
}
8686
let mut block = block.map(|line| &line[prefix.len()..]);
87-
let first = block.next().unwrap();
88-
if !first.starts_with("test ") {
89-
continue;
90-
}
91-
let name = first["test ".len()..].to_string();
87+
88+
let name = loop {
89+
match block.next() {
90+
Some(line) if line.starts_with("test ") =>
91+
break line["test ".len()..].to_string(),
92+
Some(_) => (),
93+
None => continue 'outer,
94+
}
95+
};
9296
let text: String = itertools::join(block.chain(::std::iter::once("")), "\n");
9397
assert!(!text.trim().is_empty() && text.ends_with("\n"));
9498
res.push(Test { name, text })

0 commit comments

Comments
 (0)