1 // Copyright 2012 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.
14 // This file is stolen from go/src/cmd/godoc/codewalk.go.
15 // It's an evaluator for the file address syntax implemented by acme and sam,
16 // but using Go-native regular expressions.
17 // To keep things reasonably close, this version uses (?m:re) for all user-provided
18 // regular expressions. That is the only change to the code from codewalk.go.
19 // See http://9p.io/sys/doc/sam/sam.html Table II for details on the syntax.
21 // addrToByte evaluates the given address starting at offset start in data.
22 // It returns the lo and hi byte offset of the matched region within data.
23 func addrToByteRange(addr string, start int, data []byte) (lo, hi int, err error) {
25 lo, hi = start, len(data)
35 for addr != "" && err == nil {
39 err = errors.New("invalid address syntax near " + string(c))
44 _, hi, err = addrToByteRange(addr[1:], hi, data)
49 if prevc == '+' || prevc == '-' {
50 lo, hi, err = addrNumber(data, lo, hi, prevc, 1, charOffset)
64 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
66 for i = 1; i < len(addr); i++ {
67 if addr[i] < '0' || addr[i] > '9' {
72 n, err = strconv.Atoi(addr[0:i])
76 lo, hi, err = addrNumber(data, lo, hi, dir, n, charOffset)
86 for i = 1; i < len(addr); i++ {
99 lo, hi, err = addrRegexp(data, lo, hi, dir, pattern)
108 if err == nil && dir != 0 {
109 lo, hi, err = addrNumber(data, lo, hi, dir, 1, charOffset)
117 // addrNumber applies the given dir, n, and charOffset to the address lo, hi.
118 // dir is '+' or '-', n is the count, and charOffset is true if the syntax
119 // used was #n. Applying +n (or +#n) means to advance n lines
120 // (or characters) after hi. Applying -n (or -#n) means to back up n lines
121 // (or characters) before lo.
122 // The return value is the new lo, hi.
123 func addrNumber(data []byte, lo, hi int, dir byte, n int, charOffset bool) (int, int, error) {
133 for ; n > 0 && pos < len(data); n-- {
134 _, size := utf8.DecodeRune(data[pos:])
142 // find next beginning of line
144 for hi < len(data) && data[hi-1] != '\n' {
152 for ; hi < len(data); hi++ {
153 if data[hi] != '\n' {
160 return lo, hi + 1, nil
166 // Scan backward for bytes that are not UTF-8 continuation bytes.
168 for ; pos > 0 && n > 0; pos-- {
169 if data[pos]&0xc0 != 0x80 {
178 // find earlier beginning of line
179 for lo > 0 && data[lo-1] != '\n' {
186 for ; lo >= 0; lo-- {
187 if lo > 0 && data[lo-1] != '\n' {
199 return 0, 0, errors.New("address out of range")
202 // addrRegexp searches for pattern in the given direction starting at lo, hi.
203 // The direction dir is '+' (search forward from hi) or '-' (search backward from lo).
204 // Backward searches are unimplemented.
205 func addrRegexp(data []byte, lo, hi int, dir byte, pattern string) (int, int, error) {
206 // We want ^ and $ to work as in sam/acme, so use ?m.
207 re, err := regexp.Compile("(?m:" + pattern + ")")
212 // Could implement reverse search using binary search
213 // through file, but that seems like overkill.
214 return 0, 0, errors.New("reverse search not implemented")
216 m := re.FindIndex(data[hi:])
221 // No match. Wrap to beginning of data.
222 m = re.FindIndex(data)
225 return 0, 0, errors.New("no match for " + pattern)
227 return m[0], m[1], nil