From 86a05102b45e36dceec40e9fa8400f39de4d5c71 Mon Sep 17 00:00:00 2001 From: Jake Rosoman Date: Thu, 9 May 2024 07:02:27 +1000 Subject: [PATCH 1/3] Update pattern-matching.md Explain why QuoteNode never matches --- docs/src/pattern-matching.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/src/pattern-matching.md b/docs/src/pattern-matching.md index 0cc53eb..9076761 100644 --- a/docs/src/pattern-matching.md +++ b/docs/src/pattern-matching.md @@ -91,6 +91,12 @@ Another common use case is to catch symbol literals, e.g. which will match e.g. `struct Foo ...` but not `struct Foo{V} ...` +If you want to match quoted symbols and happen to know that Julia's parser parses +them as `QuoteNode` then you might try `@capture(ex, s_QuoteNode)` and discover that +it never matches. This is because expressions are normalized before being matched +against the pattern. And `QuoteNode` get normalized to `quote` so to match a quoted +symbol you will need `@capture(ex, s_quote) && s.args[1] isa Symbol` + ### Unions `@capture` can also try to match the expression against one pattern or another, From bebf75d55af3bce43a9e0dd362232d8191c17a49 Mon Sep 17 00:00:00 2001 From: Jake Rosoman Date: Thu, 9 May 2024 09:43:37 +1000 Subject: [PATCH 2/3] Update docs/src/pattern-matching.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cédric St-Jean --- docs/src/pattern-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/pattern-matching.md b/docs/src/pattern-matching.md index 9076761..06e45da 100644 --- a/docs/src/pattern-matching.md +++ b/docs/src/pattern-matching.md @@ -94,7 +94,7 @@ which will match e.g. `struct Foo ...` but not `struct Foo{V} ...` If you want to match quoted symbols and happen to know that Julia's parser parses them as `QuoteNode` then you might try `@capture(ex, s_QuoteNode)` and discover that it never matches. This is because expressions are normalized before being matched -against the pattern. And `QuoteNode` get normalized to `quote` so to match a quoted +against the pattern. And `QuoteNode` gets normalized to `quote` so to match a quoted symbol you will need `@capture(ex, s_quote) && s.args[1] isa Symbol` ### Unions From 74d55fab567dc28e56ea490293300a62735dc0a3 Mon Sep 17 00:00:00 2001 From: Jake Rosoman Date: Thu, 9 May 2024 11:04:27 +1000 Subject: [PATCH 3/3] Update pattern-matching.md with example --- docs/src/pattern-matching.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/docs/src/pattern-matching.md b/docs/src/pattern-matching.md index 06e45da..078ea6f 100644 --- a/docs/src/pattern-matching.md +++ b/docs/src/pattern-matching.md @@ -91,10 +91,18 @@ Another common use case is to catch symbol literals, e.g. which will match e.g. `struct Foo ...` but not `struct Foo{V} ...` -If you want to match quoted symbols and happen to know that Julia's parser parses -them as `QuoteNode` then you might try `@capture(ex, s_QuoteNode)` and discover that -it never matches. This is because expressions are normalized before being matched -against the pattern. And `QuoteNode` gets normalized to `quote` so to match a quoted +If you want to match on quoted symbols like `:(:a)` you might look at the AST Julia +produces and expect to be able to use the type `QuoteNode` in your pattern. Hence, +you would expect the following to produce `1`: + +```julia +@match :(:a) begin + s_QuoteNode => 1 + s_quote => 2 +end +``` +However, it evaluates to `2` because MacroTools normalizes expressions before comparing +them to your pattern. And `QuoteNode` gets normalized to `quote` so to match a quoted symbol you will need `@capture(ex, s_quote) && s.args[1] isa Symbol` ### Unions