aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_parser/src/grammar.rs146
-rw-r--r--crates/ra_parser/src/grammar/paths.rs2
-rw-r--r--crates/ra_parser/src/grammar/patterns.rs2
-rw-r--r--crates/ra_parser/src/grammar/types.rs2
-rw-r--r--crates/ra_parser/src/lib.rs24
5 files changed, 86 insertions, 90 deletions
diff --git a/crates/ra_parser/src/grammar.rs b/crates/ra_parser/src/grammar.rs
index dee3a229d..2bcbb4184 100644
--- a/crates/ra_parser/src/grammar.rs
+++ b/crates/ra_parser/src/grammar.rs
@@ -49,98 +49,96 @@ pub(crate) fn root(p: &mut Parser) {
49 m.complete(p, SOURCE_FILE); 49 m.complete(p, SOURCE_FILE);
50} 50}
51 51
52pub(crate) fn macro_items(p: &mut Parser) { 52/// Various pieces of syntax that can be parsed by macros by example
53 let m = p.start(); 53pub(crate) mod fragments {
54 items::mod_contents(p, false); 54 use super::*;
55 m.complete(p, MACRO_ITEMS);
56}
57 55
58pub(crate) fn macro_stmts(p: &mut Parser) { 56 pub(crate) use super::{
59 let m = p.start(); 57 expressions::block, paths::type_path as path, patterns::pattern, types::type_,
60 58 };
61 while !p.at(EOF) {
62 if p.current() == T![;] {
63 p.bump();
64 continue;
65 }
66 59
67 expressions::stmt(p, expressions::StmtWithSemi::Optional); 60 pub(crate) fn expr(p: &mut Parser) {
61 let _ = expressions::expr(p);
68 } 62 }
69 63
70 m.complete(p, MACRO_STMTS); 64 pub(crate) fn stmt(p: &mut Parser, with_semi: bool) {
71} 65 let with_semi =
72 66 if with_semi { expressions::StmtWithSemi::Yes } else { expressions::StmtWithSemi::No };
73pub(crate) fn path(p: &mut Parser) {
74 paths::type_path(p);
75}
76 67
77pub(crate) fn expr(p: &mut Parser) { 68 expressions::stmt(p, with_semi)
78 expressions::expr(p); 69 }
79}
80 70
81pub(crate) fn type_(p: &mut Parser) { 71 pub(crate) fn opt_visibility(p: &mut Parser) {
82 types::type_(p) 72 let _ = super::opt_visibility(p);
83} 73 }
84 74
85pub(crate) fn pattern(p: &mut Parser) { 75 // Parse a meta item , which excluded [], e.g : #[ MetaItem ]
86 patterns::pattern(p) 76 pub(crate) fn meta_item(p: &mut Parser) {
87} 77 fn is_delimiter(p: &mut Parser) -> bool {
78 match p.current() {
79 T!['{'] | T!['('] | T!['['] => true,
80 _ => false,
81 }
82 }
88 83
89pub(crate) fn stmt(p: &mut Parser, with_semi: bool) { 84 if is_delimiter(p) {
90 let with_semi = 85 items::token_tree(p);
91 if with_semi { expressions::StmtWithSemi::Yes } else { expressions::StmtWithSemi::No }; 86 return;
87 }
92 88
93 expressions::stmt(p, with_semi) 89 let m = p.start();
94} 90 while !p.at(EOF) {
91 if is_delimiter(p) {
92 items::token_tree(p);
93 break;
94 } else {
95 // https://doc.rust-lang.org/reference/attributes.html
96 // https://doc.rust-lang.org/reference/paths.html#simple-paths
97 // The start of an meta must be a simple path
98 match p.current() {
99 IDENT | T![::] | T![super] | T![self] | T![crate] => p.bump(),
100 T![=] => {
101 p.bump();
102 match p.current() {
103 c if c.is_literal() => p.bump(),
104 T![true] | T![false] => p.bump(),
105 _ => {}
106 }
107 break;
108 }
109 _ => break,
110 }
111 }
112 }
95 113
96pub(crate) fn block(p: &mut Parser) { 114 m.complete(p, TOKEN_TREE);
97 expressions::block(p); 115 }
98}
99 116
100// Parse a meta item , which excluded [], e.g : #[ MetaItem ] 117 pub(crate) fn item(p: &mut Parser) {
101pub(crate) fn meta_item(p: &mut Parser) { 118 items::item_or_macro(p, true, items::ItemFlavor::Mod)
102 fn is_delimiter(p: &mut Parser) -> bool {
103 match p.current() {
104 T!['{'] | T!['('] | T!['['] => true,
105 _ => false,
106 }
107 } 119 }
108 120
109 if is_delimiter(p) { 121 pub(crate) fn macro_items(p: &mut Parser) {
110 items::token_tree(p); 122 let m = p.start();
111 return; 123 items::mod_contents(p, false);
124 m.complete(p, MACRO_ITEMS);
112 } 125 }
113 126
114 let m = p.start(); 127 pub(crate) fn macro_stmts(p: &mut Parser) {
115 while !p.at(EOF) { 128 let m = p.start();
116 if is_delimiter(p) { 129
117 items::token_tree(p); 130 while !p.at(EOF) {
118 break; 131 if p.current() == T![;] {
119 } else { 132 p.bump();
120 // https://doc.rust-lang.org/reference/attributes.html 133 continue;
121 // https://doc.rust-lang.org/reference/paths.html#simple-paths
122 // The start of an meta must be a simple path
123 match p.current() {
124 IDENT | T![::] | T![super] | T![self] | T![crate] => p.bump(),
125 T![=] => {
126 p.bump();
127 match p.current() {
128 c if c.is_literal() => p.bump(),
129 T![true] | T![false] => p.bump(),
130 _ => {}
131 }
132 break;
133 }
134 _ => break,
135 } 134 }
135
136 expressions::stmt(p, expressions::StmtWithSemi::Optional);
136 } 137 }
137 }
138 138
139 m.complete(p, TOKEN_TREE); 139 m.complete(p, MACRO_STMTS);
140} 140 }
141 141
142pub(crate) fn item(p: &mut Parser) {
143 items::item_or_macro(p, true, items::ItemFlavor::Mod)
144} 142}
145 143
146pub(crate) fn reparser( 144pub(crate) fn reparser(
@@ -180,7 +178,7 @@ impl BlockLike {
180 } 178 }
181} 179}
182 180
183pub(crate) fn opt_visibility(p: &mut Parser) -> bool { 181fn opt_visibility(p: &mut Parser) -> bool {
184 match p.current() { 182 match p.current() {
185 T![pub] => { 183 T![pub] => {
186 let m = p.start(); 184 let m = p.start();
diff --git a/crates/ra_parser/src/grammar/paths.rs b/crates/ra_parser/src/grammar/paths.rs
index 07eb53b0c..28c35a67d 100644
--- a/crates/ra_parser/src/grammar/paths.rs
+++ b/crates/ra_parser/src/grammar/paths.rs
@@ -18,7 +18,7 @@ pub(super) fn use_path(p: &mut Parser) {
18 path(p, Mode::Use) 18 path(p, Mode::Use)
19} 19}
20 20
21pub(super) fn type_path(p: &mut Parser) { 21pub(crate) fn type_path(p: &mut Parser) {
22 path(p, Mode::Type) 22 path(p, Mode::Type)
23} 23}
24 24
diff --git a/crates/ra_parser/src/grammar/patterns.rs b/crates/ra_parser/src/grammar/patterns.rs
index eae70ab85..32cde7de6 100644
--- a/crates/ra_parser/src/grammar/patterns.rs
+++ b/crates/ra_parser/src/grammar/patterns.rs
@@ -4,7 +4,7 @@ pub(super) const PATTERN_FIRST: TokenSet = expressions::LITERAL_FIRST
4 .union(paths::PATH_FIRST) 4 .union(paths::PATH_FIRST)
5 .union(token_set![BOX_KW, REF_KW, MUT_KW, L_PAREN, L_BRACK, AMP, UNDERSCORE, MINUS]); 5 .union(token_set![BOX_KW, REF_KW, MUT_KW, L_PAREN, L_BRACK, AMP, UNDERSCORE, MINUS]);
6 6
7pub(super) fn pattern(p: &mut Parser) { 7pub(crate) fn pattern(p: &mut Parser) {
8 pattern_r(p, PAT_RECOVERY_SET); 8 pattern_r(p, PAT_RECOVERY_SET);
9} 9}
10 10
diff --git a/crates/ra_parser/src/grammar/types.rs b/crates/ra_parser/src/grammar/types.rs
index 29d173305..9e321b2a6 100644
--- a/crates/ra_parser/src/grammar/types.rs
+++ b/crates/ra_parser/src/grammar/types.rs
@@ -7,7 +7,7 @@ pub(super) const TYPE_FIRST: TokenSet = paths::PATH_FIRST.union(token_set![
7 7
8const TYPE_RECOVERY_SET: TokenSet = token_set![R_PAREN, COMMA]; 8const TYPE_RECOVERY_SET: TokenSet = token_set![R_PAREN, COMMA];
9 9
10pub(super) fn type_(p: &mut Parser) { 10pub(crate) fn type_(p: &mut Parser) {
11 type_with_bounds_cond(p, true); 11 type_with_bounds_cond(p, true);
12} 12}
13 13
diff --git a/crates/ra_parser/src/lib.rs b/crates/ra_parser/src/lib.rs
index 3d88be642..e494fc480 100644
--- a/crates/ra_parser/src/lib.rs
+++ b/crates/ra_parser/src/lib.rs
@@ -85,22 +85,22 @@ pub fn parse(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) {
85 85
86/// Parse given tokens into the given sink as a path 86/// Parse given tokens into the given sink as a path
87pub fn parse_path(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { 87pub fn parse_path(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) {
88 parse_from_tokens(token_source, tree_sink, grammar::path); 88 parse_from_tokens(token_source, tree_sink, grammar::fragments::path);
89} 89}
90 90
91/// Parse given tokens into the given sink as a expression 91/// Parse given tokens into the given sink as a expression
92pub fn parse_expr(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { 92pub fn parse_expr(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) {
93 parse_from_tokens(token_source, tree_sink, grammar::expr); 93 parse_from_tokens(token_source, tree_sink, grammar::fragments::expr);
94} 94}
95 95
96/// Parse given tokens into the given sink as a ty 96/// Parse given tokens into the given sink as a ty
97pub fn parse_ty(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { 97pub fn parse_ty(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) {
98 parse_from_tokens(token_source, tree_sink, grammar::type_); 98 parse_from_tokens(token_source, tree_sink, grammar::fragments::type_);
99} 99}
100 100
101/// Parse given tokens into the given sink as a pattern 101/// Parse given tokens into the given sink as a pattern
102pub fn parse_pat(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { 102pub fn parse_pat(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) {
103 parse_from_tokens(token_source, tree_sink, grammar::pattern); 103 parse_from_tokens(token_source, tree_sink, grammar::fragments::pattern);
104} 104}
105 105
106/// Parse given tokens into the given sink as a statement 106/// Parse given tokens into the given sink as a statement
@@ -109,36 +109,34 @@ pub fn parse_stmt(
109 tree_sink: &mut dyn TreeSink, 109 tree_sink: &mut dyn TreeSink,
110 with_semi: bool, 110 with_semi: bool,
111) { 111) {
112 parse_from_tokens(token_source, tree_sink, |p| grammar::stmt(p, with_semi)); 112 parse_from_tokens(token_source, tree_sink, |p| grammar::fragments::stmt(p, with_semi));
113} 113}
114 114
115/// Parse given tokens into the given sink as a block 115/// Parse given tokens into the given sink as a block
116pub fn parse_block(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { 116pub fn parse_block(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) {
117 parse_from_tokens(token_source, tree_sink, grammar::block); 117 parse_from_tokens(token_source, tree_sink, grammar::fragments::block);
118} 118}
119 119
120pub fn parse_meta(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { 120pub fn parse_meta(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) {
121 parse_from_tokens(token_source, tree_sink, grammar::meta_item); 121 parse_from_tokens(token_source, tree_sink, grammar::fragments::meta_item);
122} 122}
123 123
124/// Parse given tokens into the given sink as an item 124/// Parse given tokens into the given sink as an item
125pub fn parse_item(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { 125pub fn parse_item(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) {
126 parse_from_tokens(token_source, tree_sink, grammar::item); 126 parse_from_tokens(token_source, tree_sink, grammar::fragments::item);
127} 127}
128 128
129/// Parse given tokens into the given sink as an visibility qualifier 129/// Parse given tokens into the given sink as an visibility qualifier
130pub fn parse_vis(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { 130pub fn parse_vis(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) {
131 parse_from_tokens(token_source, tree_sink, |p| { 131 parse_from_tokens(token_source, tree_sink, grammar::fragments::opt_visibility);
132 grammar::opt_visibility(p);
133 });
134} 132}
135 133
136pub fn parse_macro_items(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { 134pub fn parse_macro_items(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) {
137 parse_from_tokens(token_source, tree_sink, grammar::macro_items); 135 parse_from_tokens(token_source, tree_sink, grammar::fragments::macro_items);
138} 136}
139 137
140pub fn parse_macro_stmts(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { 138pub fn parse_macro_stmts(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) {
141 parse_from_tokens(token_source, tree_sink, grammar::macro_stmts); 139 parse_from_tokens(token_source, tree_sink, grammar::fragments::macro_stmts);
142} 140}
143 141
144/// A parsing function for a specific braced-block. 142/// A parsing function for a specific braced-block.