2019-01-30 20:17:32 +00:00
|
|
|
use ra_syntax::SmolStr;
|
|
|
|
|
2019-01-30 20:25:02 +00:00
|
|
|
use crate::macros::tt;
|
|
|
|
|
2019-01-31 08:09:19 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct MacroRules {
|
2019-01-30 20:17:32 +00:00
|
|
|
rules: Vec<Rule>,
|
|
|
|
}
|
|
|
|
|
2019-01-31 08:09:19 +00:00
|
|
|
#[derive(Debug)]
|
2019-01-30 20:17:32 +00:00
|
|
|
struct Rule {
|
2019-01-31 08:25:27 +00:00
|
|
|
lhs: Subtree,
|
|
|
|
rhs: Subtree,
|
2019-01-30 20:17:32 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 08:09:19 +00:00
|
|
|
#[derive(Debug)]
|
2019-01-30 20:17:32 +00:00
|
|
|
enum TokenTree {
|
|
|
|
Leaf(Leaf),
|
|
|
|
Subtree(Subtree),
|
2019-01-31 07:33:11 +00:00
|
|
|
Repeat(Repeat),
|
2019-01-30 20:17:32 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 08:09:19 +00:00
|
|
|
#[derive(Debug)]
|
2019-01-30 20:17:32 +00:00
|
|
|
enum Leaf {
|
|
|
|
Literal(Literal),
|
|
|
|
Punct(Punct),
|
|
|
|
Ident(Ident),
|
|
|
|
Var(Var),
|
|
|
|
}
|
|
|
|
|
2019-01-31 08:09:19 +00:00
|
|
|
#[derive(Debug)]
|
2019-01-30 20:17:32 +00:00
|
|
|
struct Subtree {
|
|
|
|
delimiter: Delimiter,
|
|
|
|
token_trees: Vec<TokenTree>,
|
|
|
|
}
|
|
|
|
|
2019-01-31 08:09:19 +00:00
|
|
|
#[derive(Debug)]
|
2019-01-30 20:17:32 +00:00
|
|
|
enum Delimiter {
|
|
|
|
Parenthesis,
|
|
|
|
Brace,
|
|
|
|
Bracket,
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
2019-01-31 08:09:19 +00:00
|
|
|
#[derive(Debug)]
|
2019-01-31 07:33:11 +00:00
|
|
|
struct Repeat {
|
|
|
|
subtree: Subtree,
|
|
|
|
kind: RepeatKind,
|
|
|
|
}
|
|
|
|
|
2019-01-31 08:09:19 +00:00
|
|
|
#[derive(Debug)]
|
2019-01-31 07:33:11 +00:00
|
|
|
enum RepeatKind {
|
|
|
|
ZeroOrMore,
|
|
|
|
OneOrMore,
|
|
|
|
ZeroOrOne,
|
|
|
|
}
|
|
|
|
|
2019-01-31 08:09:19 +00:00
|
|
|
#[derive(Debug)]
|
2019-01-30 20:17:32 +00:00
|
|
|
struct Literal {
|
|
|
|
text: SmolStr,
|
|
|
|
}
|
|
|
|
|
2019-01-31 08:09:19 +00:00
|
|
|
#[derive(Debug)]
|
2019-01-30 20:17:32 +00:00
|
|
|
struct Punct {
|
|
|
|
char: char,
|
|
|
|
}
|
|
|
|
|
2019-01-31 08:09:19 +00:00
|
|
|
#[derive(Debug)]
|
2019-01-30 20:17:32 +00:00
|
|
|
struct Ident {
|
|
|
|
text: SmolStr,
|
|
|
|
}
|
|
|
|
|
2019-01-31 08:09:19 +00:00
|
|
|
#[derive(Debug)]
|
2019-01-30 20:17:32 +00:00
|
|
|
struct Var {
|
|
|
|
text: SmolStr,
|
|
|
|
}
|
2019-01-30 20:25:02 +00:00
|
|
|
|
2019-01-31 08:25:27 +00:00
|
|
|
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> {
|
2019-01-31 08:30:21 +00:00
|
|
|
let lhs = parse_subtree(p.eat_subtree()?)?;
|
2019-01-31 09:03:16 +00:00
|
|
|
p.eat_punct('=')?;
|
|
|
|
p.eat_punct('>')?;
|
2019-01-31 08:30:21 +00:00
|
|
|
let rhs = parse_subtree(p.eat_subtree()?)?;
|
2019-01-31 08:25:27 +00:00
|
|
|
Some(Rule { lhs, rhs })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_subtree(tt: &tt::Subtree) -> Option<Subtree> {
|
2019-01-31 09:03:16 +00:00
|
|
|
Some(Subtree {
|
|
|
|
token_trees: Vec::new(),
|
|
|
|
delimiter: Delimiter::None,
|
|
|
|
})
|
2019-01-31 08:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-01-31 08:30:21 +00:00
|
|
|
fn eat_subtree(&mut self) -> Option<&'a tt::Subtree> {
|
|
|
|
match self.current()? {
|
|
|
|
tt::TokenTree::Subtree(sub) => {
|
|
|
|
self.bump();
|
|
|
|
Some(sub)
|
|
|
|
}
|
|
|
|
_ => 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,
|
|
|
|
}
|
|
|
|
}
|
2019-01-30 20:25:02 +00:00
|
|
|
}
|