Debian Codegraph @

debian/rust-vendor/fancy-regex/docs/subroutines/3_left_recursion.md

raw
1 ## Compile-time rejection of left-recursive patterns
2
3 fancy-regex's support of subroutines unlocks powerful features such as recursion.
4 With that power comes the risk of defining patterns that can recurse forever.
5
6 To guarantee termination and predictable behavior, fancy-regex rejects left-recursive patterns at compile time.
7
8 This check is conservative: if a pattern could recurse without consuming input, it is rejected - even if a particular input would not trigger that behavior.
9
10 ### What is left recursion?
11
12 A pattern is left-recursive if it can re-enter itself without consuming any input.
13
14 In other words, the engine can make recursive calls while staying at the same input position.
15
16 A simplified example looks like this:
17
18 ```regexp
19 (?<expr>\g<expr>a|a)
20 ```
21
22 Here, the subroutine expr can immediately call itself before matching anything.
23 No matter what the input is, this definition allows infinite recursion.
24
25 ### Why fancy-regex rejects these patterns
26
27 Left recursion is problematic because:
28
29 - It can cause infinite recursion or unbounded backtracking
30 - It cannot be made safe by input inspection alone
31
32 Even if a specific input would not trigger the recursion, the pattern itself is unsafe.
33
34 Rather than attempting to detect or recover from such cases at runtime, fancy-regex enforces a stronger rule:
35
36 **Every recursive call must consume input before it can recurse again.**
37
38 This guarantees that evaluation always makes progress.
39
40 ### Conservative by design
41
42 The left-recursion check is intentionally conservative.
43
44 Consider the following pattern:
45
46 ```regexp
47 (?<expr>ab|\g<expr>a)
48 ```
49
50 For the input "ab", this pattern would terminate successfully.
51 However, fancy-regex still rejects it.
52
53 Why?
54
55 Because the second alternative allows recursion before any input is consumed.
56 The engine cannot rely on runtime input to guarantee termination.
57
58 This is a deliberate design choice:
59
60 fancy-regex validates the structure of the pattern, not the behavior of a particular input.
61
62 ### What is allowed
63
64 Recursive patterns are allowed as long as they consume input before recursing.
65
66 For example:
67
68 ```regexp
69 (?<paren>\((?:[^()]*|\g<paren>)\))
70 ```
71
72 Here:
73
74 - Each recursive call to `paren` is preceded by a literal '('
75 - Input is always consumed before recursion
76 - Termination is guaranteed
77
78 This kind of recursion is safe and fully supported.
79
80 ### How to restructure left-recursive patterns
81
82 Left-recursive definitions can often be rewritten in a right-recursive or iterative form.
83
84 For example, instead of:
85
86 ```regexp
87 (?<expr>\g<expr>a|a)
88 ```
89
90 You can write:
91
92 ```regexp
93 a+
94 ```
95
96 Or, when recursion is genuinely required:
97
98 ```regexp
99 (?<expr>a\g<expr>?)
100 ```
101
102 In this version, input is consumed before the recursive call, satisfying fancy-regex's safety rules.