David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1 | use super::*; |
| 2 | |
| 3 | /// Represents a macro invocation. The Path indicates which macro |
| 4 | /// is being invoked, and the vector of token-trees contains the source |
| 5 | /// of the macro invocation. |
| 6 | /// |
David Tolnay | aed77b0 | 2016-09-23 20:50:31 -0700 | [diff] [blame] | 7 | /// NB: the additional ident for a `macro_rules`-style macro is actually |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 8 | /// stored in the enclosing item. Oog. |
| 9 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 10 | pub struct Mac { |
| 11 | pub path: Path, |
| 12 | pub tts: Vec<TokenTree>, |
| 13 | } |
| 14 | |
| 15 | /// When the main rust parser encounters a syntax-extension invocation, it |
| 16 | /// parses the arguments to the invocation as a token-tree. This is a very |
| 17 | /// loose structure, such that all sorts of different AST-fragments can |
| 18 | /// be passed to syntax extensions using a uniform type. |
| 19 | /// |
| 20 | /// If the syntax extension is an MBE macro, it will attempt to match its |
| 21 | /// LHS token tree against the provided token tree, and if it finds a |
| 22 | /// match, will transcribe the RHS token tree, splicing in any captured |
David Tolnay | aed77b0 | 2016-09-23 20:50:31 -0700 | [diff] [blame] | 23 | /// `macro_parser::matched_nonterminals` into the `SubstNt`s it finds. |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 24 | /// |
| 25 | /// The RHS of an MBE macro is the only place `SubstNt`s are substituted. |
| 26 | /// Nothing special happens to misnamed or misplaced `SubstNt`s. |
| 27 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 28 | pub enum TokenTree { |
| 29 | /// A single token |
| 30 | Token(Token), |
| 31 | /// A delimited sequence of token trees |
| 32 | Delimited(Delimited), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 36 | pub struct Delimited { |
| 37 | /// The type of delimiter |
| 38 | pub delim: DelimToken, |
| 39 | /// The delimited sequence of token trees |
| 40 | pub tts: Vec<TokenTree>, |
| 41 | } |
| 42 | |
| 43 | #[derive(Debug, Clone, Eq, PartialEq)] |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 44 | pub enum Token { |
| 45 | /* Expression-operator symbols. */ |
| 46 | Eq, |
| 47 | Lt, |
| 48 | Le, |
| 49 | EqEq, |
| 50 | Ne, |
| 51 | Ge, |
| 52 | Gt, |
| 53 | AndAnd, |
| 54 | OrOr, |
| 55 | Not, |
| 56 | Tilde, |
| 57 | BinOp(BinOpToken), |
| 58 | BinOpEq(BinOpToken), |
| 59 | |
| 60 | /* Structural symbols */ |
| 61 | At, |
| 62 | Dot, |
| 63 | DotDot, |
| 64 | DotDotDot, |
| 65 | Comma, |
| 66 | Semi, |
| 67 | Colon, |
| 68 | ModSep, |
| 69 | RArrow, |
| 70 | LArrow, |
| 71 | FatArrow, |
| 72 | Pound, |
| 73 | Dollar, |
| 74 | Question, |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 75 | |
| 76 | /* Literals */ |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame^] | 77 | Literal(Lit), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 78 | |
| 79 | /* Name components */ |
| 80 | Ident(Ident), |
| 81 | Underscore, |
| 82 | Lifetime(Ident), |
| 83 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 84 | DocComment(String), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 88 | pub enum BinOpToken { |
| 89 | Plus, |
| 90 | Minus, |
| 91 | Star, |
| 92 | Slash, |
| 93 | Percent, |
| 94 | Caret, |
| 95 | And, |
| 96 | Or, |
| 97 | Shl, |
| 98 | Shr, |
| 99 | } |
| 100 | |
| 101 | /// A delimiter token |
| 102 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 103 | pub enum DelimToken { |
| 104 | /// A round parenthesis: `(` or `)` |
| 105 | Paren, |
| 106 | /// A square bracket: `[` or `]` |
| 107 | Bracket, |
| 108 | /// A curly brace: `{` or `}` |
| 109 | Brace, |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame^] | 110 | } |
| 111 | |
| 112 | #[cfg(feature = "parsing")] |
| 113 | pub mod parsing { |
| 114 | use super::*; |
| 115 | use Lifetime; |
| 116 | use generics::parsing::lifetime; |
| 117 | use ident::parsing::ident; |
| 118 | use lit::parsing::lit; |
| 119 | use space::{block_comment, whitespace}; |
| 120 | |
| 121 | named!(pub mac -> Mac, do_parse!( |
| 122 | name: ident >> |
| 123 | punct!("!") >> |
| 124 | body: delimited >> |
| 125 | (Mac { |
| 126 | path: name.into(), |
| 127 | tts: vec![TokenTree::Delimited(body)], |
| 128 | }) |
| 129 | )); |
| 130 | |
| 131 | named!(pub delimited -> Delimited, alt!( |
| 132 | delimited!( |
| 133 | punct!("("), |
| 134 | many0!(token_tree), |
| 135 | punct!(")") |
| 136 | ) => { |tts| Delimited { delim: DelimToken::Paren, tts: tts } } |
| 137 | | |
| 138 | delimited!( |
| 139 | punct!("["), |
| 140 | many0!(token_tree), |
| 141 | punct!("]") |
| 142 | ) => { |tts| Delimited { delim: DelimToken::Bracket, tts: tts } } |
| 143 | | |
| 144 | delimited!( |
| 145 | punct!("{"), |
| 146 | many0!(token_tree), |
| 147 | punct!("}") |
| 148 | ) => { |tts| Delimited { delim: DelimToken::Brace, tts: tts } } |
| 149 | )); |
| 150 | |
| 151 | named!(token_tree -> TokenTree, alt!( |
| 152 | map!(token, TokenTree::Token) |
| 153 | | |
| 154 | map!(delimited, TokenTree::Delimited) |
| 155 | )); |
| 156 | |
| 157 | named!(token -> Token, alt!( |
| 158 | map!(bin_op_eq, Token::BinOpEq) |
| 159 | | |
| 160 | map!(bin_op, Token::BinOp) |
| 161 | | |
| 162 | punct!("=") => { |_| Token::Eq } |
| 163 | | |
| 164 | punct!("<") => { |_| Token::Lt } |
| 165 | | |
| 166 | punct!("<=") => { |_| Token::Le } |
| 167 | | |
| 168 | punct!("==") => { |_| Token::EqEq } |
| 169 | | |
| 170 | punct!("!=") => { |_| Token::Ne } |
| 171 | | |
| 172 | punct!(">=") => { |_| Token::Ge } |
| 173 | | |
| 174 | punct!(">") => { |_| Token::Gt } |
| 175 | | |
| 176 | punct!("&&") => { |_| Token::AndAnd } |
| 177 | | |
| 178 | punct!("||") => { |_| Token::OrOr } |
| 179 | | |
| 180 | punct!("!") => { |_| Token::Not } |
| 181 | | |
| 182 | punct!("~") => { |_| Token::Tilde } |
| 183 | | |
| 184 | punct!("@") => { |_| Token::At } |
| 185 | | |
| 186 | punct!(".") => { |_| Token::Dot } |
| 187 | | |
| 188 | punct!("..") => { |_| Token::DotDot } |
| 189 | | |
| 190 | punct!("...") => { |_| Token::DotDotDot } |
| 191 | | |
| 192 | punct!(",") => { |_| Token::Comma } |
| 193 | | |
| 194 | punct!(";") => { |_| Token::Semi } |
| 195 | | |
| 196 | punct!(":") => { |_| Token::Colon } |
| 197 | | |
| 198 | punct!("::") => { |_| Token::ModSep } |
| 199 | | |
| 200 | punct!("->") => { |_| Token::RArrow } |
| 201 | | |
| 202 | punct!("<-") => { |_| Token::LArrow } |
| 203 | | |
| 204 | punct!("=>") => { |_| Token::FatArrow } |
| 205 | | |
| 206 | punct!("#") => { |_| Token::Pound } |
| 207 | | |
| 208 | punct!("$") => { |_| Token::Dollar } |
| 209 | | |
| 210 | punct!("?") => { |_| Token::Question } |
| 211 | | |
| 212 | punct!("_") => { |_| Token::Underscore } |
| 213 | | |
| 214 | map!(lit, Token::Literal) |
| 215 | | |
| 216 | map!(ident, Token::Ident) |
| 217 | | |
| 218 | map!(lifetime, |lt: Lifetime| Token::Lifetime(lt.ident)) |
| 219 | | |
| 220 | map!(doc_comment, Token::DocComment) |
| 221 | )); |
| 222 | |
| 223 | named!(bin_op -> BinOpToken, alt!( |
| 224 | punct!("+") => { |_| BinOpToken::Plus } |
| 225 | | |
| 226 | punct!("-") => { |_| BinOpToken::Minus } |
| 227 | | |
| 228 | punct!("*") => { |_| BinOpToken::Star } |
| 229 | | |
| 230 | punct!("/") => { |_| BinOpToken::Slash } |
| 231 | | |
| 232 | punct!("%") => { |_| BinOpToken::Percent } |
| 233 | | |
| 234 | punct!("^") => { |_| BinOpToken::Caret } |
| 235 | | |
| 236 | punct!("&") => { |_| BinOpToken::And } |
| 237 | | |
| 238 | punct!("|") => { |_| BinOpToken::Or } |
| 239 | | |
| 240 | punct!("<<") => { |_| BinOpToken::Shl } |
| 241 | | |
| 242 | punct!(">>") => { |_| BinOpToken::Shr } |
| 243 | )); |
| 244 | |
| 245 | named!(bin_op_eq -> BinOpToken, alt!( |
| 246 | punct!("+=") => { |_| BinOpToken::Plus } |
| 247 | | |
| 248 | punct!("-=") => { |_| BinOpToken::Minus } |
| 249 | | |
| 250 | punct!("*=") => { |_| BinOpToken::Star } |
| 251 | | |
| 252 | punct!("/=") => { |_| BinOpToken::Slash } |
| 253 | | |
| 254 | punct!("%=") => { |_| BinOpToken::Percent } |
| 255 | | |
| 256 | punct!("^=") => { |_| BinOpToken::Caret } |
| 257 | | |
| 258 | punct!("&=") => { |_| BinOpToken::And } |
| 259 | | |
| 260 | punct!("|=") => { |_| BinOpToken::Or } |
| 261 | | |
| 262 | punct!("<<=") => { |_| BinOpToken::Shl } |
| 263 | | |
| 264 | punct!(">>=") => { |_| BinOpToken::Shr } |
| 265 | )); |
| 266 | |
| 267 | named!(doc_comment -> String, alt!( |
| 268 | do_parse!( |
| 269 | punct!("//!") >> |
| 270 | content: take_until!("\n") >> |
| 271 | (format!("//!{}", content)) |
| 272 | ) |
| 273 | | |
| 274 | do_parse!( |
| 275 | option!(whitespace) >> |
| 276 | peek!(tag!("/*!")) >> |
| 277 | com: block_comment >> |
| 278 | (com.to_owned()) |
| 279 | ) |
| 280 | | |
| 281 | do_parse!( |
| 282 | punct!("///") >> |
| 283 | not!(peek!(tag!("/"))) >> |
| 284 | content: take_until!("\n") >> |
| 285 | (format!("///{}", content)) |
| 286 | ) |
| 287 | | |
| 288 | do_parse!( |
| 289 | option!(whitespace) >> |
| 290 | peek!(tuple!(tag!("/**"), not!(tag!("*")))) >> |
| 291 | com: block_comment >> |
| 292 | (com.to_owned()) |
| 293 | ) |
| 294 | )); |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 295 | } |