Skip to content

Commit 6f30a4e

Browse files
committed
Remove Matchers
1 parent 38ce6d9 commit 6f30a4e

File tree

7 files changed

+33
-156
lines changed

7 files changed

+33
-156
lines changed

src/libsyntax/ast.rs

Lines changed: 12 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -641,14 +641,12 @@ pub enum KleeneOp {
641641
/// be passed to syntax extensions using a uniform type.
642642
///
643643
/// If the syntax extension is an MBE macro, it will attempt to match its
644-
/// LHS "matchers" against the provided token tree, and if it finds a
644+
/// LHS token tree against the provided token tree, and if it finds a
645645
/// match, will transcribe the RHS token tree, splicing in any captured
646-
/// `macro_parser::matched_nonterminals` into the `TtNonterminal`s it finds.
646+
/// macro_parser::matched_nonterminals into the `SubstNt`s it finds.
647647
///
648-
/// The RHS of an MBE macro is the only place a `TtNonterminal` or `TtSequence`
649-
/// makes any real sense. You could write them elsewhere but nothing
650-
/// else knows what to do with them, so you'll probably get a syntax
651-
/// error.
648+
/// The RHS of an MBE macro is the only place `SubstNt`s are substituted.
649+
/// Nothing special happens to misnamed or misplaced `SubstNt`s.
652650
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
653651
#[doc="For macro invocations; parsing is delegated to the macro"]
654652
pub enum TokenTree {
@@ -657,14 +655,19 @@ pub enum TokenTree {
657655
/// A delimited sequence of token trees
658656
TtDelimited(Span, Rc<Delimited>),
659657

660-
// This only makes sense for right-hand-sides of MBE macros:
658+
// This only makes sense in MBE macros.
661659

662-
/// A Kleene-style repetition sequence with an optional separator.
663-
// FIXME(eddyb) #6308 Use Rc<[TokenTree]> after DST.
660+
/// A kleene-style repetition sequence with a span, a TT forest,
661+
/// an optional separator, and a boolean where true indicates
662+
/// zero or more (..), and false indicates one or more (+).
663+
/// The last member denotes the number of `MATCH_NONTERMINAL`s
664+
/// in the forest.
665+
// FIXME(eddyb) #12938 Use Rc<[TokenTree]> after DST.
664666
TtSequence(Span, Rc<Vec<TokenTree>>, Option<::parse::token::Token>, KleeneOp, uint),
665667
}
666668

667669
impl TokenTree {
670+
/// For unrolling some tokens or token trees into equivalent sequences.
668671
pub fn expand_into_tts(self) -> Rc<Vec<TokenTree>> {
669672
match self {
670673
TtToken(sp, token::DocComment(name)) => {
@@ -710,69 +713,6 @@ impl TokenTree {
710713
}
711714
}
712715

713-
// Matchers are nodes defined-by and recognized-by the main rust parser and
714-
// language, but they're only ever found inside syntax-extension invocations;
715-
// indeed, the only thing that ever _activates_ the rules in the rust parser
716-
// for parsing a matcher is a matcher looking for the 'matchers' nonterminal
717-
// itself. Matchers represent a small sub-language for pattern-matching
718-
// token-trees, and are thus primarily used by the macro-defining extension
719-
// itself.
720-
//
721-
// MatchTok
722-
// --------
723-
//
724-
// A matcher that matches a single token, denoted by the token itself. So
725-
// long as there's no $ involved.
726-
//
727-
//
728-
// MatchSeq
729-
// --------
730-
//
731-
// A matcher that matches a sequence of sub-matchers, denoted various
732-
// possible ways:
733-
//
734-
// $(M)* zero or more Ms
735-
// $(M)+ one or more Ms
736-
// $(M),+ one or more comma-separated Ms
737-
// $(A B C);* zero or more semi-separated 'A B C' seqs
738-
//
739-
//
740-
// MatchNonterminal
741-
// -----------------
742-
//
743-
// A matcher that matches one of a few interesting named rust
744-
// nonterminals, such as types, expressions, items, or raw token-trees. A
745-
// black-box matcher on expr, for example, binds an expr to a given ident,
746-
// and that ident can re-occur as an interpolation in the RHS of a
747-
// macro-by-example rule. For example:
748-
//
749-
// $foo:expr => 1 + $foo // interpolate an expr
750-
// $foo:tt => $foo // interpolate a token-tree
751-
// $foo:tt => bar! $foo // only other valid interpolation
752-
// // is in arg position for another
753-
// // macro
754-
//
755-
// As a final, horrifying aside, note that macro-by-example's input is
756-
// also matched by one of these matchers. Holy self-referential! It is matched
757-
// by a MatchSeq, specifically this one:
758-
//
759-
// $( $lhs:matchers => $rhs:tt );+
760-
//
761-
// If you understand that, you have closed the loop and understand the whole
762-
// macro system. Congratulations.
763-
pub type Matcher = Spanned<Matcher_>;
764-
765-
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
766-
pub enum Matcher_ {
767-
/// Match one token
768-
MatchTok(token::Token),
769-
/// Match repetitions of a sequence: body, separator, Kleene operator,
770-
/// lo, hi position-in-match-array used:
771-
MatchSeq(Vec<Matcher>, Option<token::Token>, KleeneOp, uint, uint),
772-
/// Parse a Rust NT: name to bind, name of NT, position in match array:
773-
MatchNonterminal(Ident, Ident, uint)
774-
}
775-
776716
pub type Mac = Spanned<Mac_>;
777717

778718
/// Represents a macro invocation. The Path indicates which macro

src/libsyntax/ext/tt/macro_parser.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
7979

8080
use ast;
81-
use ast::{Matcher, TokenTree, Ident};
81+
use ast::{TokenTree, Ident};
8282
use ast::{TtDelimited, TtSequence, TtToken};
8383
use codemap::{BytePos, mk_sp};
8484
use codemap;
@@ -97,9 +97,8 @@ use std::rc::Rc;
9797
use std::collections::HashMap;
9898
use std::collections::hash_map::{Vacant, Occupied};
9999

100-
/* to avoid costly uniqueness checks, we require that `MatchSeq` always has a
101-
nonempty body. */
102-
100+
// To avoid costly uniqueness checks, we require that `MatchSeq` always has
101+
// a nonempty body.
103102

104103
/// an unzipping of `TokenTree`s
105104
#[deriving(Clone)]
@@ -157,22 +156,22 @@ pub fn initial_matcher_pos(ms: Rc<Vec<TokenTree>>, sep: Option<Token>, lo: ByteP
157156
}
158157
}
159158

160-
/// NamedMatch is a pattern-match result for a single ast::MatchNonterminal:
159+
/// NamedMatch is a pattern-match result for a single token::MATCH_NONTERMINAL:
161160
/// so it is associated with a single ident in a parse, and all
162-
/// MatchedNonterminal's in the NamedMatch have the same nonterminal type
163-
/// (expr, item, etc). All the leaves in a single NamedMatch correspond to a
164-
/// single matcher_nonterminal in the ast::Matcher that produced it.
161+
/// `MatchedNonterminal`s in the NamedMatch have the same nonterminal type
162+
/// (expr, item, etc). Each leaf in a single NamedMatch corresponds to a
163+
/// single token::MATCH_NONTERMINAL in the TokenTree that produced it.
165164
///
166165
/// The in-memory structure of a particular NamedMatch represents the match
167166
/// that occurred when a particular subset of a matcher was applied to a
168167
/// particular token tree.
169168
///
170169
/// The width of each MatchedSeq in the NamedMatch, and the identity of the
171-
/// MatchedNonterminal's, will depend on the token tree it was applied to: each
172-
/// MatchedSeq corresponds to a single MatchSeq in the originating
173-
/// ast::Matcher. The depth of the NamedMatch structure will therefore depend
174-
/// only on the nesting depth of ast::MatchSeq's in the originating
175-
/// ast::Matcher it was derived from.
170+
/// `MatchedNonterminal`s, will depend on the token tree it was applied to:
171+
/// each MatchedSeq corresponds to a single TTSeq in the originating
172+
/// token tree. The depth of the NamedMatch structure will therefore depend
173+
/// only on the nesting depth of `ast::TTSeq`s in the originating
174+
/// token tree it was derived from.
176175
177176
pub enum NamedMatch {
178177
MatchedSeq(Vec<Rc<NamedMatch>>, codemap::Span),
@@ -512,7 +511,6 @@ pub fn parse_nt(p: &mut Parser, name: &str) -> Nonterminal {
512511
p.quote_depth -= 1u;
513512
res
514513
}
515-
"matchers" => token::NtMatchers(p.parse_matchers()),
516514
_ => {
517515
p.fatal(format!("unsupported builtin nonterminal parser: {}",
518516
name).as_slice())

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ast::{Ident, Matcher_, Matcher, MatchTok, MatchNonterminal, MatchSeq, TtDelimited};
12-
use ast::{TtSequence, TtToken};
11+
use ast::{Ident, TtDelimited, TtSequence, TtToken};
1312
use ast;
1413
use codemap::{Span, DUMMY_SP};
1514
use ext::base::{ExtCtxt, MacResult, MacroDef};
@@ -21,7 +20,7 @@ use parse::lexer::new_tt_reader;
2120
use parse::parser::Parser;
2221
use parse::attr::ParserAttr;
2322
use parse::token::{special_idents, gensym_ident};
24-
use parse::token::{MatchNt, NtMatchers, NtTT};
23+
use parse::token::{MatchNt, NtTT};
2524
use parse::token;
2625
use print;
2726
use ptr::P;
@@ -207,6 +206,11 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
207206
cx.span_fatal(best_fail_spot, best_fail_msg.as_slice());
208207
}
209208

209+
// Note that macro-by-example's input is also matched against a token tree:
210+
// $( $lhs:tt => $rhs:tt );+
211+
//
212+
// Holy self-referential!
213+
210214
/// This procedure performs the expansion of the
211215
/// macro_rules! macro. It parses the RHS and adds
212216
/// an extension to the current context.

src/libsyntax/fold.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,8 +656,6 @@ pub fn noop_fold_interpolated<T: Folder>(nt: token::Nonterminal, fld: &mut T)
656656
token::NtMeta(meta_item) => token::NtMeta(fld.fold_meta_item(meta_item)),
657657
token::NtPath(box path) => token::NtPath(box fld.fold_path(path)),
658658
token::NtTT(tt) => token::NtTT(P(fld.fold_tt(&*tt))),
659-
// it looks to me like we can leave out the matchers: token::NtMatchers(matchers)
660-
_ => nt
661659
}
662660
}
663661

src/libsyntax/parse/parser.rs

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ use ast::{ItemMac, ItemMod, ItemStruct, ItemTrait, ItemTy};
3737
use ast::{LifetimeDef, Lit, Lit_};
3838
use ast::{LitBool, LitChar, LitByte, LitBinary};
3939
use ast::{LitNil, LitStr, LitInt, Local, LocalLet};
40-
use ast::{MutImmutable, MutMutable, Mac_, MacInvocTT, Matcher, MatchNonterminal, MatchNormal};
41-
use ast::{MatchSeq, MatchTok, Method, MutTy, BiMul, Mutability};
40+
use ast::{MutImmutable, MutMutable, Mac_, MacInvocTT, MatchNormal};
41+
use ast::{Method, MutTy, BiMul, Mutability};
4242
use ast::{MethodImplItem, NamedField, UnNeg, NoReturn, UnNot};
4343
use ast::{Pat, PatEnum, PatIdent, PatLit, PatRange, PatRegion, PatStruct};
4444
use ast::{PatTup, PatBox, PatWild, PatWildMulti, PatWildSingle};
@@ -2628,66 +2628,6 @@ impl<'a> Parser<'a> {
26282628
tts
26292629
}
26302630

2631-
pub fn parse_matchers(&mut self) -> Vec<Matcher> {
2632-
// unification of Matcher's and TokenTree's would vastly improve
2633-
// the interpolation of Matcher's
2634-
maybe_whole!(self, NtMatchers);
2635-
let mut name_idx = 0u;
2636-
let delim = self.expect_open_delim();
2637-
self.parse_matcher_subseq_upto(&mut name_idx, &token::CloseDelim(delim))
2638-
}
2639-
2640-
/// This goofy function is necessary to correctly match parens in Matcher's.
2641-
/// Otherwise, `$( ( )` would be a valid Matcher, and `$( () )` would be
2642-
/// invalid. It's similar to common::parse_seq.
2643-
pub fn parse_matcher_subseq_upto(&mut self,
2644-
name_idx: &mut uint,
2645-
ket: &token::Token)
2646-
-> Vec<Matcher> {
2647-
let mut ret_val = Vec::new();
2648-
let mut lparens = 0u;
2649-
2650-
while self.token != *ket || lparens > 0u {
2651-
if self.token == token::OpenDelim(token::Paren) { lparens += 1u; }
2652-
if self.token == token::CloseDelim(token::Paren) { lparens -= 1u; }
2653-
ret_val.push(self.parse_matcher(name_idx));
2654-
}
2655-
2656-
self.bump();
2657-
2658-
return ret_val;
2659-
}
2660-
2661-
pub fn parse_matcher(&mut self, name_idx: &mut uint) -> Matcher {
2662-
let lo = self.span.lo;
2663-
2664-
let m = if self.token == token::Dollar {
2665-
self.bump();
2666-
if self.token == token::OpenDelim(token::Paren) {
2667-
let name_idx_lo = *name_idx;
2668-
self.bump();
2669-
let ms = self.parse_matcher_subseq_upto(name_idx,
2670-
&token::CloseDelim(token::Paren));
2671-
if ms.len() == 0u {
2672-
self.fatal("repetition body must be nonempty");
2673-
}
2674-
let (sep, kleene_op) = self.parse_sep_and_kleene_op();
2675-
MatchSeq(ms, sep, kleene_op, name_idx_lo, *name_idx)
2676-
} else {
2677-
let bound_to = self.parse_ident();
2678-
self.expect(&token::Colon);
2679-
let nt_name = self.parse_ident();
2680-
let m = MatchNonterminal(bound_to, nt_name, *name_idx);
2681-
*name_idx += 1;
2682-
m
2683-
}
2684-
} else {
2685-
MatchTok(self.bump_and_get())
2686-
};
2687-
2688-
return spanned(lo, self.span.hi, m);
2689-
}
2690-
26912631
/// Parse a prefix-operator expr
26922632
pub fn parse_prefix_expr(&mut self) -> P<Expr> {
26932633
let lo = self.span.lo;

src/libsyntax/parse/token.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ pub enum Nonterminal {
337337
NtMeta(P<ast::MetaItem>),
338338
NtPath(Box<ast::Path>),
339339
NtTT(P<ast::TokenTree>), // needs P'ed to break a circularity
340-
NtMatchers(Vec<ast::Matcher>)
341340
}
342341

343342
impl fmt::Show for Nonterminal {
@@ -353,7 +352,6 @@ impl fmt::Show for Nonterminal {
353352
NtMeta(..) => f.pad("NtMeta(..)"),
354353
NtPath(..) => f.pad("NtPath(..)"),
355354
NtTT(..) => f.pad("NtTT(..)"),
356-
NtMatchers(..) => f.pad("NtMatchers(..)"),
357355
}
358356
}
359357
}

src/libsyntax/print/pprust.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ pub fn token_to_string(tok: &Token) -> String {
272272
token::NtPat(..) => "an interpolated pattern".into_string(),
273273
token::NtIdent(..) => "an interpolated identifier".into_string(),
274274
token::NtTT(..) => "an interpolated tt".into_string(),
275-
token::NtMatchers(..) => "an interpolated matcher sequence".into_string(),
276275
}
277276
}
278277
}

0 commit comments

Comments
 (0)