Skip to content

Commit 14e88a8

Browse files
committed
---
yaml --- r: 20743 b: refs/heads/snap-stage3 c: a28812c h: refs/heads/master i: 20741: c2b92f1 20739: b7a41fe 20735: 2213fe5 v: v3
1 parent aa33b35 commit 14e88a8

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: e430a699f2c60890d9b86069fd0c68a70ece7120
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 19922fcd93bfeac123719e319b7166f50660f847
4+
refs/heads/snap-stage3: a28812cfd66115c013b306c4c809dc3dc8c8b6ac
55
refs/heads/try: ffbe0e0e00374358b789b0037bcb3a577cd218be
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,69 @@ import ast::{ident, matcher_, matcher, match_tok,
55
import parse::lexer::{new_tt_reader, tt_reader_as_reader, reader};
66
import parse::token::{FAT_ARROW, SEMI, LBRACE, RBRACE, nt_matchers, nt_tt};
77
import parse::parser::{parser, SOURCE_FILE};
8-
import earley_parser::{parse, success, failure, named_match,
8+
import earley_parser::{parse, parse_or_else, success, failure, named_match,
99
matched_seq, matched_nonterminal};
1010
import std::map::hashmap;
1111

12-
13-
1412
fn add_new_extension(cx: ext_ctxt, sp: span, name: ident,
1513
arg: ~[ast::token_tree]) -> base::mac_result {
1614
// these spans won't matter, anyways
1715
fn ms(m: matcher_) -> matcher {
1816
{node: m, span: {lo: 0u, hi: 0u, expn_info: none}}
1917
}
2018

19+
// The grammar for macro_rules! is:
20+
// $( $lhs:mtcs => $rhs:tt );+
21+
// ...quasiquoting this would be nice.
2122
let argument_gram = ~[
2223
ms(match_seq(~[
2324
ms(match_nonterminal(@~"lhs",@~"matchers", 0u)),
2425
ms(match_tok(FAT_ARROW)),
2526
ms(match_nonterminal(@~"rhs",@~"tt", 1u)),
2627
], some(SEMI), false, 0u, 2u))];
2728

29+
30+
// Parse the macro_rules! invocation (`none` is for no interpolations):
2831
let arg_reader = new_tt_reader(cx.parse_sess().span_diagnostic,
2932
cx.parse_sess().interner, none, arg);
30-
let arguments = alt parse(cx.parse_sess(), cx.cfg(),
31-
arg_reader as reader, argument_gram) {
32-
success(m) { m }
33-
failure(sp, msg) { cx.span_fatal(sp, msg); }
34-
};
33+
let argument_map = parse_or_else(cx.parse_sess(), cx.cfg(),
34+
arg_reader as reader, argument_gram);
3535

36-
let lhses = alt arguments.get(@~"lhs") {
36+
// Extract the arguments:
37+
let lhses:~[@named_match] = alt argument_map.get(@~"lhs") {
3738
@matched_seq(s, sp) { s }
3839
_ { cx.span_bug(sp, ~"wrong-structured lhs") }
3940
};
40-
let rhses = alt arguments.get(@~"rhs") {
41+
let rhses:~[@named_match] = alt argument_map.get(@~"rhs") {
4142
@matched_seq(s, sp) { s }
4243
_ { cx.span_bug(sp, ~"wrong-structured rhs") }
4344
};
4445

46+
// Given `lhses` and `rhses`, this is the new macro we create
4547
fn generic_extension(cx: ext_ctxt, sp: span, arg: ~[ast::token_tree],
4648
lhses: ~[@named_match], rhses: ~[@named_match])
4749
-> mac_result {
50+
// Which arm's failure should we report? (the one furthest along)
4851
let mut best_fail_spot = {lo: 0u, hi: 0u, expn_info: none};
4952
let mut best_fail_msg = ~"internal error: ran no matchers";
5053

5154
let s_d = cx.parse_sess().span_diagnostic;
5255
let itr = cx.parse_sess().interner;
5356

54-
for lhses.eachi() |i, lhs| {
57+
for lhses.eachi() |i, lhs| { // try each arm's matchers
5558
alt lhs {
5659
@matched_nonterminal(nt_matchers(mtcs)) {
60+
// `none` is because we're not interpolating
5761
let arg_rdr = new_tt_reader(s_d, itr, none, arg) as reader;
5862
alt parse(cx.parse_sess(), cx.cfg(), arg_rdr, mtcs) {
59-
success(m) {
60-
let rhs = alt rhses[i] {
63+
success(named_matches) {
64+
let rhs = alt rhses[i] { // okay, what's your transcriber?
6165
@matched_nonterminal(nt_tt(@tt)) { tt }
6266
_ { cx.span_bug(sp, ~"bad thing in rhs") }
6367
};
64-
let trncbr = new_tt_reader(s_d, itr, some(m), ~[rhs]);
68+
// rhs has holes ( `$id` and `$(...)` that need filled)
69+
let trncbr = new_tt_reader(s_d, itr, some(named_matches),
70+
~[rhs]);
6571
let p = parser(cx.parse_sess(), cx.cfg(),
6672
trncbr as reader, SOURCE_FILE);
6773
ret mr_expr(p.parse_expr());

0 commit comments

Comments
 (0)