Make vis matcher optional and fix typo

This commit is contained in:
Edwin Cheng 2019-05-02 21:24:51 +08:00
parent b0e7022afe
commit 35c4633150
3 changed files with 28 additions and 5 deletions

View File

@ -206,8 +206,24 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings,
);
}
"vis" => {
let vis = input.eat_vis().ok_or(ExpandError::UnexpectedToken)?.clone();
res.inner.insert(text.clone(), Binding::Simple(vis.into()));
// `vis` is optional
if let Some(vis) = input.try_eat_vis() {
let vis = vis.clone();
res.inner.insert(text.clone(), Binding::Simple(vis.into()));
} else {
// FIXME: Do we have a better way to represent an empty token ?
// Insert an empty subtree for empty token
res.inner.insert(
text.clone(),
Binding::Simple(
tt::Subtree {
delimiter: tt::Delimiter::None,
token_trees: vec![],
}
.into(),
),
);
}
}
_ => return Err(ExpandError::UnexpectedToken),

View File

@ -319,7 +319,7 @@ fn convert_ident(ident: &tt::Ident) -> TtToken {
fn convert_punct(p: &tt::Punct) -> TtToken {
let kind = match p.char {
// lexer may produce combpund tokens for these ones
// lexer may produce compound tokens for these ones
'.' => DOT,
':' => COLON,
'=' => EQ,

View File

@ -149,9 +149,16 @@ impl<'a> TtCursor<'a> {
self.eat_ident().cloned().map(|ident| tt::Leaf::from(ident).into())
}
pub(crate) fn eat_vis(&mut self) -> Option<tt::TokenTree> {
pub(crate) fn try_eat_vis(&mut self) -> Option<tt::TokenTree> {
// `vis` matcher is optional
let old_pos = self.pos;
let parser = Parser::new(&mut self.pos, self.subtree);
parser.parse_vis()
let res = parser.parse_vis();
if res.is_none() {
self.pos = old_pos;
}
res
}
pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ParseError> {