From c89abd42621daff2d652566be4e9e4789599268c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 2 Sep 2019 18:51:03 +0300 Subject: simplify --- crates/ra_parser/src/grammar.rs | 7 +--- crates/ra_parser/src/lib.rs | 84 ++++++++++++++++------------------------- 2 files changed, 35 insertions(+), 56 deletions(-) (limited to 'crates/ra_parser') diff --git a/crates/ra_parser/src/grammar.rs b/crates/ra_parser/src/grammar.rs index 2bcbb4184..d0f0dd4ac 100644 --- a/crates/ra_parser/src/grammar.rs +++ b/crates/ra_parser/src/grammar.rs @@ -61,11 +61,8 @@ pub(crate) mod fragments { let _ = expressions::expr(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 stmt(p: &mut Parser) { + expressions::stmt(p, expressions::StmtWithSemi::No) } pub(crate) fn opt_visibility(p: &mut Parser) { diff --git a/crates/ra_parser/src/lib.rs b/crates/ra_parser/src/lib.rs index e494fc480..45241e566 100644 --- a/crates/ra_parser/src/lib.rs +++ b/crates/ra_parser/src/lib.rs @@ -83,60 +83,42 @@ pub fn parse(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { parse_from_tokens(token_source, tree_sink, grammar::root); } -/// 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::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::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::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::fragments::pattern); -} - -/// Parse given tokens into the given sink as a statement -pub fn parse_stmt( +pub enum FragmentKind { + Path, + Expr, + Statement, + Type, + Pattern, + Item, + Block, + Visibility, + MetaItem, + + // These kinds are used when parsing the result of expansion + // FIXME: use separate fragment kinds for macro inputs and outputs? + Items, + Statements, +} + +pub fn parse_fragment( token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink, - with_semi: bool, + fragment_kind: FragmentKind, ) { - 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::fragments::block); -} - -pub fn parse_meta(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) { - 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::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, 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::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::fragments::macro_stmts); + let parser: fn(&'_ mut parser::Parser) = match fragment_kind { + FragmentKind::Path => grammar::fragments::path, + FragmentKind::Expr => grammar::fragments::expr, + FragmentKind::Type => grammar::fragments::type_, + FragmentKind::Pattern => grammar::fragments::pattern, + FragmentKind::Item => grammar::fragments::item, + FragmentKind::Block => grammar::fragments::block, + FragmentKind::Visibility => grammar::fragments::opt_visibility, + FragmentKind::MetaItem => grammar::fragments::meta_item, + FragmentKind::Statement => grammar::fragments::stmt, + FragmentKind::Items => grammar::fragments::macro_items, + FragmentKind::Statements => grammar::fragments::macro_stmts, + }; + parse_from_tokens(token_source, tree_sink, parser) } /// A parsing function for a specific braced-block. -- cgit v1.2.3