typenative 0.5.0 → 0.6.1

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