exec.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package regexp
  5. import (
  6. "io"
  7. "regexp/syntax"
  8. )
  9. // A queue is a 'sparse array' holding pending threads of execution.
  10. // See http://research.swtch.com/2008/03/using-uninitialized-memory-for-fun-and.html
  11. type queue struct {
  12. sparse []uint32
  13. dense []entry
  14. }
  15. // A entry is an entry on a queue.
  16. // It holds both the instruction pc and the actual thread.
  17. // Some queue entries are just place holders so that the machine
  18. // knows it has considered that pc. Such entries have t == nil.
  19. type entry struct {
  20. pc uint32
  21. t *thread
  22. }
  23. // A thread is the state of a single path through the machine:
  24. // an instruction and a corresponding capture array.
  25. // See http://swtch.com/~rsc/regexp/regexp2.html
  26. type thread struct {
  27. inst *syntax.Inst
  28. cap []int
  29. }
  30. // A machine holds all the state during an NFA simulation for p.
  31. type machine struct {
  32. re *Regexp // corresponding Regexp
  33. p *syntax.Prog // compiled program
  34. op *onePassProg // compiled onepass program, or notOnePass
  35. q0, q1 queue // two queues for runq, nextq
  36. pool []*thread // pool of available threads
  37. matched bool // whether a match was found
  38. matchcap []int // capture information for the match
  39. // cached inputs, to avoid allocation
  40. inputBytes inputBytes
  41. inputString inputString
  42. inputReader inputReader
  43. }
  44. func (m *machine) newInputBytes(b []byte) input {
  45. m.inputBytes.str = b
  46. return &m.inputBytes
  47. }
  48. func (m *machine) newInputString(s string) input {
  49. m.inputString.str = s
  50. return &m.inputString
  51. }
  52. func (m *machine) newInputReader(r io.RuneReader) input {
  53. m.inputReader.r = r
  54. m.inputReader.atEOT = false
  55. m.inputReader.pos = 0
  56. return &m.inputReader
  57. }
  58. // progMachine returns a new machine running the prog p.
  59. func progMachine(p *syntax.Prog, op *onePassProg) *machine {
  60. m := &machine{p: p, op: op}
  61. n := len(m.p.Inst)
  62. m.q0 = queue{make([]uint32, n), make([]entry, 0, n)}
  63. m.q1 = queue{make([]uint32, n), make([]entry, 0, n)}
  64. ncap := p.NumCap
  65. if ncap < 2 {
  66. ncap = 2
  67. }
  68. m.matchcap = make([]int, ncap)
  69. return m
  70. }
  71. func (m *machine) init(ncap int) {
  72. for _, t := range m.pool {
  73. t.cap = t.cap[:ncap]
  74. }
  75. m.matchcap = m.matchcap[:ncap]
  76. }
  77. // alloc allocates a new thread with the given instruction.
  78. // It uses the free pool if possible.
  79. func (m *machine) alloc(i *syntax.Inst) *thread {
  80. var t *thread
  81. if n := len(m.pool); n > 0 {
  82. t = m.pool[n-1]
  83. m.pool = m.pool[:n-1]
  84. } else {
  85. t = new(thread)
  86. t.cap = make([]int, len(m.matchcap), cap(m.matchcap))
  87. }
  88. t.inst = i
  89. return t
  90. }
  91. // free returns t to the free pool.
  92. func (m *machine) free(t *thread) {
  93. m.inputBytes.str = nil
  94. m.inputString.str = ""
  95. m.inputReader.r = nil
  96. m.pool = append(m.pool, t)
  97. }
  98. // match runs the machine over the input starting at pos.
  99. // It reports whether a match was found.
  100. // If so, m.matchcap holds the submatch information.
  101. func (m *machine) match(i input, pos int) bool {
  102. startCond := m.re.cond
  103. if startCond == ^syntax.EmptyOp(0) { // impossible
  104. return false
  105. }
  106. m.matched = false
  107. for i := range m.matchcap {
  108. m.matchcap[i] = -1
  109. }
  110. runq, nextq := &m.q0, &m.q1
  111. r, r1 := endOfText, endOfText
  112. width, width1 := 0, 0
  113. r, width = i.step(pos)
  114. if r != endOfText {
  115. r1, width1 = i.step(pos + width)
  116. }
  117. var flag syntax.EmptyOp
  118. if pos == 0 {
  119. flag = syntax.EmptyOpContext(-1, r)
  120. } else {
  121. flag = i.context(pos)
  122. }
  123. for {
  124. if len(runq.dense) == 0 {
  125. if startCond&syntax.EmptyBeginText != 0 && pos != 0 {
  126. // Anchored match, past beginning of text.
  127. break
  128. }
  129. if m.matched {
  130. // Have match; finished exploring alternatives.
  131. break
  132. }
  133. if len(m.re.prefix) > 0 && r1 != m.re.prefixRune && i.canCheckPrefix() {
  134. // Match requires literal prefix; fast search for it.
  135. advance := i.index(m.re, pos)
  136. if advance < 0 {
  137. break
  138. }
  139. pos += advance
  140. r, width = i.step(pos)
  141. r1, width1 = i.step(pos + width)
  142. }
  143. }
  144. if !m.matched {
  145. if len(m.matchcap) > 0 {
  146. m.matchcap[0] = pos
  147. }
  148. m.add(runq, uint32(m.p.Start), pos, m.matchcap, flag, nil)
  149. }
  150. flag = syntax.EmptyOpContext(r, r1)
  151. m.step(runq, nextq, pos, pos+width, r, flag)
  152. if width == 0 {
  153. break
  154. }
  155. if len(m.matchcap) == 0 && m.matched {
  156. // Found a match and not paying attention
  157. // to where it is, so any match will do.
  158. break
  159. }
  160. pos += width
  161. r, width = r1, width1
  162. if r != endOfText {
  163. r1, width1 = i.step(pos + width)
  164. }
  165. runq, nextq = nextq, runq
  166. }
  167. m.clear(nextq)
  168. return m.matched
  169. }
  170. // clear frees all threads on the thread queue.
  171. func (m *machine) clear(q *queue) {
  172. for _, d := range q.dense {
  173. if d.t != nil {
  174. // m.free(d.t)
  175. m.pool = append(m.pool, d.t)
  176. }
  177. }
  178. q.dense = q.dense[:0]
  179. }
  180. // step executes one step of the machine, running each of the threads
  181. // on runq and appending new threads to nextq.
  182. // The step processes the rune c (which may be endOfText),
  183. // which starts at position pos and ends at nextPos.
  184. // nextCond gives the setting for the empty-width flags after c.
  185. func (m *machine) step(runq, nextq *queue, pos, nextPos int, c rune, nextCond syntax.EmptyOp) {
  186. longest := m.re.longest
  187. for j := 0; j < len(runq.dense); j++ {
  188. d := &runq.dense[j]
  189. t := d.t
  190. if t == nil {
  191. continue
  192. }
  193. if longest && m.matched && len(t.cap) > 0 && m.matchcap[0] < t.cap[0] {
  194. // m.free(t)
  195. m.pool = append(m.pool, t)
  196. continue
  197. }
  198. i := t.inst
  199. add := false
  200. switch i.Op {
  201. default:
  202. panic("bad inst")
  203. case syntax.InstMatch:
  204. if len(t.cap) > 0 && (!longest || !m.matched || m.matchcap[1] < pos) {
  205. t.cap[1] = pos
  206. copy(m.matchcap, t.cap)
  207. }
  208. if !longest {
  209. // First-match mode: cut off all lower-priority threads.
  210. for _, d := range runq.dense[j+1:] {
  211. if d.t != nil {
  212. // m.free(d.t)
  213. m.pool = append(m.pool, d.t)
  214. }
  215. }
  216. runq.dense = runq.dense[:0]
  217. }
  218. m.matched = true
  219. case syntax.InstRune:
  220. add = i.MatchRune(c)
  221. case syntax.InstRune1:
  222. add = c == i.Rune[0]
  223. case syntax.InstRuneAny:
  224. add = true
  225. case syntax.InstRuneAnyNotNL:
  226. add = c != '\n'
  227. }
  228. if add {
  229. t = m.add(nextq, i.Out, nextPos, t.cap, nextCond, t)
  230. }
  231. if t != nil {
  232. // m.free(t)
  233. m.pool = append(m.pool, t)
  234. }
  235. }
  236. runq.dense = runq.dense[:0]
  237. }
  238. // add adds an entry to q for pc, unless the q already has such an entry.
  239. // It also recursively adds an entry for all instructions reachable from pc by following
  240. // empty-width conditions satisfied by cond. pos gives the current position
  241. // in the input.
  242. func (m *machine) add(q *queue, pc uint32, pos int, cap []int, cond syntax.EmptyOp, t *thread) *thread {
  243. if pc == 0 {
  244. return t
  245. }
  246. if j := q.sparse[pc]; j < uint32(len(q.dense)) && q.dense[j].pc == pc {
  247. return t
  248. }
  249. j := len(q.dense)
  250. q.dense = q.dense[:j+1]
  251. d := &q.dense[j]
  252. d.t = nil
  253. d.pc = pc
  254. q.sparse[pc] = uint32(j)
  255. i := &m.p.Inst[pc]
  256. switch i.Op {
  257. default:
  258. panic("unhandled")
  259. case syntax.InstFail:
  260. // nothing
  261. case syntax.InstAlt, syntax.InstAltMatch:
  262. t = m.add(q, i.Out, pos, cap, cond, t)
  263. t = m.add(q, i.Arg, pos, cap, cond, t)
  264. case syntax.InstEmptyWidth:
  265. if syntax.EmptyOp(i.Arg)&^cond == 0 {
  266. t = m.add(q, i.Out, pos, cap, cond, t)
  267. }
  268. case syntax.InstNop:
  269. t = m.add(q, i.Out, pos, cap, cond, t)
  270. case syntax.InstCapture:
  271. if int(i.Arg) < len(cap) {
  272. opos := cap[i.Arg]
  273. cap[i.Arg] = pos
  274. m.add(q, i.Out, pos, cap, cond, nil)
  275. cap[i.Arg] = opos
  276. } else {
  277. t = m.add(q, i.Out, pos, cap, cond, t)
  278. }
  279. case syntax.InstMatch, syntax.InstRune, syntax.InstRune1, syntax.InstRuneAny, syntax.InstRuneAnyNotNL:
  280. if t == nil {
  281. t = m.alloc(i)
  282. } else {
  283. t.inst = i
  284. }
  285. if len(cap) > 0 && &t.cap[0] != &cap[0] {
  286. copy(t.cap, cap)
  287. }
  288. d.t = t
  289. t = nil
  290. }
  291. return t
  292. }
  293. // onepass runs the machine over the input starting at pos.
  294. // It reports whether a match was found.
  295. // If so, m.matchcap holds the submatch information.
  296. func (m *machine) onepass(i input, pos int) bool {
  297. startCond := m.re.cond
  298. if startCond == ^syntax.EmptyOp(0) { // impossible
  299. return false
  300. }
  301. m.matched = false
  302. for i := range m.matchcap {
  303. m.matchcap[i] = -1
  304. }
  305. r, r1 := endOfText, endOfText
  306. width, width1 := 0, 0
  307. r, width = i.step(pos)
  308. if r != endOfText {
  309. r1, width1 = i.step(pos + width)
  310. }
  311. var flag syntax.EmptyOp
  312. if pos == 0 {
  313. flag = syntax.EmptyOpContext(-1, r)
  314. } else {
  315. flag = i.context(pos)
  316. }
  317. pc := m.op.Start
  318. inst := m.op.Inst[pc]
  319. // If there is a simple literal prefix, skip over it.
  320. if pos == 0 && syntax.EmptyOp(inst.Arg)&^flag == 0 &&
  321. len(m.re.prefix) > 0 && i.canCheckPrefix() {
  322. // Match requires literal prefix; fast search for it.
  323. if i.hasPrefix(m.re) {
  324. pos += len(m.re.prefix)
  325. r, width = i.step(pos)
  326. r1, width1 = i.step(pos + width)
  327. flag = i.context(pos)
  328. pc = int(m.re.prefixEnd)
  329. } else {
  330. return m.matched
  331. }
  332. }
  333. for {
  334. inst = m.op.Inst[pc]
  335. pc = int(inst.Out)
  336. switch inst.Op {
  337. default:
  338. panic("bad inst")
  339. case syntax.InstMatch:
  340. m.matched = true
  341. if len(m.matchcap) > 0 {
  342. m.matchcap[0] = 0
  343. m.matchcap[1] = pos
  344. }
  345. return m.matched
  346. case syntax.InstRune:
  347. if !inst.MatchRune(r) {
  348. return m.matched
  349. }
  350. case syntax.InstRune1:
  351. if r != inst.Rune[0] {
  352. return m.matched
  353. }
  354. case syntax.InstRuneAny:
  355. // Nothing
  356. case syntax.InstRuneAnyNotNL:
  357. if r == '\n' {
  358. return m.matched
  359. }
  360. // peek at the input rune to see which branch of the Alt to take
  361. case syntax.InstAlt, syntax.InstAltMatch:
  362. pc = int(onePassNext(&inst, r))
  363. continue
  364. case syntax.InstFail:
  365. return m.matched
  366. case syntax.InstNop:
  367. continue
  368. case syntax.InstEmptyWidth:
  369. if syntax.EmptyOp(inst.Arg)&^flag != 0 {
  370. return m.matched
  371. }
  372. continue
  373. case syntax.InstCapture:
  374. if int(inst.Arg) < len(m.matchcap) {
  375. m.matchcap[inst.Arg] = pos
  376. }
  377. continue
  378. }
  379. if width == 0 {
  380. break
  381. }
  382. flag = syntax.EmptyOpContext(r, r1)
  383. pos += width
  384. r, width = r1, width1
  385. if r != endOfText {
  386. r1, width1 = i.step(pos + width)
  387. }
  388. }
  389. return m.matched
  390. }
  391. // empty is a non-nil 0-element slice,
  392. // so doExecute can avoid an allocation
  393. // when 0 captures are requested from a successful match.
  394. var empty = make([]int, 0)
  395. // doExecute finds the leftmost match in the input and returns
  396. // the position of its subexpressions.
  397. func (re *Regexp) doExecute(r io.RuneReader, b []byte, s string, pos int, ncap int) []int {
  398. m := re.get()
  399. var i input
  400. if r != nil {
  401. i = m.newInputReader(r)
  402. } else if b != nil {
  403. i = m.newInputBytes(b)
  404. } else {
  405. i = m.newInputString(s)
  406. }
  407. if m.op != notOnePass {
  408. if !m.onepass(i, pos) {
  409. re.put(m)
  410. return nil
  411. }
  412. } else {
  413. m.init(ncap)
  414. if !m.match(i, pos) {
  415. re.put(m)
  416. return nil
  417. }
  418. }
  419. if ncap == 0 {
  420. re.put(m)
  421. return empty // empty but not nil
  422. }
  423. cap := make([]int, len(m.matchcap))
  424. copy(cap, m.matchcap)
  425. re.put(m)
  426. return cap
  427. }