From 32bebfaf0e24651316c4d70d1e23ef170224bd7c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 2 Sep 2019 17:37:48 +0300 Subject: cleanup --- crates/ra_parser/src/grammar.rs | 146 +++++++++++++++---------------- crates/ra_parser/src/grammar/paths.rs | 2 +- crates/ra_parser/src/grammar/patterns.rs | 2 +- crates/ra_parser/src/grammar/types.rs | 2 +- crates/ra_parser/src/lib.rs | 24 +++-- 5 files changed, 86 insertions(+), 90 deletions(-) (limited to 'crates') 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) { 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(); - - while !p.at(EOF) { - if p.current() == T![;] { - p.bump(); - continue; - } + pub(crate) use super::{ + expressions::block, paths::type_path as path, patterns::pattern, types::type_, + }; - 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 path(p: &mut Parser) { - paths::type_path(p); -} + 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 expr(p: &mut Parser) { - expressions::expr(p); -} + expressions::stmt(p, with_semi) + } -pub(crate) fn type_(p: &mut Parser) { - types::type_(p) -} + pub(crate) fn opt_visibility(p: &mut Parser) { + let _ = super::opt_visibility(p); + } -pub(crate) fn pattern(p: &mut Parser) { - patterns::pattern(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, + } + } -pub(crate) fn stmt(p: &mut Parser, with_semi: bool) { - let with_semi = - if with_semi { expressions::StmtWithSemi::Yes } else { expressions::StmtWithSemi::No }; + if is_delimiter(p) { + items::token_tree(p); + return; + } - expressions::stmt(p, with_semi) -} + 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, + } + } + } -pub(crate) fn block(p: &mut Parser) { - expressions::block(p); -} + m.complete(p, TOKEN_TREE); + } -// 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, - } + pub(crate) fn item(p: &mut Parser) { + items::item_or_macro(p, true, items::ItemFlavor::Mod) } - if is_delimiter(p) { - items::token_tree(p); - return; + pub(crate) fn macro_items(p: &mut Parser) { + let m = p.start(); + items::mod_contents(p, false); + m.complete(p, MACRO_ITEMS); } - 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, + 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, TOKEN_TREE); -} + 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 @@ impl BlockLike { } } -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 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) { 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 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 .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 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![ 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 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) { /// 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. -- cgit v1.2.3