|
| 1 | +//! Convert macro-by-example tokens which are specific to macro expansion into a |
| 2 | +//! format that works for our parser. |
| 3 | +
|
| 4 | +use syntax::{lex_single_syntax_kind, SyntaxKind, SyntaxKind::*, T}; |
| 5 | +use tt::buffer::TokenBuffer; |
| 6 | + |
| 7 | +pub(crate) fn to_parser_tokens(buffer: &TokenBuffer) -> parser::Tokens { |
| 8 | + let mut res = parser::Tokens::default(); |
| 9 | + |
| 10 | + let mut current = buffer.begin(); |
| 11 | + |
| 12 | + while !current.eof() { |
| 13 | + let cursor = current; |
| 14 | + let tt = cursor.token_tree(); |
| 15 | + |
| 16 | + // Check if it is lifetime |
| 17 | + if let Some(tt::buffer::TokenTreeRef::Leaf(tt::Leaf::Punct(punct), _)) = tt { |
| 18 | + if punct.char == '\'' { |
| 19 | + let next = cursor.bump(); |
| 20 | + match next.token_tree() { |
| 21 | + Some(tt::buffer::TokenTreeRef::Leaf(tt::Leaf::Ident(_ident), _)) => { |
| 22 | + res.push(LIFETIME_IDENT); |
| 23 | + current = next.bump(); |
| 24 | + continue; |
| 25 | + } |
| 26 | + _ => panic!("Next token must be ident : {:#?}", next.token_tree()), |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + current = match tt { |
| 32 | + Some(tt::buffer::TokenTreeRef::Leaf(leaf, _)) => { |
| 33 | + match leaf { |
| 34 | + tt::Leaf::Literal(lit) => { |
| 35 | + let is_negated = lit.text.starts_with('-'); |
| 36 | + let inner_text = &lit.text[if is_negated { 1 } else { 0 }..]; |
| 37 | + |
| 38 | + let kind = lex_single_syntax_kind(inner_text) |
| 39 | + .map(|(kind, _error)| kind) |
| 40 | + .filter(|kind| { |
| 41 | + kind.is_literal() |
| 42 | + && (!is_negated || matches!(kind, FLOAT_NUMBER | INT_NUMBER)) |
| 43 | + }) |
| 44 | + .unwrap_or_else(|| panic!("Fail to convert given literal {:#?}", &lit)); |
| 45 | + |
| 46 | + res.push(kind); |
| 47 | + } |
| 48 | + tt::Leaf::Ident(ident) => match ident.text.as_ref() { |
| 49 | + "_" => res.push(T![_]), |
| 50 | + i if i.starts_with('\'') => res.push(LIFETIME_IDENT), |
| 51 | + _ => match SyntaxKind::from_keyword(&ident.text) { |
| 52 | + Some(kind) => res.push(kind), |
| 53 | + None => { |
| 54 | + let contextual_keyword = |
| 55 | + SyntaxKind::from_contextual_keyword(&ident.text) |
| 56 | + .unwrap_or(SyntaxKind::IDENT); |
| 57 | + res.push_ident(contextual_keyword); |
| 58 | + } |
| 59 | + }, |
| 60 | + }, |
| 61 | + tt::Leaf::Punct(punct) => { |
| 62 | + let kind = SyntaxKind::from_char(punct.char) |
| 63 | + .unwrap_or_else(|| panic!("{:#?} is not a valid punct", punct)); |
| 64 | + res.push(kind); |
| 65 | + if punct.spacing == tt::Spacing::Joint { |
| 66 | + res.was_joint(); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + cursor.bump() |
| 71 | + } |
| 72 | + Some(tt::buffer::TokenTreeRef::Subtree(subtree, _)) => { |
| 73 | + if let Some(d) = subtree.delimiter_kind() { |
| 74 | + res.push(match d { |
| 75 | + tt::DelimiterKind::Parenthesis => T!['('], |
| 76 | + tt::DelimiterKind::Brace => T!['{'], |
| 77 | + tt::DelimiterKind::Bracket => T!['['], |
| 78 | + }); |
| 79 | + } |
| 80 | + cursor.subtree().unwrap() |
| 81 | + } |
| 82 | + None => match cursor.end() { |
| 83 | + Some(subtree) => { |
| 84 | + if let Some(d) = subtree.delimiter_kind() { |
| 85 | + res.push(match d { |
| 86 | + tt::DelimiterKind::Parenthesis => T![')'], |
| 87 | + tt::DelimiterKind::Brace => T!['}'], |
| 88 | + tt::DelimiterKind::Bracket => T![']'], |
| 89 | + }) |
| 90 | + } |
| 91 | + cursor.bump() |
| 92 | + } |
| 93 | + None => continue, |
| 94 | + }, |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | + res |
| 99 | +} |
0 commit comments