aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-02-04 14:06:00 +0000
committerAleksey Kladov <[email protected]>2018-02-04 14:06:07 +0000
commitc119e8fd8b0106bb2e031e76ac1be89bd10a0674 (patch)
tree3badb041e7a5551f54fe2b31868d99bb158eed10
parent85c42fba1291f1cc41fb7bfec63117895b394fc5 (diff)
G: unsafe fn
-rw-r--r--src/parser/grammar/items/mod.rs92
-rw-r--r--src/parser/parser.rs6
-rw-r--r--tests/data/parser/inline/0011_unsafe_fn.rs1
-rw-r--r--tests/data/parser/inline/0011_unsafe_fn.txt13
-rw-r--r--tests/data/parser/inline/0012_unsafe_extern_fn.rs1
-rw-r--r--tests/data/parser/inline/0012_unsafe_extern_fn.txt18
-rw-r--r--tests/data/parser/inline/0013_unsafe_block_in_mod.rs1
-rw-r--r--tests/data/parser/inline/0013_unsafe_block_in_mod.txt27
8 files changed, 128 insertions, 31 deletions
diff --git a/src/parser/grammar/items/mod.rs b/src/parser/grammar/items/mod.rs
index 4afe2e418..588fca4c6 100644
--- a/src/parser/grammar/items/mod.rs
+++ b/src/parser/grammar/items/mod.rs
@@ -81,38 +81,74 @@ fn item(p: &mut Parser) {
81 CONST_ITEM 81 CONST_ITEM
82 } 82 }
83 }, 83 },
84 // test unsafe_trait 84 UNSAFE_KW => {
85 // unsafe trait T {}
86 UNSAFE_KW if la == TRAIT_KW => {
87 p.bump(); 85 p.bump();
88 traits::trait_item(p); 86 let la = p.nth(1);
89 TRAIT_ITEM 87 match p.current() {
90 } 88 // test unsafe_trait
89 // unsafe trait T {}
90 TRAIT_KW => {
91 traits::trait_item(p);
92 TRAIT_ITEM
93 }
91 94
92 // test unsafe_auto_trait 95 // test unsafe_auto_trait
93 // unsafe auto trait T {} 96 // unsafe auto trait T {}
94 UNSAFE_KW if p.at_kw(1, "auto") && p.nth(2) == TRAIT_KW => { 97 IDENT if p.at_kw("auto") && la == TRAIT_KW => {
95 p.bump(); 98 p.bump_remap(AUTO_KW);
96 p.bump_remap(AUTO_KW); 99 traits::trait_item(p);
97 traits::trait_item(p); 100 TRAIT_ITEM
98 TRAIT_ITEM 101 }
99 }
100 102
101 // test unsafe_impl 103 // test unsafe_impl
102 // unsafe impl Foo {} 104 // unsafe impl Foo {}
103 UNSAFE_KW if la == IMPL_KW => { 105 IMPL_KW => {
104 p.bump(); 106 traits::impl_item(p);
105 traits::impl_item(p); 107 IMPL_ITEM
106 IMPL_ITEM 108 }
107 }
108 109
109 // test unsafe_default_impl 110 // test unsafe_default_impl
110 // unsafe default impl Foo {} 111 // unsafe default impl Foo {}
111 UNSAFE_KW if p.at_kw(1, "default") && p.nth(2) == IMPL_KW => { 112 IDENT if p.at_kw("default") && la == IMPL_KW => {
112 p.bump(); 113 p.bump_remap(DEFAULT_KW);
113 p.bump_remap(DEFAULT_KW); 114 traits::impl_item(p);
114 traits::impl_item(p); 115 IMPL_ITEM
115 IMPL_ITEM 116 }
117
118 // test unsafe_extern_fn
119 // unsafe extern "C" fn foo() {}
120 EXTERN_KW => {
121 abi(p);
122 if !p.at(FN_KW) {
123 item.abandon(p);
124 p.error().message("expected function").emit();
125 return
126 }
127 fn_item(p);
128 FN_ITEM
129 }
130
131 // test unsafe_fn
132 // unsafe fn foo() {}
133 FN_KW => {
134 fn_item(p);
135 FN_ITEM
136 }
137
138 t => {
139 item.abandon(p);
140 let message = "expected `trait`, `impl` or `fn`";
141
142 // test unsafe_block_in_mod
143 // fn foo(){} unsafe { } fn bar(){}
144 if t == L_CURLY {
145 error_block(p, message);
146 } else {
147 p.error().message(message).emit();
148 }
149 return;
150 }
151 }
116 } 152 }
117 153
118 MOD_KW => { 154 MOD_KW => {
diff --git a/src/parser/parser.rs b/src/parser/parser.rs
index 7e1b22ee5..53a116fb7 100644
--- a/src/parser/parser.rs
+++ b/src/parser/parser.rs
@@ -1,6 +1,6 @@
1use super::Event; 1use super::Event;
2use super::input::{InputPosition, ParserInput}; 2use super::input::{InputPosition, ParserInput};
3use SyntaxKind::{self, EOF, TOMBSTONE, IDENT}; 3use SyntaxKind::{self, EOF, TOMBSTONE};
4 4
5pub(crate) struct Marker { 5pub(crate) struct Marker {
6 pos: u32, 6 pos: u32,
@@ -161,8 +161,8 @@ impl<'t> Parser<'t> {
161 self.inp.kind(self.pos + n) 161 self.inp.kind(self.pos + n)
162 } 162 }
163 163
164 pub(crate) fn at_kw(&self, n: u32, t: &str) -> bool { 164 pub(crate) fn at_kw(&self, t: &str) -> bool {
165 self.nth(n) == IDENT && self.inp.text(self.pos + n) == t 165 self.inp.text(self.pos) == t
166 } 166 }
167 167
168 pub(crate) fn current(&self) -> SyntaxKind { 168 pub(crate) fn current(&self) -> SyntaxKind {
diff --git a/tests/data/parser/inline/0011_unsafe_fn.rs b/tests/data/parser/inline/0011_unsafe_fn.rs
new file mode 100644
index 000000000..33cfc4cd7
--- /dev/null
+++ b/tests/data/parser/inline/0011_unsafe_fn.rs
@@ -0,0 +1 @@
unsafe fn foo() {}
diff --git a/tests/data/parser/inline/0011_unsafe_fn.txt b/tests/data/parser/inline/0011_unsafe_fn.txt
new file mode 100644
index 000000000..23eca1d65
--- /dev/null
+++ b/tests/data/parser/inline/0011_unsafe_fn.txt
@@ -0,0 +1,13 @@
1FILE@[0; 19)
2 FN_ITEM@[0; 19)
3 UNSAFE_KW@[0; 6)
4 WHITESPACE@[6; 7)
5 FN_KW@[7; 9)
6 WHITESPACE@[9; 10)
7 IDENT@[10; 13) "foo"
8 L_PAREN@[13; 14)
9 R_PAREN@[14; 15)
10 WHITESPACE@[15; 16)
11 L_CURLY@[16; 17)
12 R_CURLY@[17; 18)
13 WHITESPACE@[18; 19)
diff --git a/tests/data/parser/inline/0012_unsafe_extern_fn.rs b/tests/data/parser/inline/0012_unsafe_extern_fn.rs
new file mode 100644
index 000000000..1295c2cd2
--- /dev/null
+++ b/tests/data/parser/inline/0012_unsafe_extern_fn.rs
@@ -0,0 +1 @@
unsafe extern "C" fn foo() {}
diff --git a/tests/data/parser/inline/0012_unsafe_extern_fn.txt b/tests/data/parser/inline/0012_unsafe_extern_fn.txt
new file mode 100644
index 000000000..547cb499d
--- /dev/null
+++ b/tests/data/parser/inline/0012_unsafe_extern_fn.txt
@@ -0,0 +1,18 @@
1FILE@[0; 30)
2 FN_ITEM@[0; 30)
3 UNSAFE_KW@[0; 6)
4 ABI@[6; 18)
5 WHITESPACE@[6; 7)
6 EXTERN_KW@[7; 13)
7 WHITESPACE@[13; 14)
8 STRING@[14; 17)
9 WHITESPACE@[17; 18)
10 FN_KW@[18; 20)
11 WHITESPACE@[20; 21)
12 IDENT@[21; 24) "foo"
13 L_PAREN@[24; 25)
14 R_PAREN@[25; 26)
15 WHITESPACE@[26; 27)
16 L_CURLY@[27; 28)
17 R_CURLY@[28; 29)
18 WHITESPACE@[29; 30)
diff --git a/tests/data/parser/inline/0013_unsafe_block_in_mod.rs b/tests/data/parser/inline/0013_unsafe_block_in_mod.rs
new file mode 100644
index 000000000..26141e904
--- /dev/null
+++ b/tests/data/parser/inline/0013_unsafe_block_in_mod.rs
@@ -0,0 +1 @@
fn foo(){} unsafe { } fn bar(){}
diff --git a/tests/data/parser/inline/0013_unsafe_block_in_mod.txt b/tests/data/parser/inline/0013_unsafe_block_in_mod.txt
new file mode 100644
index 000000000..2e319be73
--- /dev/null
+++ b/tests/data/parser/inline/0013_unsafe_block_in_mod.txt
@@ -0,0 +1,27 @@
1FILE@[0; 33)
2 FN_ITEM@[0; 11)
3 FN_KW@[0; 2)
4 WHITESPACE@[2; 3)
5 IDENT@[3; 6) "foo"
6 L_PAREN@[6; 7)
7 R_PAREN@[7; 8)
8 L_CURLY@[8; 9)
9 R_CURLY@[9; 10)
10 WHITESPACE@[10; 11)
11 UNSAFE_KW@[11; 17)
12 ERROR@[17; 22)
13 err: `expected `trait`, `impl` or `fn``
14 WHITESPACE@[17; 18)
15 L_CURLY@[18; 19)
16 WHITESPACE@[19; 20)
17 R_CURLY@[20; 21)
18 WHITESPACE@[21; 22)
19 FN_ITEM@[22; 33)
20 FN_KW@[22; 24)
21 WHITESPACE@[24; 25)
22 IDENT@[25; 28) "bar"
23 L_PAREN@[28; 29)
24 R_PAREN@[29; 30)
25 L_CURLY@[30; 31)
26 R_CURLY@[31; 32)
27 WHITESPACE@[32; 33)