From 1a3800df60ca789f858dcf40edff4aee01b53188 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 5 May 2019 00:49:50 +0800 Subject: Fix `$true` and `$false` as $var name error --- crates/ra_mbe/src/mbe_parser.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/mbe_parser.rs b/crates/ra_mbe/src/mbe_parser.rs index 8e1e31e7d..797c70bc7 100644 --- a/crates/ra_mbe/src/mbe_parser.rs +++ b/crates/ra_mbe/src/mbe_parser.rs @@ -28,6 +28,16 @@ fn parse_rule(p: &mut TtCursor) -> Result { Ok(crate::Rule { lhs, rhs }) } +fn is_boolean_literal(lit: Option<&tt::TokenTree>) -> bool { + if let Some(tt::TokenTree::Leaf(tt::Leaf::Literal(lit))) = lit { + if lit.text == "true" || lit.text == "false" { + return true; + } + } + + false +} + fn parse_subtree(tt: &tt::Subtree, transcriber: bool) -> Result { let mut token_trees = Vec::new(); let mut p = TtCursor::new(tt); @@ -35,7 +45,8 @@ fn parse_subtree(tt: &tt::Subtree, transcriber: bool) -> Result match leaf { tt::Leaf::Punct(tt::Punct { char: '$', spacing }) => { - if p.at_ident().is_some() { + // mbe var can be an ident or keyword, including `true` and `false` + if p.at_ident().is_some() || is_boolean_literal(p.current()) { crate::Leaf::from(parse_var(&mut p, transcriber)?).into() } else if let Some(tt::TokenTree::Subtree(_)) = p.current() { parse_repeat(&mut p, transcriber)?.into() @@ -60,8 +71,16 @@ fn parse_subtree(tt: &tt::Subtree, transcriber: bool) -> Result Result { - let ident = p.eat_ident().unwrap(); - let text = ident.text.clone(); + let text = { + if is_boolean_literal(p.current()) { + let lit = p.eat_literal().unwrap(); + lit.text.clone() + } else { + let ident = p.eat_ident().unwrap(); + ident.text.clone() + } + }; + let kind = if !transcriber && p.at_char(':') { p.bump(); if let Some(ident) = p.eat_ident() { @@ -125,6 +144,8 @@ mod tests { is_valid("($i:ident) => ()"); is_valid("($($i:ident)*) => ($_)"); + is_valid("($($true:ident)*) => ($true)"); + is_valid("($($false:ident)*) => ($false)"); expect_err("$i:ident => ()", "subtree"); expect_err("($i:ident) ()", "`=`"); -- cgit v1.2.3