@@ -642,31 +642,31 @@ applies to the module or crate in which it appears.
642
642
643
643
## Syntax extensions
644
644
645
- There are plans to support user-defined syntax (macros) in Rust. This
646
- currently only exists in very limited form.
647
-
648
645
The compiler defines a few built-in syntax extensions. The most useful
649
- one is ` # fmt` , a printf -style text formatting macro that is expanded
646
+ one is ` fmt! ` , a sprintf -style text formatter that is expanded
650
647
at compile time.
651
648
652
649
~~~~
653
650
io::println(fmt!("%s is %d", ~"the answer", 42));
654
651
~~~~
655
652
656
- ` # fmt` supports most of the directives that [ printf] [ pf ] supports, but
653
+ ` fmt! ` supports most of the directives that [ printf] [ pf ] supports, but
657
654
will give you a compile-time error when the types of the directives
658
655
don't match the types of the arguments.
659
656
660
657
[ pf ] : http://en.cppreference.com/w/cpp/io/c/fprintf
661
658
662
- All syntax extensions look like ` #word ` . Another built-in one is
663
- ` # env` , which will look up its argument as an environment variable at
659
+ All syntax extensions look like ` extension_name! ` . Another built-in one is
660
+ ` env! ` , which will look up its argument as an environment variable at
664
661
compile-time.
665
662
666
663
~~~~
667
664
io::println(env!("PATH"));
668
665
~~~~
669
666
667
+ It is possible for the user to define new syntax extensions, within certain
668
+ limits. These are called [ macros] ( #macros ) .
669
+
670
670
# Control structures
671
671
672
672
## Conditionals
@@ -902,7 +902,7 @@ So except in code that needs to be really, really fast,
902
902
you should feel free to scatter around debug logging statements, and
903
903
leave them in.
904
904
905
- Three macros that combine text-formatting (as with ` # fmt` ) and logging
905
+ Three macros that combine text-formatting (as with ` fmt! ` ) and logging
906
906
are available. These take a string and any number of format arguments,
907
907
and will log the formatted string:
908
908
@@ -912,7 +912,7 @@ warn!("only %d seconds remaining", 10);
912
912
error!("fatal: %s", get_error_string());
913
913
~~~~
914
914
915
- Because the macros ` # debug` , ` # warn` , and ` # error` expand to calls to ` log ` ,
915
+ Because the macros ` debug! ` , ` warn! ` , and ` error! ` expand to calls to ` log ` ,
916
916
their arguments are also lazily evaluated.
917
917
918
918
# Functions
@@ -2334,6 +2334,151 @@ This makes it possible to rebind a variable without actually mutating
2334
2334
it, which is mostly useful for destructuring (which can rebind, but
2335
2335
not assign).
2336
2336
2337
+ # Macros
2338
+
2339
+ Functions are the programmer's primary tool of abstraction, but there are
2340
+ cases in which they are insufficient, because the programmer wants to
2341
+ abstract over concepts not represented as values. Consider the following
2342
+ example:
2343
+
2344
+ ~~~~
2345
+ # enum t { special_a(uint), special_b(uint) };
2346
+ # fn f() -> uint {
2347
+ # let input_1 = special_a(0), input_2 = special_a(0);
2348
+ match input_1 {
2349
+ special_a(x) => { return x; }
2350
+ _ => {}
2351
+ }
2352
+ // ...
2353
+ match input_2 {
2354
+ special_b(x) => { return x; }
2355
+ _ => {}
2356
+ }
2357
+ # return 0u;
2358
+ # }
2359
+ ~~~~
2360
+
2361
+ This code could become tiresome if repeated many times. However, there is
2362
+ no reasonable function that could be written to solve this problem. In such a
2363
+ case, it's possible to define a macro to solve the problem. Macros are
2364
+ lightweight custom syntax extensions, themselves defined using the
2365
+ ` macro_rules! ` syntax extension:
2366
+
2367
+ ~~~~
2368
+ # enum t { special_a(uint), special_b(uint) };
2369
+ # fn f() -> uint {
2370
+ # let input_1 = special_a(0), input_2 = special_a(0);
2371
+ macro_rules! early_return(
2372
+ ($inp:expr $sp:ident) => ( //invoke it like `(input_5 special_e)`
2373
+ match $inp {
2374
+ $sp(x) => { return x; }
2375
+ _ => {}
2376
+ }
2377
+ );
2378
+ );
2379
+ // ...
2380
+ early_return!(input_1 special_a);
2381
+ // ...
2382
+ early_return!(input_2 special_b);
2383
+ # return 0;
2384
+ # }
2385
+ ~~~~
2386
+
2387
+ Macros are defined in pattern-matching style:
2388
+
2389
+ ## Invocation syntax
2390
+
2391
+ On the left-hand-side of the ` => ` is the macro invocation syntax. It is
2392
+ free-form, excepting the following rules:
2393
+
2394
+ 1 . It must be surrounded in parentheses.
2395
+ 2 . ` $ ` has special meaning.
2396
+ 3 . The ` () ` s, ` [] ` s, and ` {} ` s it contains must balance. For example, ` ([) ` is
2397
+ forbidden.
2398
+
2399
+ To take as an argument a fragment of Rust code, write ` $ ` followed by a name
2400
+ (for use on the right-hand side), followed by a ` : ` , followed by the sort of
2401
+ fragment to match (the most common ones are ` ident ` , ` expr ` , ` ty ` , ` pat ` , and
2402
+ ` block ` ). Anything not preceeded by a ` $ ` is taken literally. The standard
2403
+ rules of tokenization apply,
2404
+
2405
+ So ` ($x:ident => (($e:expr))) ` , though excessively fancy, would create a macro
2406
+ that could be invoked like ` my_macro!(i=>(( 2+2 ))) ` .
2407
+
2408
+ ## Transcription syntax
2409
+
2410
+ The right-hand side of the ` => ` follows the same rules as the left-hand side,
2411
+ except that ` $ ` need only be followed by the name of the syntactic fragment
2412
+ to transcribe.
2413
+
2414
+ ## Multiplicity
2415
+
2416
+ ### Invocation
2417
+
2418
+ Going back to the motivating example, suppose that we wanted each invocation
2419
+ of ` early_return ` to potentially accept multiple "special" identifiers. The
2420
+ syntax ` $(...)* ` accepts zero or more occurences of its contents, much like
2421
+ the Kleene star operator in regular expressions. It also supports a separator
2422
+ token (a comma-separated list could be written ` $(...),* ` ), and ` + ` instead of
2423
+ ` * ` to mean "at least one".
2424
+
2425
+ ~~~~
2426
+ # enum t { special_a(uint),special_b(uint),special_c(uint),special_d(uint)};
2427
+ # fn f() -> uint {
2428
+ # let input_1 = special_a(0), input_2 = special_a(0);
2429
+ macro_rules! early_return(
2430
+ ($inp:expr, [ $($sp:ident)|+ ]) => (
2431
+ match $inp {
2432
+ $(
2433
+ $sp(x) => { return x; }
2434
+ )+
2435
+ _ => {}
2436
+ }
2437
+ );
2438
+ );
2439
+ // ...
2440
+ early_return!(input_1, [special_a|special_c|special_d]);
2441
+ // ...
2442
+ early_return!(input_2, [special_b]);
2443
+ # return 0;
2444
+ # }
2445
+ ~~~~
2446
+
2447
+ ### Transcription
2448
+
2449
+ As the above example demonstrates, ` $(...)* ` is also valid on the right-hand
2450
+ side of a macro definition. The behavior of Kleene star in transcription,
2451
+ especially in cases where multiple stars are nested, and multiple different
2452
+ names are involved, can seem somewhat magical and intuitive at first. The
2453
+ system that interprets them is called "Macro By Example". The two rules to
2454
+ keep in mind are (1) the behavior of ` $(...)* ` is to walk through one "layer"
2455
+ of repetitions for all of the ` $name ` s it contains in lockstep, and (2) each
2456
+ ` $name ` must be under at least as many ` $(...)* ` s as it was matched against.
2457
+ If it is under more, it'll will be repeated, as appropriate.
2458
+
2459
+ ## Parsing limitations
2460
+
2461
+ The parser used by the macro system is reasonably powerful, but the parsing of
2462
+ Rust syntax is restricted in two ways:
2463
+
2464
+ 1 . The parser will always parse as much as possible. For example, if the comma
2465
+ were omitted from the syntax of ` early_return! ` above, ` input_1 [ ` would've
2466
+ been interpreted as the beginning of an array index. In fact, invoking the
2467
+ macro would have been impossible.
2468
+ 2 . The parser must have eliminated all ambiguity by the time it reaches a
2469
+ ` $name:fragment_specifier ` . This most often affects them when they occur in
2470
+ the beginning of, or immediately after, a ` $(...)* ` ; requiring a distinctive
2471
+ token in front can solve the problem.
2472
+
2473
+ ## A final note
2474
+
2475
+ Macros, as currently implemented, are not for the faint of heart. Even
2476
+ ordinary syntax errors can be more difficult to debug when they occur inside
2477
+ a macro, and errors caused by parse problems in generated code can be very
2478
+ tricky. Invoking the ` log_syntax! ` macro can help elucidate intermediate
2479
+ states, and using ` --pretty expanded ` as an argument to the compiler will
2480
+ show the result of expansion.
2481
+
2337
2482
# Traits
2338
2483
2339
2484
Traits are Rust's take on value polymorphism—the thing that
0 commit comments