From 9a043a163c59dd2625727f7ff5466d586625a423 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 31 Jan 2019 13:32:40 +0300 Subject: parses simple macro --- crates/ra_hir/src/macros/mbe.rs | 148 ++++++++++++++++++++++++++++++++++------ crates/ra_hir/src/macros/tt.rs | 2 +- 2 files changed, 129 insertions(+), 21 deletions(-) (limited to 'crates/ra_hir/src/macros') diff --git a/crates/ra_hir/src/macros/mbe.rs b/crates/ra_hir/src/macros/mbe.rs index 62b7fa24c..6c93ae5e3 100644 --- a/crates/ra_hir/src/macros/mbe.rs +++ b/crates/ra_hir/src/macros/mbe.rs @@ -1,6 +1,6 @@ use ra_syntax::SmolStr; -use crate::macros::tt; +use crate::macros::tt::{self, Delimiter}; #[derive(Debug)] pub(crate) struct MacroRules { @@ -19,6 +19,7 @@ enum TokenTree { Subtree(Subtree), Repeat(Repeat), } +impl_froms!(TokenTree: Leaf, Subtree, Repeat); #[derive(Debug)] enum Leaf { @@ -27,6 +28,7 @@ enum Leaf { Ident(Ident), Var(Var), } +impl_froms!(Leaf: Literal, Punct, Ident, Var); #[derive(Debug)] struct Subtree { @@ -34,18 +36,11 @@ struct Subtree { token_trees: Vec, } -#[derive(Debug)] -enum Delimiter { - Parenthesis, - Brace, - Bracket, - None, -} - #[derive(Debug)] struct Repeat { subtree: Subtree, kind: RepeatKind, + separator: Option, } #[derive(Debug)] @@ -73,6 +68,7 @@ struct Ident { #[derive(Debug)] struct Var { text: SmolStr, + kind: Option, } pub(crate) fn parse(tt: &tt::Subtree) -> Option { @@ -86,16 +82,81 @@ pub(crate) fn parse(tt: &tt::Subtree) -> Option { fn parse_rule(p: &mut RulesParser) -> Option { let lhs = parse_subtree(p.eat_subtree()?)?; - p.eat_punct('=')?; - p.eat_punct('>')?; + p.expect_char('=')?; + p.expect_char('>')?; let rhs = parse_subtree(p.eat_subtree()?)?; Some(Rule { lhs, rhs }) } fn parse_subtree(tt: &tt::Subtree) -> Option { + let mut token_trees = Vec::new(); + let mut p = RulesParser::new(tt); + while let Some(tt) = p.eat() { + let child: TokenTree = match tt { + tt::TokenTree::Leaf(leaf) => match leaf { + tt::Leaf::Punct(tt::Punct { char: '$' }) => { + if p.at_ident().is_some() { + Leaf::from(parse_var(&mut p)?).into() + } else { + parse_repeat(&mut p)?.into() + } + } + tt::Leaf::Punct(tt::Punct { char }) => Leaf::from(Punct { char: *char }).into(), + tt::Leaf::Ident(tt::Ident { text }) => { + Leaf::from(Ident { text: text.clone() }).into() + } + tt::Leaf::Literal(tt::Literal { text }) => { + Leaf::from(Literal { text: text.clone() }).into() + } + }, + tt::TokenTree::Subtree(subtree) => parse_subtree(subtree)?.into(), + }; + token_trees.push(child); + } Some(Subtree { - token_trees: Vec::new(), - delimiter: Delimiter::None, + token_trees, + delimiter: tt.delimiter, + }) +} + +fn parse_var(p: &mut RulesParser) -> Option { + 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() { + Some(ident.text.clone()) + } else { + // ugly as hell :( + p.pos -= 1; + None + } + } else { + None + }; + Some(Var { text, kind }) +} + +fn parse_repeat(p: &mut RulesParser) -> Option { + let subtree = p.eat_subtree().unwrap(); + let subtree = parse_subtree(subtree)?; + let mut sep = p.eat_punct()?; + let (separator, rep) = match sep.char { + '*' | '+' | '?' => (None, sep.char), + char => (Some(Punct { char }), p.eat_punct()?.char), + }; + + let kind = match rep { + '*' => RepeatKind::ZeroOrMore, + '+' => RepeatKind::OneOrMore, + '?' => RepeatKind::ZeroOrMore, + _ => return None, + }; + p.bump(); + Some(Repeat { + subtree, + kind, + separator, }) } @@ -117,9 +178,41 @@ impl<'a> RulesParser<'a> { self.subtree.token_trees.get(self.pos) } + fn at_punct(&self) -> Option<&'a tt::Punct> { + match self.current() { + Some(tt::TokenTree::Leaf(tt::Leaf::Punct(it))) => Some(it), + _ => None, + } + } + + fn at_char(&self, char: char) -> bool { + match self.at_punct() { + Some(tt::Punct { char: c }) if *c == char => true, + _ => false, + } + } + + fn at_ident(&mut self) -> Option<&'a tt::Ident> { + match self.current() { + Some(tt::TokenTree::Leaf(tt::Leaf::Ident(i))) => Some(i), + _ => None, + } + } + fn bump(&mut self) { self.pos += 1; } + + fn eat(&mut self) -> Option<&'a tt::TokenTree> { + match self.current() { + Some(it) => { + self.bump(); + Some(it) + } + None => None, + } + } + fn eat_subtree(&mut self) -> Option<&'a tt::Subtree> { match self.current()? { tt::TokenTree::Subtree(sub) => { @@ -129,13 +222,28 @@ impl<'a> RulesParser<'a> { _ => return None, } } - fn eat_punct(&mut self, char: char) -> Option<()> { - match self.current()? { - tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { char: c })) if *c == char => { - self.bump(); - Some(()) - } - _ => None, + + fn eat_punct(&mut self) -> Option<&'a tt::Punct> { + if let Some(it) = self.at_punct() { + self.bump(); + return Some(it); + } + None + } + + fn eat_ident(&mut self) -> Option<&'a tt::Ident> { + if let Some(i) = self.at_ident() { + self.bump(); + return Some(i); + } + None + } + + fn expect_char(&mut self, char: char) -> Option<()> { + if self.at_char(char) { + self.bump(); + return Some(()); } + None } } diff --git a/crates/ra_hir/src/macros/tt.rs b/crates/ra_hir/src/macros/tt.rs index 02ff422b5..64e88ddc5 100644 --- a/crates/ra_hir/src/macros/tt.rs +++ b/crates/ra_hir/src/macros/tt.rs @@ -21,7 +21,7 @@ pub(crate) struct Subtree { pub(crate) token_trees: Vec, } -#[derive(Debug)] +#[derive(Clone, Copy, Debug)] pub(crate) enum Delimiter { Parenthesis, Brace, -- cgit v1.2.3