From 6646d49f238bb92d55fcb4900830f19faa2994a5 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 13 Apr 2019 18:38:31 +0800 Subject: Fix bug and add expr , pat , ty matcher --- crates/ra_parser/src/lib.rs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'crates/ra_parser/src/lib.rs') diff --git a/crates/ra_parser/src/lib.rs b/crates/ra_parser/src/lib.rs index 3ceeeebd7..56755c394 100644 --- a/crates/ra_parser/src/lib.rs +++ b/crates/ra_parser/src/lib.rs @@ -53,20 +53,39 @@ pub trait TreeSink { fn error(&mut self, error: ParseError); } -/// Parse given tokens into the given sink as a rust file. -pub fn parse(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) { +fn parse_from_tokens(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink, f: F) +where + F: FnOnce(&mut parser::Parser), +{ let mut p = parser::Parser::new(token_source); - grammar::root(&mut p); + f(&mut p); let events = p.finish(); event::process(tree_sink, events); } +/// Parse given tokens into the given sink as a rust file. +pub fn parse(token_source: &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: &dyn TokenSource, tree_sink: &mut dyn TreeSink) { - let mut p = parser::Parser::new(token_source); - grammar::path(&mut p); - let events = p.finish(); - event::process(tree_sink, events); + parse_from_tokens(token_source, tree_sink, grammar::path); +} + +/// Parse given tokens into the given sink as a expression +pub fn parse_expr(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) { + parse_from_tokens(token_source, tree_sink, grammar::expr); +} + +/// Parse given tokens into the given sink as a ty +pub fn parse_ty(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) { + parse_from_tokens(token_source, tree_sink, grammar::type_); +} + +/// Parse given tokens into the given sink as a pattern +pub fn parse_pat(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) { + parse_from_tokens(token_source, tree_sink, grammar::pattern); } /// A parsing function for a specific braced-block. -- cgit v1.2.3