summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/textproc/prefixreplacer.go
diff options
context:
space:
mode:
authorrillig <rillig@pkgsrc.org>2017-01-17 22:37:27 +0000
committerrillig <rillig@pkgsrc.org>2017-01-17 22:37:27 +0000
commitc487f6b01167530f572c9d1dbaa6620231366ec7 (patch)
treeb92b2370df2bfe70ed17e57a1bc3b312ad3ad350 /pkgtools/pkglint/files/textproc/prefixreplacer.go
parent9ffc784ed21c6a3a54c6edad8ffd681998a8073b (diff)
downloadpkgsrc-c487f6b01167530f572c9d1dbaa6620231366ec7.tar.gz
Updated pkglint to 5.4.16.
Changes since 5.4.15: * updated vardefs from mk/defaults/mk.conf from v1.118 (2006) to v1.269 Gone: * PKG_SUFX * USETBL * PKGSRC_SHOW_PATCH_ERRORMSG * USE_XPKGWEDGE * PKGVULNDIR Adjusted: * USE_GAMESGROUP * BIN_INSTALL_FLAG -> BIN_INSTALL_FLAGS * fixed license parsing to be more realistic (the previous version didn't handle parentheses correctly) * lots of housekeeping * moved some code to separate packages, allowing re-use * separated Line checks into LineChecker type * separated MkLine checks into MkLineChecker type * made Line an interface, for further refactorings
Diffstat (limited to 'pkgtools/pkglint/files/textproc/prefixreplacer.go')
-rw-r--r--pkgtools/pkglint/files/textproc/prefixreplacer.go136
1 files changed, 136 insertions, 0 deletions
diff --git a/pkgtools/pkglint/files/textproc/prefixreplacer.go b/pkgtools/pkglint/files/textproc/prefixreplacer.go
new file mode 100644
index 00000000000..535a78dea62
--- /dev/null
+++ b/pkgtools/pkglint/files/textproc/prefixreplacer.go
@@ -0,0 +1,136 @@
+package textproc
+
+import (
+ "fmt"
+ "netbsd.org/pkglint/regex"
+ "netbsd.org/pkglint/trace"
+ "strings"
+)
+
+var Testing bool
+
+type PrefixReplacerMark string
+
+type PrefixReplacer struct {
+ rest string
+ s string
+ m []string
+}
+
+func NewPrefixReplacer(s string) *PrefixReplacer {
+ return &PrefixReplacer{s, "", nil}
+}
+
+func (pr *PrefixReplacer) EOF() bool {
+ return pr.rest == ""
+}
+
+func (pr *PrefixReplacer) Rest() string {
+ return pr.rest
+}
+
+// Match returns a matching group from the last matched AdvanceRegexp.
+func (pr *PrefixReplacer) Group(index int) string {
+ return pr.m[index]
+}
+
+// Rest returns the last match from AdvanceStr, AdvanceBytesFunc or AdvanceHspace.
+func (pr *PrefixReplacer) Str() string {
+ return pr.s
+}
+
+func (pr *PrefixReplacer) AdvanceStr(prefix string) bool {
+ pr.m = nil
+ pr.s = ""
+ if strings.HasPrefix(pr.rest, prefix) {
+ if trace.Tracing {
+ trace.Stepf("PrefixReplacer.AdvanceStr(%q, %q)", pr.rest, prefix)
+ }
+ pr.s = prefix
+ pr.rest = pr.rest[len(prefix):]
+ return true
+ }
+ return false
+}
+
+func (pr *PrefixReplacer) AdvanceBytesFunc(fn func(c byte) bool) bool {
+ i := 0
+ for i < len(pr.rest) && fn(pr.rest[i]) {
+ i++
+ }
+ if i != 0 {
+ pr.s = pr.rest[:i]
+ pr.rest = pr.rest[i:]
+ return true
+ }
+ return false
+}
+
+func (pr *PrefixReplacer) AdvanceHspace() bool {
+ i := 0
+ rest := pr.rest
+ for i < len(rest) && (rest[i] == ' ' || rest[i] == '\t') {
+ i++
+ }
+ if i != 0 {
+ pr.s = pr.rest[:i]
+ pr.rest = pr.rest[i:]
+ return true
+ }
+ return false
+}
+
+func (pr *PrefixReplacer) AdvanceRegexp(re regex.RegexPattern) bool {
+ pr.m = nil
+ pr.s = ""
+ if !strings.HasPrefix(string(re), "^") {
+ panic(fmt.Sprintf("PrefixReplacer.AdvanceRegexp: regular expression %q must have prefix %q.", re, "^"))
+ }
+ if Testing && regex.Matches("", re) {
+ panic(fmt.Sprintf("PrefixReplacer.AdvanceRegexp: the empty string must not match the regular expression %q.", re))
+ }
+ if m := regex.Match(pr.rest, re); m != nil {
+ if trace.Tracing {
+ trace.Stepf("PrefixReplacer.AdvanceRegexp(%q, %q, %q)", pr.rest, re, m[0])
+ }
+ pr.rest = pr.rest[len(m[0]):]
+ pr.m = m
+ pr.s = m[0]
+ return true
+ }
+ return false
+}
+
+func (pr *PrefixReplacer) PeekByte() int {
+ rest := pr.rest
+ if rest == "" {
+ return -1
+ }
+ return int(rest[0])
+}
+
+func (pr *PrefixReplacer) Mark() PrefixReplacerMark {
+ return PrefixReplacerMark(pr.rest)
+}
+
+func (pr *PrefixReplacer) Reset(mark PrefixReplacerMark) {
+ pr.rest = string(mark)
+}
+
+func (pr *PrefixReplacer) Skip(n int) {
+ pr.rest = pr.rest[n:]
+}
+
+func (pr *PrefixReplacer) SkipSpace() {
+ pr.rest = strings.TrimLeft(pr.rest, " \t")
+}
+
+func (pr *PrefixReplacer) Since(mark PrefixReplacerMark) string {
+ return string(mark[:len(mark)-len(pr.rest)])
+}
+
+func (pr *PrefixReplacer) AdvanceRest() string {
+ rest := pr.rest
+ pr.rest = ""
+ return rest
+}