feat: add source hyperlinks + remove thin from-source.md
Every source reference now links to elixir-lang/elixir at commit f4e1b34. 122 hyperlinks across 11 topic files. Added PATTERN_COMPLETE sentinels. Removed from-source.md (326 lines, shallow) — covered by existing files.
This commit is contained in:
+14
-12
@@ -6,7 +6,7 @@ Patterns extracted from the Elixir standard library source code.
|
||||
|
||||
## 1. Context-Aware Macros (__CALLER__.context)
|
||||
|
||||
**Source:** `lib/elixir/lib/kernel.ex` lines 2032–2067 (or/and operators)
|
||||
**Source:** [lib/elixir/lib/kernel.ex#L2032](https://github.com/elixir-lang/elixir/blob/f4e1b34617ef92052b65781f18eae5b88a490098/lib/elixir/lib/kernel.ex#L2032) (or/and operators)
|
||||
|
||||
**What it does:** Macros check `__CALLER__.context` to determine if they're being invoked in a guard, match, or normal context, and generate different code accordingly.
|
||||
|
||||
@@ -100,7 +100,7 @@ end
|
||||
|
||||
## 2. defguard — Macro for Guard-Safe Expressions
|
||||
|
||||
**Source:** `lib/elixir/lib/kernel.ex` lines 5889–5966
|
||||
**Source:** [lib/elixir/lib/kernel.ex#L5889](https://github.com/elixir-lang/elixir/blob/f4e1b34617ef92052b65781f18eae5b88a490098/lib/elixir/lib/kernel.ex#L5889)
|
||||
|
||||
**What it does:** `defguard` creates a public macro that the compiler verifies is valid in guard clauses. It raises at compile time if the guard body uses non-guard-safe expressions.
|
||||
|
||||
@@ -191,7 +191,7 @@ end
|
||||
|
||||
## 3. quote + unquote for Code Generation
|
||||
|
||||
**Source:** `lib/elixir/lib/kernel.ex` lines 5624–5640 (defstruct)
|
||||
**Source:** [lib/elixir/lib/kernel.ex#L5624](https://github.com/elixir-lang/elixir/blob/f4e1b34617ef92052b65781f18eae5b88a490098/lib/elixir/lib/kernel.ex#L5624) (defstruct)
|
||||
|
||||
**What it does:** `quote bind_quoted: [fields: fields]` captures the macro argument into a variable available inside the quoted block. `unquote` injects computed values back into the AST.
|
||||
|
||||
@@ -284,7 +284,7 @@ end
|
||||
|
||||
## 4. var! for Breaking Hygiene
|
||||
|
||||
**Source:** `lib/elixir/lib/kernel.ex` lines 4884–4901
|
||||
**Source:** [lib/elixir/lib/kernel.ex#L4884](https://github.com/elixir-lang/elixir/blob/f4e1b34617ef92052b65781f18eae5b88a490098/lib/elixir/lib/kernel.ex#L4884)
|
||||
|
||||
**What it does:** `var!` marks a variable inside `quote` as unhygienic — it will refer to the variable in the *caller's* scope rather than creating a new hygienic binding.
|
||||
|
||||
@@ -376,7 +376,7 @@ double_it(5) # => 10, explicit, no hidden dependencies
|
||||
|
||||
## 5. Macro Expanding with Macro.expand
|
||||
|
||||
**Source:** `lib/elixir/lib/kernel.ex` lines 2246–2273 (raise), 2319–2340 (reraise)
|
||||
**Source:** [lib/elixir/lib/kernel.ex#L2246](https://github.com/elixir-lang/elixir/blob/f4e1b34617ef92052b65781f18eae5b88a490098/lib/elixir/lib/kernel.ex#L2246) (raise), 2319–2340 (reraise)
|
||||
|
||||
**What it does:** Before generating code, the macro calls `Macro.expand(message, __CALLER__)` to resolve aliases at compile time. This determines the code path: if the expanded value is an atom (module name), it generates exception-specific code.
|
||||
|
||||
@@ -464,7 +464,7 @@ end
|
||||
|
||||
## 6. assert_no_match_or_guard_scope Pattern
|
||||
|
||||
**Source:** `lib/elixir/lib/kernel.ex` lines 5384–5385 (def), 5415–5416 (defp), 5444–5445 (defmacro)
|
||||
**Source:** [lib/elixir/lib/kernel.ex#L5384](https://github.com/elixir-lang/elixir/blob/f4e1b34617ef92052b65781f18eae5b88a490098/lib/elixir/lib/kernel.ex#L5384) (def), 5415–5416 (defp), 5444–5445 (defmacro)
|
||||
|
||||
**What it does:** Macros that define module-level constructs (def, defp, defmacro, defmacrop) immediately assert they're not being called inside a guard or match context.
|
||||
|
||||
@@ -551,7 +551,7 @@ end
|
||||
|
||||
## 7. Protocol Definition as a Macro (defprotocol)
|
||||
|
||||
**Source:** `lib/elixir/lib/kernel.ex` lines 5734–5745, `lib/elixir/lib/protocol.ex` lines 290–318
|
||||
**Source:** [lib/elixir/lib/kernel.ex#L5734](https://github.com/elixir-lang/elixir/blob/f4e1b34617ef92052b65781f18eae5b88a490098/lib/elixir/lib/kernel.ex#L5734), [lib/elixir/lib/protocol.ex#L290](https://github.com/elixir-lang/elixir/blob/f4e1b34617ef92052b65781f18eae5b88a490098/lib/elixir/lib/protocol.ex#L290)
|
||||
|
||||
**What it does:** `defprotocol` is a macro that creates a module with auto-generated dispatch functions. Inside the protocol, `def` is redefined as a macro that generates both a callback spec and the dispatch implementation.
|
||||
|
||||
@@ -651,7 +651,7 @@ def format(binary) when is_binary(binary), do: # ...
|
||||
|
||||
## 8. @fallback_to_any in Protocols
|
||||
|
||||
**Source:** `lib/elixir/lib/inspect.ex` line 162, `lib/elixir/lib/protocol.ex` lines 115–131
|
||||
**Source:** [lib/elixir/lib/inspect.ex#L162](https://github.com/elixir-lang/elixir/blob/f4e1b34617ef92052b65781f18eae5b88a490098/lib/elixir/lib/inspect.ex#L162), [lib/elixir/lib/protocol.ex#L115](https://github.com/elixir-lang/elixir/blob/f4e1b34617ef92052b65781f18eae5b88a490098/lib/elixir/lib/protocol.ex#L115)
|
||||
|
||||
**What it does:** Sets `@fallback_to_any true` inside a protocol definition to enable a default implementation via `defimpl Protocol, for: Any`.
|
||||
|
||||
@@ -751,7 +751,7 @@ end
|
||||
|
||||
## 9. use/2 as Macro Injection Point
|
||||
|
||||
**Source:** `lib/elixir/lib/kernel.ex` lines 6130–6145
|
||||
**Source:** [lib/elixir/lib/kernel.ex#L6130](https://github.com/elixir-lang/elixir/blob/f4e1b34617ef92052b65781f18eae5b88a490098/lib/elixir/lib/kernel.ex#L6130)
|
||||
|
||||
**What it does:** `use Module, opts` is a macro that `require`s the module then calls `Module.__using__(opts)`. The `__using__/1` macro returns quoted code injected into the caller.
|
||||
|
||||
@@ -863,7 +863,7 @@ import MyHelpers
|
||||
|
||||
## 10. Sigil Macros (Pattern for DSL Literals)
|
||||
|
||||
**Source:** `lib/elixir/lib/kernel.ex` lines 6500–6850+ (sigil_S, sigil_s, sigil_r, sigil_D, etc.)
|
||||
**Source:** [lib/elixir/lib/kernel.ex#L6500](https://github.com/elixir-lang/elixir/blob/f4e1b34617ef92052b65781f18eae5b88a490098/lib/elixir/lib/kernel.ex#L6500)+ (sigil_S, sigil_s, sigil_r, sigil_D, etc.)
|
||||
|
||||
**What it does:** Each sigil (`~r`, `~D`, `~s`, etc.) is implemented as a `defmacro sigil_X(term, modifiers)` that receives the raw string content and modifier characters, then transforms them at compile time.
|
||||
|
||||
@@ -934,7 +934,7 @@ name = String.upcase("hello")
|
||||
|
||||
## 11. Pipe Operator as a Macro
|
||||
|
||||
**Source:** `lib/elixir/lib/kernel.ex` line 4509
|
||||
**Source:** [lib/elixir/lib/kernel.ex#L4509](https://github.com/elixir-lang/elixir/blob/f4e1b34617ef92052b65781f18eae5b88a490098/lib/elixir/lib/kernel.ex#L4509)
|
||||
|
||||
**What it does:** The `|>` pipe operator is a macro that rewrites `left |> right` into `right(left)`, inserting the left expression as the first argument of the right expression.
|
||||
|
||||
@@ -1016,7 +1016,7 @@ send_to(socket, encoded) # Clear which arg is which
|
||||
|
||||
## 12. Macro.generate_unique_arguments for Hygiene
|
||||
|
||||
**Source:** `lib/elixir/lib/macro.ex` lines 507–520
|
||||
**Source:** [lib/elixir/lib/macro.ex#L507](https://github.com/elixir-lang/elixir/blob/f4e1b34617ef92052b65781f18eae5b88a490098/lib/elixir/lib/macro.ex#L507)
|
||||
|
||||
**What it does:** `Macro.generate_unique_arguments(n, context)` creates `n` unique variable AST nodes that won't conflict with any user variables.
|
||||
|
||||
@@ -1104,3 +1104,5 @@ end
|
||||
```
|
||||
|
||||
**Why:** Variables created in `quote` are already hygienic by default — they can't clash with caller variables. `generate_unique_arguments` is needed when you're generating *multiple* variables dynamically (e.g., function parameters for a generated clause) where you need distinct names that also interoperate correctly.
|
||||
|
||||
<!-- PATTERN_COMPLETE -->
|
||||
|
||||
Reference in New Issue
Block a user