8 // A queue is a 'sparse array' holding pending threads of execution.
9 // See http://research.swtch.com/2008/03/using-uninitialized-memory-for-fun-and.html
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.
24 // A thread is the state of a single path through the machine:
25 // an instruction and a corresponding capture array.
26 // See http://swtch.com/~rsc/regexp/regexp2.html
32 // A machine holds all the state during an NFA simulation for p.
34 re *Regexp // corresponding Regexp
35 p *syntax.Prog // compiled program
36 q0, q1 queue // two queues for runq, nextq
37 pool []*thread // pool of available threads
38 matched bool // whether a match was found
39 matchcap []int // capture information for the match
41 // cached inputs, to avoid allocation
43 inputString inputString
44 inputReader inputReader
47 func (m *machine) newInputBytes(b []byte) input {
52 func (m *machine) newInputString(s string) input {
57 func (m *machine) newInputReader(r io.RuneReader) input {
59 m.inputReader.atEOT = false
64 // progMachine returns a new machine running the prog p.
65 func progMachine(p *syntax.Prog) *machine {
68 m.q0 = queue{make([]uint32, n), make([]entry, 0, n)}
69 m.q1 = queue{make([]uint32, n), make([]entry, 0, n)}
74 m.matchcap = make([]int, ncap)
78 func (m *machine) init(ncap int) {
79 for _, t := range m.pool {
82 m.matchcap = m.matchcap[:ncap]
85 // alloc allocates a new thread with the given instruction.
86 // It uses the free pool if possible.
87 func (m *machine) alloc(i *syntax.Inst) *thread {
89 if n := len(m.pool); n > 0 {
94 t.cap = make([]int, len(m.matchcap), cap(m.matchcap))
100 // free returns t to the free pool.
101 func (m *machine) free(t *thread) {
102 m.inputBytes.str = nil
103 m.inputString.str = ""
104 m.inputReader.r = nil
105 m.pool = append(m.pool, t)
108 // match runs the machine over the input starting at pos.
109 // It reports whether a match was found.
110 // If so, m.matchcap holds the submatch information.
111 func (m *machine) match(i input, pos int) bool {
112 startCond := m.re.cond
113 if startCond == ^syntax.EmptyOp(0) { // impossible
117 for i := range m.matchcap {
120 runq, nextq := &m.q0, &m.q1
121 r, r1 := endOfText, endOfText
122 width, width1 := 0, 0
123 r, width = i.step(pos)
125 r1, width1 = i.step(pos + width)
127 var flag syntax.EmptyOp
129 flag = syntax.EmptyOpContext(-1, r)
131 flag = i.context(pos)
134 if len(runq.dense) == 0 {
135 if startCond&syntax.EmptyBeginText != 0 && pos != 0 {
136 // Anchored match, past beginning of text.
140 // Have match; finished exploring alternatives.
143 if len(m.re.prefix) > 0 && r1 != m.re.prefixRune && i.canCheckPrefix() {
144 // Match requires literal prefix; fast search for it.
145 advance := i.index(m.re, pos)
150 r, width = i.step(pos)
151 r1, width1 = i.step(pos + width)
155 if len(m.matchcap) > 0 {
158 m.add(runq, uint32(m.p.Start), pos, m.matchcap, flag, nil)
160 flag = syntax.EmptyOpContext(r, r1)
161 m.step(runq, nextq, pos, pos+width, r, flag)
165 if len(m.matchcap) == 0 && m.matched {
166 // Found a match and not paying attention
167 // to where it is, so any match will do.
171 r, width = r1, width1
173 r1, width1 = i.step(pos + width)
175 runq, nextq = nextq, runq
181 // clear frees all threads on the thread queue.
182 func (m *machine) clear(q *queue) {
183 for _, d := range q.dense {
186 m.pool = append(m.pool, d.t)
189 q.dense = q.dense[:0]
192 // step executes one step of the machine, running each of the threads
193 // on runq and appending new threads to nextq.
194 // The step processes the rune c (which may be endOfText),
195 // which starts at position pos and ends at nextPos.
196 // nextCond gives the setting for the empty-width flags after c.
197 func (m *machine) step(runq, nextq *queue, pos, nextPos int, c rune, nextCond syntax.EmptyOp) {
198 longest := m.re.longest
199 for j := 0; j < len(runq.dense); j++ {
205 if longest && m.matched && len(t.cap) > 0 && m.matchcap[0] < t.cap[0] {
207 m.pool = append(m.pool, t)
216 case syntax.InstMatch:
217 if len(t.cap) > 0 && (!longest || !m.matched || m.matchcap[1] < pos) {
219 copy(m.matchcap, t.cap)
222 // First-match mode: cut off all lower-priority threads.
223 for _, d := range runq.dense[j+1:] {
226 m.pool = append(m.pool, d.t)
229 runq.dense = runq.dense[:0]
233 case syntax.InstRune:
235 case syntax.InstRune1:
237 case syntax.InstRuneAny:
239 case syntax.InstRuneAnyNotNL:
243 t = m.add(nextq, i.Out, nextPos, t.cap, nextCond, t)
247 m.pool = append(m.pool, t)
250 runq.dense = runq.dense[:0]
253 // add adds an entry to q for pc, unless the q already has such an entry.
254 // It also recursively adds an entry for all instructions reachable from pc by following
255 // empty-width conditions satisfied by cond. pos gives the current position
257 func (m *machine) add(q *queue, pc uint32, pos int, cap []int, cond syntax.EmptyOp, t *thread) *thread {
261 if j := q.sparse[pc]; j < uint32(len(q.dense)) && q.dense[j].pc == pc {
266 q.dense = q.dense[:j+1]
270 q.sparse[pc] = uint32(j)
276 case syntax.InstFail:
278 case syntax.InstAlt, syntax.InstAltMatch:
279 t = m.add(q, i.Out, pos, cap, cond, t)
280 t = m.add(q, i.Arg, pos, cap, cond, t)
281 case syntax.InstEmptyWidth:
282 if syntax.EmptyOp(i.Arg)&^cond == 0 {
283 t = m.add(q, i.Out, pos, cap, cond, t)
286 t = m.add(q, i.Out, pos, cap, cond, t)
287 case syntax.InstCapture:
288 if int(i.Arg) < len(cap) {
291 m.add(q, i.Out, pos, cap, cond, nil)
294 t = m.add(q, i.Out, pos, cap, cond, t)
296 case syntax.InstMatch, syntax.InstRune, syntax.InstRune1, syntax.InstRuneAny, syntax.InstRuneAnyNotNL:
302 if len(cap) > 0 && &t.cap[0] != &cap[0] {
311 // empty is a non-nil 0-element slice,
312 // so doExecute can avoid an allocation
313 // when 0 captures are requested from a successful match.
314 var empty = make([]int, 0)
316 // doExecute finds the leftmost match in the input and returns
317 // the position of its subexpressions.
318 func (re *Regexp) doExecute(r io.RuneReader, b []byte, s string, pos int, ncap int) []int {
322 i = m.newInputReader(r)
324 i = m.newInputBytes(b)
326 i = m.newInputString(s)
329 if !m.match(i, pos) {
335 return empty // empty but not nil
337 cap := make([]int, ncap)
338 copy(cap, m.matchcap)