diff options
author | Aleksey Kladov <[email protected]> | 2018-01-13 10:42:19 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-01-13 10:42:19 +0000 |
commit | 55891be06a1f0a051638cb59f1d15167faf5ab82 (patch) | |
tree | 2816530bc237e66b6d7b40f29838d7bbed085f7f /src | |
parent | 29b2e0adcc35834dda2884222624bfcc268a3eff (diff) |
G: use trees
Diffstat (limited to 'src')
-rw-r--r-- | src/parser/event_parser/grammar/items.rs | 56 | ||||
-rw-r--r-- | src/parser/event_parser/grammar/paths.rs | 22 | ||||
-rw-r--r-- | src/parser/mod.rs | 2 | ||||
-rw-r--r-- | src/syntax_kinds.rs | 14 |
4 files changed, 80 insertions, 14 deletions
diff --git a/src/parser/event_parser/grammar/items.rs b/src/parser/event_parser/grammar/items.rs index f7310c09a..2f64111ab 100644 --- a/src/parser/event_parser/grammar/items.rs +++ b/src/parser/event_parser/grammar/items.rs | |||
@@ -74,9 +74,63 @@ fn mod_item(p: &mut Parser) { | |||
74 | p.curly_block(mod_contents); | 74 | p.curly_block(mod_contents); |
75 | } | 75 | } |
76 | 76 | ||
77 | pub(super) fn is_use_tree_start(kind: SyntaxKind) -> bool { | ||
78 | kind == STAR || kind == L_CURLY | ||
79 | } | ||
80 | |||
77 | fn use_item(p: &mut Parser) { | 81 | fn use_item(p: &mut Parser) { |
78 | paths::use_path(p); | 82 | use_tree(p); |
79 | p.expect(SEMI); | 83 | p.expect(SEMI); |
84 | |||
85 | fn use_tree(p: &mut Parser) -> bool{ | ||
86 | if node_if(p, STAR, USE_TREE, |_| ()) { | ||
87 | return true | ||
88 | } | ||
89 | if node_if(p, [COLONCOLON, STAR], USE_TREE, |_| ()) { | ||
90 | return true | ||
91 | } | ||
92 | if [COLONCOLON, L_CURLY].is_ahead(p) || L_CURLY.is_ahead(p) { | ||
93 | node(p, USE_TREE, |p| { | ||
94 | p.eat(COLONCOLON); | ||
95 | p.curly_block(|p| { | ||
96 | comma_list(p, EOF, use_tree); | ||
97 | }); | ||
98 | }); | ||
99 | return true; | ||
100 | } | ||
101 | if paths::is_path_start(p) { | ||
102 | node(p, USE_TREE, |p| { | ||
103 | paths::use_path(p); | ||
104 | match p.current() { | ||
105 | AS_KW => { | ||
106 | alias(p); | ||
107 | } | ||
108 | COLONCOLON => { | ||
109 | p.bump(); | ||
110 | match p.current() { | ||
111 | STAR => { | ||
112 | p.bump(); | ||
113 | } | ||
114 | L_CURLY => { | ||
115 | p.curly_block(|p| { | ||
116 | comma_list(p, EOF, use_tree); | ||
117 | }); | ||
118 | } | ||
119 | _ => { | ||
120 | // is this unreachable? | ||
121 | p.error() | ||
122 | .message("expected `{` or `*`") | ||
123 | .emit(); | ||
124 | } | ||
125 | } | ||
126 | } | ||
127 | _ => (), | ||
128 | } | ||
129 | }); | ||
130 | return true; | ||
131 | } | ||
132 | false | ||
133 | } | ||
80 | } | 134 | } |
81 | 135 | ||
82 | fn struct_field(p: &mut Parser) -> bool { | 136 | fn struct_field(p: &mut Parser) -> bool { |
diff --git a/src/parser/event_parser/grammar/paths.rs b/src/parser/event_parser/grammar/paths.rs index d6887a9ba..62d1a3bb2 100644 --- a/src/parser/event_parser/grammar/paths.rs +++ b/src/parser/event_parser/grammar/paths.rs | |||
@@ -1,7 +1,11 @@ | |||
1 | use super::*; | 1 | use super::*; |
2 | 2 | ||
3 | pub (crate) fn is_path_start(p: &Parser) -> bool { | ||
4 | AnyOf(&[IDENT, SELF_KW, SUPER_KW, COLONCOLON]).is_ahead(p) | ||
5 | } | ||
6 | |||
3 | pub(crate) fn use_path(p: &mut Parser) { | 7 | pub(crate) fn use_path(p: &mut Parser) { |
4 | if !AnyOf(&[IDENT, SELF_KW, SUPER_KW, COLONCOLON]).is_ahead(p) { | 8 | if !is_path_start(p) { |
5 | return; | 9 | return; |
6 | } | 10 | } |
7 | let mut prev = p.mark(); | 11 | let mut prev = p.mark(); |
@@ -10,11 +14,17 @@ pub(crate) fn use_path(p: &mut Parser) { | |||
10 | }); | 14 | }); |
11 | many(p, |p| { | 15 | many(p, |p| { |
12 | let curr = p.mark(); | 16 | let curr = p.mark(); |
13 | node_if(p, COLONCOLON, PATH, |p| { | 17 | if p.current() == COLONCOLON && !items::is_use_tree_start(p.raw_lookahead(1)) { |
14 | path_segment(p, false); | 18 | node(p, PATH, |p| { |
15 | p.forward_parent(prev, curr); | 19 | p.bump(); |
16 | prev = curr; | 20 | path_segment(p, false); |
17 | }) | 21 | p.forward_parent(prev, curr); |
22 | prev = curr; | ||
23 | }); | ||
24 | true | ||
25 | } else { | ||
26 | false | ||
27 | } | ||
18 | }); | 28 | }); |
19 | } | 29 | } |
20 | 30 | ||
diff --git a/src/parser/mod.rs b/src/parser/mod.rs index c08d32d08..0c29442f9 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs | |||
@@ -29,7 +29,7 @@ fn from_events_to_file( | |||
29 | } | 29 | } |
30 | 30 | ||
31 | match event { | 31 | match event { |
32 | &Event::Start { kind, forward_parent } => { | 32 | &Event::Start { .. } => { |
33 | forward_parents.clear(); | 33 | forward_parents.clear(); |
34 | let mut idx = i; | 34 | let mut idx = i; |
35 | loop { | 35 | loop { |
diff --git a/src/syntax_kinds.rs b/src/syntax_kinds.rs index 48f55fa97..8bc63a210 100644 --- a/src/syntax_kinds.rs +++ b/src/syntax_kinds.rs | |||
@@ -77,13 +77,14 @@ pub const ATTR: SyntaxKind = SyntaxKind(72); | |||
77 | pub const META_ITEM: SyntaxKind = SyntaxKind(73); | 77 | pub const META_ITEM: SyntaxKind = SyntaxKind(73); |
78 | pub const MOD_ITEM: SyntaxKind = SyntaxKind(74); | 78 | pub const MOD_ITEM: SyntaxKind = SyntaxKind(74); |
79 | pub const USE_ITEM: SyntaxKind = SyntaxKind(75); | 79 | pub const USE_ITEM: SyntaxKind = SyntaxKind(75); |
80 | pub const PATH: SyntaxKind = SyntaxKind(76); | 80 | pub const USE_TREE: SyntaxKind = SyntaxKind(76); |
81 | pub const PATH_SEGMENT: SyntaxKind = SyntaxKind(77); | 81 | pub const PATH: SyntaxKind = SyntaxKind(77); |
82 | pub const LITERAL: SyntaxKind = SyntaxKind(78); | 82 | pub const PATH_SEGMENT: SyntaxKind = SyntaxKind(78); |
83 | pub const ALIAS: SyntaxKind = SyntaxKind(79); | 83 | pub const LITERAL: SyntaxKind = SyntaxKind(79); |
84 | pub const VISIBILITY: SyntaxKind = SyntaxKind(80); | 84 | pub const ALIAS: SyntaxKind = SyntaxKind(80); |
85 | pub const VISIBILITY: SyntaxKind = SyntaxKind(81); | ||
85 | 86 | ||
86 | static INFOS: [SyntaxInfo; 81] = [ | 87 | static INFOS: [SyntaxInfo; 82] = [ |
87 | SyntaxInfo { name: "USE_KW" }, | 88 | SyntaxInfo { name: "USE_KW" }, |
88 | SyntaxInfo { name: "FN_KW" }, | 89 | SyntaxInfo { name: "FN_KW" }, |
89 | SyntaxInfo { name: "STRUCT_KW" }, | 90 | SyntaxInfo { name: "STRUCT_KW" }, |
@@ -160,6 +161,7 @@ static INFOS: [SyntaxInfo; 81] = [ | |||
160 | SyntaxInfo { name: "META_ITEM" }, | 161 | SyntaxInfo { name: "META_ITEM" }, |
161 | SyntaxInfo { name: "MOD_ITEM" }, | 162 | SyntaxInfo { name: "MOD_ITEM" }, |
162 | SyntaxInfo { name: "USE_ITEM" }, | 163 | SyntaxInfo { name: "USE_ITEM" }, |
164 | SyntaxInfo { name: "USE_TREE" }, | ||
163 | SyntaxInfo { name: "PATH" }, | 165 | SyntaxInfo { name: "PATH" }, |
164 | SyntaxInfo { name: "PATH_SEGMENT" }, | 166 | SyntaxInfo { name: "PATH_SEGMENT" }, |
165 | SyntaxInfo { name: "LITERAL" }, | 167 | SyntaxInfo { name: "LITERAL" }, |