parsing scaffold

This commit is contained in:
Aleksey Kladov 2019-01-31 11:25:27 +03:00
parent 2980508ad2
commit e28a5e048a

View file

@ -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<MacroRules> {
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<Rule> {
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<Subtree> {
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;
}
}