Skip to content

Commit 830cbcc

Browse files
committed
---
yaml --- r: 31476 b: refs/heads/dist-snap c: e11e90f h: refs/heads/master v: v3
1 parent 776041d commit 830cbcc

File tree

10 files changed

+232
-196
lines changed

10 files changed

+232
-196
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
99
refs/heads/incoming: d9317a174e434d4c99fc1a37fd7dc0d2f5328d37
10-
refs/heads/dist-snap: eabd233dcd208bc21ca0f8eea02d87d56e5314eb
10+
refs/heads/dist-snap: e11e90f31cedabec1e84b505bbf64103c3421574
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/dist-snap/src/libsyntax/ast.rs

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -362,46 +362,68 @@ type capture_item = @{
362362
#[auto_serialize]
363363
type capture_clause = @~[capture_item];
364364

365+
//
366+
// When the main rust parser encounters a syntax-extension invocation, it
367+
// parses the arguments to the invocation as a token-tree. This is a very
368+
// loose structure, such that all sorts of different AST-fragments can
369+
// be passed to syntax extensions using a uniform type.
370+
//
371+
// If the syntax extension is an MBE macro, it will attempt to match its
372+
// LHS "matchers" against the provided token tree, and if it finds a
373+
// match, will transcribe the RHS token tree, splicing in any captured
374+
// early_parser::matched_nonterminals into the tt_nonterminals it finds.
375+
//
376+
// The RHS of an MBE macro is the only place a tt_nonterminal or tt_seq
377+
// makes any real sense. You could write them elsewhere but nothing
378+
// else knows what to do with them, so you'll probably get a syntax
379+
// error.
380+
//
365381
#[auto_serialize]
366382
#[doc="For macro invocations; parsing is delegated to the macro"]
367383
enum token_tree {
384+
tt_tok(span, token::token),
368385
tt_delim(~[token_tree]),
369-
tt_flat(span, token::token),
370-
/* These only make sense for right-hand-sides of MBE macros*/
371-
tt_dotdotdot(span, ~[token_tree], option<token::token>, bool),
372-
tt_interpolate(span, ident)
386+
// These only make sense for right-hand-sides of MBE macros
387+
tt_seq(span, ~[token_tree], option<token::token>, bool),
388+
tt_nonterminal(span, ident)
373389
}
374390

375-
#[auto_serialize]
376-
type matcher = spanned<matcher_>;
377-
378-
#[auto_serialize]
379391
//
380392
// Matchers are nodes defined-by and recognized-by the main rust parser and
381-
// language, but they're only ever found inside syntax-extension invocations.
382-
// They represent a small sub-language for pattern-matching token-trees, and
383-
// are thus primarily used by the macro-defining extension itself.
393+
// language, but they're only ever found inside syntax-extension invocations;
394+
// indeed, the only thing that ever _activates_ the rules in the rust parser
395+
// for parsing a matcher is a matcher looking for the 'mtcs' nonterminal
396+
// itself. Matchers represent a small sub-language for pattern-matching
397+
// token-trees, and are thus primarily used by the macro-defining extension
398+
// itself.
384399
//
385-
// mtc_tok ===> A matcher that matches a single token,
386-
// denoted by the token itself. So long as
387-
// there's no $ involved.
400+
// match_tok
401+
// ---------
388402
//
403+
// A matcher that matches a single token, denoted by the token itself. So
404+
// long as there's no $ involved.
389405
//
390-
// mtc_rep ===> A matcher that matches a sequence of
391-
// sub-matchers, denoted various ways:
406+
//
407+
// match_seq
408+
// ---------
409+
//
410+
// A matcher that matches a sequence of sub-matchers, denoted various
411+
// possible ways:
392412
//
393413
// $(M)* zero or more Ms
394414
// $(M)+ one or more Ms
395415
// $(M),+ one or more comma-separated Ms
396416
// $(A B C);* zero or more semi-separated 'A B C' seqs
397417
//
398418
//
399-
// mtc_bb ===> A matcher that matches one of a few interesting named rust
400-
// nonterminals, such as types, expressions, items, or raw
401-
// token-trees. A black-box matcher on expr, for example, binds an
402-
// expr to a given ident, and that ident can re-occur as an
403-
// interpolation in the RHS of a macro-by-example rule. For
404-
// example:
419+
// match_nonterminal
420+
// -----------------
421+
//
422+
// A matcher that matches one of a few interesting named rust
423+
// nonterminals, such as types, expressions, items, or raw token-trees. A
424+
// black-box matcher on expr, for example, binds an expr to a given ident,
425+
// and that ident can re-occur as an interpolation in the RHS of a
426+
// macro-by-example rule. For example:
405427
//
406428
// $foo:expr => 1 + $foo // interpolate an expr
407429
// $foo:tt => $foo // interpolate a token-tree
@@ -411,21 +433,25 @@ type matcher = spanned<matcher_>;
411433
//
412434
// As a final, horrifying aside, note that macro-by-example's input is
413435
// also matched by one of these matchers. Holy self-referential! It is matched
414-
// by an mtc_rep, specifically this one:
436+
// by an match_seq, specifically this one:
415437
//
416438
// $( $lhs:mtcs => $rhs:tt );+
417439
//
418440
// If you understand that, you have closed to loop and understand the whole
419441
// macro system. Congratulations.
420442
//
443+
#[auto_serialize]
444+
type matcher = spanned<matcher_>;
445+
446+
#[auto_serialize]
421447
enum matcher_ {
422-
/* match one token */
423-
mtc_tok(token::token),
424-
/* match repetitions of a sequence: body, separator, zero ok?,
425-
lo, hi position-in-match-array used: */
426-
mtc_rep(~[matcher], option<token::token>, bool, uint, uint),
427-
/* parse a Rust NT: name to bind, name of NT, position in match array : */
428-
mtc_bb(ident, ident, uint)
448+
// match one token
449+
match_tok(token::token),
450+
// match repetitions of a sequence: body, separator, zero ok?,
451+
// lo, hi position-in-match-array used:
452+
match_seq(~[matcher], option<token::token>, bool, uint, uint),
453+
// parse a Rust NT: name to bind, name of NT, position in match array:
454+
match_nonterminal(ident, ident, uint)
429455
}
430456

431457
#[auto_serialize]

branches/dist-snap/src/libsyntax/ext/base.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@ import diagnostic::span_handler;
44
import codemap::{codemap, span, expn_info, expanded_from};
55
import std::map::str_hash;
66

7-
8-
// Nomenclature / abbreviations in the ext modules:
9-
//
10-
// ms: matcher span, wraps a matcher with fake span
11-
// mtc: matcher
12-
// mtcs: matchers
13-
// tt: token tree
14-
// bt: backtrace
15-
// cx: expansion context
16-
// mr: macro result
17-
//
18-
197
// obsolete old-style #macro code:
208
//
219
// syntax_expander, normal, macro_defining, macro_definer,
@@ -288,28 +276,29 @@ fn get_mac_body(cx: ext_ctxt, sp: span, args: ast::mac_body)
288276
// using new syntax. This will be obsolete when #old_macros go away.
289277
fn tt_args_to_original_flavor(cx: ext_ctxt, sp: span, arg: ~[ast::token_tree])
290278
-> ast::mac_arg {
291-
import ast::{matcher, matcher_, mtc_tok, mtc_rep, mtc_bb};
279+
import ast::{matcher, matcher_, match_tok, match_seq, match_nonterminal};
292280
import parse::lexer::{new_tt_reader, tt_reader_as_reader, reader};
293-
import tt::earley_parser::{parse_or_else, seq, leaf};
281+
import tt::earley_parser::{parse_or_else, matched_seq,
282+
matched_nonterminal};
294283

295284
// these spans won't matter, anyways
296285
fn ms(m: matcher_) -> matcher {
297286
{node: m, span: {lo: 0u, hi: 0u, expn_info: none}}
298287
}
299288

300-
let argument_gram = ~[ms(mtc_rep(~[
301-
ms(mtc_bb(@~"arg",@~"expr", 0u))
289+
let argument_gram = ~[ms(match_seq(~[
290+
ms(match_nonterminal(@~"arg",@~"expr", 0u))
302291
], some(parse::token::COMMA), true, 0u, 1u))];
303292

304293
let arg_reader = new_tt_reader(cx.parse_sess().span_diagnostic,
305294
cx.parse_sess().interner, none, arg);
306295
let args =
307296
alt parse_or_else(cx.parse_sess(), cx.cfg(), arg_reader as reader,
308297
argument_gram).get(@~"arg") {
309-
@seq(s, _) {
298+
@matched_seq(s, _) {
310299
do s.map() |lf| {
311300
alt lf {
312-
@leaf(parse::token::w_expr(arg)) {
301+
@matched_nonterminal(parse::token::nt_expr(arg)) {
313302
arg /* whew! list of exprs, here we come! */
314303
}
315304
_ { fail ~"badly-structured parse result"; }

branches/dist-snap/src/libsyntax/ext/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import std::map::hashmap;
22

33
import ast::{crate, expr_, expr_mac, mac_invoc, mac_invoc_tt,
4-
tt_delim, tt_flat, item_mac};
4+
tt_delim, tt_tok, item_mac};
55
import fold::*;
66
import ext::base::*;
77
import ext::qquote::{qq_helper};

0 commit comments

Comments
 (0)