diff --git a/crates/ra_parser/src/grammar.rs b/crates/ra_parser/src/grammar.rs index dee3a229d60..2bcbb418427 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) { m.complete(p, SOURCE_FILE); } -pub(crate) fn macro_items(p: &mut Parser) { - let m = p.start(); - items::mod_contents(p, false); - m.complete(p, MACRO_ITEMS); -} +/// Various pieces of syntax that can be parsed by macros by example +pub(crate) mod fragments { + use super::*; -pub(crate) fn macro_stmts(p: &mut Parser) { - let m = p.start(); + pub(crate) use super::{ + expressions::block, paths::type_path as path, patterns::pattern, types::type_, + }; - while !p.at(EOF) { - if p.current() == T![;] { - p.bump(); - continue; - } - - expressions::stmt(p, expressions::StmtWithSemi::Optional); + pub(crate) fn expr(p: &mut Parser) { + let _ = expressions::expr(p); } - m.complete(p, MACRO_STMTS); -} + pub(crate) fn stmt(p: &mut Parser, with_semi: bool) { + let with_semi = + if with_semi { expressions::StmtWithSemi::Yes } else { expressions::StmtWithSemi::No }; -pub(crate) fn path(p: &mut Parser) { - paths::type_path(p); -} - -pub(crate) fn expr(p: &mut Parser) { - expressions::expr(p); -} - -pub(crate) fn type_(p: &mut Parser) { - types::type_(p) -} - -pub(crate) fn pattern(p: &mut Parser) { - patterns::pattern(p) -} - -pub(crate) fn stmt(p: &mut Parser, with_semi: bool) { - let with_semi = - if with_semi { expressions::StmtWithSemi::Yes } else { expressions::StmtWithSemi::No }; - - expressions::stmt(p, with_semi) -} - -pub(crate) fn block(p: &mut Parser) { - expressions::block(p); -} - -// Parse a meta item , which excluded [], e.g : #[ MetaItem ] -pub(crate) fn meta_item(p: &mut Parser) { - fn is_delimiter(p: &mut Parser) -> bool { - match p.current() { - T!['{'] | T!['('] | T!['['] => true, - _ => false, - } + expressions::stmt(p, with_semi) } - if is_delimiter(p) { - items::token_tree(p); - return; + pub(crate) fn opt_visibility(p: &mut Parser) { + let _ = super::opt_visibility(p); } - let m = p.start(); - while !p.at(EOF) { - if is_delimiter(p) { - items::token_tree(p); - break; - } else { - // https://doc.rust-lang.org/reference/attributes.html - // https://doc.rust-lang.org/reference/paths.html#simple-paths - // The start of an meta must be a simple path + // Parse a meta item , which excluded [], e.g : #[ MetaItem ] + pub(crate) fn meta_item(p: &mut Parser) { + fn is_delimiter(p: &mut Parser) -> bool { match p.current() { - IDENT | T![::] | T![super] | T![self] | T![crate] => p.bump(), - T![=] => { - p.bump(); - match p.current() { - c if c.is_literal() => p.bump(), - T![true] | T![false] => p.bump(), - _ => {} - } - break; - } - _ => break, + T!['{'] | T!['('] | T!['['] => true, + _ => false, } } + + if is_delimiter(p) { + items::token_tree(p); + return; + } + + let m = p.start(); + while !p.at(EOF) { + if is_delimiter(p) { + items::token_tree(p); + break; + } else { + // https://doc.rust-lang.org/reference/attributes.html + // https://doc.rust-lang.org/reference/paths.html#simple-paths + // The start of an meta must be a simple path + match p.current() { + IDENT | T![::] | T![super] | T![self] | T![crate] => p.bump(), + T![=] => { + p.bump(); + match p.current() { + c if c.is_literal() => p.bump(), + T![true] | T![false] => p.bump(), + _ => {} + } + break; + } + _ => break, + } + } + } + + m.complete(p, TOKEN_TREE); } - m.complete(p, TOKEN_TREE); -} + pub(crate) fn item(p: &mut Parser) { + items::item_or_macro(p, true, items::ItemFlavor::Mod) + } + + pub(crate) fn macro_items(p: &mut Parser) { + let m = p.start(); + items::mod_contents(p, false); + m.complete(p, MACRO_ITEMS); + } + + pub(crate) fn macro_stmts(p: &mut Parser) { + let m = p.start(); + + while !p.at(EOF) { + if p.current() == T![;] { + p.bump(); + continue; + } + + expressions::stmt(p, expressions::StmtWithSemi::Optional); + } + + m.complete(p, MACRO_STMTS); + } -pub(crate) fn item(p: &mut Parser) { - items::item_or_macro(p, true, items::ItemFlavor::Mod) } pub(crate) fn reparser( @@ -180,7 +178,7 @@ fn is_block(self) -> bool { } } -pub(crate) fn opt_visibility(p: &mut Parser) -> bool { +fn opt_visibility(p: &mut Parser) -> bool { match p.current() { T![pub] => { let m = p.start(); diff --git a/crates/ra_parser/src/grammar/paths.rs b/crates/ra_parser/src/grammar/paths.rs index 07eb53b0c70..28c35a67d5f 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) { path(p, Mode::Use) } -pub(super) fn type_path(p: &mut Parser) { +pub(crate) fn type_path(p: &mut Parser) { path(p, Mode::Type) } diff --git a/crates/ra_parser/src/grammar/patterns.rs b/crates/ra_parser/src/grammar/patterns.rs index eae70ab85fb..32cde7de671 100644 --- a/crates/ra_parser/src/grammar/patterns.rs +++ b/crates/ra_parser/src/grammar/patterns.rs @@ -4,7 +4,7 @@ .union(paths::PATH_FIRST) .union(token_set![BOX_KW, REF_KW, MUT_KW, L_PAREN, L_BRACK, AMP, UNDERSCORE, MINUS]); -pub(super) fn pattern(p: &mut Parser) { +pub(crate) fn pattern(p: &mut Parser) { pattern_r(p, PAT_RECOVERY_SET); } diff --git a/crates/ra_parser/src/grammar/types.rs b/crates/ra_parser/src/grammar/types.rs index 29d173305cf..9e321b2a699 100644 --- a/crates/ra_parser/src/grammar/types.rs +++ b/crates/ra_parser/src/grammar/types.rs @@ -7,7 +7,7 @@ const TYPE_RECOVERY_SET: TokenSet = token_set![R_PAREN, COMMA]; -pub(super) fn type_(p: &mut Parser) { +pub(crate) fn type_(p: &mut Parser) { type_with_bounds_cond(p, true); } diff --git a/crates/ra_parser/src/lib.rs b/crates/ra_parser/src/lib.rs index 3d88be6425e..e494fc4804b 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) { /// Parse given tokens into the given sink as a path pub fn parse_path(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { - parse_from_tokens(token_source, tree_sink, grammar::path); + parse_from_tokens(token_source, tree_sink, grammar::fragments::path); } /// Parse given tokens into the given sink as a expression pub fn parse_expr(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { - parse_from_tokens(token_source, tree_sink, grammar::expr); + parse_from_tokens(token_source, tree_sink, grammar::fragments::expr); } /// Parse given tokens into the given sink as a ty pub fn parse_ty(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { - parse_from_tokens(token_source, tree_sink, grammar::type_); + parse_from_tokens(token_source, tree_sink, grammar::fragments::type_); } /// Parse given tokens into the given sink as a pattern pub fn parse_pat(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { - parse_from_tokens(token_source, tree_sink, grammar::pattern); + parse_from_tokens(token_source, tree_sink, grammar::fragments::pattern); } /// Parse given tokens into the given sink as a statement @@ -109,36 +109,34 @@ pub fn parse_stmt( tree_sink: &mut dyn TreeSink, with_semi: bool, ) { - parse_from_tokens(token_source, tree_sink, |p| grammar::stmt(p, with_semi)); + parse_from_tokens(token_source, tree_sink, |p| grammar::fragments::stmt(p, with_semi)); } /// Parse given tokens into the given sink as a block pub fn parse_block(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { - parse_from_tokens(token_source, tree_sink, grammar::block); + parse_from_tokens(token_source, tree_sink, grammar::fragments::block); } pub fn parse_meta(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { - parse_from_tokens(token_source, tree_sink, grammar::meta_item); + parse_from_tokens(token_source, tree_sink, grammar::fragments::meta_item); } /// Parse given tokens into the given sink as an item pub fn parse_item(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { - parse_from_tokens(token_source, tree_sink, grammar::item); + parse_from_tokens(token_source, tree_sink, grammar::fragments::item); } /// Parse given tokens into the given sink as an visibility qualifier pub fn parse_vis(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { - parse_from_tokens(token_source, tree_sink, |p| { - grammar::opt_visibility(p); - }); + parse_from_tokens(token_source, tree_sink, grammar::fragments::opt_visibility); } pub fn parse_macro_items(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { - parse_from_tokens(token_source, tree_sink, grammar::macro_items); + parse_from_tokens(token_source, tree_sink, grammar::fragments::macro_items); } pub fn parse_macro_stmts(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { - parse_from_tokens(token_source, tree_sink, grammar::macro_stmts); + parse_from_tokens(token_source, tree_sink, grammar::fragments::macro_stmts); } /// A parsing function for a specific braced-block.