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.
12 // A queue is a 'sparse array' holding pending threads of execution.
13 // See http://research.swtch.com/2008/03/using-uninitialized-memory-for-fun-and.html
19 // A entry is an entry on a queue.
20 // It holds both the instruction pc and the actual thread.
21 // Some queue entries are just place holders so that the machine
22 // knows it has considered that pc. Such entries have t == nil.
28 // A thread is the state of a single path through the machine:
29 // an instruction and a corresponding capture array.
30 // See http://swtch.com/~rsc/regexp/regexp2.html
36 // A machine holds all the state during an NFA simulation for p.
38 re
*Regexp
// corresponding Regexp
39 p
*syntax
.Prog
// compiled program
40 q0
, q1 queue
// two queues for runq, nextq
41 pool
[]*thread
// pool of available threads
42 matched
bool // whether a match was found
43 matchcap
[]int // capture information for the match
45 // cached inputs, to avoid allocation
47 inputString inputString
48 inputReader inputReader
51 func (m
*machine
) newInputBytes(b
[]byte) input
{
56 func (m
*machine
) newInputString(s
string) input
{
61 func (m
*machine
) newInputReader(r io
.RuneReader
) input
{
63 m
.inputReader
.atEOT
= false
68 // progMachine returns a new machine running the prog p.
69 func progMachine(p
*syntax
.Prog
) *machine
{
72 m
.q0
= queue
{make([]uint32, n
), make([]entry
, 0, n
)}
73 m
.q1
= queue
{make([]uint32, n
), make([]entry
, 0, n
)}
78 m
.matchcap
= make([]int, ncap
)
82 func (m
*machine
) init(ncap
int) {
83 for _
, t
:= range m
.pool
{
86 m
.matchcap
= m
.matchcap
[:ncap
]
89 // alloc allocates a new thread with the given instruction.
90 // It uses the free pool if possible.
91 func (m
*machine
) alloc(i
*syntax
.Inst
) *thread
{
93 if n
:= len(m
.pool
); n
> 0 {
98 t
.cap = make([]int, len(m
.matchcap
), cap(m
.matchcap
))
104 // free returns t to the free pool.
105 func (m
*machine
) free(t
*thread
) {
106 m
.inputBytes
.str
= nil
107 m
.inputString
.str
= ""
108 m
.inputReader
.r
= nil
109 m
.pool
= append(m
.pool
, t
)
112 // match runs the machine over the input starting at pos.
113 // It reports whether a match was found.
114 // If so, m.matchcap holds the submatch information.
115 func (m
*machine
) match(i input
, pos
int) bool {
116 startCond
:= m
.re
.cond
117 if startCond
== ^syntax
.EmptyOp(0) { // impossible
121 for i
:= range m
.matchcap
{
124 runq
, nextq
:= &m
.q0
, &m
.q1
125 r
, r1
:= endOfText
, endOfText
126 width
, width1
:= 0, 0
127 r
, width
= i
.step(pos
)
129 r1
, width1
= i
.step(pos
+ width
)
131 var flag syntax
.EmptyOp
133 flag
= syntax
.EmptyOpContext(-1, r
)
135 flag
= i
.context(pos
)
138 if len(runq
.dense
) == 0 {
139 if startCond
&syntax
.EmptyBeginText
!= 0 && pos
!= 0 {
140 // Anchored match, past beginning of text.
144 // Have match; finished exploring alternatives.
147 if len(m
.re
.prefix
) > 0 && r1
!= m
.re
.prefixRune
&& i
.canCheckPrefix() {
148 // Match requires literal prefix; fast search for it.
149 advance
:= i
.index(m
.re
, pos
)
154 r
, width
= i
.step(pos
)
155 r1
, width1
= i
.step(pos
+ width
)
159 if len(m
.matchcap
) > 0 {
162 m
.add(runq
, uint32(m
.p
.Start
), pos
, m
.matchcap
, flag
, nil)
164 flag
= syntax
.EmptyOpContext(r
, r1
)
165 m
.step(runq
, nextq
, pos
, pos
+width
, r
, flag
)
169 if len(m
.matchcap
) == 0 && m
.matched
{
170 // Found a match and not paying attention
171 // to where it is, so any match will do.
175 r
, width
= r1
, width1
177 r1
, width1
= i
.step(pos
+ width
)
179 runq
, nextq
= nextq
, runq
185 // clear frees all threads on the thread queue.
186 func (m
*machine
) clear(q
*queue
) {
187 for _
, d
:= range q
.dense
{
190 m
.pool
= append(m
.pool
, d
.t
)
193 q
.dense
= q
.dense
[:0]
196 // step executes one step of the machine, running each of the threads
197 // on runq and appending new threads to nextq.
198 // The step processes the rune c (which may be endOfText),
199 // which starts at position pos and ends at nextPos.
200 // nextCond gives the setting for the empty-width flags after c.
201 func (m
*machine
) step(runq
, nextq
*queue
, pos
, nextPos
int, c rune
, nextCond syntax
.EmptyOp
) {
202 longest
:= m
.re
.longest
203 for j
:= 0; j
< len(runq
.dense
); j
++ {
209 if longest
&& m
.matched
&& len(t
.cap) > 0 && m
.matchcap
[0] < t
.cap[0] {
211 m
.pool
= append(m
.pool
, t
)
220 case syntax
.InstMatch
:
221 if len(t
.cap) > 0 && (!longest ||
!m
.matched || m
.matchcap
[1] < pos
) {
223 copy(m
.matchcap
, t
.cap)
226 // First-match mode: cut off all lower-priority threads.
227 for _
, d
:= range runq
.dense
[j
+1:] {
230 m
.pool
= append(m
.pool
, d
.t
)
233 runq
.dense
= runq
.dense
[:0]
237 case syntax
.InstRune
:
239 case syntax
.InstRune1
:
241 case syntax
.InstRuneAny
:
243 case syntax
.InstRuneAnyNotNL
:
247 t
= m
.add(nextq
, i
.Out
, nextPos
, t
.cap, nextCond
, t
)
251 m
.pool
= append(m
.pool
, t
)
254 runq
.dense
= runq
.dense
[:0]
257 // add adds an entry to q for pc, unless the q already has such an entry.
258 // It also recursively adds an entry for all instructions reachable from pc by following
259 // empty-width conditions satisfied by cond. pos gives the current position
261 func (m
*machine
) add(q
*queue
, pc
uint32, pos
int, cap []int, cond syntax
.EmptyOp
, t
*thread
) *thread
{
265 if j
:= q
.sparse
[pc
]; j
< uint32(len(q
.dense
)) && q
.dense
[j
].pc
== pc
{
270 q
.dense
= q
.dense
[:j
+1]
274 q
.sparse
[pc
] = uint32(j
)
280 case syntax
.InstFail
:
282 case syntax
.InstAlt
, syntax
.InstAltMatch
:
283 t
= m
.add(q
, i
.Out
, pos
, cap, cond
, t
)
284 t
= m
.add(q
, i
.Arg
, pos
, cap, cond
, t
)
285 case syntax
.InstEmptyWidth
:
286 if syntax
.EmptyOp(i
.Arg
)&^cond
== 0 {
287 t
= m
.add(q
, i
.Out
, pos
, cap, cond
, t
)
290 t
= m
.add(q
, i
.Out
, pos
, cap, cond
, t
)
291 case syntax
.InstCapture
:
292 if int(i
.Arg
) < len(cap) {
295 m
.add(q
, i
.Out
, pos
, cap, cond
, nil)
298 t
= m
.add(q
, i
.Out
, pos
, cap, cond
, t
)
300 case syntax
.InstMatch
, syntax
.InstRune
, syntax
.InstRune1
, syntax
.InstRuneAny
, syntax
.InstRuneAnyNotNL
:
306 if len(cap) > 0 && &t
.cap[0] != &cap[0] {
315 // empty is a non-nil 0-element slice,
316 // so doExecute can avoid an allocation
317 // when 0 captures are requested from a successful match.
318 var empty
= make([]int, 0)
320 // doExecute finds the leftmost match in the input and returns
321 // the position of its subexpressions.
322 func (re
*Regexp
) doExecute(r io
.RuneReader
, b
[]byte, s
string, pos
int, ncap
int) []int {
326 i
= m
.newInputReader(r
)
328 i
= m
.newInputBytes(b
)
330 i
= m
.newInputString(s
)
333 if !m
.match(i
, pos
) {
339 return empty
// empty but not nil
341 cap := make([]int, ncap
)
342 copy(cap, m
.matchcap
)