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), |
| 33 | |
| 34 | // This only makes sense in MBE macros. |
| 35 | /// A kleene-style repetition sequence with a span |
| 36 | Sequence(SequenceRepetition), |
| 37 | } |
| 38 | |
| 39 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 40 | pub struct Delimited { |
| 41 | /// The type of delimiter |
| 42 | pub delim: DelimToken, |
| 43 | /// The delimited sequence of token trees |
| 44 | pub tts: Vec<TokenTree>, |
| 45 | } |
| 46 | |
| 47 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 48 | pub struct SequenceRepetition { |
| 49 | /// The sequence of token trees |
| 50 | pub tts: Vec<TokenTree>, |
| 51 | /// The optional separator |
| 52 | pub separator: Option<Token>, |
| 53 | /// Whether the sequence can be repeated zero (*), or one or more times (+) |
| 54 | pub op: KleeneOp, |
| 55 | /// The number of `MatchNt`s that appear in the sequence (and subsequences) |
| 56 | pub num_captures: usize, |
| 57 | } |
| 58 | |
| 59 | /// A Kleene-style [repetition operator](http://en.wikipedia.org/wiki/Kleene_star) |
| 60 | /// for token sequences. |
| 61 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 62 | pub enum KleeneOp { |
| 63 | ZeroOrMore, |
| 64 | OneOrMore, |
| 65 | } |
| 66 | |
| 67 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 68 | pub enum Token { |
| 69 | /* Expression-operator symbols. */ |
| 70 | Eq, |
| 71 | Lt, |
| 72 | Le, |
| 73 | EqEq, |
| 74 | Ne, |
| 75 | Ge, |
| 76 | Gt, |
| 77 | AndAnd, |
| 78 | OrOr, |
| 79 | Not, |
| 80 | Tilde, |
| 81 | BinOp(BinOpToken), |
| 82 | BinOpEq(BinOpToken), |
| 83 | |
| 84 | /* Structural symbols */ |
| 85 | At, |
| 86 | Dot, |
| 87 | DotDot, |
| 88 | DotDotDot, |
| 89 | Comma, |
| 90 | Semi, |
| 91 | Colon, |
| 92 | ModSep, |
| 93 | RArrow, |
| 94 | LArrow, |
| 95 | FatArrow, |
| 96 | Pound, |
| 97 | Dollar, |
| 98 | Question, |
| 99 | /// An opening delimiter, eg. `{` |
| 100 | OpenDelim(DelimToken), |
| 101 | /// A closing delimiter, eg. `}` |
| 102 | CloseDelim(DelimToken), |
| 103 | |
| 104 | /* Literals */ |
| 105 | Literal(Lit, Option<String>), |
| 106 | |
| 107 | /* Name components */ |
| 108 | Ident(Ident), |
| 109 | Underscore, |
| 110 | Lifetime(Ident), |
| 111 | |
| 112 | // Can be expanded into several tokens. |
| 113 | /// Doc comment |
| 114 | DocComment(String), |
| 115 | // In left-hand-sides of MBE macros: |
| 116 | /// Parse a nonterminal (name to bind, name of NT) |
| 117 | MatchNt(Ident, Ident), |
| 118 | // In right-hand-sides of MBE macros: |
| 119 | /// A syntactic variable that will be filled in by macro expansion. |
| 120 | SubstNt(Ident), |
| 121 | } |
| 122 | |
| 123 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 124 | pub enum BinOpToken { |
| 125 | Plus, |
| 126 | Minus, |
| 127 | Star, |
| 128 | Slash, |
| 129 | Percent, |
| 130 | Caret, |
| 131 | And, |
| 132 | Or, |
| 133 | Shl, |
| 134 | Shr, |
| 135 | } |
| 136 | |
| 137 | /// A delimiter token |
| 138 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 139 | pub enum DelimToken { |
| 140 | /// A round parenthesis: `(` or `)` |
| 141 | Paren, |
| 142 | /// A square bracket: `[` or `]` |
| 143 | Bracket, |
| 144 | /// A curly brace: `{` or `}` |
| 145 | Brace, |
| 146 | /// An empty delimiter |
| 147 | NoDelim, |
| 148 | } |