Replace option with result in mbe

This commit is contained in:
Wilco Kusee 2019-03-02 20:20:26 +01:00
parent 592b906604
commit d3a252b559
4 changed files with 93 additions and 74 deletions

View file

@ -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<T> = ::std::result::Result<T, MacroRulesError>;
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<MacroRules> {
pub fn parse(tt: &tt::Subtree) -> Result<MacroRules> {
mbe_parser::parse(tt)
}
pub fn expand(&self, tt: &tt::Subtree) -> Option<tt::Subtree> {
mbe_expander::exapnd(self, tt)
pub fn expand(&self, tt: &tt::Subtree) -> Result<tt::Subtree> {
mbe_expander::expand(self, tt)
}
}

View file

@ -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<tt::Subtree> {
rules.rules.iter().find_map(|it| expand_rule(it, input))
pub(crate) fn expand(rules: &crate::MacroRules, input: &tt::Subtree) -> Result<tt::Subtree> {
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<tt::Subtree> {
fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Result<tt::Subtree> {
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<Bindings> {
fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings> {
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<Bindings>
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<Bindings>
_ => {}
}
}
Some(res)
Ok(res)
}
fn expand_subtree(
template: &crate::Subtree,
bindings: &Bindings,
nesting: &mut Vec<usize>,
) -> Option<tt::Subtree> {
) -> Result<tt::Subtree> {
let token_trees = template
.token_trees
.iter()
.map(|it| expand_tt(it, bindings, nesting))
.collect::<Option<Vec<_>>>()?;
.collect::<Result<Vec<_>>>()?;
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<usize>,
) -> Option<tt::TokenTree> {
) -> Result<tt::TokenTree> {
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)
}

View file

@ -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<crate::MacroRules> {
pub(crate) fn parse(tt: &tt::Subtree) -> Result<crate::MacroRules> {
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<crate::Rule> {
fn parse_rule(p: &mut TtCursor) -> Result<crate::Rule> {
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<crate::Subtree> {
fn parse_subtree(tt: &tt::Subtree) -> Result<crate::Subtree> {
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<crate::Subtree> {
};
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<crate::Var> {
fn parse_var(p: &mut TtCursor) -> Result<crate::Var> {
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<crate::Var> {
} else {
None
};
Some(crate::Var { text, kind })
Ok(crate::Var { text, kind })
}
fn parse_repeat(p: &mut TtCursor) -> Option<crate::Repeat> {
fn parse_repeat(p: &mut TtCursor) -> Result<crate::Repeat> {
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::Repeat> {
'*' => 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 })
}

View file

@ -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)
}
}