Skip to content

Commit 0de224a

Browse files
authored
Merge pull request #220 from KeenS/1.9-macros
4.34 Macros (1.9)
2 parents fcdfd0b + 3e4f914 commit 0de224a

File tree

2 files changed

+25
-99
lines changed

2 files changed

+25
-99
lines changed

1.9/ja/book/macros.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,12 @@ LOG(state)
378378

379379
```text
380380
const char *state = "reticulating splines";
381-
int state = get_log_state();
382-
if (state > 0) {
383-
printf("log(%d): %s\n", state, state);
384-
}
381+
{
382+
int state = get_log_state();
383+
if (state > 0) {
384+
printf("log(%d): %s\n", state, state);
385+
}
386+
}
385387
```
386388

387389
<!-- The second variable named `state` shadows the first one. This is a problem -->
@@ -437,10 +439,9 @@ fn main() {
437439
}
438440
```
439441

440-
<!-- Instead you need to pass the variable name into the invocation, so it’s tagged -->
441-
<!-- with the right syntax context. -->
442-
代わりに変数名を呼出時に渡す必要があります、
443-
呼出時に渡す事で正しい構文コンテキストでタグ付けされます。
442+
<!-- Instead you need to pass the variable name into the invocation, so that it’s -->
443+
<!-- tagged with the right syntax context. -->
444+
呼出時に渡す事で正しい構文コンテキストでタグ付けされるように、代わりに変数名を呼出時に渡す必要があります。
444445

445446
```rust
446447
macro_rules! foo {
@@ -613,7 +614,7 @@ Rustはこの曖昧性を判定するために単純なルールを利用しま
613614
<!-- * `ty`: a type. Examples: `i32`; `Vec<(char, String)>`; `&T`. -->
614615
<!-- * `pat`: a pattern. Examples: `Some(t)`; `(17, 'a')`; `_`. -->
615616
<!-- * `stmt`: a single statement. Example: `let x = 3`. -->
616-
<!-- * `block`: a brace-delimited sequence of statements. Example: -->
617+
<!-- * `block`: a brace-delimited sequence of statements and optionally an expression. Example: -->
617618
<!-- `{ log(error, "hi"); return 12; }`. -->
618619
<!-- * `item`: an [item][item]. Examples: `fn foo() { }`; `struct Bar;`. -->
619620
<!-- * `meta`: a "meta item", as found in attributes. Example: `cfg(target_os = "windows")`. -->
@@ -624,37 +625,37 @@ Rustはこの曖昧性を判定するために単純なルールを利用しま
624625
* `ty`: 型。 例: `i32`; `Vec<(char, String)>`; `&T`
625626
* `pat`: パターン。 例: `Some(t)`; `(17, 'a')`; `_`
626627
* `stmt`: 単一の文。 例: `let x = 3`
627-
* `block`: 波括弧で区切られた文のシーケンス。 例: `{ log(error, "hi"); return 12 }`
628+
* `block`: 波括弧で区切られた文のシーケンスと場合によっては式も付く。 例: `{ log(error, "hi"); return 12 }`
628629
* `item`: [アイテム][item]。 例: `fn foo() { }`; `struct Bar;`
629630
* `meta`: アトリビュートで見られるような「メタアイテム」。 例: `cfg(target_os = "windows")`
630631
* `tt`: 単一のトークンの木
631632

632633
<!-- There are additional rules regarding the next token after a metavariable: -->
633634
またメタ変数の次のトークンについて以下のルールが存在します:
634635

635-
<!-- * `expr` variables may only be followed by one of: `=> , ;` -->
636-
<!-- * `ty` and `path` variables may only be followed by one of: `=> , : = > as` -->
637-
<!-- * `pat` variables may only be followed by one of: `=> , = if in` -->
636+
<!-- * `expr` and `stmt` variables may only be followed by one of: `=> , ;` -->
637+
<!-- * `ty` and `path` variables may only be followed by one of: `=> , = | ; : > [ { as where` -->
638+
<!-- * `pat` variables may only be followed by one of: `=> , = | if in` -->
638639
<!-- * Other variables may be followed by any token. -->
639-
* `expr` 変数は `=> , ;` のどれか一つのみが次に現れます
640-
* `ty``path` 変数は `=> , : = > as` のどれか一つのみが次に現れます
641-
* `pat` 変数は `=> , = if in` のどれか一つのみが次に現れます
640+
* `expr` 変数と `stmt` 変数は `=> , ;` のどれか一つのみが次に現れます
641+
* `ty``path` 変数は `=> , = | ; : > [ { as where` のどれか一つのみが次に現れます
642+
* `pat` 変数は `=> , = | if in` のどれか一つのみが次に現れます
642643
* その他の変数は任意のトークンが次に現れます
643644

644645
<!-- These rules provide some flexibility for Rust’s syntax to evolve without -->
645646
<!-- breaking existing macros. -->
646647
これらのルールは既存のマクロを破壊すること無くRustの構文を拡張するための自由度を与えます。
647648

648649
<!-- The macro system does not deal with parse ambiguity at all. For example, the -->
649-
<!-- grammar `$($t:ty)* $e:expr` will always fail to parse, because the parser would -->
650-
<!-- be forced to choose between parsing `$t` and parsing `$e`. Changing the -->
650+
<!-- grammar `$($i:ident)* $e:expr` will always fail to parse, because the parser would -->
651+
<!-- be forced to choose between parsing `$i` and parsing `$e`. Changing the -->
651652
<!-- invocation syntax to put a distinctive token in front can solve the problem. In -->
652-
<!-- this case, you can write `$(T $t:ty)* E $e:exp`. -->
653+
<!-- this case, you can write `$(I $i:ident)* E $e:expr`. -->
653654
マクロシステムはパースの曖昧さについては何も対処しません。
654-
例えば、 `$($t:ty)* $e:expr` は常にパースが失敗します、
655-
なぜならパーサーは `$t` をパースするか、 `$e` をパースするかを選ぶことを強制されるためです。
655+
例えば、 `$($i:ident)* $e:expr` は常にパースが失敗します、
656+
なぜならパーサーは `$i` をパースするか、 `$e` をパースするかを選ぶことを強制されるためです。
656657
呼出構文を変更して識別可能なトークンを先頭につけることでこの問題は回避することができます。
657-
そのようにする場合、例えば `$(T $t:ty)* E $e:exp` のように書くことができます。
658+
そのようにする場合、例えば `$(I $i:ident)* E $e:expr` のように書くことができます。
658659

659660
[item]: ../reference.html#items
660661

@@ -821,10 +822,9 @@ macro_rules! inc {
821822

822823

823824
<!-- To keep this system simple and correct, `#[macro_use] extern crate ...` may -->
824-
<!-- only appear at the root of your crate, not inside `mod`. This ensures that -->
825-
<!-- `$crate` is a single identifier. -->
825+
<!-- only appear at the root of your crate, not inside `mod`. -->
826826
このシステムを簡潔で正しく保つために、 `#[macro_use] extern crate ...` はクレートのルートにしか登場せず、
827-
`mod` の中には現れません。これは `$crate` が単一の識別子を持つことを確実にします。
827+
`mod` の中には現れません。
828828

829829

830830
<!-- # The deep end -->

diff-1.6.0..1.9.0/src/doc/book/macros.md

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)