3 import "exp/regexp/syntax"
5 // A queue is a 'sparse array' holding pending threads of execution.
6 // See http://research.swtch.com/2008/03/using-uninitialized-memory-for-fun-and.html
12 // A entry is an entry on a queue.
13 // It holds both the instruction pc and the actual thread.
14 // Some queue entries are just place holders so that the machine
15 // knows it has considered that pc. Such entries have t == nil.
21 // A thread is the state of a single path through the machine:
22 // an instruction and a corresponding capture array.
23 // See http://swtch.com/~rsc/regexp/regexp2.html
29 // A machine holds all the state during an NFA simulation for p.
31 re *Regexp // corresponding Regexp
32 p *syntax.Prog // compiled program
33 q0, q1 queue // two queues for runq, nextq
34 pool []*thread // pool of available threads
35 matched bool // whether a match was found
36 matchcap []int // capture information for the match
39 // progMachine returns a new machine running the prog p.
40 func progMachine(p *syntax.Prog) *machine {
43 m.q0 = queue{make([]uint32, n), make([]entry, 0, n)}
44 m.q1 = queue{make([]uint32, n), make([]entry, 0, n)}
49 m.matchcap = make([]int, ncap)
53 // alloc allocates a new thread with the given instruction.
54 // It uses the free pool if possible.
55 func (m *machine) alloc(i *syntax.Inst) *thread {
57 if n := len(m.pool); n > 0 {
62 t.cap = make([]int, cap(m.matchcap))
64 t.cap = t.cap[:len(m.matchcap)]
69 // free returns t to the free pool.
70 func (m *machine) free(t *thread) {
71 m.pool = append(m.pool, t)
74 // match runs the machine over the input starting at pos.
75 // It reports whether a match was found.
76 // If so, m.matchcap holds the submatch information.
77 func (m *machine) match(i input, pos int) bool {
78 startCond := m.re.cond
79 if startCond == ^syntax.EmptyOp(0) { // impossible
83 for i := range m.matchcap {
86 runq, nextq := &m.q0, &m.q1
87 rune, rune1 := endOfText, endOfText
89 rune, width = i.step(pos)
90 if rune != endOfText {
91 rune1, width1 = i.step(pos + width)
93 // TODO: Let caller specify the initial flag setting.
94 // For now assume pos == 0 is beginning of text and
95 // pos != 0 is not even beginning of line.
96 // TODO: Word boundary.
97 var flag syntax.EmptyOp
99 flag = syntax.EmptyBeginText | syntax.EmptyBeginLine
102 // Update flag using lookahead rune.
104 flag |= syntax.EmptyEndLine
106 if rune1 == endOfText {
107 flag |= syntax.EmptyEndText
111 if len(runq.dense) == 0 {
112 if startCond&syntax.EmptyBeginText != 0 && pos != 0 {
113 // Anchored match, past beginning of text.
117 // Have match; finished exploring alternatives.
120 if len(m.re.prefix) > 0 && rune1 != m.re.prefixRune && i.canCheckPrefix() {
121 // Match requires literal prefix; fast search for it.
122 advance := i.index(m.re, pos)
127 rune, width = i.step(pos)
128 rune1, width1 = i.step(pos + width)
132 if len(m.matchcap) > 0 {
135 m.add(runq, uint32(m.p.Start), pos, m.matchcap, flag)
137 // TODO: word boundary
140 flag |= syntax.EmptyBeginLine
143 flag |= syntax.EmptyEndLine
145 if rune1 == endOfText {
146 flag |= syntax.EmptyEndText
148 m.step(runq, nextq, pos, pos+width, rune, flag)
153 rune, width = rune1, width1
154 if rune != endOfText {
155 rune1, width1 = i.step(pos + width)
157 runq, nextq = nextq, runq
163 // clear frees all threads on the thread queue.
164 func (m *machine) clear(q *queue) {
165 for _, d := range q.dense {
170 q.dense = q.dense[:0]
173 // step executes one step of the machine, running each of the threads
174 // on runq and appending new threads to nextq.
175 // The step processes the rune c (which may be endOfText),
176 // which starts at position pos and ends at nextPos.
177 // nextCond gives the setting for the empty-width flags after c.
178 func (m *machine) step(runq, nextq *queue, pos, nextPos, c int, nextCond syntax.EmptyOp) {
179 for j := 0; j < len(runq.dense); j++ {
186 * If we support leftmost-longest matching:
187 if longest && matched && match[0] < t.cap[0] {
198 case syntax.InstMatch:
201 copy(m.matchcap, t.cap)
204 for _, d := range runq.dense[j+1:] {
209 runq.dense = runq.dense[:0]
211 case syntax.InstRune:
213 m.add(nextq, i.Out, nextPos, t.cap, nextCond)
218 runq.dense = runq.dense[:0]
221 // add adds an entry to q for pc, unless the q already has such an entry.
222 // It also recursively adds an entry for all instructions reachable from pc by following
223 // empty-width conditions satisfied by cond. pos gives the current position
225 func (m *machine) add(q *queue, pc uint32, pos int, cap []int, cond syntax.EmptyOp) {
229 if j := q.sparse[pc]; j < uint32(len(q.dense)) && q.dense[j].pc == pc {
234 q.dense = q.dense[:j+1]
238 q.sparse[pc] = uint32(j)
244 case syntax.InstFail:
246 case syntax.InstAlt, syntax.InstAltMatch:
247 m.add(q, i.Out, pos, cap, cond)
248 m.add(q, i.Arg, pos, cap, cond)
249 case syntax.InstEmptyWidth:
250 if syntax.EmptyOp(i.Arg)&^cond == 0 {
251 m.add(q, i.Out, pos, cap, cond)
254 m.add(q, i.Out, pos, cap, cond)
255 case syntax.InstCapture:
256 if int(i.Arg) < len(cap) {
259 m.add(q, i.Out, pos, cap, cond)
262 m.add(q, i.Out, pos, cap, cond)
264 case syntax.InstMatch, syntax.InstRune:
273 // empty is a non-nil 0-element slice,
274 // so doExecute can avoid an allocation
275 // when 0 captures are requested from a successful match.
276 var empty = make([]int, 0)
278 // doExecute finds the leftmost match in the input and returns
279 // the position of its subexpressions.
280 func (re *Regexp) doExecute(i input, pos int, ncap int) []int {
282 m.matchcap = m.matchcap[:ncap]
283 if !m.match(i, pos) {
289 return empty // empty but not nil
291 cap := make([]int, ncap)
292 copy(cap, m.matchcap)