From d3a252b559489fa4fb21d9b5d1ddf83fe8e825d7 Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Sat, 2 Mar 2019 20:20:26 +0100 Subject: Replace option with result in mbe --- crates/ra_mbe/src/lib.rs | 15 ++++++++-- crates/ra_mbe/src/mbe_expander.rs | 63 ++++++++++++++++++++++----------------- crates/ra_mbe/src/mbe_parser.rs | 33 ++++++++++---------- crates/ra_mbe/src/tt_cursor.rs | 56 +++++++++++++++++----------------- 4 files changed, 93 insertions(+), 74 deletions(-) diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 907402f5f..8a2d6ff63 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -24,6 +24,15 @@ use ra_syntax::SmolStr; pub use tt::{Delimiter, Punct}; +#[derive(Debug, PartialEq, Eq)] +pub enum MacroRulesError { + NoMatchingRule, + UnexpectedToken, + BindingError(String), + ParseError, +} + +pub type Result = ::std::result::Result; pub use crate::syntax_bridge::{ast_to_token_tree, token_tree_to_ast_item_list}; /// This struct contains AST for a single `macro_rules` definition. What might @@ -36,11 +45,11 @@ pub struct MacroRules { } impl MacroRules { - pub fn parse(tt: &tt::Subtree) -> Option { + pub fn parse(tt: &tt::Subtree) -> Result { mbe_parser::parse(tt) } - pub fn expand(&self, tt: &tt::Subtree) -> Option { - mbe_expander::exapnd(self, tt) + pub fn expand(&self, tt: &tt::Subtree) -> Result { + mbe_expander::expand(self, tt) } } diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index 1acba86ea..c393d8487 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs @@ -5,17 +5,19 @@ use rustc_hash::FxHashMap; use ra_syntax::SmolStr; use tt::TokenId; +use crate::{MacroRulesError, Result}; use crate::tt_cursor::TtCursor; -pub(crate) fn exapnd(rules: &crate::MacroRules, input: &tt::Subtree) -> Option { - rules.rules.iter().find_map(|it| expand_rule(it, input)) +pub(crate) fn expand(rules: &crate::MacroRules, input: &tt::Subtree) -> Result { + rules.rules.iter().find_map(|it| expand_rule(it, input).ok()) + .ok_or(MacroRulesError::NoMatchingRule) } -fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Option { +fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Result { let mut input = TtCursor::new(input); let bindings = match_lhs(&rule.lhs, &mut input)?; if !input.is_eof() { - return None; + return Err(MacroRulesError::UnexpectedToken); } expand_subtree(&rule.rhs, &bindings, &mut Vec::new()) } @@ -77,40 +79,47 @@ enum Binding { } impl Bindings { - fn get(&self, name: &SmolStr, nesting: &[usize]) -> Option<&tt::TokenTree> { - let mut b = self.inner.get(name)?; + fn get(&self, name: &SmolStr, nesting: &[usize]) -> Result<&tt::TokenTree> { + let mut b = self.inner.get(name).ok_or(MacroRulesError::BindingError( + format!("could not find binding {}", name) + ))?; for &idx in nesting.iter() { b = match b { Binding::Simple(_) => break, - Binding::Nested(bs) => bs.get(idx)?, + Binding::Nested(bs) => bs.get(idx).ok_or(MacroRulesError::BindingError( + format!("could not find nested binding {}", name)) + )?, }; } match b { - Binding::Simple(it) => Some(it), - Binding::Nested(_) => None, + Binding::Simple(it) => Ok(it), + Binding::Nested(_) => Err(MacroRulesError::BindingError( + format!("expected simple binding, found nested binding {}", name))), } } - fn push_nested(&mut self, nested: Bindings) -> Option<()> { + + fn push_nested(&mut self, nested: Bindings) -> Result<()> { for (key, value) in nested.inner { if !self.inner.contains_key(&key) { self.inner.insert(key.clone(), Binding::Nested(Vec::new())); } match self.inner.get_mut(&key) { Some(Binding::Nested(it)) => it.push(value), - _ => return None, + _ => return Err(MacroRulesError::BindingError( + format!("nested binding for {} not found", key))), } } - Some(()) + Ok(()) } } -fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Option { +fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result { let mut res = Bindings::default(); for pat in pattern.token_trees.iter() { match pat { crate::TokenTree::Leaf(leaf) => match leaf { crate::Leaf::Var(crate::Var { text, kind }) => { - let kind = kind.clone()?; + let kind = kind.clone().ok_or(MacroRulesError::ParseError)?; match kind.as_str() { "ident" => { let ident = input.eat_ident()?.clone(); @@ -119,28 +128,28 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Option Binding::Simple(tt::Leaf::from(ident).into()), ); } - _ => return None, + _ => return Err(MacroRulesError::UnexpectedToken), } } crate::Leaf::Punct(punct) => { if input.eat_punct()? != punct { - return None; + return Err(MacroRulesError::UnexpectedToken); } } crate::Leaf::Ident(ident) => { if input.eat_ident()?.text != ident.text { - return None; + return Err(MacroRulesError::UnexpectedToken); } } - _ => return None, + _ => return Err(MacroRulesError::UnexpectedToken), }, crate::TokenTree::Repeat(crate::Repeat { subtree, kind: _, separator }) => { - while let Some(nested) = match_lhs(subtree, input) { + while let Ok(nested) = match_lhs(subtree, input) { res.push_nested(nested)?; if let Some(separator) = *separator { if !input.is_eof() { if input.eat_punct()?.char != separator { - return None; + return Err(MacroRulesError::UnexpectedToken); } } } @@ -149,34 +158,34 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Option _ => {} } } - Some(res) + Ok(res) } fn expand_subtree( template: &crate::Subtree, bindings: &Bindings, nesting: &mut Vec, -) -> Option { +) -> Result { let token_trees = template .token_trees .iter() .map(|it| expand_tt(it, bindings, nesting)) - .collect::>>()?; + .collect::>>()?; - Some(tt::Subtree { token_trees, delimiter: template.delimiter }) + Ok(tt::Subtree { token_trees, delimiter: template.delimiter }) } fn expand_tt( template: &crate::TokenTree, bindings: &Bindings, nesting: &mut Vec, -) -> Option { +) -> Result { let res: tt::TokenTree = match template { crate::TokenTree::Subtree(subtree) => expand_subtree(subtree, bindings, nesting)?.into(), crate::TokenTree::Repeat(repeat) => { let mut token_trees = Vec::new(); nesting.push(0); - while let Some(t) = expand_subtree(&repeat.subtree, bindings, nesting) { + while let Ok(t) = expand_subtree(&repeat.subtree, bindings, nesting) { let idx = nesting.pop().unwrap(); nesting.push(idx + 1); token_trees.push(t.into()) @@ -194,5 +203,5 @@ fn expand_tt( crate::Leaf::Literal(l) => tt::Leaf::from(tt::Literal { text: l.text.clone() }).into(), }, }; - Some(res) + Ok(res) } diff --git a/crates/ra_mbe/src/mbe_parser.rs b/crates/ra_mbe/src/mbe_parser.rs index 58e2533f1..ee1b11091 100644 --- a/crates/ra_mbe/src/mbe_parser.rs +++ b/crates/ra_mbe/src/mbe_parser.rs @@ -1,40 +1,41 @@ /// This module parses a raw `tt::TokenStream` into macro-by-example token /// stream. This is a *mostly* identify function, expect for handling of /// `$var:tt_kind` and `$(repeat),*` constructs. +use crate::{MacroRulesError, Result}; use crate::tt_cursor::TtCursor; -pub(crate) fn parse(tt: &tt::Subtree) -> Option { +pub(crate) fn parse(tt: &tt::Subtree) -> Result { let mut parser = TtCursor::new(tt); let mut rules = Vec::new(); while !parser.is_eof() { rules.push(parse_rule(&mut parser)?); - if parser.expect_char(';') == None { + if let Err(e) = parser.expect_char(';') { if !parser.is_eof() { - return None; + return Err(e); } break; } } - Some(crate::MacroRules { rules }) + Ok(crate::MacroRules { rules }) } -fn parse_rule(p: &mut TtCursor) -> Option { +fn parse_rule(p: &mut TtCursor) -> Result { let lhs = parse_subtree(p.eat_subtree()?)?; p.expect_char('=')?; p.expect_char('>')?; let mut rhs = parse_subtree(p.eat_subtree()?)?; rhs.delimiter = crate::Delimiter::None; - Some(crate::Rule { lhs, rhs }) + Ok(crate::Rule { lhs, rhs }) } -fn parse_subtree(tt: &tt::Subtree) -> Option { +fn parse_subtree(tt: &tt::Subtree) -> Result { let mut token_trees = Vec::new(); let mut p = TtCursor::new(tt); - while let Some(tt) = p.eat() { + while let Ok(tt) = p.eat() { let child: crate::TokenTree = match tt { tt::TokenTree::Leaf(leaf) => match leaf { tt::Leaf::Punct(tt::Punct { char: '$', .. }) => { - if p.at_ident().is_some() { + if p.at_ident().is_ok() { crate::Leaf::from(parse_var(&mut p)?).into() } else { parse_repeat(&mut p)?.into() @@ -52,15 +53,15 @@ fn parse_subtree(tt: &tt::Subtree) -> Option { }; token_trees.push(child); } - Some(crate::Subtree { token_trees, delimiter: tt.delimiter }) + Ok(crate::Subtree { token_trees, delimiter: tt.delimiter }) } -fn parse_var(p: &mut TtCursor) -> Option { +fn parse_var(p: &mut TtCursor) -> Result { let ident = p.eat_ident().unwrap(); let text = ident.text.clone(); let kind = if p.at_char(':') { p.bump(); - if let Some(ident) = p.eat_ident() { + if let Ok(ident) = p.eat_ident() { Some(ident.text.clone()) } else { p.rev_bump(); @@ -69,10 +70,10 @@ fn parse_var(p: &mut TtCursor) -> Option { } else { None }; - Some(crate::Var { text, kind }) + Ok(crate::Var { text, kind }) } -fn parse_repeat(p: &mut TtCursor) -> Option { +fn parse_repeat(p: &mut TtCursor) -> Result { let subtree = p.eat_subtree().unwrap(); let mut subtree = parse_subtree(subtree)?; subtree.delimiter = crate::Delimiter::None; @@ -86,8 +87,8 @@ fn parse_repeat(p: &mut TtCursor) -> Option { '*' => crate::RepeatKind::ZeroOrMore, '+' => crate::RepeatKind::OneOrMore, '?' => crate::RepeatKind::ZeroOrOne, - _ => return None, + _ => return Err(MacroRulesError::ParseError), }; p.bump(); - Some(crate::Repeat { subtree, kind, separator }) + Ok(crate::Repeat { subtree, kind, separator }) } diff --git a/crates/ra_mbe/src/tt_cursor.rs b/crates/ra_mbe/src/tt_cursor.rs index 30c8eda67..117471841 100644 --- a/crates/ra_mbe/src/tt_cursor.rs +++ b/crates/ra_mbe/src/tt_cursor.rs @@ -1,3 +1,5 @@ +use crate::{MacroRulesError, Result}; + #[derive(Clone)] pub(crate) struct TtCursor<'a> { subtree: &'a tt::Subtree, @@ -17,24 +19,24 @@ impl<'a> TtCursor<'a> { self.subtree.token_trees.get(self.pos) } - pub(crate) fn at_punct(&self) -> Option<&'a tt::Punct> { + pub(crate) fn at_punct(&self) -> Result<&'a tt::Punct> { match self.current() { - Some(tt::TokenTree::Leaf(tt::Leaf::Punct(it))) => Some(it), - _ => None, + Some(tt::TokenTree::Leaf(tt::Leaf::Punct(it))) => Ok(it), + _ => Err(MacroRulesError::ParseError), } } pub(crate) fn at_char(&self, char: char) -> bool { match self.at_punct() { - Some(tt::Punct { char: c, .. }) if *c == char => true, + Ok(tt::Punct { char: c, .. }) if *c == char => true, _ => false, } } - pub(crate) fn at_ident(&mut self) -> Option<&'a tt::Ident> { + pub(crate) fn at_ident(&mut self) -> Result<&'a tt::Ident> { match self.current() { - Some(tt::TokenTree::Leaf(tt::Leaf::Ident(i))) => Some(i), - _ => None, + Some(tt::TokenTree::Leaf(tt::Leaf::Ident(i))) => Ok(i), + _ => Err(MacroRulesError::ParseError), } } @@ -45,47 +47,45 @@ impl<'a> TtCursor<'a> { self.pos -= 1; } - pub(crate) fn eat(&mut self) -> Option<&'a tt::TokenTree> { + pub(crate) fn eat(&mut self) -> Result<&'a tt::TokenTree> { match self.current() { Some(it) => { self.bump(); - Some(it) + Ok(it) } - None => None, + None => Err(MacroRulesError::ParseError), } } - pub(crate) fn eat_subtree(&mut self) -> Option<&'a tt::Subtree> { - match self.current()? { - tt::TokenTree::Subtree(sub) => { + pub(crate) fn eat_subtree(&mut self) -> Result<&'a tt::Subtree> { + match self.current() { + Some(tt::TokenTree::Subtree(sub)) => { self.bump(); - Some(sub) + Ok(sub) } - _ => return None, + _ => Err(MacroRulesError::ParseError), } } - pub(crate) fn eat_punct(&mut self) -> Option<&'a tt::Punct> { - if let Some(it) = self.at_punct() { + pub(crate) fn eat_punct(&mut self) -> Result<&'a tt::Punct> { + self.at_punct().map(|it| { self.bump(); - return Some(it); - } - None + it + }) } - pub(crate) fn eat_ident(&mut self) -> Option<&'a tt::Ident> { - if let Some(i) = self.at_ident() { + pub(crate) fn eat_ident(&mut self) -> Result<&'a tt::Ident> { + self.at_ident().map(|i| { self.bump(); - return Some(i); - } - None + i + }) } - pub(crate) fn expect_char(&mut self, char: char) -> Option<()> { + pub(crate) fn expect_char(&mut self, char: char) -> Result<()> { if self.at_char(char) { self.bump(); - return Some(()); + return Ok(()); } - None + Err(MacroRulesError::ParseError) } } -- cgit v1.2.3 From dffe318701dcbd7da2b241cd8f623be9d4744ee3 Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Sat, 2 Mar 2019 20:49:13 +0100 Subject: Formatting --- crates/ra_mbe/src/mbe_expander.rs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index c393d8487..0a5bbf6b1 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs @@ -9,7 +9,10 @@ use crate::{MacroRulesError, Result}; use crate::tt_cursor::TtCursor; pub(crate) fn expand(rules: &crate::MacroRules, input: &tt::Subtree) -> Result { - rules.rules.iter().find_map(|it| expand_rule(it, input).ok()) + rules + .rules + .iter() + .find_map(|it| expand_rule(it, input).ok()) .ok_or(MacroRulesError::NoMatchingRule) } @@ -80,21 +83,24 @@ enum Binding { impl Bindings { fn get(&self, name: &SmolStr, nesting: &[usize]) -> Result<&tt::TokenTree> { - let mut b = self.inner.get(name).ok_or(MacroRulesError::BindingError( - format!("could not find binding {}", name) - ))?; + let mut b = self + .inner + .get(name) + .ok_or(MacroRulesError::BindingError(format!("could not find binding {}", name)))?; for &idx in nesting.iter() { b = match b { Binding::Simple(_) => break, Binding::Nested(bs) => bs.get(idx).ok_or(MacroRulesError::BindingError( - format!("could not find nested binding {}", name)) - )?, + format!("could not find nested binding {}", name), + ))?, }; } match b { Binding::Simple(it) => Ok(it), - Binding::Nested(_) => Err(MacroRulesError::BindingError( - format!("expected simple binding, found nested binding {}", name))), + Binding::Nested(_) => Err(MacroRulesError::BindingError(format!( + "expected simple binding, found nested binding {}", + name + ))), } } @@ -105,8 +111,12 @@ impl Bindings { } match self.inner.get_mut(&key) { Some(Binding::Nested(it)) => it.push(value), - _ => return Err(MacroRulesError::BindingError( - format!("nested binding for {} not found", key))), + _ => { + return Err(MacroRulesError::BindingError(format!( + "nested binding for {} not found", + key + ))) + } } } Ok(()) -- cgit v1.2.3 From 725805dc795159399c90a5c9b6f11ffeadec2e0f Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Sun, 3 Mar 2019 10:40:03 +0100 Subject: Split parse and expand errors --- crates/ra_mbe/src/lib.rs | 13 +++++--- crates/ra_mbe/src/mbe_expander.rs | 65 ++++++++++++++++++++------------------- crates/ra_mbe/src/mbe_parser.rs | 24 +++++++-------- crates/ra_mbe/src/tt_cursor.rs | 44 +++++++++++++------------- 4 files changed, 74 insertions(+), 72 deletions(-) diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 8a2d6ff63..34840dfa1 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -25,14 +25,17 @@ use ra_syntax::SmolStr; pub use tt::{Delimiter, Punct}; #[derive(Debug, PartialEq, Eq)] -pub enum MacroRulesError { +pub enum ParseError { + ParseError, +} + +#[derive(Debug, PartialEq, Eq)] +pub enum ExpandError { NoMatchingRule, UnexpectedToken, BindingError(String), - ParseError, } -pub type Result = ::std::result::Result; pub use crate::syntax_bridge::{ast_to_token_tree, token_tree_to_ast_item_list}; /// This struct contains AST for a single `macro_rules` definition. What might @@ -45,10 +48,10 @@ pub struct MacroRules { } impl MacroRules { - pub fn parse(tt: &tt::Subtree) -> Result { + pub fn parse(tt: &tt::Subtree) -> Result { mbe_parser::parse(tt) } - pub fn expand(&self, tt: &tt::Subtree) -> Result { + pub fn expand(&self, tt: &tt::Subtree) -> Result { mbe_expander::expand(self, tt) } } diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index 0a5bbf6b1..d4ce3bfe3 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs @@ -5,22 +5,21 @@ use rustc_hash::FxHashMap; use ra_syntax::SmolStr; use tt::TokenId; -use crate::{MacroRulesError, Result}; +use crate::ExpandError; use crate::tt_cursor::TtCursor; -pub(crate) fn expand(rules: &crate::MacroRules, input: &tt::Subtree) -> Result { - rules - .rules - .iter() - .find_map(|it| expand_rule(it, input).ok()) - .ok_or(MacroRulesError::NoMatchingRule) +pub(crate) fn expand( + rules: &crate::MacroRules, + input: &tt::Subtree, +) -> Result { + rules.rules.iter().find_map(|it| expand_rule(it, input).ok()).ok_or(ExpandError::NoMatchingRule) } -fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Result { +fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Result { let mut input = TtCursor::new(input); let bindings = match_lhs(&rule.lhs, &mut input)?; if !input.is_eof() { - return Err(MacroRulesError::UnexpectedToken); + return Err(ExpandError::UnexpectedToken); } expand_subtree(&rule.rhs, &bindings, &mut Vec::new()) } @@ -82,29 +81,30 @@ enum Binding { } impl Bindings { - fn get(&self, name: &SmolStr, nesting: &[usize]) -> Result<&tt::TokenTree> { + fn get(&self, name: &SmolStr, nesting: &[usize]) -> Result<&tt::TokenTree, ExpandError> { let mut b = self .inner .get(name) - .ok_or(MacroRulesError::BindingError(format!("could not find binding {}", name)))?; + .ok_or(ExpandError::BindingError(format!("could not find binding {}", name)))?; for &idx in nesting.iter() { b = match b { Binding::Simple(_) => break, - Binding::Nested(bs) => bs.get(idx).ok_or(MacroRulesError::BindingError( - format!("could not find nested binding {}", name), - ))?, + Binding::Nested(bs) => bs.get(idx).ok_or(ExpandError::BindingError(format!( + "could not find nested binding {}", + name + )))?, }; } match b { Binding::Simple(it) => Ok(it), - Binding::Nested(_) => Err(MacroRulesError::BindingError(format!( + Binding::Nested(_) => Err(ExpandError::BindingError(format!( "expected simple binding, found nested binding {}", name ))), } } - fn push_nested(&mut self, nested: Bindings) -> Result<()> { + fn push_nested(&mut self, nested: Bindings) -> Result<(), ExpandError> { for (key, value) in nested.inner { if !self.inner.contains_key(&key) { self.inner.insert(key.clone(), Binding::Nested(Vec::new())); @@ -112,10 +112,10 @@ impl Bindings { match self.inner.get_mut(&key) { Some(Binding::Nested(it)) => it.push(value), _ => { - return Err(MacroRulesError::BindingError(format!( + return Err(ExpandError::BindingError(format!( "nested binding for {} not found", key - ))) + ))); } } } @@ -123,43 +123,44 @@ impl Bindings { } } -fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result { +fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result { let mut res = Bindings::default(); for pat in pattern.token_trees.iter() { match pat { crate::TokenTree::Leaf(leaf) => match leaf { crate::Leaf::Var(crate::Var { text, kind }) => { - let kind = kind.clone().ok_or(MacroRulesError::ParseError)?; + let kind = kind.clone().ok_or(ExpandError::UnexpectedToken)?; match kind.as_str() { "ident" => { - let ident = input.eat_ident()?.clone(); + let ident = + input.eat_ident().ok_or(ExpandError::UnexpectedToken)?.clone(); res.inner.insert( text.clone(), Binding::Simple(tt::Leaf::from(ident).into()), ); } - _ => return Err(MacroRulesError::UnexpectedToken), + _ => return Err(ExpandError::UnexpectedToken), } } crate::Leaf::Punct(punct) => { - if input.eat_punct()? != punct { - return Err(MacroRulesError::UnexpectedToken); + if input.eat_punct() != Some(punct) { + return Err(ExpandError::UnexpectedToken); } } crate::Leaf::Ident(ident) => { - if input.eat_ident()?.text != ident.text { - return Err(MacroRulesError::UnexpectedToken); + if input.eat_ident().map(|i| &i.text) != Some(&ident.text) { + return Err(ExpandError::UnexpectedToken); } } - _ => return Err(MacroRulesError::UnexpectedToken), + _ => return Err(ExpandError::UnexpectedToken), }, crate::TokenTree::Repeat(crate::Repeat { subtree, kind: _, separator }) => { while let Ok(nested) = match_lhs(subtree, input) { res.push_nested(nested)?; if let Some(separator) = *separator { if !input.is_eof() { - if input.eat_punct()?.char != separator { - return Err(MacroRulesError::UnexpectedToken); + if input.eat_punct().map(|p| p.char) != Some(separator) { + return Err(ExpandError::UnexpectedToken); } } } @@ -175,12 +176,12 @@ fn expand_subtree( template: &crate::Subtree, bindings: &Bindings, nesting: &mut Vec, -) -> Result { +) -> Result { let token_trees = template .token_trees .iter() .map(|it| expand_tt(it, bindings, nesting)) - .collect::>>()?; + .collect::, ExpandError>>()?; Ok(tt::Subtree { token_trees, delimiter: template.delimiter }) } @@ -189,7 +190,7 @@ fn expand_tt( template: &crate::TokenTree, bindings: &Bindings, nesting: &mut Vec, -) -> Result { +) -> Result { let res: tt::TokenTree = match template { crate::TokenTree::Subtree(subtree) => expand_subtree(subtree, bindings, nesting)?.into(), crate::TokenTree::Repeat(repeat) => { diff --git a/crates/ra_mbe/src/mbe_parser.rs b/crates/ra_mbe/src/mbe_parser.rs index ee1b11091..d067e5248 100644 --- a/crates/ra_mbe/src/mbe_parser.rs +++ b/crates/ra_mbe/src/mbe_parser.rs @@ -1,10 +1,10 @@ /// This module parses a raw `tt::TokenStream` into macro-by-example token /// stream. This is a *mostly* identify function, expect for handling of /// `$var:tt_kind` and `$(repeat),*` constructs. -use crate::{MacroRulesError, Result}; +use crate::ParseError; use crate::tt_cursor::TtCursor; -pub(crate) fn parse(tt: &tt::Subtree) -> Result { +pub(crate) fn parse(tt: &tt::Subtree) -> Result { let mut parser = TtCursor::new(tt); let mut rules = Vec::new(); while !parser.is_eof() { @@ -19,7 +19,7 @@ pub(crate) fn parse(tt: &tt::Subtree) -> Result { Ok(crate::MacroRules { rules }) } -fn parse_rule(p: &mut TtCursor) -> Result { +fn parse_rule(p: &mut TtCursor) -> Result { let lhs = parse_subtree(p.eat_subtree()?)?; p.expect_char('=')?; p.expect_char('>')?; @@ -28,14 +28,14 @@ fn parse_rule(p: &mut TtCursor) -> Result { Ok(crate::Rule { lhs, rhs }) } -fn parse_subtree(tt: &tt::Subtree) -> Result { +fn parse_subtree(tt: &tt::Subtree) -> Result { let mut token_trees = Vec::new(); let mut p = TtCursor::new(tt); - while let Ok(tt) = p.eat() { + while let Some(tt) = p.eat() { let child: crate::TokenTree = match tt { tt::TokenTree::Leaf(leaf) => match leaf { tt::Leaf::Punct(tt::Punct { char: '$', .. }) => { - if p.at_ident().is_ok() { + if p.at_ident().is_some() { crate::Leaf::from(parse_var(&mut p)?).into() } else { parse_repeat(&mut p)?.into() @@ -56,12 +56,12 @@ fn parse_subtree(tt: &tt::Subtree) -> Result { Ok(crate::Subtree { token_trees, delimiter: tt.delimiter }) } -fn parse_var(p: &mut TtCursor) -> Result { +fn parse_var(p: &mut TtCursor) -> Result { let ident = p.eat_ident().unwrap(); let text = ident.text.clone(); let kind = if p.at_char(':') { p.bump(); - if let Ok(ident) = p.eat_ident() { + if let Some(ident) = p.eat_ident() { Some(ident.text.clone()) } else { p.rev_bump(); @@ -73,21 +73,21 @@ fn parse_var(p: &mut TtCursor) -> Result { Ok(crate::Var { text, kind }) } -fn parse_repeat(p: &mut TtCursor) -> Result { +fn parse_repeat(p: &mut TtCursor) -> Result { let subtree = p.eat_subtree().unwrap(); let mut subtree = parse_subtree(subtree)?; subtree.delimiter = crate::Delimiter::None; - let sep = p.eat_punct()?; + let sep = p.eat_punct().ok_or(ParseError::ParseError)?; let (separator, rep) = match sep.char { '*' | '+' | '?' => (None, sep.char), - char => (Some(char), p.eat_punct()?.char), + char => (Some(char), p.eat_punct().ok_or(ParseError::ParseError)?.char), }; let kind = match rep { '*' => crate::RepeatKind::ZeroOrMore, '+' => crate::RepeatKind::OneOrMore, '?' => crate::RepeatKind::ZeroOrOne, - _ => return Err(MacroRulesError::ParseError), + _ => return Err(ParseError::ParseError), }; p.bump(); Ok(crate::Repeat { subtree, kind, separator }) diff --git a/crates/ra_mbe/src/tt_cursor.rs b/crates/ra_mbe/src/tt_cursor.rs index 117471841..d06bfbf9f 100644 --- a/crates/ra_mbe/src/tt_cursor.rs +++ b/crates/ra_mbe/src/tt_cursor.rs @@ -1,4 +1,4 @@ -use crate::{MacroRulesError, Result}; +use crate::ParseError; #[derive(Clone)] pub(crate) struct TtCursor<'a> { @@ -19,24 +19,24 @@ impl<'a> TtCursor<'a> { self.subtree.token_trees.get(self.pos) } - pub(crate) fn at_punct(&self) -> Result<&'a tt::Punct> { + pub(crate) fn at_punct(&self) -> Option<&'a tt::Punct> { match self.current() { - Some(tt::TokenTree::Leaf(tt::Leaf::Punct(it))) => Ok(it), - _ => Err(MacroRulesError::ParseError), + Some(tt::TokenTree::Leaf(tt::Leaf::Punct(it))) => Some(it), + _ => None, } } pub(crate) fn at_char(&self, char: char) -> bool { match self.at_punct() { - Ok(tt::Punct { char: c, .. }) if *c == char => true, + Some(tt::Punct { char: c, .. }) if *c == char => true, _ => false, } } - pub(crate) fn at_ident(&mut self) -> Result<&'a tt::Ident> { + pub(crate) fn at_ident(&mut self) -> Option<&'a tt::Ident> { match self.current() { - Some(tt::TokenTree::Leaf(tt::Leaf::Ident(i))) => Ok(i), - _ => Err(MacroRulesError::ParseError), + Some(tt::TokenTree::Leaf(tt::Leaf::Ident(i))) => Some(i), + _ => None, } } @@ -47,45 +47,43 @@ impl<'a> TtCursor<'a> { self.pos -= 1; } - pub(crate) fn eat(&mut self) -> Result<&'a tt::TokenTree> { - match self.current() { - Some(it) => { - self.bump(); - Ok(it) - } - None => Err(MacroRulesError::ParseError), - } + pub(crate) fn eat(&mut self) -> Option<&'a tt::TokenTree> { + self.current().map(|it| { + self.bump(); + it + }) } - pub(crate) fn eat_subtree(&mut self) -> Result<&'a tt::Subtree> { + pub(crate) fn eat_subtree(&mut self) -> Result<&'a tt::Subtree, ParseError> { match self.current() { Some(tt::TokenTree::Subtree(sub)) => { self.bump(); Ok(sub) } - _ => Err(MacroRulesError::ParseError), + _ => Err(ParseError::ParseError), } } - pub(crate) fn eat_punct(&mut self) -> Result<&'a tt::Punct> { + pub(crate) fn eat_punct(&mut self) -> Option<&'a tt::Punct> { self.at_punct().map(|it| { self.bump(); it }) } - pub(crate) fn eat_ident(&mut self) -> Result<&'a tt::Ident> { + pub(crate) fn eat_ident(&mut self) -> Option<&'a tt::Ident> { self.at_ident().map(|i| { self.bump(); i }) } - pub(crate) fn expect_char(&mut self, char: char) -> Result<()> { + pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ParseError> { if self.at_char(char) { self.bump(); - return Ok(()); + Ok(()) + } else { + Err(ParseError::ParseError) } - Err(MacroRulesError::ParseError) } } -- cgit v1.2.3 From d149bb885bc82c56947f23a597d5d2e08f589655 Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Sun, 3 Mar 2019 12:45:30 +0100 Subject: Add parser unit tests --- crates/ra_mbe/src/lib.rs | 2 +- crates/ra_mbe/src/mbe_parser.rs | 58 ++++++++++++++++++++++++++++++++++++++--- crates/ra_mbe/src/tt_cursor.rs | 4 +-- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 34840dfa1..2c75b7b4f 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -26,7 +26,7 @@ pub use tt::{Delimiter, Punct}; #[derive(Debug, PartialEq, Eq)] pub enum ParseError { - ParseError, + Expected(String), } #[derive(Debug, PartialEq, Eq)] diff --git a/crates/ra_mbe/src/mbe_parser.rs b/crates/ra_mbe/src/mbe_parser.rs index d067e5248..e3669f66c 100644 --- a/crates/ra_mbe/src/mbe_parser.rs +++ b/crates/ra_mbe/src/mbe_parser.rs @@ -77,18 +77,70 @@ fn parse_repeat(p: &mut TtCursor) -> Result { let subtree = p.eat_subtree().unwrap(); let mut subtree = parse_subtree(subtree)?; subtree.delimiter = crate::Delimiter::None; - let sep = p.eat_punct().ok_or(ParseError::ParseError)?; + let sep = p.eat_punct().ok_or(ParseError::Expected(String::from("separator")))?; let (separator, rep) = match sep.char { '*' | '+' | '?' => (None, sep.char), - char => (Some(char), p.eat_punct().ok_or(ParseError::ParseError)?.char), + char => { + (Some(char), p.eat_punct().ok_or(ParseError::Expected(String::from("separator")))?.char) + } }; let kind = match rep { '*' => crate::RepeatKind::ZeroOrMore, '+' => crate::RepeatKind::OneOrMore, '?' => crate::RepeatKind::ZeroOrOne, - _ => return Err(ParseError::ParseError), + _ => return Err(ParseError::Expected(String::from("repeat"))), }; p.bump(); Ok(crate::Repeat { subtree, kind, separator }) } + +#[cfg(test)] +mod tests { + use ra_syntax::{ast, AstNode}; + + use super::*; + use crate::ast_to_token_tree; + + #[test] + fn test_invalid_parse() { + expect_err("invalid", "subtree"); + + is_valid("($i:ident) => ()"); + expect_err("$i:ident => ()", "subtree"); + expect_err("($i:ident) ()", "`=`"); + expect_err("($($i:ident)_) => ()", "separator"); + } + + fn expect_err(macro_body: &str, expected: &str) { + assert_eq!( + create_rules(&format_macro(macro_body)), + Err(ParseError::Expected(String::from(expected))) + ); + } + + fn is_valid(macro_body: &str) { + assert!(create_rules(&format_macro(macro_body)).is_ok()); + } + + fn format_macro(macro_body: &str) -> String { + format!( + " + macro_rules! foo {{ + {} + }} +", + macro_body + ) + } + + fn create_rules(macro_definition: &str) -> Result { + let source_file = ast::SourceFile::parse(macro_definition); + let macro_definition = + source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); + + let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap(); + parse(&definition_tt) + } + +} diff --git a/crates/ra_mbe/src/tt_cursor.rs b/crates/ra_mbe/src/tt_cursor.rs index d06bfbf9f..3128cb9ae 100644 --- a/crates/ra_mbe/src/tt_cursor.rs +++ b/crates/ra_mbe/src/tt_cursor.rs @@ -60,7 +60,7 @@ impl<'a> TtCursor<'a> { self.bump(); Ok(sub) } - _ => Err(ParseError::ParseError), + _ => Err(ParseError::Expected(String::from("subtree"))), } } @@ -83,7 +83,7 @@ impl<'a> TtCursor<'a> { self.bump(); Ok(()) } else { - Err(ParseError::ParseError) + Err(ParseError::Expected(format!("`{}`", char))) } } } -- cgit v1.2.3 From c9d6efc468b2e845aba3237331ea2e02af1b8cc2 Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Sun, 3 Mar 2019 20:33:50 +0100 Subject: Add expander unit tests --- crates/ra_mbe/src/mbe_expander.rs | 73 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index d4ce3bfe3..2dd97b665 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs @@ -85,12 +85,12 @@ impl Bindings { let mut b = self .inner .get(name) - .ok_or(ExpandError::BindingError(format!("could not find binding {}", name)))?; + .ok_or(ExpandError::BindingError(format!("could not find binding `{}`", name)))?; for &idx in nesting.iter() { b = match b { Binding::Simple(_) => break, Binding::Nested(bs) => bs.get(idx).ok_or(ExpandError::BindingError(format!( - "could not find nested binding {}", + "could not find nested binding `{}`", name )))?, }; @@ -98,7 +98,7 @@ impl Bindings { match b { Binding::Simple(it) => Ok(it), Binding::Nested(_) => Err(ExpandError::BindingError(format!( - "expected simple binding, found nested binding {}", + "expected simple binding, found nested binding `{}`", name ))), } @@ -113,7 +113,7 @@ impl Bindings { Some(Binding::Nested(it)) => it.push(value), _ => { return Err(ExpandError::BindingError(format!( - "nested binding for {} not found", + "could not find binding `{}`", key ))); } @@ -216,3 +216,68 @@ fn expand_tt( }; Ok(res) } + +#[cfg(test)] +mod tests { + use ra_syntax::{ast, AstNode}; + + use super::*; + use crate::ast_to_token_tree; + + #[test] + fn test_expand_rule() { + assert_err( + "($i:ident) => ($j)", + "foo!{a}", + ExpandError::BindingError(String::from("could not find binding `j`")), + ); + + assert_err( + "($($i:ident);*) => ($i)", + "foo!{a}", + ExpandError::BindingError(String::from( + "expected simple binding, found nested binding `i`", + )), + ); + + assert_err("($i) => ($i)", "foo!{a}", ExpandError::UnexpectedToken); + assert_err("($i:) => ($i)", "foo!{a}", ExpandError::UnexpectedToken); + } + + fn assert_err(macro_body: &str, invocation: &str, err: ExpandError) { + assert_eq!(expand_first(&create_rules(&format_macro(macro_body)), invocation), Err(err)); + } + + fn format_macro(macro_body: &str) -> String { + format!( + " + macro_rules! foo {{ + {} + }} +", + macro_body + ) + } + + fn create_rules(macro_definition: &str) -> crate::MacroRules { + let source_file = ast::SourceFile::parse(macro_definition); + let macro_definition = + source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); + + let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap(); + crate::MacroRules::parse(&definition_tt).unwrap() + } + + fn expand_first( + rules: &crate::MacroRules, + invocation: &str, + ) -> Result { + let source_file = ast::SourceFile::parse(invocation); + let macro_invocation = + source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); + + let (invocation_tt, _) = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap(); + + expand_rule(&rules.rules[0], &invocation_tt) + } +} -- cgit v1.2.3