Skip to content

Commit a28812c

Browse files
committed
Improve some documentation.
1 parent 19922fc commit a28812c

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

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)