aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe')
-rw-r--r--crates/ra_mbe/src/mbe_parser.rs21
1 files changed, 11 insertions, 10 deletions
diff --git a/crates/ra_mbe/src/mbe_parser.rs b/crates/ra_mbe/src/mbe_parser.rs
index f37c422d3..0710062d9 100644
--- a/crates/ra_mbe/src/mbe_parser.rs
+++ b/crates/ra_mbe/src/mbe_parser.rs
@@ -20,15 +20,15 @@ pub(crate) fn parse(tt: &tt::Subtree) -> Result<crate::MacroRules, ParseError> {
20} 20}
21 21
22fn parse_rule(p: &mut TtCursor) -> Result<crate::Rule, ParseError> { 22fn parse_rule(p: &mut TtCursor) -> Result<crate::Rule, ParseError> {
23 let lhs = parse_subtree(p.eat_subtree()?)?; 23 let lhs = parse_subtree(p.eat_subtree()?, false)?;
24 p.expect_char('=')?; 24 p.expect_char('=')?;
25 p.expect_char('>')?; 25 p.expect_char('>')?;
26 let mut rhs = parse_subtree(p.eat_subtree()?)?; 26 let mut rhs = parse_subtree(p.eat_subtree()?, true)?;
27 rhs.delimiter = crate::Delimiter::None; 27 rhs.delimiter = crate::Delimiter::None;
28 Ok(crate::Rule { lhs, rhs }) 28 Ok(crate::Rule { lhs, rhs })
29} 29}
30 30
31fn parse_subtree(tt: &tt::Subtree) -> Result<crate::Subtree, ParseError> { 31fn parse_subtree(tt: &tt::Subtree, transcriber: bool) -> Result<crate::Subtree, ParseError> {
32 let mut token_trees = Vec::new(); 32 let mut token_trees = Vec::new();
33 let mut p = TtCursor::new(tt); 33 let mut p = TtCursor::new(tt);
34 while let Some(tt) = p.eat() { 34 while let Some(tt) = p.eat() {
@@ -36,9 +36,9 @@ fn parse_subtree(tt: &tt::Subtree) -> Result<crate::Subtree, ParseError> {
36 tt::TokenTree::Leaf(leaf) => match leaf { 36 tt::TokenTree::Leaf(leaf) => match leaf {
37 tt::Leaf::Punct(tt::Punct { char: '$', .. }) => { 37 tt::Leaf::Punct(tt::Punct { char: '$', .. }) => {
38 if p.at_ident().is_some() { 38 if p.at_ident().is_some() {
39 crate::Leaf::from(parse_var(&mut p)?).into() 39 crate::Leaf::from(parse_var(&mut p, transcriber)?).into()
40 } else { 40 } else {
41 parse_repeat(&mut p)?.into() 41 parse_repeat(&mut p, transcriber)?.into()
42 } 42 }
43 } 43 }
44 tt::Leaf::Punct(punct) => crate::Leaf::from(*punct).into(), 44 tt::Leaf::Punct(punct) => crate::Leaf::from(*punct).into(),
@@ -49,17 +49,17 @@ fn parse_subtree(tt: &tt::Subtree) -> Result<crate::Subtree, ParseError> {
49 crate::Leaf::from(crate::Literal { text: text.clone() }).into() 49 crate::Leaf::from(crate::Literal { text: text.clone() }).into()
50 } 50 }
51 }, 51 },
52 tt::TokenTree::Subtree(subtree) => parse_subtree(&subtree)?.into(), 52 tt::TokenTree::Subtree(subtree) => parse_subtree(&subtree, transcriber)?.into(),
53 }; 53 };
54 token_trees.push(child); 54 token_trees.push(child);
55 } 55 }
56 Ok(crate::Subtree { token_trees, delimiter: tt.delimiter }) 56 Ok(crate::Subtree { token_trees, delimiter: tt.delimiter })
57} 57}
58 58
59fn parse_var(p: &mut TtCursor) -> Result<crate::Var, ParseError> { 59fn parse_var(p: &mut TtCursor, transcriber: bool) -> Result<crate::Var, ParseError> {
60 let ident = p.eat_ident().unwrap(); 60 let ident = p.eat_ident().unwrap();
61 let text = ident.text.clone(); 61 let text = ident.text.clone();
62 let kind = if p.at_char(':') { 62 let kind = if !transcriber && p.at_char(':') {
63 p.bump(); 63 p.bump();
64 if let Some(ident) = p.eat_ident() { 64 if let Some(ident) = p.eat_ident() {
65 Some(ident.text.clone()) 65 Some(ident.text.clone())
@@ -70,12 +70,13 @@ fn parse_var(p: &mut TtCursor) -> Result<crate::Var, ParseError> {
70 } else { 70 } else {
71 None 71 None
72 }; 72 };
73
73 Ok(crate::Var { text, kind }) 74 Ok(crate::Var { text, kind })
74} 75}
75 76
76fn parse_repeat(p: &mut TtCursor) -> Result<crate::Repeat, ParseError> { 77fn parse_repeat(p: &mut TtCursor, transcriber: bool) -> Result<crate::Repeat, ParseError> {
77 let subtree = p.eat_subtree().unwrap(); 78 let subtree = p.eat_subtree().unwrap();
78 let mut subtree = parse_subtree(subtree)?; 79 let mut subtree = parse_subtree(subtree, transcriber)?;
79 subtree.delimiter = crate::Delimiter::None; 80 subtree.delimiter = crate::Delimiter::None;
80 let sep = p.eat_punct().ok_or(ParseError::Expected(String::from("separator")))?; 81 let sep = p.eat_punct().ok_or(ParseError::Expected(String::from("separator")))?;
81 let (separator, rep) = match sep.char { 82 let (separator, rep) = match sep.char {