|
| 1 | +use std::fmt::Display; |
| 2 | +use std::str::FromStr; |
| 3 | + |
| 4 | +use rustc_ast::token::{self, Lit}; |
| 5 | +use rustc_ast::tokenstream; |
| 6 | +use rustc_ast_pretty::pprust; |
| 7 | +use rustc_errors::{Applicability, PResult}; |
| 8 | +use rustc_session::parse::ParseSess; |
| 9 | + |
| 10 | +use rustc_span::symbol::Ident; |
| 11 | +use rustc_span::Span; |
| 12 | + |
| 13 | +/// A meta-variable expression, for expansions based on properties of meta-variables. |
| 14 | +#[derive(Debug, Clone, PartialEq, Encodable, Decodable)] |
| 15 | +crate enum MetaVarExpr { |
| 16 | + /// The number of repetitions of an identifier, optionally limited to a number |
| 17 | + /// of outer-most repetition depths. If the depth limit is `None` then the depth is unlimited. |
| 18 | + Count(Ident, Option<usize>), |
| 19 | + |
| 20 | + /// Ignore a meta-variable for repetition without expansion. |
| 21 | + Ignore(Ident), |
| 22 | + |
| 23 | + /// The index of the repetition at a particular depth, where 0 is the inner-most |
| 24 | + /// repetition. The `usize` is the depth. |
| 25 | + Index(usize), |
| 26 | + |
| 27 | + /// The length of the repetition at a particular depth, where 0 is the inner-most |
| 28 | + /// repetition. The `usize` is the depth. |
| 29 | + Length(usize), |
| 30 | +} |
| 31 | + |
| 32 | +impl MetaVarExpr { |
| 33 | + /// Attempt to parse a meta-variable expression from a token stream. |
| 34 | + crate fn parse<'sess>( |
| 35 | + input: &tokenstream::TokenStream, |
| 36 | + sess: &'sess ParseSess, |
| 37 | + ) -> PResult<'sess, MetaVarExpr> { |
| 38 | + let mut tts = input.trees(); |
| 39 | + match tts.next() { |
| 40 | + Some(tokenstream::TokenTree::Token(token)) if let Some((ident, false)) = token.ident() => { |
| 41 | + let Some(tokenstream::TokenTree::Delimited(_, token::Paren, args)) = tts.next() else { |
| 42 | + let msg = "meta-variable expression paramter must be wrapped in parentheses"; |
| 43 | + return Err(sess.span_diagnostic.struct_span_err(ident.span, msg)); |
| 44 | + }; |
| 45 | + let mut iter = args.trees(); |
| 46 | + let rslt = match &*ident.as_str() { |
| 47 | + "count" => parse_count(&mut iter, sess, ident.span)?, |
| 48 | + "ignore" => MetaVarExpr::Ignore(parse_ident(&mut iter, sess, ident.span)?), |
| 49 | + "index" => MetaVarExpr::Index(parse_depth(&mut iter, sess, ident.span)?), |
| 50 | + "length" => MetaVarExpr::Length(parse_depth(&mut iter, sess, ident.span)?), |
| 51 | + _ => { |
| 52 | + let msg = "unrecognised meta-variable expression. Supported expressions \ |
| 53 | + are count, ignore, index and length"; |
| 54 | + return Err(sess.span_diagnostic.struct_span_err(ident.span, msg)); |
| 55 | + } |
| 56 | + }; |
| 57 | + if let Some(arg) = iter.next() { |
| 58 | + let msg = "unexpected meta-variable expression argument"; |
| 59 | + return Err(sess.span_diagnostic.struct_span_err(arg.span(), msg)); |
| 60 | + } |
| 61 | + Ok(rslt) |
| 62 | + } |
| 63 | + Some(tokenstream::TokenTree::Token(token)) => { |
| 64 | + return Err(sess.span_diagnostic.struct_span_err( |
| 65 | + token.span, |
| 66 | + &format!( |
| 67 | + "expected meta-variable expression, found `{}`", |
| 68 | + pprust::token_to_string(&token), |
| 69 | + ), |
| 70 | + )); |
| 71 | + } |
| 72 | + _ => return Err(sess.span_diagnostic.struct_err("expected meta-variable expression")) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + crate fn ident(&self) -> Option<&Ident> { |
| 77 | + match self { |
| 78 | + MetaVarExpr::Count(ident, _) | MetaVarExpr::Ignore(ident) => Some(&ident), |
| 79 | + MetaVarExpr::Index(..) | MetaVarExpr::Length(..) => None, |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/// Tries to convert a literal to an arbitrary type |
| 85 | +fn convert_literal<T>(lit: Lit, sess: &ParseSess, span: Span) -> PResult<'_, T> |
| 86 | +where |
| 87 | + T: FromStr, |
| 88 | + <T as FromStr>::Err: Display, |
| 89 | +{ |
| 90 | + if lit.suffix.is_some() { |
| 91 | + let msg = "literal suffixes are not supported in meta-variable expressions"; |
| 92 | + return Err(sess.span_diagnostic.struct_span_err(span, msg)); |
| 93 | + } |
| 94 | + lit.symbol.as_str().parse::<T>().map_err(|e| { |
| 95 | + sess.span_diagnostic.struct_span_err( |
| 96 | + span, |
| 97 | + &format!("failed to parse meta-variable expression argument: {}", e), |
| 98 | + ) |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +/// Parse a meta-variable `count` expression: `count(ident[, depth])` |
| 103 | +fn parse_count<'sess>( |
| 104 | + iter: &mut tokenstream::Cursor, |
| 105 | + sess: &'sess ParseSess, |
| 106 | + span: Span, |
| 107 | +) -> PResult<'sess, MetaVarExpr> { |
| 108 | + let ident = parse_ident(iter, sess, span)?; |
| 109 | + let depth = if try_eat_comma(iter) { Some(parse_depth(iter, sess, span)?) } else { None }; |
| 110 | + Ok(MetaVarExpr::Count(ident, depth)) |
| 111 | +} |
| 112 | + |
| 113 | +/// Parses the depth used by index(depth) and length(depth). |
| 114 | +fn parse_depth<'sess>( |
| 115 | + iter: &mut tokenstream::Cursor, |
| 116 | + sess: &'sess ParseSess, |
| 117 | + span: Span, |
| 118 | +) -> PResult<'sess, usize> { |
| 119 | + let Some(tt) = iter.next() else { return Ok(0) }; |
| 120 | + let tokenstream::TokenTree::Token(token::Token { |
| 121 | + kind: token::TokenKind::Literal(lit), |
| 122 | + span: literal_span, |
| 123 | + }) = tt else { |
| 124 | + return Err(sess.span_diagnostic.struct_span_err( |
| 125 | + span, |
| 126 | + "meta-expression depth must be a literal" |
| 127 | + )); |
| 128 | + }; |
| 129 | + convert_literal::<usize>(lit, sess, literal_span) |
| 130 | +} |
| 131 | + |
| 132 | +/// Parses an generic ident |
| 133 | +fn parse_ident<'sess>( |
| 134 | + iter: &mut tokenstream::Cursor, |
| 135 | + sess: &'sess ParseSess, |
| 136 | + span: Span, |
| 137 | +) -> PResult<'sess, Ident> { |
| 138 | + let err_fn = |
| 139 | + || sess.span_diagnostic.struct_span_err(span, "could not find an expected `ident` element"); |
| 140 | + if let Some(tt) = iter.next() { |
| 141 | + match tt { |
| 142 | + tokenstream::TokenTree::Token(token) => { |
| 143 | + if let Some((elem, false)) = token.ident() { |
| 144 | + return Ok(elem); |
| 145 | + } |
| 146 | + let mut err = err_fn(); |
| 147 | + err.span_suggestion( |
| 148 | + token.span, |
| 149 | + &format!("Try removing `{}`", pprust::token_to_string(&token)), |
| 150 | + <_>::default(), |
| 151 | + Applicability::MaybeIncorrect, |
| 152 | + ); |
| 153 | + return Err(err); |
| 154 | + } |
| 155 | + tokenstream::TokenTree::Delimited(delim_span, _, _) => { |
| 156 | + let mut err = err_fn(); |
| 157 | + err.span_suggestion( |
| 158 | + delim_span.entire(), |
| 159 | + "Try removing the delimiter", |
| 160 | + <_>::default(), |
| 161 | + Applicability::MaybeIncorrect, |
| 162 | + ); |
| 163 | + return Err(err); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + Err(err_fn()) |
| 168 | +} |
| 169 | + |
| 170 | +/// Tries to move the iterator forward returning `true` if there is a comma. If not, then the |
| 171 | +/// iterator is not modified and the result is `false`. |
| 172 | +fn try_eat_comma(iter: &mut tokenstream::Cursor) -> bool { |
| 173 | + if let Some(tokenstream::TokenTree::Token(token::Token { kind: token::Comma, .. })) = |
| 174 | + iter.look_ahead(0) |
| 175 | + { |
| 176 | + let _ = iter.next(); |
| 177 | + return true; |
| 178 | + } |
| 179 | + false |
| 180 | +} |
0 commit comments