handle multibyte tokens

This commit is contained in:
Aleksey Kladov 2019-01-31 12:03:16 +03:00
parent f3489e8111
commit 0d9210e9bc
2 changed files with 31 additions and 23 deletions

View File

@ -221,27 +221,32 @@ fn convert_tt(tt: &SyntaxNode) -> Option<tt::Subtree> {
if child == first_child || child == last_child || child.kind().is_trivia() { if child == first_child || child == last_child || child.kind().is_trivia() {
continue; continue;
} }
let child: tt::TokenTree = if child.kind() == TOKEN_TREE { if child.kind().is_punct() {
convert_tt(child)?.into() let leaves = child
} else if child.kind().is_keyword() || child.kind() == IDENT { .leaf_text()
let text = child.leaf_text().unwrap().clone(); .unwrap()
tt::Leaf::from(tt::Ident { text }).into() .chars()
} else if child.kind().is_punct() { .map(|char| tt::Punct { char })
// FIXME: multibyte tokens .map(tt::Leaf::from)
tt::Leaf::from(tt::Punct { .map(tt::TokenTree::from);
char: child.text().char_at(0)?, token_trees.extend(leaves);
})
.into()
} else if child.kind().is_literal() {
tt::Leaf::from(tt::Literal {
text: child.leaf_text().unwrap().clone(),
})
.into()
} else { } else {
log::error!("unknown kind: {:?}", child); let child: tt::TokenTree = if child.kind() == TOKEN_TREE {
return None; convert_tt(child)?.into()
}; } else if child.kind().is_keyword() || child.kind() == IDENT {
token_trees.push(child) let text = child.leaf_text().unwrap().clone();
tt::Leaf::from(tt::Ident { text }).into()
} else if child.kind().is_literal() {
tt::Leaf::from(tt::Literal {
text: child.leaf_text().unwrap().clone(),
})
.into()
} else {
log::error!("unknown kind: {:?}", child);
return None;
};
token_trees.push(child)
}
} }
let res = tt::Subtree { let res = tt::Subtree {

View File

@ -86,14 +86,17 @@ pub(crate) fn parse(tt: &tt::Subtree) -> Option<MacroRules> {
fn parse_rule(p: &mut RulesParser) -> Option<Rule> { fn parse_rule(p: &mut RulesParser) -> Option<Rule> {
let lhs = parse_subtree(p.eat_subtree()?)?; let lhs = parse_subtree(p.eat_subtree()?)?;
p.eat_punct('='); p.eat_punct('=')?;
p.eat_punct('>'); p.eat_punct('>')?;
let rhs = parse_subtree(p.eat_subtree()?)?; let rhs = parse_subtree(p.eat_subtree()?)?;
Some(Rule { lhs, rhs }) Some(Rule { lhs, rhs })
} }
fn parse_subtree(tt: &tt::Subtree) -> Option<Subtree> { fn parse_subtree(tt: &tt::Subtree) -> Option<Subtree> {
None Some(Subtree {
token_trees: Vec::new(),
delimiter: Delimiter::None,
})
} }
struct RulesParser<'a> { struct RulesParser<'a> {