From e28a5e048a9da3091897c86e48bc7d4fb2cf601c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 31 Jan 2019 11:25:27 +0300 Subject: [PATCH] parsing scaffold --- crates/ra_hir/src/macros/mbe.rs | 49 ++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/crates/ra_hir/src/macros/mbe.rs b/crates/ra_hir/src/macros/mbe.rs index 7af7066fb8..2e7f9cbf48 100644 --- a/crates/ra_hir/src/macros/mbe.rs +++ b/crates/ra_hir/src/macros/mbe.rs @@ -9,8 +9,8 @@ pub(crate) struct MacroRules { #[derive(Debug)] struct Rule { - lhs: TokenTree, - rhs: TokenTree, + lhs: Subtree, + rhs: Subtree, } #[derive(Debug)] @@ -75,6 +75,47 @@ struct Var { text: SmolStr, } -pub(crate) fn parse(tt: &tt::Subtree) -> MacroRules { - MacroRules { rules: Vec::new() } +pub(crate) fn parse(tt: &tt::Subtree) -> Option { + let mut parser = RulesParser::new(tt); + let mut rules = Vec::new(); + while !parser.is_eof() { + rules.push(parse_rule(&mut parser)?) + } + Some(MacroRules { rules }) +} + +fn parse_rule(p: &mut RulesParser) -> Option { + let lhs = match p.current()? { + tt::TokenTree::Subtree(sub) => parse_subtree(sub)?, + _ => return None, + }; + let rhs = unimplemented!(); + Some(Rule { lhs, rhs }) +} + +fn parse_subtree(tt: &tt::Subtree) -> Option { + unimplemented!() +} + +struct RulesParser<'a> { + subtree: &'a tt::Subtree, + pos: usize, +} + +impl<'a> RulesParser<'a> { + fn new(subtree: &'a tt::Subtree) -> RulesParser<'a> { + RulesParser { subtree, pos: 0 } + } + + fn is_eof(&self) -> bool { + self.pos == self.subtree.token_trees.len() + } + + fn current(&self) -> Option<&'a tt::TokenTree> { + self.subtree.token_trees.get(self.pos) + } + + fn bump(&mut self) { + self.pos += 1; + } }