diff options
author | Aleksey Kladov <[email protected]> | 2018-01-11 20:01:12 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-01-11 20:01:12 +0000 |
commit | 9a8e9bc4c6339051ef260f7794603481b6ff0bf2 (patch) | |
tree | b9d23b06feda7249873ca40c88419e1b5c5d9bd6 | |
parent | 89699c4803a0d12155adf653742f463872667610 (diff) |
G: item outer attributes
-rw-r--r-- | src/parser/event_parser/grammar/attributes.rs | 18 | ||||
-rw-r--r-- | src/parser/event_parser/grammar/items.rs | 2 | ||||
-rw-r--r-- | src/parser/event_parser/grammar/mod.rs | 10 | ||||
-rw-r--r-- | src/parser/event_parser/parser.rs | 4 | ||||
-rw-r--r-- | tests/data/parser/ok/0011_outer_attribute.rs | 3 | ||||
-rw-r--r-- | tests/data/parser/ok/0011_outer_attribute.txt | 29 |
6 files changed, 57 insertions, 9 deletions
diff --git a/src/parser/event_parser/grammar/attributes.rs b/src/parser/event_parser/grammar/attributes.rs index 52210ccad..d774f8827 100644 --- a/src/parser/event_parser/grammar/attributes.rs +++ b/src/parser/event_parser/grammar/attributes.rs | |||
@@ -1,22 +1,26 @@ | |||
1 | use super::*; | 1 | use super::*; |
2 | 2 | ||
3 | enum AttrKind { | ||
4 | Inner, Outer | ||
5 | } | ||
6 | |||
3 | pub(super) fn inner_attributes(p: &mut Parser) { | 7 | pub(super) fn inner_attributes(p: &mut Parser) { |
4 | many(p, |p| attribute(p, true)) | 8 | many(p, |p| attribute(p, AttrKind::Inner)) |
5 | } | 9 | } |
6 | 10 | ||
7 | pub(super) fn outer_attributes(_: &mut Parser) { | 11 | pub(super) fn outer_attributes(p: &mut Parser) { |
12 | many(p, |p| attribute(p, AttrKind::Outer)) | ||
8 | } | 13 | } |
9 | 14 | ||
10 | 15 | ||
11 | fn attribute(p: &mut Parser, inner: bool) -> bool { | 16 | fn attribute(p: &mut Parser, kind: AttrKind) -> bool { |
12 | fn attr_tail(p: &mut Parser) { | 17 | fn attr_tail(p: &mut Parser) { |
13 | meta_item(p) && p.expect(R_BRACK); | 18 | meta_item(p) && p.expect(R_BRACK); |
14 | } | 19 | } |
15 | 20 | ||
16 | if inner { | 21 | match kind { |
17 | node_if(p, [POUND, EXCL, L_BRACK], ATTR, attr_tail) | 22 | AttrKind::Inner => node_if(p, [POUND, EXCL, L_BRACK], ATTR, attr_tail), |
18 | } else { | 23 | AttrKind::Outer => node_if(p, [POUND, L_BRACK], ATTR, attr_tail), |
19 | node_if(p, [POUND, L_BRACK], ATTR, attr_tail) | ||
20 | } | 24 | } |
21 | } | 25 | } |
22 | 26 | ||
diff --git a/src/parser/event_parser/grammar/items.rs b/src/parser/event_parser/grammar/items.rs index 950e02a4d..522986ed0 100644 --- a/src/parser/event_parser/grammar/items.rs +++ b/src/parser/event_parser/grammar/items.rs | |||
@@ -12,7 +12,7 @@ pub(super) fn mod_contents(p: &mut Parser) { | |||
12 | 12 | ||
13 | fn item_first(p: &Parser) -> bool { | 13 | fn item_first(p: &Parser) -> bool { |
14 | match p.current() { | 14 | match p.current() { |
15 | STRUCT_KW | FN_KW | EXTERN_KW | MOD_KW | USE_KW => true, | 15 | STRUCT_KW | FN_KW | EXTERN_KW | MOD_KW | USE_KW | POUND => true, |
16 | _ => false, | 16 | _ => false, |
17 | } | 17 | } |
18 | } | 18 | } |
diff --git a/src/parser/event_parser/grammar/mod.rs b/src/parser/event_parser/grammar/mod.rs index 60458ce70..76f62b714 100644 --- a/src/parser/event_parser/grammar/mod.rs +++ b/src/parser/event_parser/grammar/mod.rs | |||
@@ -41,7 +41,15 @@ fn node<F: FnOnce(&mut Parser)>(p: &mut Parser, node_kind: SyntaxKind, rest: F) | |||
41 | } | 41 | } |
42 | 42 | ||
43 | fn many<F: Fn(&mut Parser) -> bool>(p: &mut Parser, f: F) { | 43 | fn many<F: Fn(&mut Parser) -> bool>(p: &mut Parser, f: F) { |
44 | while f(p) { } | 44 | loop { |
45 | let pos = p.pos(); | ||
46 | if !f(p) { | ||
47 | return | ||
48 | } | ||
49 | if pos == p.pos() { | ||
50 | panic!("Infinite loop in parser") | ||
51 | } | ||
52 | } | ||
45 | } | 53 | } |
46 | 54 | ||
47 | fn comma_list<F: Fn(&mut Parser) -> bool>(p: &mut Parser, end: SyntaxKind, f: F) { | 55 | fn comma_list<F: Fn(&mut Parser) -> bool>(p: &mut Parser, end: SyntaxKind, f: F) { |
diff --git a/src/parser/event_parser/parser.rs b/src/parser/event_parser/parser.rs index a1a0ebfea..d7d24fa27 100644 --- a/src/parser/event_parser/parser.rs +++ b/src/parser/event_parser/parser.rs | |||
@@ -44,6 +44,10 @@ impl<'t> Parser<'t> { | |||
44 | } | 44 | } |
45 | } | 45 | } |
46 | 46 | ||
47 | pub(crate) fn pos(&self) -> usize { | ||
48 | self.pos | ||
49 | } | ||
50 | |||
47 | pub(crate) fn into_events(self) -> Vec<Event> { | 51 | pub(crate) fn into_events(self) -> Vec<Event> { |
48 | assert!(self.curly_limit.is_none()); | 52 | assert!(self.curly_limit.is_none()); |
49 | assert!(self.current() == EOF); | 53 | assert!(self.current() == EOF); |
diff --git a/tests/data/parser/ok/0011_outer_attribute.rs b/tests/data/parser/ok/0011_outer_attribute.rs new file mode 100644 index 000000000..8b80c0d90 --- /dev/null +++ b/tests/data/parser/ok/0011_outer_attribute.rs | |||
@@ -0,0 +1,3 @@ | |||
1 | #[cfg(test)] | ||
2 | #[ignore] | ||
3 | fn foo() {} | ||
diff --git a/tests/data/parser/ok/0011_outer_attribute.txt b/tests/data/parser/ok/0011_outer_attribute.txt new file mode 100644 index 000000000..a790f4095 --- /dev/null +++ b/tests/data/parser/ok/0011_outer_attribute.txt | |||
@@ -0,0 +1,29 @@ | |||
1 | FILE@[0; 35) | ||
2 | ATTR@[0; 13) | ||
3 | POUND@[0; 1) | ||
4 | L_BRACK@[1; 2) | ||
5 | META_ITEM@[2; 11) | ||
6 | IDENT@[2; 5) | ||
7 | L_PAREN@[5; 6) | ||
8 | META_ITEM@[6; 10) | ||
9 | IDENT@[6; 10) | ||
10 | R_PAREN@[10; 11) | ||
11 | R_BRACK@[11; 12) | ||
12 | WHITESPACE@[12; 13) | ||
13 | ATTR@[13; 23) | ||
14 | POUND@[13; 14) | ||
15 | L_BRACK@[14; 15) | ||
16 | META_ITEM@[15; 21) | ||
17 | IDENT@[15; 21) | ||
18 | R_BRACK@[21; 22) | ||
19 | WHITESPACE@[22; 23) | ||
20 | FN_ITEM@[23; 35) | ||
21 | FN_KW@[23; 25) | ||
22 | WHITESPACE@[25; 26) | ||
23 | IDENT@[26; 29) | ||
24 | L_PAREN@[29; 30) | ||
25 | R_PAREN@[30; 31) | ||
26 | WHITESPACE@[31; 32) | ||
27 | L_CURLY@[32; 33) | ||
28 | R_CURLY@[33; 34) | ||
29 | WHITESPACE@[34; 35) | ||