typenative 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/.github/workflows/ci.yml +2 -2
  2. package/.github/workflows/npm-publish-github-packages.yml +2 -2
  3. package/CHANGELOG.md +271 -1
  4. package/CONTRIBUTION.md +2 -1
  5. package/README.md +15 -7
  6. package/bin/cli.js +1 -0
  7. package/bin/index.js +62 -3
  8. package/bin/main.js +1 -0
  9. package/bin/node/core.js +41 -0
  10. package/bin/node/events.js +15 -10
  11. package/bin/node/process.js +13 -0
  12. package/bin/node/punycode.js +2 -2
  13. package/bin/node/timers.js +4 -2
  14. package/bin/node/url.js +1 -1
  15. package/bin/parse-node.js +2 -2
  16. package/bin/regex_vm.js +2057 -0
  17. package/bin/transpiler.js +17225 -4830
  18. package/bin/typecheck-node.js +2 -1
  19. package/bin/version.js +1 -1
  20. package/conformance.md +57 -15
  21. package/examples/README.md +46 -23
  22. package/examples/angular-templates/README.md +139 -0
  23. package/examples/angular-templates/bench.mjs +173 -0
  24. package/examples/angular-templates/dom-type-globals.d.ts +21 -0
  25. package/examples/angular-templates/dom-type-stubs.ts +21 -0
  26. package/examples/angular-templates/main.ts +135 -0
  27. package/examples/angular-templates/parity.mjs +157 -0
  28. package/examples/angular-templates/run-node.mjs +69 -0
  29. package/examples/angular-templates/vendor/angular/compiler/LICENSE +21 -0
  30. package/examples/desktop-notes/README.md +11 -13
  31. package/examples/dupe-finder/README.md +7 -5
  32. package/examples/fibonacci/README.md +7 -7
  33. package/examples/hello-world/README.md +6 -6
  34. package/examples/json-server/README.md +19 -18
  35. package/examples/mandelbrot-png/README.md +11 -13
  36. package/examples/marked-cli/README.md +38 -37
  37. package/examples/openai-chat/README.md +148 -0
  38. package/examples/openai-chat/bench.mjs +175 -0
  39. package/examples/openai-chat/main.ts +488 -0
  40. package/examples/react-compiler/README.md +170 -0
  41. package/examples/react-compiler/bench.mjs +181 -0
  42. package/examples/react-compiler/main.ts +158 -0
  43. package/examples/react-compiler/parity.mjs +362 -0
  44. package/examples/react-compiler/run-node.mjs +88 -0
  45. package/examples/react-compiler/vendor/babel/LICENSE +22 -0
  46. package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-lower.json +15 -0
  47. package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-upper.json +51 -0
  48. package/examples/react-compiler/vendor/charcodes/package.json +25 -0
  49. package/examples/react-compiler/vendor/invariant/LICENSE +21 -0
  50. package/examples/react-compiler/vendor/invariant/invariant.cjs +51 -0
  51. package/examples/react-compiler/vendor/invariant/invariant.d.cts +11 -0
  52. package/examples/react-compiler/vendor/invariant/types/LICENSE +21 -0
  53. package/examples/react-compiler/vendor/jridgewell/gen-mapping/LICENSE +19 -0
  54. package/examples/react-compiler/vendor/jridgewell/resolve-uri/LICENSE +19 -0
  55. package/examples/react-compiler/vendor/jridgewell/sourcemap-codec/LICENSE +19 -0
  56. package/examples/react-compiler/vendor/jridgewell/trace-mapping/LICENSE +19 -0
  57. package/examples/react-compiler/vendor/js-tokens/LICENSE +21 -0
  58. package/examples/react-compiler/vendor/js-tokens/index.cjs +23 -0
  59. package/examples/react-compiler/vendor/js-tokens/index.d.cts +13 -0
  60. package/examples/react-compiler/vendor/jsesc/LICENSE-MIT.txt +20 -0
  61. package/examples/react-compiler/vendor/jsesc/jsesc.cjs +337 -0
  62. package/examples/react-compiler/vendor/jsesc/jsesc.d.cts +384 -0
  63. package/examples/react-compiler/vendor/jsesc/types/LICENSE +21 -0
  64. package/examples/react-compiler/vendor/picocolors/LICENSE +15 -0
  65. package/examples/react-compiler/vendor/picocolors/picocolors.cjs +75 -0
  66. package/examples/react-compiler/vendor/picocolors/picocolors.d.cts +5 -0
  67. package/examples/react-compiler/vendor/picocolors/types.d.ts +51 -0
  68. package/examples/react-compiler/vendor/react-compiler/LICENSE +21 -0
  69. package/examples/react-compiler/vendor/stubs/babel-core.ts +31 -0
  70. package/examples/react-compiler/vendor/stubs/debug.ts +18 -0
  71. package/examples/react-compiler/vendor/stubs/js-tokens.d.ts +13 -0
  72. package/examples/react-compiler/vendor/stubs/pretty-format.ts +18 -0
  73. package/examples/react-compiler/vendor/zod/LICENSE +21 -0
  74. package/examples/react-compiler/vendor/zod-validation-error/LICENSE +9 -0
  75. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/NonEmptyArray.ts +5 -0
  76. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinPath.ts +49 -0
  77. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinValues.ts +48 -0
  78. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/stringify.ts +50 -0
  79. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/titleCase.ts +6 -0
  80. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/MessageBuilder.ts +126 -0
  81. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/ValidationError.ts +39 -0
  82. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/custom.ts +12 -0
  83. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/errorMap.ts +78 -0
  84. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/index.ts +2 -0
  85. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidElement.ts +12 -0
  86. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidKey.ts +12 -0
  87. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidStringFormat.ts +131 -0
  88. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidType.ts +54 -0
  89. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidUnion.ts +12 -0
  90. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidValue.ts +40 -0
  91. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/notMultipleOf.ts +12 -0
  92. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooBig.ts +76 -0
  93. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooSmall.ts +76 -0
  94. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/types.ts +27 -0
  95. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/unrecognizedKeys.ts +27 -0
  96. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromError.ts +13 -0
  97. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodError.ts +61 -0
  98. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodIssue.ts +38 -0
  99. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/index.ts +20 -0
  100. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationError.ts +5 -0
  101. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationErrorLike.ts +8 -0
  102. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isZodErrorLike.ts +11 -0
  103. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/toValidationError.ts +20 -0
  104. package/examples/react-compiler/vendor.mjs +688 -0
  105. package/examples/todo-cli/README.md +9 -9
  106. package/examples/yaml-cli/README.md +38 -28
  107. package/package.json +8 -5
  108. package/performance/README.md +195 -0
  109. package/performance/any-dynamic.ts +21 -0
  110. package/performance/any-record-view.ts +20 -0
  111. package/performance/arr-hof.ts +21 -0
  112. package/performance/arr-push-index.ts +15 -0
  113. package/performance/arr-sort.ts +21 -0
  114. package/performance/async-await.ts +24 -0
  115. package/performance/class-dispatch.ts +52 -0
  116. package/performance/events-emit.ts +27 -0
  117. package/performance/exc-throw-rare.ts +25 -0
  118. package/performance/exc-try-catch.ts +26 -0
  119. package/performance/fn-closures.ts +29 -0
  120. package/performance/fn-recursion-tree.ts +31 -0
  121. package/performance/gen-for-of.ts +46 -0
  122. package/performance/json-roundtrip.ts +27 -0
  123. package/performance/map-delete.ts +24 -0
  124. package/performance/map-string-keys.ts +22 -0
  125. package/performance/num-arith-loop.ts +14 -0
  126. package/performance/num-bitwise.ts +21 -0
  127. package/performance/num-mod-int.ts +16 -0
  128. package/performance/num-to-string.ts +16 -0
  129. package/performance/obj-alloc.ts +32 -0
  130. package/performance/regex-exec.ts +29 -0
  131. package/performance/regex-site.ts +28 -0
  132. package/performance/set-ops.ts +20 -0
  133. package/performance/str-char-scan.ts +35 -0
  134. package/performance/str-concat-plus.ts +18 -0
  135. package/performance/str-concat.ts +24 -0
  136. package/performance/str-short-scan.ts +26 -0
  137. package/performance/str-split-join.ts +23 -0
  138. package/performance/str-template.ts +16 -0
  139. package/performance/union-switch.ts +57 -0
  140. package/tsparser/check.go +2 -1
  141. package/tsparser/main.go +1 -0
  142. package/types/typenative-npm.d.ts +3 -2
  143. package/types/typenative.d.ts +42 -12
  144. package/ROUND2-HANDOFF.md +0 -200
@@ -0,0 +1,2057 @@
1
+ // The JS regex engine of compiled programs (Go source, the fancyRegex helper's
2
+ // engine): a backtracking VM over a flat program. tnRxCompile flattens the
3
+ // parser's tree; tnRxMatcher.run executes it on byte offsets with an explicit
4
+ // stack, so a match allocates nothing but its result.
5
+ const REGEX_VM = `// ---- Regex VM ----
6
+ const (
7
+ tnRxChar = iota // one ASCII byte (c)
8
+ tnRxLit // a string (lit)
9
+ tnRxSet // one rune of a set
10
+ tnRxSplit // try x, then y
11
+ tnRxSplitFirst // the same, but x only when the next byte can start it (first)
12
+ tnRxSplitLazy // try x, then y, but x only when the next byte can start it (first)
13
+ tnRxJmp // go to x
14
+ tnRxSave // the position into slot x
15
+ tnRxBol // ^
16
+ tnRxBolLine // ^ with m
17
+ tnRxEol // $
18
+ tnRxEolLine // $ with m
19
+ tnRxWordB // \\\\b
20
+ tnRxNotWordB // \\\\B
21
+ tnRxBackref // the text of group slots x, x+1
22
+ tnRxBackrefFold // the same, ignoring case
23
+ tnRxLook // a lookaround: its body follows, the rest of the pattern is at y
24
+ tnRxLookEnd // the end of a lookaround's body
25
+ tnRxPeek // a lookahead of one rune (set)
26
+ tnRxRepGreedy // a greedy repeat of one rune (set, min, max)
27
+ tnRxRepLazy // the lazy one
28
+ tnRxLoopInit // a counted loop starts: registers x, x+1
29
+ tnRxLoopHead // iterate (next instruction) or leave (y)
30
+ tnRxLoopEnter // an iteration starts
31
+ tnRxLoopTail // an iteration ends: back to y
32
+ tnRxResetCaps // slots x up to y are unset (each iteration, as in JS)
33
+ tnRxEnd // the match
34
+ )
35
+
36
+ // Backtrack stack entries
37
+ const (
38
+ tnRxChoice = iota // resume at pc, pos
39
+ tnRxUndo // slot pc held pos
40
+ tnRxGreedy // a tnRxRepGreedy at pc can give back n more runes
41
+ tnRxLazy // a tnRxRepLazy at pc can take n more runes (-1: no limit)
42
+ tnRxBarrier // a lookaround's start
43
+ )
44
+
45
+ type tnRxCharSet struct {
46
+ ascii [utf8.RuneSelf]bool
47
+ match func(rune) bool // (any rune; ascii answers for the ASCII ones)
48
+ high bool // it can match a non-ASCII rune
49
+ }
50
+
51
+ type tnRxInst struct {
52
+ op uint8
53
+ c byte
54
+ neg bool
55
+ behind bool
56
+ greedy bool
57
+ possessive bool // what follows can't match inside the repeat: nothing to give back
58
+ next int16 // the byte what follows must start with (-1: unknown)
59
+ x, y int32
60
+ min, max int32
61
+ set *tnRxCharSet
62
+ first *[256]bool
63
+ lit string
64
+ }
65
+
66
+ type tnRxProg struct {
67
+ insts []tnRxInst
68
+ ncaps int // capture slots
69
+ nslots int // capture slots, then loop registers
70
+ first *[256]bool // the bytes a match can start with (nil: it can be empty)
71
+ prefix string // the text every match starts with
72
+ anchored bool // it can only match at the input's start
73
+ contextual bool // its ^, \\\\b or \\\\B see the text before the match
74
+ limit int // backtracks before RE2 takes over (0: RE2 can't run it)
75
+ }
76
+
77
+ type tnRxFrame struct {
78
+ kind int32
79
+ pc int32
80
+ pos int
81
+ n int
82
+ }
83
+
84
+ type tnRxMatcher struct {
85
+ s string
86
+ caps []int
87
+ stack []tnRxFrame
88
+ chunk []string // what's left of the block match arrays are carved from
89
+ end int
90
+ steps int
91
+ limit int
92
+ over bool
93
+ }
94
+
95
+ // (one idle matcher: regexes run one at a time, unless workers share them)
96
+ var tnRxIdle atomic.Pointer[tnRxMatcher]
97
+
98
+ func tnRxAcquire(p *tnRxProg, s string, start int) *tnRxMatcher {
99
+ m := tnRxIdle.Swap(nil)
100
+ if m == nil {
101
+ m = &tnRxMatcher{stack: make([]tnRxFrame, 0, 256)}
102
+ }
103
+ if cap(m.caps) < p.nslots {
104
+ m.caps = make([]int, p.nslots, p.nslots+16)
105
+ }
106
+ m.caps = m.caps[:p.nslots]
107
+ m.s, m.steps, m.over, m.limit = s, 0, false, 0
108
+ // (past offset 0 RE2 can't serve a contextual pattern: no budget, as JS)
109
+ if p.limit > 0 && (start == 0 || !p.contextual) {
110
+ m.limit = p.limit + 8*len(s)
111
+ }
112
+ return m
113
+ }
114
+
115
+ func tnRxRelease(m *tnRxMatcher) {
116
+ m.s = ""
117
+ if cap(m.stack) > 1<<16 {
118
+ m.stack = make([]tnRxFrame, 0, 256)
119
+ }
120
+ tnRxIdle.Store(m)
121
+ }
122
+
123
+ // A miss found without running anything
124
+ func (p *tnRxProg) miss(s string, start int, sticky bool) bool {
125
+ if p.anchored && start > 0 {
126
+ return true
127
+ }
128
+ if sticky || p.anchored {
129
+ return p.first != nil && (start >= len(s) || !p.first[s[start]])
130
+ }
131
+ return p.candidate(s, start) < 0
132
+ }
133
+
134
+ // The next place at or after pos a match can start (-1: none)
135
+ func (p *tnRxProg) candidate(s string, pos int) int {
136
+ if p.prefix != "" {
137
+ i := strings.Index(s[pos:], p.prefix)
138
+ if i < 0 {
139
+ return -1
140
+ }
141
+ return pos + i
142
+ }
143
+ if p.first != nil {
144
+ for pos < len(s) && !p.first[s[pos]] {
145
+ pos++
146
+ }
147
+ if pos == len(s) {
148
+ return -1
149
+ }
150
+ }
151
+ return pos
152
+ }
153
+
154
+ // The leftmost match at or after start (only at start when sticky), or -1;
155
+ // its captures are in m.caps, its end in m.end
156
+ func (p *tnRxProg) search(m *tnRxMatcher, start int, sticky bool) int {
157
+ s := m.s
158
+ scan := !sticky && !p.anchored
159
+ for pos := start; ; {
160
+ if scan {
161
+ if pos = p.candidate(s, pos); pos < 0 {
162
+ return -1
163
+ }
164
+ }
165
+ caps := m.caps[:p.ncaps]
166
+ for i := range caps {
167
+ caps[i] = -1
168
+ }
169
+ m.stack = m.stack[:0]
170
+ if m.run(p.insts, 0, pos, -1) {
171
+ return pos
172
+ }
173
+ if !scan || m.over || pos >= len(s) {
174
+ return -1
175
+ }
176
+ if s[pos] < utf8.RuneSelf {
177
+ pos++
178
+ } else {
179
+ _, w := utf8.DecodeRuneInString(s[pos:])
180
+ pos += w
181
+ }
182
+ }
183
+ }
184
+
185
+ // Line terminators (as JS defines them) around a byte offset
186
+ // The match array of the match at lo, hi: n groups from the captures
187
+ func (m *tnRxMatcher) groups(n int, lo int, hi int) []string {
188
+ s := m.s
189
+ // (capped at their hidden slots: an append moves them out of the chunk)
190
+ if len(m.chunk) < n+2 {
191
+ m.chunk = make([]string, max(n+2, 256))
192
+ }
193
+ groups := m.chunk[:n:n+2]
194
+ m.chunk = m.chunk[n+2:]
195
+ groups[0] = s[lo:hi]
196
+ for i := 1; i < n; i++ {
197
+ if a, b := m.caps[2*i], m.caps[2*i+1]; a >= 0 && b >= a {
198
+ groups[i] = s[a:b]
199
+ } else {
200
+ groups[i] = tnUnmatched
201
+ }
202
+ }
203
+ tnMatchTag(groups, s, lo)
204
+ return groups
205
+ }
206
+
207
+ func tnRxLineEndAt(s string, pos int) bool {
208
+ switch c := s[pos]; c {
209
+ case '\\n', '\\r':
210
+ return true
211
+ case 0xe2:
212
+ return strings.HasPrefix(s[pos:], "\\u2028") || strings.HasPrefix(s[pos:], "\\u2029")
213
+ }
214
+ return false
215
+ }
216
+
217
+ func tnRxLineEndBefore(s string, pos int) bool {
218
+ if c := s[pos-1]; c == '\\n' || c == '\\r' {
219
+ return true
220
+ }
221
+ return pos >= 3 && (s[pos-3:pos] == "\\u2028" || s[pos-3:pos] == "\\u2029")
222
+ }
223
+
224
+ var tnRxWord = func() (t [256]bool) {
225
+ for c := range t {
226
+ t[c] = c == '_' || c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'
227
+ }
228
+ return
229
+ }()
230
+
231
+ // Runs from pc at pos until tnRxEnd, or tnRxLookEnd (at target, when one is
232
+ // given). True leaves the frames it pushed on the stack; false unwinds them.
233
+ func (m *tnRxMatcher) run(prog []tnRxInst, pc int32, pos int, target int) bool {
234
+ s, caps, st := m.s, m.caps, m.stack
235
+ base := len(st)
236
+ vm:
237
+ for {
238
+ in := &prog[pc]
239
+ // (a case that matches continues; one that falls out of the switch fails)
240
+ switch in.op {
241
+ case tnRxChar:
242
+ if pos < len(s) && s[pos] == in.c {
243
+ pos++
244
+ pc++
245
+ continue
246
+ }
247
+ case tnRxLit:
248
+ if strings.HasPrefix(s[pos:], in.lit) {
249
+ pos += len(in.lit)
250
+ pc++
251
+ continue
252
+ }
253
+ case tnRxSet:
254
+ if pos < len(s) {
255
+ if c := s[pos]; c < utf8.RuneSelf {
256
+ if in.set.ascii[c] {
257
+ pos++
258
+ pc++
259
+ continue
260
+ }
261
+ } else if r, w := utf8.DecodeRuneInString(s[pos:]); in.set.match(r) {
262
+ pos += w
263
+ pc++
264
+ continue
265
+ }
266
+ }
267
+ case tnRxSplit:
268
+ st = append(st, tnRxFrame{kind: tnRxChoice, pc: in.y, pos: pos})
269
+ pc = in.x
270
+ continue
271
+ case tnRxSplitFirst:
272
+ if pos < len(s) && in.first[s[pos]] {
273
+ st = append(st, tnRxFrame{kind: tnRxChoice, pc: in.y, pos: pos})
274
+ pc = in.x
275
+ } else {
276
+ pc = in.y
277
+ }
278
+ continue
279
+ case tnRxSplitLazy:
280
+ // (a lazy loop's exit: only worth trying where what follows can start)
281
+ if pos < len(s) && !in.first[s[pos]] {
282
+ pc = in.y
283
+ } else {
284
+ st = append(st, tnRxFrame{kind: tnRxChoice, pc: in.y, pos: pos})
285
+ pc = in.x
286
+ }
287
+ continue
288
+ case tnRxJmp:
289
+ pc = in.x
290
+ continue
291
+ case tnRxSave:
292
+ if len(st) > 0 {
293
+ st = append(st, tnRxFrame{kind: tnRxUndo, pc: in.x, pos: caps[in.x]})
294
+ }
295
+ caps[in.x] = pos
296
+ pc++
297
+ continue
298
+ case tnRxBol:
299
+ if pos == 0 {
300
+ pc++
301
+ continue
302
+ }
303
+ case tnRxBolLine:
304
+ if pos == 0 || tnRxLineEndBefore(s, pos) {
305
+ pc++
306
+ continue
307
+ }
308
+ case tnRxEol:
309
+ if pos == len(s) {
310
+ pc++
311
+ continue
312
+ }
313
+ case tnRxEolLine:
314
+ if pos == len(s) || tnRxLineEndAt(s, pos) {
315
+ pc++
316
+ continue
317
+ }
318
+ case tnRxWordB, tnRxNotWordB:
319
+ boundary := (pos > 0 && tnRxWord[s[pos-1]]) != (pos < len(s) && tnRxWord[s[pos]])
320
+ if boundary == (in.op == tnRxWordB) {
321
+ pc++
322
+ continue
323
+ }
324
+ case tnRxBackref:
325
+ a, b := caps[in.x], caps[in.x+1]
326
+ if a < 0 || b < 0 {
327
+ pc++
328
+ continue
329
+ }
330
+ if pos+b-a <= len(s) && s[pos:pos+b-a] == s[a:b] {
331
+ pos += b - a
332
+ pc++
333
+ continue
334
+ }
335
+ case tnRxBackrefFold:
336
+ a, b := caps[in.x], caps[in.x+1]
337
+ if a < 0 || b < 0 {
338
+ pc++
339
+ continue
340
+ }
341
+ p, same := pos, true
342
+ for _, r := range s[a:b] {
343
+ if p >= len(s) {
344
+ same = false
345
+ break
346
+ }
347
+ r2, w := utf8.DecodeRuneInString(s[p:])
348
+ if unicode.ToLower(r) != unicode.ToLower(r2) {
349
+ same = false
350
+ break
351
+ }
352
+ p += w
353
+ }
354
+ if same {
355
+ pos = p
356
+ pc++
357
+ continue
358
+ }
359
+ case tnRxLook:
360
+ // (a lookahead whose body can't start at the next byte)
361
+ if in.first != nil && pos < len(s) && !in.first[s[pos]] {
362
+ if in.neg {
363
+ pc = in.y
364
+ continue
365
+ }
366
+ break
367
+ }
368
+ // (the barrier keeps undo entries on: a body's captures must be undone)
369
+ st = append(st, tnRxFrame{kind: tnRxBarrier})
370
+ mark := len(st)
371
+ m.stack = st
372
+ matched := false
373
+ if !in.behind {
374
+ matched = m.run(prog, pc+1, pos, -1)
375
+ } else {
376
+ // (starts from pos back, as far as the lookbehind's longest match)
377
+ start := pos
378
+ for steps := 0; ; steps++ {
379
+ if m.run(prog, pc+1, start, pos) {
380
+ matched = true
381
+ break
382
+ }
383
+ if m.over || start == 0 || (in.max >= 0 && steps >= int(in.max)) {
384
+ break
385
+ }
386
+ if s[start-1] < utf8.RuneSelf {
387
+ start--
388
+ } else {
389
+ _, w := utf8.DecodeLastRuneInString(s[:start])
390
+ start -= w
391
+ }
392
+ }
393
+ }
394
+ st = m.stack
395
+ if m.over {
396
+ m.stack = st[:base]
397
+ return false
398
+ }
399
+ if matched && !in.neg {
400
+ // atomic: its choices go, but its captures stay undoable
401
+ w := mark - 1
402
+ for i := mark; i < len(st); i++ {
403
+ if st[i].kind == tnRxUndo {
404
+ st[w] = st[i]
405
+ w++
406
+ }
407
+ }
408
+ st = st[:w]
409
+ pc = in.y
410
+ continue
411
+ }
412
+ if matched {
413
+ for i := len(st) - 1; i >= mark; i-- {
414
+ if st[i].kind == tnRxUndo {
415
+ caps[st[i].pc] = st[i].pos
416
+ }
417
+ }
418
+ }
419
+ st = st[:mark-1]
420
+ if !matched && in.neg {
421
+ pc = in.y
422
+ continue
423
+ }
424
+ case tnRxLookEnd:
425
+ if target < 0 || pos == target {
426
+ m.stack = st
427
+ return true
428
+ }
429
+ case tnRxPeek:
430
+ hit := false
431
+ if pos < len(s) {
432
+ if c := s[pos]; c < utf8.RuneSelf {
433
+ hit = in.set.ascii[c]
434
+ } else {
435
+ r, _ := utf8.DecodeRuneInString(s[pos:])
436
+ hit = in.set.match(r)
437
+ }
438
+ }
439
+ if hit != in.neg {
440
+ pc++
441
+ continue
442
+ }
443
+ case tnRxRepGreedy:
444
+ set, count := in.set, 0
445
+ for pos < len(s) && (in.max < 0 || count < int(in.max)) {
446
+ if c := s[pos]; c < utf8.RuneSelf {
447
+ if !set.ascii[c] {
448
+ break
449
+ }
450
+ pos++
451
+ } else {
452
+ r, w := utf8.DecodeRuneInString(s[pos:])
453
+ if !set.match(r) {
454
+ break
455
+ }
456
+ pos += w
457
+ }
458
+ count++
459
+ }
460
+ if count >= int(in.min) {
461
+ if extra := count - int(in.min); extra > 0 && !in.possessive {
462
+ st = append(st, tnRxFrame{kind: tnRxGreedy, pc: pc, pos: pos, n: extra})
463
+ }
464
+ pc++
465
+ continue
466
+ }
467
+ case tnRxRepLazy:
468
+ set, count := in.set, 0
469
+ for count < int(in.min) && pos < len(s) {
470
+ if c := s[pos]; c < utf8.RuneSelf {
471
+ if !set.ascii[c] {
472
+ break
473
+ }
474
+ pos++
475
+ } else {
476
+ r, w := utf8.DecodeRuneInString(s[pos:])
477
+ if !set.match(r) {
478
+ break
479
+ }
480
+ pos += w
481
+ }
482
+ count++
483
+ }
484
+ if count >= int(in.min) {
485
+ if in.max < 0 {
486
+ st = append(st, tnRxFrame{kind: tnRxLazy, pc: pc, pos: pos, n: -1})
487
+ } else if in.max > in.min {
488
+ st = append(st, tnRxFrame{kind: tnRxLazy, pc: pc, pos: pos, n: int(in.max - in.min)})
489
+ }
490
+ pc++
491
+ continue
492
+ }
493
+ case tnRxLoopInit:
494
+ if len(st) > 0 {
495
+ st = append(st, tnRxFrame{kind: tnRxUndo, pc: in.x, pos: caps[in.x]}, tnRxFrame{kind: tnRxUndo, pc: in.x + 1, pos: caps[in.x+1]})
496
+ }
497
+ caps[in.x], caps[in.x+1] = 0, -1
498
+ pc++
499
+ continue
500
+ case tnRxLoopHead:
501
+ count := caps[in.x]
502
+ switch {
503
+ case count < int(in.min):
504
+ pc++
505
+ case in.max >= 0 && count >= int(in.max):
506
+ pc = in.y
507
+ case in.greedy:
508
+ st = append(st, tnRxFrame{kind: tnRxChoice, pc: in.y, pos: pos})
509
+ pc++
510
+ default:
511
+ st = append(st, tnRxFrame{kind: tnRxChoice, pc: pc + 1, pos: pos})
512
+ pc = in.y
513
+ }
514
+ continue
515
+ case tnRxLoopEnter:
516
+ if len(st) > 0 {
517
+ st = append(st, tnRxFrame{kind: tnRxUndo, pc: in.x, pos: caps[in.x]}, tnRxFrame{kind: tnRxUndo, pc: in.x + 1, pos: caps[in.x+1]})
518
+ }
519
+ caps[in.x]++
520
+ caps[in.x+1] = pos
521
+ pc++
522
+ continue
523
+ case tnRxLoopTail:
524
+ // (past the minimum, an iteration that matched nothing fails)
525
+ if pos != caps[in.x+1] || caps[in.x] <= int(in.min) {
526
+ pc = in.y
527
+ continue
528
+ }
529
+ case tnRxResetCaps:
530
+ for i := in.x; i < in.y; i++ {
531
+ if caps[i] != -1 {
532
+ if len(st) > 0 {
533
+ st = append(st, tnRxFrame{kind: tnRxUndo, pc: i, pos: caps[i]})
534
+ }
535
+ caps[i] = -1
536
+ }
537
+ }
538
+ pc++
539
+ continue
540
+ case tnRxEnd:
541
+ m.end = pos
542
+ m.stack = st
543
+ return true
544
+ }
545
+ // Failure: back to the latest choice
546
+ for {
547
+ if len(st) == base {
548
+ m.stack = st
549
+ return false
550
+ }
551
+ m.steps++
552
+ if m.limit > 0 && m.steps > m.limit {
553
+ m.over = true
554
+ m.stack = st[:base]
555
+ return false
556
+ }
557
+ f := &st[len(st)-1]
558
+ switch f.kind {
559
+ case tnRxChoice:
560
+ pc, pos = f.pc, f.pos
561
+ st = st[:len(st)-1]
562
+ continue vm
563
+ case tnRxUndo:
564
+ caps[f.pc] = f.pos
565
+ st = st[:len(st)-1]
566
+ case tnRxGreedy:
567
+ // one rune back, or to where what follows can start
568
+ next, p, n := prog[f.pc].next, f.pos, f.n
569
+ found := false
570
+ for n > 0 && !found {
571
+ if s[p-1] < utf8.RuneSelf {
572
+ p--
573
+ } else {
574
+ _, w := utf8.DecodeLastRuneInString(s[:p])
575
+ p -= w
576
+ }
577
+ n--
578
+ found = next < 0 || (p < len(s) && s[p] == byte(next))
579
+ }
580
+ pcNext := f.pc + 1
581
+ if n == 0 {
582
+ st = st[:len(st)-1]
583
+ } else {
584
+ f.pos, f.n = p, n
585
+ }
586
+ if found {
587
+ pc, pos = pcNext, p
588
+ continue vm
589
+ }
590
+ case tnRxLazy:
591
+ // one rune more, or on to where what follows can start
592
+ inst := &prog[f.pc]
593
+ next, p, n := inst.next, f.pos, f.n
594
+ found := false
595
+ for !found && n != 0 && p < len(s) {
596
+ if c := s[p]; c < utf8.RuneSelf {
597
+ if !inst.set.ascii[c] {
598
+ n = 0
599
+ break
600
+ }
601
+ p++
602
+ } else {
603
+ r, w := utf8.DecodeRuneInString(s[p:])
604
+ if !inst.set.match(r) {
605
+ n = 0
606
+ break
607
+ }
608
+ p += w
609
+ }
610
+ if n > 0 {
611
+ n--
612
+ }
613
+ found = next < 0 || (p < len(s) && s[p] == byte(next))
614
+ }
615
+ pcNext := f.pc + 1
616
+ if found && n != 0 {
617
+ f.pos, f.n = p, n
618
+ } else {
619
+ st = st[:len(st)-1]
620
+ }
621
+ if found {
622
+ pc, pos = pcNext, p
623
+ continue vm
624
+ }
625
+ default:
626
+ st = st[:len(st)-1]
627
+ }
628
+ }
629
+ }
630
+ }
631
+
632
+ // ---- Compiling the tree to a program ----
633
+
634
+ type tnRxCompiler struct {
635
+ insts []tnRxInst
636
+ sets map[*tnBtNode]*tnRxCharSet
637
+ icase, multiline, dotAll bool
638
+ slots int
639
+ }
640
+
641
+ var tnRxPrograms = map[string]*tnRxProg{}
642
+ var tnRxProgramsMu sync.Mutex
643
+
644
+ // A pattern's program, compiled once however many regex objects use it (nil:
645
+ // the parser can't read it)
646
+ func tnRxProgramOf(source string, flags string) *tnRxProg {
647
+ key := flags + "/" + source
648
+ tnRxProgramsMu.Lock()
649
+ defer tnRxProgramsMu.Unlock()
650
+ p, ok := tnRxPrograms[key]
651
+ if !ok {
652
+ p = tnRxCompile(source, flags)
653
+ tnRxPrograms[key] = p
654
+ }
655
+ return p
656
+ }
657
+
658
+ func tnRxCompile(source string, flags string) *tnRxProg {
659
+ // (v: unicode mode with class set syntax; RE2 can't read it, so a v pattern
660
+ // the parser rejects gets no program)
661
+ sets := strings.Contains(flags, "v")
662
+ bt := tnBtCompile(source, sets || strings.Contains(flags, "u"), sets, strings.Contains(flags, "i"))
663
+ if bt == nil {
664
+ return nil
665
+ }
666
+ c := &tnRxCompiler{sets: map[*tnBtNode]*tnRxCharSet{}, icase: strings.Contains(flags, "i"), multiline: strings.Contains(flags, "m"), dotAll: strings.Contains(flags, "s")}
667
+ ncaps := (bt.ngroups + 1) * 2
668
+ c.slots = ncaps
669
+ c.node(bt.root, nil, true)
670
+ c.emit(tnRxInst{op: tnRxEnd})
671
+ p := &tnRxProg{insts: c.insts, ncaps: ncaps, nslots: c.slots, contextual: tnPatternUsesContext(source)}
672
+ p.anchored = !c.multiline && tnBtStartAnchored(bt.root)
673
+ first := new([256]bool)
674
+ if !c.firstOf(bt.root, first) {
675
+ p.first = first
676
+ }
677
+ p.prefix = c.prefixOf(bt.root)
678
+ if p.prefix == "" && p.first != nil {
679
+ count, only := 0, 0
680
+ for b, in := range p.first {
681
+ if in {
682
+ count, only = count+1, b
683
+ }
684
+ }
685
+ if count == 1 && only < utf8.RuneSelf {
686
+ p.prefix = string(rune(only))
687
+ }
688
+ }
689
+ if !tnBtNeedsBacktracking(bt.root) {
690
+ p.limit = 1 << 22
691
+ }
692
+ return p
693
+ }
694
+
695
+ // Whether a tree uses what only backtracking can run (RE2 can't take over)
696
+ func tnBtNeedsBacktracking(n *tnBtNode) bool {
697
+ if n == nil {
698
+ return false
699
+ }
700
+ if n.op == "look" || n.op == "backref" {
701
+ return true
702
+ }
703
+ for _, c := range n.seq {
704
+ if tnBtNeedsBacktracking(c) {
705
+ return true
706
+ }
707
+ }
708
+ for _, b := range n.branches {
709
+ for _, c := range b {
710
+ if tnBtNeedsBacktracking(c) {
711
+ return true
712
+ }
713
+ }
714
+ }
715
+ return tnBtNeedsBacktracking(n.child)
716
+ }
717
+
718
+ func (c *tnRxCompiler) emit(in tnRxInst) int32 {
719
+ in.next = -1
720
+ c.insts = append(c.insts, in)
721
+ return int32(len(c.insts) - 1)
722
+ }
723
+
724
+ func (c *tnRxCompiler) here() int32 {
725
+ return int32(len(c.insts))
726
+ }
727
+
728
+ func (c *tnRxCompiler) setOf(n *tnBtNode) *tnRxCharSet {
729
+ if set, ok := c.sets[n]; ok {
730
+ return set
731
+ }
732
+ icase := c.icase
733
+ var in func(rune) bool
734
+ high := true
735
+ switch n.op {
736
+ case "char":
737
+ ch := n.ch
738
+ high = ch >= utf8.RuneSelf || icase
739
+ if icase {
740
+ lower := unicode.ToLower(ch)
741
+ in = func(r rune) bool { return r == ch || unicode.ToLower(r) == lower }
742
+ } else {
743
+ in = func(r rune) bool { return r == ch }
744
+ }
745
+ case "any":
746
+ if c.dotAll {
747
+ in = func(rune) bool { return true }
748
+ } else {
749
+ in = func(r rune) bool { return !tnBtLineEnd(r) }
750
+ }
751
+ default:
752
+ cls, neg := n.cls, n.negCls
753
+ high = !n.asciiOnly || neg || icase
754
+ in = func(r rune) bool {
755
+ v := cls(r)
756
+ if !v && icase {
757
+ v = cls(unicode.ToLower(r)) || cls(unicode.ToUpper(r))
758
+ }
759
+ return v != neg
760
+ }
761
+ }
762
+ set := &tnRxCharSet{match: in, high: high}
763
+ for b := rune(0); b < utf8.RuneSelf; b++ {
764
+ set.ascii[b] = in(b)
765
+ }
766
+ c.sets[n] = set
767
+ return set
768
+ }
769
+
770
+ func tnBtIsSingle(n *tnBtNode) bool {
771
+ return n.op == "char" || n.op == "any" || n.op == "class"
772
+ }
773
+
774
+ // Adds the bytes n can start with to set; reports whether n can match
775
+ // without consuming anything (a zero-width test counts as that)
776
+ func (c *tnRxCompiler) firstOf(n *tnBtNode, set *[256]bool) bool {
777
+ switch n.op {
778
+ case "char", "any", "class":
779
+ rs := c.setOf(n)
780
+ for b, in := range rs.ascii {
781
+ set[b] = set[b] || in
782
+ }
783
+ if rs.high {
784
+ for b := utf8.RuneSelf; b < 256; b++ {
785
+ set[b] = true
786
+ }
787
+ }
788
+ return false
789
+ case "cat":
790
+ return c.firstOfSeq(n.seq, set)
791
+ case "alt":
792
+ nullable := false
793
+ for _, br := range n.branches {
794
+ nullable = c.firstOfSeq(br, set) || nullable
795
+ }
796
+ return nullable
797
+ case "group":
798
+ return c.firstOf(n.child, set)
799
+ case "rep":
800
+ nullable := c.firstOf(n.child, set)
801
+ return nullable || n.min == 0
802
+ case "anchor", "look":
803
+ return true
804
+ }
805
+ // (a backreference: its text is not known here)
806
+ for b := range set {
807
+ set[b] = true
808
+ }
809
+ return true
810
+ }
811
+
812
+ func (c *tnRxCompiler) firstOfSeq(nodes []*tnBtNode, set *[256]bool) bool {
813
+ for _, n := range nodes {
814
+ if !c.firstOf(n, set) {
815
+ return false
816
+ }
817
+ }
818
+ return true
819
+ }
820
+
821
+ // firstOf for a lookahead's body, tested where there is a next byte: there
822
+ // a $ without m can't match, so it adds nothing
823
+ func (c *tnRxCompiler) firstOfLook(n *tnBtNode, set *[256]bool) bool {
824
+ switch n.op {
825
+ case "anchor":
826
+ return c.multiline || n.anchor != '$'
827
+ case "cat":
828
+ for _, item := range n.seq {
829
+ if !c.firstOfLook(item, set) {
830
+ return false
831
+ }
832
+ }
833
+ return true
834
+ case "alt":
835
+ nullable := false
836
+ for _, br := range n.branches {
837
+ if c.firstOfLook(&tnBtNode{op: "cat", seq: br}, set) {
838
+ nullable = true
839
+ }
840
+ }
841
+ return nullable
842
+ case "group":
843
+ return c.firstOfLook(n.child, set)
844
+ }
845
+ return c.firstOf(n, set)
846
+ }
847
+
848
+ // The literal text every match starts with
849
+ func (c *tnRxCompiler) prefixOf(n *tnBtNode) string {
850
+ if n.op != "cat" || c.icase {
851
+ return ""
852
+ }
853
+ var b strings.Builder
854
+ for _, item := range n.seq {
855
+ if item.op != "char" {
856
+ break
857
+ }
858
+ b.WriteRune(item.ch)
859
+ }
860
+ return b.String()
861
+ }
862
+
863
+ // What the code after a repeat of one rune tells: whether its runes can be
864
+ // kept (nothing after can match inside them), and the byte it must start with
865
+ func (c *tnRxCompiler) repHints(in *tnRxInst, tail []*tnBtNode, end bool) {
866
+ if len(tail) == 0 {
867
+ in.possessive = end && in.greedy
868
+ return
869
+ }
870
+ var first [256]bool
871
+ if c.firstOfSeq(tail, &first) {
872
+ return
873
+ }
874
+ count, only, high := 0, 0, false
875
+ for b, on := range first {
876
+ if on {
877
+ count, only = count+1, b
878
+ high = high || b >= utf8.RuneSelf
879
+ }
880
+ }
881
+ if count == 1 && !high {
882
+ in.next = int16(only)
883
+ }
884
+ if !in.greedy || high {
885
+ return
886
+ }
887
+ for b, on := range in.set.ascii {
888
+ if on && first[b] {
889
+ return
890
+ }
891
+ }
892
+ in.possessive = true
893
+ }
894
+
895
+ // A lazy loop's exit at inst at: tested against the bytes tail can start with
896
+ func (c *tnRxCompiler) lazyExit(at int32, tail []*tnBtNode) {
897
+ first := new([256]bool)
898
+ if len(tail) > 0 && !c.firstOfTail(tail, first) {
899
+ c.insts[at].op = tnRxSplitLazy
900
+ c.insts[at].first = first
901
+ }
902
+ }
903
+
904
+ // firstOf for what follows a position: as in firstOfLook, a $ can't match
905
+ // before a next byte (the exit test only trusts the set there)
906
+ func (c *tnRxCompiler) firstOfTail(nodes []*tnBtNode, set *[256]bool) bool {
907
+ return c.firstOfLook(&tnBtNode{op: "cat", seq: nodes}, set)
908
+ }
909
+
910
+ func tnRxConcat(a []*tnBtNode, b []*tnBtNode) []*tnBtNode {
911
+ return append(append([]*tnBtNode{}, a...), b...)
912
+ }
913
+
914
+ func (c *tnRxCompiler) exact(n *tnBtNode) bool {
915
+ if n.op != "char" {
916
+ return false
917
+ }
918
+ return !c.icase || n.ch < utf8.RuneSelf && !unicode.IsLetter(n.ch)
919
+ }
920
+
921
+ // Emits nodes; tail is what follows them (for the hints above), end says the
922
+ // match ends right after tail
923
+ func (c *tnRxCompiler) seq(nodes []*tnBtNode, tail []*tnBtNode, end bool) {
924
+ for i := 0; i < len(nodes); i++ {
925
+ n := nodes[i]
926
+ // (runs of plain characters are one literal)
927
+ if c.exact(n) {
928
+ j := i
929
+ var b strings.Builder
930
+ for j < len(nodes) && c.exact(nodes[j]) {
931
+ b.WriteRune(nodes[j].ch)
932
+ j++
933
+ }
934
+ if lit := b.String(); len(lit) == 1 {
935
+ c.emit(tnRxInst{op: tnRxChar, c: lit[0]})
936
+ } else {
937
+ c.emit(tnRxInst{op: tnRxLit, lit: lit})
938
+ }
939
+ i = j - 1
940
+ continue
941
+ }
942
+ c.node(n, tnRxConcat(nodes[i+1:], tail), end)
943
+ }
944
+ }
945
+
946
+ // A branch point: body (at pc+1) or else, which the caller patches into y
947
+ func (c *tnRxCompiler) split(body *tnBtNode, seq []*tnBtNode) int32 {
948
+ first := new([256]bool)
949
+ nullable := false
950
+ if body != nil {
951
+ nullable = c.firstOf(body, first)
952
+ } else {
953
+ nullable = c.firstOfSeq(seq, first)
954
+ }
955
+ in := tnRxInst{op: tnRxSplitFirst, first: first, x: c.here() + 1}
956
+ if nullable {
957
+ in.op = tnRxSplit
958
+ in.first = nil
959
+ }
960
+ return c.emit(in)
961
+ }
962
+
963
+ func (c *tnRxCompiler) node(n *tnBtNode, tail []*tnBtNode, end bool) {
964
+ switch n.op {
965
+ case "char", "any", "class":
966
+ c.emit(tnRxInst{op: tnRxSet, set: c.setOf(n)})
967
+ case "anchor":
968
+ switch n.anchor {
969
+ case '^':
970
+ if c.multiline {
971
+ c.emit(tnRxInst{op: tnRxBolLine})
972
+ } else {
973
+ c.emit(tnRxInst{op: tnRxBol})
974
+ }
975
+ case '$':
976
+ if c.multiline {
977
+ c.emit(tnRxInst{op: tnRxEolLine})
978
+ } else {
979
+ c.emit(tnRxInst{op: tnRxEol})
980
+ }
981
+ case 'b':
982
+ c.emit(tnRxInst{op: tnRxWordB})
983
+ default:
984
+ c.emit(tnRxInst{op: tnRxNotWordB})
985
+ }
986
+ case "cat":
987
+ c.seq(n.seq, tail, end)
988
+ case "alt":
989
+ var exits []int32
990
+ for i, br := range n.branches {
991
+ if i == len(n.branches)-1 {
992
+ c.seq(br, tail, end)
993
+ break
994
+ }
995
+ split := c.split(nil, br)
996
+ c.seq(br, tail, end)
997
+ exits = append(exits, c.emit(tnRxInst{op: tnRxJmp}))
998
+ c.insts[split].y = c.here()
999
+ }
1000
+ for _, at := range exits {
1001
+ c.insts[at].x = c.here()
1002
+ }
1003
+ case "group":
1004
+ c.emit(tnRxInst{op: tnRxSave, x: int32(n.group * 2)})
1005
+ c.node(n.child, tail, end)
1006
+ c.emit(tnRxInst{op: tnRxSave, x: int32(n.group*2 + 1)})
1007
+ case "backref":
1008
+ op := uint8(tnRxBackref)
1009
+ if c.icase {
1010
+ op = tnRxBackrefFold
1011
+ }
1012
+ c.emit(tnRxInst{op: op, x: int32(n.group * 2)})
1013
+ case "look":
1014
+ c.look(n)
1015
+ case "rep":
1016
+ c.rep(n, tail, end)
1017
+ }
1018
+ }
1019
+
1020
+ func (c *tnRxCompiler) look(n *tnBtNode) {
1021
+ if !n.behind && n.child.op == "cat" && len(n.child.seq) == 1 && tnBtIsSingle(n.child.seq[0]) {
1022
+ c.emit(tnRxInst{op: tnRxPeek, set: c.setOf(n.child.seq[0]), neg: n.negative})
1023
+ return
1024
+ }
1025
+ at := c.emit(tnRxInst{op: tnRxLook, neg: n.negative, behind: n.behind, max: int32(n.max)})
1026
+ if !n.behind {
1027
+ first := new([256]bool)
1028
+ if !c.firstOfLook(n.child, first) {
1029
+ c.insts[at].first = first
1030
+ }
1031
+ }
1032
+ c.node(n.child, nil, !n.behind)
1033
+ c.emit(tnRxInst{op: tnRxLookEnd})
1034
+ c.insts[at].y = c.here()
1035
+ }
1036
+
1037
+ // The capture slots inside a tree (lo == hi: none)
1038
+ func tnBtCaptureSlots(n *tnBtNode) (int32, int32) {
1039
+ lo, hi := int32(-1), int32(-1)
1040
+ var walk func(*tnBtNode)
1041
+ walk = func(n *tnBtNode) {
1042
+ if n == nil {
1043
+ return
1044
+ }
1045
+ if n.op == "group" {
1046
+ if lo < 0 || int32(n.group*2) < lo {
1047
+ lo = int32(n.group * 2)
1048
+ }
1049
+ if int32(n.group*2+2) > hi {
1050
+ hi = int32(n.group*2 + 2)
1051
+ }
1052
+ }
1053
+ for _, s := range n.seq {
1054
+ walk(s)
1055
+ }
1056
+ for _, b := range n.branches {
1057
+ for _, s := range b {
1058
+ walk(s)
1059
+ }
1060
+ }
1061
+ walk(n.child)
1062
+ }
1063
+ walk(n)
1064
+ return lo, hi
1065
+ }
1066
+
1067
+ func (c *tnRxCompiler) rep(n *tnBtNode, tail []*tnBtNode, end bool) {
1068
+ if n.max == 0 {
1069
+ return
1070
+ }
1071
+ if tnBtIsSingle(n.child) {
1072
+ in := tnRxInst{op: tnRxRepGreedy, greedy: n.greedy, set: c.setOf(n.child), min: int32(n.min), max: int32(n.max), next: -1}
1073
+ if !n.greedy {
1074
+ in.op = tnRxRepLazy
1075
+ }
1076
+ c.repHints(&in, tail, end)
1077
+ next := in.next
1078
+ at := c.emit(in)
1079
+ c.insts[at].next = next
1080
+ return
1081
+ }
1082
+ lo, hi := tnBtCaptureSlots(n.child)
1083
+ reset := func() {
1084
+ if hi > lo {
1085
+ c.emit(tnRxInst{op: tnRxResetCaps, x: lo, y: hi})
1086
+ }
1087
+ }
1088
+ var first [256]bool
1089
+ nullable := c.firstOf(n.child, &first)
1090
+ switch {
1091
+ case !nullable && n.min == 0 && n.max == 1:
1092
+ var at int32
1093
+ if n.greedy {
1094
+ at = c.split(n.child, nil)
1095
+ } else {
1096
+ at = c.emit(tnRxInst{op: tnRxSplit})
1097
+ c.insts[at].y = at + 1
1098
+ }
1099
+ reset()
1100
+ c.node(n.child, nil, false)
1101
+ if n.greedy {
1102
+ c.insts[at].y = c.here()
1103
+ } else {
1104
+ c.insts[at].x = c.here()
1105
+ }
1106
+ case !nullable && n.min == 0 && n.max < 0:
1107
+ var at int32
1108
+ if n.greedy {
1109
+ at = c.split(n.child, nil)
1110
+ } else {
1111
+ at = c.emit(tnRxInst{op: tnRxSplit})
1112
+ c.insts[at].y = at + 1
1113
+ }
1114
+ reset()
1115
+ c.node(n.child, nil, false)
1116
+ c.emit(tnRxInst{op: tnRxJmp, x: at})
1117
+ if n.greedy {
1118
+ c.insts[at].y = c.here()
1119
+ } else {
1120
+ c.insts[at].x = c.here()
1121
+ c.lazyExit(at, tail)
1122
+ }
1123
+ case !nullable && n.min == 1 && n.max < 0:
1124
+ loop := c.here()
1125
+ reset()
1126
+ c.node(n.child, nil, false)
1127
+ at := c.emit(tnRxInst{op: tnRxSplit, x: loop})
1128
+ if n.greedy {
1129
+ c.insts[at].y = c.here()
1130
+ } else {
1131
+ c.insts[at].x = c.here()
1132
+ c.insts[at].y = loop
1133
+ c.lazyExit(at, tail)
1134
+ }
1135
+ default:
1136
+ r := int32(c.slots)
1137
+ c.slots += 2
1138
+ c.emit(tnRxInst{op: tnRxLoopInit, x: r})
1139
+ head := c.emit(tnRxInst{op: tnRxLoopHead, x: r, min: int32(n.min), max: int32(n.max), greedy: n.greedy})
1140
+ c.emit(tnRxInst{op: tnRxLoopEnter, x: r})
1141
+ reset()
1142
+ c.node(n.child, nil, false)
1143
+ c.emit(tnRxInst{op: tnRxLoopTail, x: r, y: head, min: int32(n.min)})
1144
+ c.insts[head].y = c.here()
1145
+ }
1146
+ }
1147
+
1148
+ // ---- The pattern parser ----
1149
+
1150
+ type tnBtNode struct {
1151
+ op string
1152
+ ch rune
1153
+ cls func(rune) bool
1154
+ negCls bool
1155
+ asciiOnly bool // (a class that matches no non-ASCII rune, negation aside)
1156
+ seq []*tnBtNode
1157
+ branches [][]*tnBtNode
1158
+ child *tnBtNode
1159
+ min, max int
1160
+ greedy bool
1161
+ anchor byte
1162
+ group int
1163
+ behind bool
1164
+ negative bool
1165
+ }
1166
+
1167
+ type tnBt struct {
1168
+ root *tnBtNode
1169
+ ngroups int
1170
+ }
1171
+
1172
+ type tnBtParser struct {
1173
+ src []rune
1174
+ pos int
1175
+ ngrp int
1176
+ names map[string]int
1177
+ unicode bool
1178
+ // v flag: classes are set expressions (nesting, &&, --, \\q{…}), and with
1179
+ // icase each operand is case-closed before the set operations, as in JS
1180
+ sets bool
1181
+ icase bool
1182
+ }
1183
+
1184
+ func tnBtCompile(pattern string, unicode bool, sets bool, icase bool) *tnBt {
1185
+ p := &tnBtParser{src: []rune(pattern), unicode: unicode, sets: sets, icase: icase}
1186
+ root, err := p.parseAlt()
1187
+ if err != nil || p.pos < len(p.src) {
1188
+ return nil
1189
+ }
1190
+ return &tnBt{root: root, ngroups: p.ngrp}
1191
+ }
1192
+
1193
+ func (p *tnBtParser) peek() (rune, bool) {
1194
+ if p.pos < len(p.src) {
1195
+ return p.src[p.pos], true
1196
+ }
1197
+ return 0, false
1198
+ }
1199
+
1200
+ func (p *tnBtParser) parseAlt() (*tnBtNode, error) {
1201
+ var branches [][]*tnBtNode
1202
+ for {
1203
+ seq, err := p.parseSeq()
1204
+ if err != nil {
1205
+ return nil, err
1206
+ }
1207
+ branches = append(branches, seq)
1208
+ if ch, ok := p.peek(); ok && ch == '|' {
1209
+ p.pos++
1210
+ continue
1211
+ }
1212
+ break
1213
+ }
1214
+ if len(branches) == 1 {
1215
+ return &tnBtNode{op: "cat", seq: branches[0]}, nil
1216
+ }
1217
+ return &tnBtNode{op: "alt", branches: branches}, nil
1218
+ }
1219
+
1220
+ func (p *tnBtParser) parseSeq() ([]*tnBtNode, error) {
1221
+ var seq []*tnBtNode
1222
+ for {
1223
+ ch, ok := p.peek()
1224
+ if !ok || ch == '|' || ch == ')' {
1225
+ break
1226
+ }
1227
+ atom, err := p.parseAtom()
1228
+ if err != nil {
1229
+ return nil, err
1230
+ }
1231
+ if atom == nil {
1232
+ continue
1233
+ }
1234
+ node, err := p.parseQuantifier(atom)
1235
+ if err != nil {
1236
+ return nil, err
1237
+ }
1238
+ seq = append(seq, node)
1239
+ }
1240
+ return seq, nil
1241
+ }
1242
+
1243
+ func (p *tnBtParser) parseQuantifier(atom *tnBtNode) (*tnBtNode, error) {
1244
+ ch, ok := p.peek()
1245
+ if !ok {
1246
+ return atom, nil
1247
+ }
1248
+ lo, hi := 0, 0
1249
+ switch ch {
1250
+ case '*':
1251
+ lo, hi = 0, -1
1252
+ case '+':
1253
+ lo, hi = 1, -1
1254
+ case '?':
1255
+ lo, hi = 0, 1
1256
+ case '{':
1257
+ save := p.pos
1258
+ p.pos++
1259
+ parsedLo, parsedHi, ok2 := p.parseBraces()
1260
+ if !ok2 {
1261
+ p.pos = save
1262
+ return atom, nil
1263
+ }
1264
+ lo, hi = parsedLo, parsedHi
1265
+ default:
1266
+ return atom, nil
1267
+ }
1268
+ p.pos++
1269
+ greedy := true
1270
+ if c, ok2 := p.peek(); ok2 && c == '?' {
1271
+ greedy = false
1272
+ p.pos++
1273
+ }
1274
+ return &tnBtNode{op: "rep", child: atom, min: lo, max: hi, greedy: greedy}, nil
1275
+ }
1276
+
1277
+ // {m}, {m,}, {m,n} — returns ok=false when the brace is a literal
1278
+ func (p *tnBtParser) parseBraces() (int, int, bool) {
1279
+ lo := 0
1280
+ digits := 0
1281
+ for {
1282
+ c, ok := p.peek()
1283
+ if !ok || c < '0' || c > '9' {
1284
+ break
1285
+ }
1286
+ lo = lo*10 + int(c-'0')
1287
+ digits++
1288
+ p.pos++
1289
+ }
1290
+ if digits == 0 {
1291
+ return 0, 0, false
1292
+ }
1293
+ hi := lo
1294
+ if c, ok := p.peek(); ok && c == ',' {
1295
+ p.pos++
1296
+ hi = -1
1297
+ hv := 0
1298
+ hd := 0
1299
+ for {
1300
+ c2, ok2 := p.peek()
1301
+ if !ok2 || c2 < '0' || c2 > '9' {
1302
+ break
1303
+ }
1304
+ hv = hv*10 + int(c2-'0')
1305
+ hd++
1306
+ p.pos++
1307
+ }
1308
+ if hd > 0 {
1309
+ hi = hv
1310
+ }
1311
+ }
1312
+ if c, ok := p.peek(); !ok || c != '}' {
1313
+ return 0, 0, false
1314
+ }
1315
+ return lo, hi, true
1316
+ }
1317
+
1318
+ // JS's whitespace escape: ASCII whitespace, Unicode space separators, line terminators and the BOM
1319
+ func tnJSSpace(r rune) bool {
1320
+ switch r {
1321
+ case ' ', '\\t', '\\n', '\\r', '\\f', '\\v', 0xa0, 0x1680, 0x2028, 0x2029, 0x202f, 0x205f, 0x3000, 0xfeff:
1322
+ return true
1323
+ }
1324
+ return r >= 0x2000 && r <= 0x200a
1325
+ }
1326
+
1327
+ func tnCharClass(escape rune) func(rune) bool {
1328
+ switch escape {
1329
+ case 'd':
1330
+ return func(r rune) bool { return r >= '0' && r <= '9' }
1331
+ case 'D':
1332
+ return func(r rune) bool { return !(r >= '0' && r <= '9') }
1333
+ case 'w':
1334
+ return func(r rune) bool {
1335
+ return r == '_' || (r >= '0' && r <= '9') || (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z')
1336
+ }
1337
+ case 'W':
1338
+ return func(r rune) bool {
1339
+ return !(r == '_' || (r >= '0' && r <= '9') || (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z'))
1340
+ }
1341
+ case 's':
1342
+ return tnJSSpace
1343
+ case 'S':
1344
+ return func(r rune) bool { return !tnJSSpace(r) }
1345
+ }
1346
+ return nil
1347
+ }
1348
+
1349
+ func (p *tnBtParser) parseAtom() (*tnBtNode, error) {
1350
+ ch, ok := p.peek()
1351
+ if !ok {
1352
+ return nil, fmt.Errorf("unexpected end")
1353
+ }
1354
+ p.pos++
1355
+ switch ch {
1356
+ case '(':
1357
+ kind := "group"
1358
+ negative := false
1359
+ behind := false
1360
+ if c, ok2 := p.peek(); ok2 && c == '?' {
1361
+ p.pos++
1362
+ c2, _ := p.peek()
1363
+ switch c2 {
1364
+ case ':':
1365
+ p.pos++
1366
+ kind = "noncap"
1367
+ case '=':
1368
+ p.pos++
1369
+ kind = "look"
1370
+ case '!':
1371
+ p.pos++
1372
+ kind = "look"
1373
+ negative = true
1374
+ case '<':
1375
+ p.pos++
1376
+ c3, ok3 := p.peek()
1377
+ if !ok3 {
1378
+ return nil, fmt.Errorf("bad (?<")
1379
+ }
1380
+ if c3 == '=' || c3 == '!' {
1381
+ p.pos++
1382
+ kind = "look"
1383
+ behind = true
1384
+ negative = c3 == '!'
1385
+ } else {
1386
+ name := p.groupName()
1387
+ if name == "" {
1388
+ return nil, fmt.Errorf("unterminated group name")
1389
+ }
1390
+ if p.names == nil {
1391
+ p.names = map[string]int{}
1392
+ }
1393
+ p.names[name] = p.ngrp + 1
1394
+ }
1395
+ case 'P':
1396
+ p.pos++
1397
+ if c3, ok3 := p.peek(); ok3 && c3 == '<' {
1398
+ p.pos++
1399
+ name := p.groupName()
1400
+ if name == "" {
1401
+ return nil, fmt.Errorf("unterminated group name")
1402
+ }
1403
+ if p.names == nil {
1404
+ p.names = map[string]int{}
1405
+ }
1406
+ p.names[name] = p.ngrp + 1
1407
+ } else {
1408
+ return nil, fmt.Errorf("bad (?P")
1409
+ }
1410
+ default:
1411
+ return nil, fmt.Errorf("unsupported group")
1412
+ }
1413
+ }
1414
+ // (a group's number is its opening parenthesis's position, as in JS)
1415
+ group := 0
1416
+ if kind == "group" {
1417
+ p.ngrp++
1418
+ group = p.ngrp
1419
+ }
1420
+ body, err := p.parseAlt()
1421
+ if err != nil {
1422
+ return nil, err
1423
+ }
1424
+ if c, ok2 := p.peek(); !ok2 || c != ')' {
1425
+ return nil, fmt.Errorf("missing )")
1426
+ }
1427
+ p.pos++
1428
+ switch kind {
1429
+ case "noncap":
1430
+ return body, nil
1431
+ case "look":
1432
+ return &tnBtNode{op: "look", child: body, behind: behind, negative: negative, max: tnBtMaxRunes(body)}, nil
1433
+ default:
1434
+ return &tnBtNode{op: "group", group: group, child: body}, nil
1435
+ }
1436
+ case '[':
1437
+ return p.parseClass()
1438
+ case '.':
1439
+ return &tnBtNode{op: "any"}, nil
1440
+ case '^':
1441
+ return &tnBtNode{op: "anchor", anchor: '^'}, nil
1442
+ case '$':
1443
+ return &tnBtNode{op: "anchor", anchor: '$'}, nil
1444
+ case '\\\\':
1445
+ return p.parseEscape()
1446
+ default:
1447
+ return &tnBtNode{op: "char", ch: ch}, nil
1448
+ }
1449
+ }
1450
+
1451
+ // The name of a (?<name> group, after the '<', through the '>'
1452
+ func (p *tnBtParser) groupName() string {
1453
+ start := p.pos
1454
+ for p.pos < len(p.src) && p.src[p.pos] != '>' {
1455
+ p.pos++
1456
+ }
1457
+ if p.pos >= len(p.src) {
1458
+ return ""
1459
+ }
1460
+ name := string(p.src[start:p.pos])
1461
+ p.pos++
1462
+ return name
1463
+ }
1464
+
1465
+ // The hex digits from the cursor (n of them, or {…} when n is 0): their value
1466
+ func (p *tnBtParser) hexValue(n int) (rune, bool) {
1467
+ end := p.pos + n
1468
+ if n == 0 {
1469
+ if p.pos >= len(p.src) || p.src[p.pos] != '{' {
1470
+ return 0, false
1471
+ }
1472
+ close := p.pos + 1
1473
+ for close < len(p.src) && p.src[close] != '}' {
1474
+ close++
1475
+ }
1476
+ if close >= len(p.src) {
1477
+ return 0, false
1478
+ }
1479
+ v, err := strconv.ParseUint(string(p.src[p.pos+1:close]), 16, 32)
1480
+ if err != nil {
1481
+ return 0, false
1482
+ }
1483
+ p.pos = close + 1
1484
+ return rune(v), true
1485
+ }
1486
+ if end > len(p.src) {
1487
+ return 0, false
1488
+ }
1489
+ v, err := strconv.ParseUint(string(p.src[p.pos:end]), 16, 32)
1490
+ if err != nil {
1491
+ return 0, false
1492
+ }
1493
+ p.pos = end
1494
+ return rune(v), true
1495
+ }
1496
+
1497
+ // The character or class an escape (after its backslash) stands for, as JS
1498
+ // reads it: d w s (JS whitespace), p{…} / P{…}, xHH, x{…}, uHHHH, u{…}, cX, 0,
1499
+ // n t r f v (and b, a backspace in a class)
1500
+ func (p *tnBtParser) escapeValue(inClass bool) (rune, func(rune) bool, bool) {
1501
+ c, ok := p.peek()
1502
+ if !ok {
1503
+ return 0, nil, false
1504
+ }
1505
+ p.pos++
1506
+ switch c {
1507
+ case 'n':
1508
+ return '\\n', nil, true
1509
+ case 't':
1510
+ return '\\t', nil, true
1511
+ case 'r':
1512
+ return '\\r', nil, true
1513
+ case 'f':
1514
+ return '\\f', nil, true
1515
+ case 'v':
1516
+ return '\\v', nil, true
1517
+ case '0':
1518
+ return 0, nil, true
1519
+ case 'b':
1520
+ if inClass {
1521
+ return '\\b', nil, true
1522
+ }
1523
+ case 'x':
1524
+ if v, ok := p.hexValue(2); ok {
1525
+ return v, nil, true
1526
+ }
1527
+ if v, ok := p.hexValue(0); ok {
1528
+ return v, nil, true
1529
+ }
1530
+ return 'x', nil, true
1531
+ case 'u':
1532
+ if v, ok := p.hexValue(4); ok {
1533
+ return v, nil, true
1534
+ }
1535
+ if v, ok := p.hexValue(0); ok {
1536
+ return v, nil, true
1537
+ }
1538
+ return 'u', nil, true
1539
+ case 'c':
1540
+ if n, ok := p.peek(); ok && ((n >= 'a' && n <= 'z') || (n >= 'A' && n <= 'Z')) {
1541
+ p.pos++
1542
+ return n % 32, nil, true
1543
+ }
1544
+ return '\\\\', nil, true
1545
+ case 'p', 'P':
1546
+ if n, ok := p.peek(); ok && n == '{' && p.unicode {
1547
+ close := p.pos + 1
1548
+ for close < len(p.src) && p.src[close] != '}' {
1549
+ close++
1550
+ }
1551
+ if close < len(p.src) {
1552
+ if table := tnUnicodeProperty(string(p.src[p.pos+1 : close])); table != nil {
1553
+ p.pos = close + 1
1554
+ if c == 'P' {
1555
+ return 0, func(r rune) bool { return !unicode.Is(table, r) }, true
1556
+ }
1557
+ return 0, func(r rune) bool { return unicode.Is(table, r) }, true
1558
+ }
1559
+ }
1560
+ }
1561
+ }
1562
+ if cls := tnCharClass(c); cls != nil {
1563
+ return 0, cls, true
1564
+ }
1565
+ return c, nil, true
1566
+ }
1567
+
1568
+ // A p{…} property's table: a general category (L, Lu, P, Letter…), a
1569
+ // script (Greek, Script=Greek) or a binary property
1570
+ func tnUnicodeProperty(name string) *unicode.RangeTable {
1571
+ if eq := strings.IndexByte(name, '='); eq >= 0 {
1572
+ name = name[eq+1:]
1573
+ }
1574
+ long := map[string]string{
1575
+ "Letter": "L", "Lowercase_Letter": "Ll", "Uppercase_Letter": "Lu", "Titlecase_Letter": "Lt", "Modifier_Letter": "Lm", "Other_Letter": "Lo",
1576
+ "Mark": "M", "Number": "N", "Decimal_Number": "Nd", "Letter_Number": "Nl", "Other_Number": "No",
1577
+ "Punctuation": "P", "Connector_Punctuation": "Pc", "Dash_Punctuation": "Pd", "Open_Punctuation": "Ps", "Close_Punctuation": "Pe",
1578
+ "Initial_Punctuation": "Pi", "Final_Punctuation": "Pf", "Other_Punctuation": "Po",
1579
+ "Symbol": "S", "Math_Symbol": "Sm", "Currency_Symbol": "Sc", "Modifier_Symbol": "Sk", "Other_Symbol": "So",
1580
+ "Separator": "Z", "Space_Separator": "Zs", "Line_Separator": "Zl", "Paragraph_Separator": "Zp",
1581
+ "Other": "C", "Control": "Cc", "Format": "Cf", "Private_Use": "Co", "Surrogate": "Cs",
1582
+ }
1583
+ if short, ok := long[name]; ok {
1584
+ name = short
1585
+ }
1586
+ if t, ok := unicode.Categories[name]; ok {
1587
+ return t
1588
+ }
1589
+ if t, ok := unicode.Scripts[name]; ok {
1590
+ return t
1591
+ }
1592
+ if t, ok := unicode.Properties[name]; ok {
1593
+ return t
1594
+ }
1595
+ return nil
1596
+ }
1597
+
1598
+ func (p *tnBtParser) parseEscape() (*tnBtNode, error) {
1599
+ c, ok := p.peek()
1600
+ if !ok {
1601
+ return nil, fmt.Errorf("trailing backslash")
1602
+ }
1603
+ switch c {
1604
+ case 'b':
1605
+ p.pos++
1606
+ return &tnBtNode{op: "anchor", anchor: 'b'}, nil
1607
+ case 'B':
1608
+ p.pos++
1609
+ return &tnBtNode{op: "anchor", anchor: 'B'}, nil
1610
+ case '1', '2', '3', '4', '5', '6', '7', '8', '9':
1611
+ p.pos++
1612
+ return &tnBtNode{op: "backref", group: int(c - '0')}, nil
1613
+ case 'k':
1614
+ // k<name>: the named group's text
1615
+ if n, ok := p.peekAt(1); ok && n == '<' {
1616
+ p.pos += 2
1617
+ name := p.groupName()
1618
+ if index, found := p.names[name]; found {
1619
+ return &tnBtNode{op: "backref", group: index}, nil
1620
+ }
1621
+ return nil, fmt.Errorf("unknown group name")
1622
+ }
1623
+ }
1624
+ ch, cls, ok := p.escapeValue(false)
1625
+ if !ok {
1626
+ return nil, fmt.Errorf("trailing backslash")
1627
+ }
1628
+ if cls != nil {
1629
+ return &tnBtNode{op: "class", cls: cls, asciiOnly: c == 'd' || c == 'w'}, nil
1630
+ }
1631
+ return &tnBtNode{op: "char", ch: ch}, nil
1632
+ }
1633
+
1634
+ // A v-flag class's contents: the code points it matches, plus its strings of
1635
+ // zero or two-plus code points (from \\q{…})
1636
+ type tnBtClassSet struct {
1637
+ has func(rune) bool
1638
+ strs map[string]bool
1639
+ }
1640
+
1641
+ // A v-flag class (after its [) as a node: a class, or when it has strings an
1642
+ // alternation trying them longest first, then the code points, then the
1643
+ // empty string, as JS does
1644
+ func (p *tnBtParser) parseClassSetNode() (*tnBtNode, error) {
1645
+ set, negate, err := p.parseClassSet()
1646
+ if err != nil {
1647
+ return nil, err
1648
+ }
1649
+ cls := &tnBtNode{op: "class", cls: set.has, negCls: negate}
1650
+ if len(set.strs) == 0 {
1651
+ return cls, nil
1652
+ }
1653
+ var strs []string
1654
+ empty := false
1655
+ for s := range set.strs {
1656
+ if s == "" {
1657
+ empty = true
1658
+ continue
1659
+ }
1660
+ strs = append(strs, s)
1661
+ }
1662
+ // longest first; equal lengths in code point order, so programs are stable
1663
+ for i := 1; i < len(strs); i++ {
1664
+ for j := i; j > 0; j-- {
1665
+ a, b := utf8.RuneCountInString(strs[j-1]), utf8.RuneCountInString(strs[j])
1666
+ if a > b || (a == b && strs[j-1] < strs[j]) {
1667
+ break
1668
+ }
1669
+ strs[j-1], strs[j] = strs[j], strs[j-1]
1670
+ }
1671
+ }
1672
+ var branches [][]*tnBtNode
1673
+ for _, s := range strs {
1674
+ var seq []*tnBtNode
1675
+ for _, r := range s {
1676
+ seq = append(seq, &tnBtNode{op: "char", ch: r})
1677
+ }
1678
+ branches = append(branches, seq)
1679
+ }
1680
+ branches = append(branches, []*tnBtNode{cls})
1681
+ if empty {
1682
+ branches = append(branches, nil)
1683
+ }
1684
+ return &tnBtNode{op: "alt", branches: branches}, nil
1685
+ }
1686
+
1687
+ // A v-flag class's set expression, after the [ through the ]: a union
1688
+ // (operands side by side, with a-z ranges), an intersection (A&&B&&…) or a
1689
+ // subtraction (A--B--…); mixing them needs a nested class, as in JS
1690
+ func (p *tnBtParser) parseClassSet() (*tnBtClassSet, bool, error) {
1691
+ negate := false
1692
+ if c, ok := p.peek(); ok && c == '^' {
1693
+ negate = true
1694
+ p.pos++
1695
+ }
1696
+ var operands []*tnBtClassSet
1697
+ var op rune // 0: union, '&': intersection, '-': subtraction
1698
+ for {
1699
+ c, ok := p.peek()
1700
+ if !ok {
1701
+ return nil, false, fmt.Errorf("unterminated class")
1702
+ }
1703
+ if c == ']' {
1704
+ p.pos++
1705
+ break
1706
+ }
1707
+ if n, _ := p.peekAt(1); (c == '&' || c == '-') && n == c {
1708
+ if len(operands) == 0 || (op == 0 && len(operands) > 1) || (op != 0 && op != c) {
1709
+ return nil, false, fmt.Errorf("class set operators mixed")
1710
+ }
1711
+ op = c
1712
+ p.pos += 2
1713
+ operand, _, _, err := p.classSetOperand()
1714
+ if err != nil {
1715
+ return nil, false, err
1716
+ }
1717
+ operands = append(operands, operand)
1718
+ continue
1719
+ }
1720
+ if op != 0 {
1721
+ return nil, false, fmt.Errorf("class set operand without an operator")
1722
+ }
1723
+ operand, lo, single, err := p.classSetOperand()
1724
+ if err != nil {
1725
+ return nil, false, err
1726
+ }
1727
+ // a range: one character, a single -, another character
1728
+ if n, _ := p.peekAt(1); single && n != '-' {
1729
+ if c, ok := p.peek(); ok && c == '-' {
1730
+ p.pos++
1731
+ _, hi, single2, err := p.classSetOperand()
1732
+ if err != nil {
1733
+ return nil, false, err
1734
+ }
1735
+ if !single2 || hi < lo {
1736
+ return nil, false, fmt.Errorf("bad class range")
1737
+ }
1738
+ operand = &tnBtClassSet{has: p.caseClosed(func(r rune) bool { return r >= lo && r <= hi })}
1739
+ }
1740
+ }
1741
+ operands = append(operands, operand)
1742
+ }
1743
+ set := &tnBtClassSet{has: func(rune) bool { return false }}
1744
+ if len(operands) > 0 {
1745
+ set = operands[0]
1746
+ }
1747
+ if len(operands) > 1 {
1748
+ has := make([]func(rune) bool, len(operands))
1749
+ for i, o := range operands {
1750
+ has[i] = o.has
1751
+ }
1752
+ strs := map[string]bool{}
1753
+ switch op {
1754
+ case 0:
1755
+ set = &tnBtClassSet{has: func(r rune) bool {
1756
+ for _, h := range has {
1757
+ if h(r) {
1758
+ return true
1759
+ }
1760
+ }
1761
+ return false
1762
+ }}
1763
+ for _, o := range operands {
1764
+ for s := range o.strs {
1765
+ strs[s] = true
1766
+ }
1767
+ }
1768
+ case '&':
1769
+ set = &tnBtClassSet{has: func(r rune) bool {
1770
+ for _, h := range has {
1771
+ if !h(r) {
1772
+ return false
1773
+ }
1774
+ }
1775
+ return true
1776
+ }}
1777
+ for s := range operands[0].strs {
1778
+ all := true
1779
+ for _, o := range operands[1:] {
1780
+ all = all && o.strs[s]
1781
+ }
1782
+ if all {
1783
+ strs[s] = true
1784
+ }
1785
+ }
1786
+ default:
1787
+ first, rest := has[0], has[1:]
1788
+ set = &tnBtClassSet{has: func(r rune) bool {
1789
+ if !first(r) {
1790
+ return false
1791
+ }
1792
+ for _, h := range rest {
1793
+ if h(r) {
1794
+ return false
1795
+ }
1796
+ }
1797
+ return true
1798
+ }}
1799
+ for s := range operands[0].strs {
1800
+ kept := true
1801
+ for _, o := range operands[1:] {
1802
+ kept = kept && !o.strs[s]
1803
+ }
1804
+ if kept {
1805
+ strs[s] = true
1806
+ }
1807
+ }
1808
+ }
1809
+ set.strs = strs
1810
+ }
1811
+ if negate && len(set.strs) > 0 {
1812
+ return nil, false, fmt.Errorf("negated class with strings")
1813
+ }
1814
+ return set, negate, nil
1815
+ }
1816
+
1817
+ // One operand of a v-flag class: a nested class, a \\q{…} string list, an
1818
+ // escape or a character (single: a character, which can start or end a range)
1819
+ func (p *tnBtParser) classSetOperand() (*tnBtClassSet, rune, bool, error) {
1820
+ c, ok := p.peek()
1821
+ if !ok {
1822
+ return nil, 0, false, fmt.Errorf("unterminated class")
1823
+ }
1824
+ p.pos++
1825
+ switch c {
1826
+ case '[':
1827
+ set, negate, err := p.parseClassSet()
1828
+ if err != nil {
1829
+ return nil, 0, false, err
1830
+ }
1831
+ if negate {
1832
+ has := set.has
1833
+ set = &tnBtClassSet{has: func(r rune) bool { return !has(r) }}
1834
+ }
1835
+ return set, 0, false, nil
1836
+ case ']':
1837
+ return nil, 0, false, fmt.Errorf("missing class operand")
1838
+ case '\\\\':
1839
+ if n, _ := p.peek(); n == 'q' {
1840
+ if b, _ := p.peekAt(1); b == '{' {
1841
+ p.pos += 2
1842
+ return p.classStrings()
1843
+ }
1844
+ }
1845
+ ch, cls, ok := p.escapeValue(true)
1846
+ if !ok {
1847
+ return nil, 0, false, fmt.Errorf("trailing backslash in class")
1848
+ }
1849
+ if cls != nil {
1850
+ return &tnBtClassSet{has: p.caseClosed(cls)}, 0, false, nil
1851
+ }
1852
+ c = ch
1853
+ }
1854
+ ch := c
1855
+ return &tnBtClassSet{has: p.caseClosed(func(r rune) bool { return r == ch })}, ch, true, nil
1856
+ }
1857
+
1858
+ // \\q{a|bc|…} (after the {, through the }): one-code-point strings join the
1859
+ // code points, the rest are the class's strings
1860
+ func (p *tnBtParser) classStrings() (*tnBtClassSet, rune, bool, error) {
1861
+ var singles []rune
1862
+ strs := map[string]bool{}
1863
+ var cur []rune
1864
+ for {
1865
+ c, ok := p.peek()
1866
+ if !ok {
1867
+ return nil, 0, false, fmt.Errorf("unterminated \\\\q{")
1868
+ }
1869
+ p.pos++
1870
+ if c == '|' || c == '}' {
1871
+ if len(cur) == 1 {
1872
+ singles = append(singles, cur[0])
1873
+ } else {
1874
+ strs[string(cur)] = true
1875
+ }
1876
+ cur = nil
1877
+ if c == '}' {
1878
+ break
1879
+ }
1880
+ continue
1881
+ }
1882
+ if c == '\\\\' {
1883
+ ch, cls, ok := p.escapeValue(true)
1884
+ if !ok || cls != nil {
1885
+ return nil, 0, false, fmt.Errorf("bad escape in \\\\q{")
1886
+ }
1887
+ c = ch
1888
+ }
1889
+ cur = append(cur, c)
1890
+ }
1891
+ in := func(r rune) bool {
1892
+ for _, s := range singles {
1893
+ if r == s {
1894
+ return true
1895
+ }
1896
+ }
1897
+ return false
1898
+ }
1899
+ return &tnBtClassSet{has: p.caseClosed(in), strs: strs}, 0, false, nil
1900
+ }
1901
+
1902
+ // With the i flag, a v-flag operand matches every case of what it holds, so
1903
+ // [\\p{L}--[a-z]] leaves out A-Z too
1904
+ func (p *tnBtParser) caseClosed(in func(rune) bool) func(rune) bool {
1905
+ if !p.icase {
1906
+ return in
1907
+ }
1908
+ return func(r rune) bool { return in(r) || in(unicode.ToLower(r)) || in(unicode.ToUpper(r)) }
1909
+ }
1910
+
1911
+ func (p *tnBtParser) parseClass() (*tnBtNode, error) {
1912
+ if p.sets {
1913
+ return p.parseClassSetNode()
1914
+ }
1915
+ negate := false
1916
+ if c, ok := p.peek(); ok && c == '^' {
1917
+ negate = true
1918
+ p.pos++
1919
+ }
1920
+ var items []func(rune) bool
1921
+ asciiOnly := true
1922
+ for {
1923
+ c, ok := p.peek()
1924
+ if !ok {
1925
+ return nil, fmt.Errorf("unterminated class")
1926
+ }
1927
+ // (as in JS, a ] right at the start closes it: [] matches nothing, [^] anything)
1928
+ if c == ']' {
1929
+ p.pos++
1930
+ break
1931
+ }
1932
+ p.pos++
1933
+ var lo rune
1934
+ if c == '\\\\' {
1935
+ ch, cls, ok2 := p.escapeValue(true)
1936
+ if !ok2 {
1937
+ return nil, fmt.Errorf("trailing backslash in class")
1938
+ }
1939
+ if cls != nil {
1940
+ items = append(items, cls)
1941
+ e := p.src[p.pos-1]
1942
+ asciiOnly = asciiOnly && (e == 'd' || e == 'w')
1943
+ continue
1944
+ }
1945
+ lo = ch
1946
+ } else {
1947
+ lo = c
1948
+ }
1949
+ // range?
1950
+ if c3, ok3 := p.peek(); ok3 && c3 == '-' {
1951
+ if c4, ok4 := p.peekAt(1); ok4 && c4 != ']' {
1952
+ p.pos++ // consume '-'
1953
+ hiChar, _ := p.peek()
1954
+ p.pos++
1955
+ hi := hiChar
1956
+ if hiChar == '\\\\' {
1957
+ hc, _, ok5 := p.escapeValue(true)
1958
+ if !ok5 {
1959
+ return nil, fmt.Errorf("trailing backslash in class")
1960
+ }
1961
+ hi = hc
1962
+ }
1963
+ l, h := lo, hi
1964
+ asciiOnly = asciiOnly && h < utf8.RuneSelf
1965
+ items = append(items, func(r rune) bool { return r >= l && r <= h })
1966
+ continue
1967
+ }
1968
+ }
1969
+ l := lo
1970
+ asciiOnly = asciiOnly && l < utf8.RuneSelf
1971
+ items = append(items, func(r rune) bool { return r == l })
1972
+ }
1973
+ matchers := items
1974
+ return &tnBtNode{op: "class", cls: func(r rune) bool {
1975
+ for _, m := range matchers {
1976
+ if m(r) {
1977
+ return true
1978
+ }
1979
+ }
1980
+ return false
1981
+ }, negCls: negate, asciiOnly: asciiOnly}, nil
1982
+ }
1983
+
1984
+ func (p *tnBtParser) peekAt(offset int) (rune, bool) {
1985
+ if p.pos+offset < len(p.src) {
1986
+ return p.src[p.pos+offset], true
1987
+ }
1988
+ return 0, false
1989
+ }
1990
+
1991
+ // Line terminators for . and multiline ^ $ (as JS defines them)
1992
+ func tnBtLineEnd(r rune) bool {
1993
+ return r == '\\n' || r == '\\r' || r == 0x2028 || r == 0x2029
1994
+ }
1995
+
1996
+ // The most runes a node can match (-1: no bound), for lookbehinds
1997
+ func tnBtMaxRunes(n *tnBtNode) int {
1998
+ switch n.op {
1999
+ case "char", "any", "class":
2000
+ return 1
2001
+ case "anchor", "look":
2002
+ return 0
2003
+ case "cat":
2004
+ total := 0
2005
+ for _, c := range n.seq {
2006
+ w := tnBtMaxRunes(c)
2007
+ if w < 0 {
2008
+ return -1
2009
+ }
2010
+ total += w
2011
+ }
2012
+ return total
2013
+ case "alt":
2014
+ most := 0
2015
+ for _, br := range n.branches {
2016
+ w := tnBtMaxRunes(&tnBtNode{op: "cat", seq: br})
2017
+ if w < 0 {
2018
+ return -1
2019
+ }
2020
+ if w > most {
2021
+ most = w
2022
+ }
2023
+ }
2024
+ return most
2025
+ case "group":
2026
+ return tnBtMaxRunes(n.child)
2027
+ case "rep":
2028
+ w := tnBtMaxRunes(n.child)
2029
+ if w < 0 || n.max < 0 {
2030
+ return -1
2031
+ }
2032
+ return w * n.max
2033
+ }
2034
+ return -1
2035
+ }
2036
+
2037
+ // Whether a pattern only matches at the input's start (it begins with ^)
2038
+ func tnBtStartAnchored(n *tnBtNode) bool {
2039
+ switch n.op {
2040
+ case "anchor":
2041
+ return n.anchor == '^'
2042
+ case "cat":
2043
+ return len(n.seq) > 0 && tnBtStartAnchored(n.seq[0])
2044
+ case "group":
2045
+ return tnBtStartAnchored(n.child)
2046
+ case "alt":
2047
+ for _, br := range n.branches {
2048
+ if len(br) == 0 || !tnBtStartAnchored(br[0]) {
2049
+ return false
2050
+ }
2051
+ }
2052
+ return len(n.branches) > 0
2053
+ }
2054
+ return false
2055
+ }
2056
+ `;
2057
+ export const REGEX_ENGINE_HELPER = REGEX_VM;