func (i *inputBytes) Context(pos int) syntax.EmptyOp { r1, r2 := endOfText, endOfText if pos > 0 && pos <= len(i.str) { r1, _ = utf8.DecodeLastRune(i.str[:pos]) } if pos < len(i.str) { r2, _ = utf8.DecodeRune(i.str[pos:]) } return syntax.EmptyOpContext(r1, r2) }
// As I understand it this thing should check the rune before and the one // after the given location? IS THIS THING EVER USED?! func (i *inputReadSeeker) Context(pos int) syntax.EmptyOp { pos = pos + i.start //log.Println("Context: ", pos) //log.Println(pos) ol, _ := i.r.Seek(0, 1) defer i.r.Seek(ol, 0) r1, r2 := endOfText, endOfText if len(i.buf) != 8 { if cap(i.buf) < 8 { i.buf = make([]byte, 8) } else { // TODO: Is this how to extend a slice to its maximum capacity? i.buf = i.buf[:8] } } // Try to read out the surrounding 8 bytes but take care so that // if we are to early we read fewer before posa := pos - 4 if pos < 4 { posa = 0 } _, e := i.r.Seek(int64(posa), 0) if e != nil { return syntax.EmptyOpContext(r1, r2) } n, e := i.r.Read(i.buf) if pos > 0 && n > 0 { r1, _ = utf8.DecodeLastRune(i.buf[:pos-posa]) } if n > pos-posa { r2, _ = utf8.DecodeRune(i.buf[pos-posa:]) } return syntax.EmptyOpContext(r1, r2) }
func (f *File) Context(pos int) syntax.EmptyOp { pos += f.pos r1, r2 := endOfText, endOfText if pos > 0 && pos <= f.Length() { if pos < f.gapStart { r1, _ = utf8.DecodeLastRune(f.b[:pos]) } else { r1, _ = utf8.DecodeLastRune(f.b[:pos+(f.gapEnd-f.gapStart)]) } } if pos < f.Length() { if pos < f.gapStart { r2, _ = utf8.DecodeRune(f.b[pos:]) } else { r2, _ = utf8.DecodeRune(f.b[pos+(f.gapEnd-f.gapStart):]) } } return syntax.EmptyOpContext(r1, r2) }
// match runs the machine over the input starting at pos. // It reports whether a match was found. // If so, m.matchcap holds the submatch information. func (m *machine) match(i Input, pos int) bool { startCond := m.re.cond if startCond == ^syntax.EmptyOp(0) { // impossible return false } m.matched = false for i := range m.matchcap { m.matchcap[i] = -1 } runq, nextq := &m.q0, &m.q1 r, r1 := endOfText, endOfText width, width1 := 0, 0 r, width = i.Step(pos) if r != endOfText { r1, width1 = i.Step(pos + width) } var flag syntax.EmptyOp if pos == 0 { flag = syntax.EmptyOpContext(-1, r) } else { flag = i.Context(pos) } for { if len(runq.dense) == 0 { if startCond&syntax.EmptyBeginText != 0 && pos != 0 { // Anchored match, past beginning of text. break } if m.matched { // Have match; finished exploring alternatives. break } if len(m.re.prefix) > 0 && r1 != m.re.prefixRune && i.CanCheckPrefix() { // Match requires literal prefix; fast search for it. advance := i.Index(m.re, pos) if advance < 0 { break } pos += advance r, width = i.Step(pos) r1, width1 = i.Step(pos + width) } } if !m.matched { if len(m.matchcap) > 0 { m.matchcap[0] = pos } m.add(runq, uint32(m.p.Start), pos, m.matchcap, flag, nil) } flag = syntax.EmptyOpContext(r, r1) m.step(runq, nextq, pos, pos+width, r, flag) if width == 0 { break } if len(m.matchcap) == 0 && m.matched { // Found a match and not paying attention // to where it is, so any match will do. break } pos += width r, width = r1, width1 if r != endOfText { r1, width1 = i.Step(pos + width) } runq, nextq = nextq, runq } m.clear(nextq) return m.matched }