From 960e80014c5a48034b8a659f7848c157cb6cc78d Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Tue, 14 Oct 2008 22:16:45 -0700 Subject: Make regexp build and install officially R=rsc DELTA=335 (172 added, 156 deleted, 7 changed) OCL=17167 CL=17180 --- src/lib/clean.bash | 2 +- src/lib/container/vector.go | 4 ++ src/lib/make.bash | 2 +- src/lib/regexp/Makefile | 16 +++-- src/lib/regexp/main.go | 160 -------------------------------------------- src/lib/regexp/test.go | 160 ++++++++++++++++++++++++++++++++++++++++++++ src/run.bash | 6 ++ 7 files changed, 183 insertions(+), 167 deletions(-) delete mode 100644 src/lib/regexp/main.go create mode 100644 src/lib/regexp/test.go (limited to 'src') diff --git a/src/lib/clean.bash b/src/lib/clean.bash index 93bb64a15..e3d7b612f 100755 --- a/src/lib/clean.bash +++ b/src/lib/clean.bash @@ -6,7 +6,7 @@ rm -f $GOROOT/pkg/* -for i in syscall os math net time +for i in syscall os math net time http regexp do cd $i make nuke diff --git a/src/lib/container/vector.go b/src/lib/container/vector.go index 72a0fff74..c64691767 100644 --- a/src/lib/container/vector.go +++ b/src/lib/container/vector.go @@ -56,6 +56,10 @@ func (v *Vector) Remove(i int) Element { } +func (v *Vector) Reset() { + v.elem = v.elem[0:0]; +} + func (v *Vector) Insert(i int, e Element) { n := v.Len(); // range check unnecessary - done by runtime diff --git a/src/lib/make.bash b/src/lib/make.bash index c791b2393..5cc976262 100755 --- a/src/lib/make.bash +++ b/src/lib/make.bash @@ -33,7 +33,7 @@ do 6g -o $GOROOT/pkg/$base.6 $i done -for i in net time http +for i in net time http regexp do echo; echo; echo %%%% making lib/$i %%%%; echo cd $i diff --git a/src/lib/regexp/Makefile b/src/lib/regexp/Makefile index ac466a0f1..86c1680f3 100644 --- a/src/lib/regexp/Makefile +++ b/src/lib/regexp/Makefile @@ -5,16 +5,22 @@ A=6 G=$(A)g L=$(A)l +PKG=$(GOROOT)/pkg/regexp.$A -all: main +test: main.$A test.$A + $L -o test test.$A + ./test -main: main.6 - $L -o main main.6 +install: regexp.$A + cp regexp.$A $(PKG) -main.6: regexp.6 +main: main.$A + $L -o main main.$A + +main.$A: regexp.$A clean: - rm -f *.6 main + rm -f *.6 test %.6: %.go $G $< diff --git a/src/lib/regexp/main.go b/src/lib/regexp/main.go deleted file mode 100644 index c89f9b557..000000000 --- a/src/lib/regexp/main.go +++ /dev/null @@ -1,160 +0,0 @@ -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package main - -import ( - "os"; - "regexp"; -) - -var good_re = []string{ - ``, - `.`, - `^.$`, - `a`, - `a*`, - `a+`, - `a?`, - `a|b`, - `a*|b*`, - `(a*|b)(c*|d)`, - `[a-z]`, - `[a-abc-c\-\]\[]`, - `[a-z]+`, - `[]`, - `[abc]`, - `[^1234]`, -} - -// TODO: nice to do this with a map but we don't have an iterator -type StringError struct { - re string; - err *os.Error; -} -var bad_re = []StringError{ - StringError{ `*`, regexp.ErrBareClosure }, - StringError{ `(abc`, regexp.ErrUnmatchedLpar }, - StringError{ `abc)`, regexp.ErrUnmatchedRpar }, - StringError{ `x[a-z`, regexp.ErrUnmatchedLbkt }, - StringError{ `abc]`, regexp.ErrUnmatchedRbkt }, - StringError{ `[z-a]`, regexp.ErrBadRange }, - StringError{ `abc\`, regexp.ErrExtraneousBackslash }, - StringError{ `a**`, regexp.ErrBadClosure }, - StringError{ `a*+`, regexp.ErrBadClosure }, - StringError{ `a??`, regexp.ErrBadClosure }, - StringError{ `*`, regexp.ErrBareClosure }, - StringError{ `\x`, regexp.ErrBadBackslash }, -} - -type Vec [20]int; - -type Tester struct { - re string; - text string; - match Vec; -} - -const END = -1000 - -var matches = []Tester { - Tester{ ``, "", Vec{0,0, END} }, - Tester{ `a`, "a", Vec{0,1, END} }, - Tester{ `b`, "abc", Vec{1,2, END} }, - Tester{ `.`, "a", Vec{0,1, END} }, - Tester{ `.*`, "abcdef", Vec{0,6, END} }, - Tester{ `^abcd$`, "abcd", Vec{0,4, END} }, - Tester{ `^bcd'`, "abcdef", Vec{END} }, - Tester{ `^abcd$`, "abcde", Vec{END} }, - Tester{ `a+`, "baaab", Vec{1,4, END} }, - Tester{ `a*`, "baaab", Vec{0,0, END} }, - Tester{ `[a-z]+`, "abcd", Vec{0,4, END} }, - Tester{ `[^a-z]+`, "ab1234cd", Vec{2,6, END} }, - Tester{ `[a\-\]z]+`, "az]-bcz", Vec{0,4, END} }, - Tester{ `[日本語]+`, "日本語日本語", Vec{0,18, END} }, - Tester{ `()`, "", Vec{0,0, 0,0, END} }, - Tester{ `(a)`, "a", Vec{0,1, 0,1, END} }, - Tester{ `(.)(.)`, "日a", Vec{0,4, 0,3, 3,4, END} }, - Tester{ `(.*)`, "", Vec{0,0, 0,0, END} }, - Tester{ `(.*)`, "abcd", Vec{0,4, 0,4, END} }, - Tester{ `(..)(..)`, "abcd", Vec{0,4, 0,2, 2,4, END} }, - Tester{ `(([^xyz]*)(d))`, "abcd", Vec{0,4, 0,4, 0,3, 3,4, END} }, - Tester{ `((a|b|c)*(d))`, "abcd", Vec{0,4, 0,4, 2,3, 3,4, END} }, - Tester{ `(((a|b|c)*)(d))`, "abcd", Vec{0,4, 0,4, 0,3, 2,3, 3,4, END} }, - Tester{ `a*(|(b))c*`, "aacc", Vec{0,4, 2,2, -1,-1, END} }, -} - -func Compile(expr string, error *os.Error) regexp.Regexp { - re, err := regexp.Compile(expr); - if err != error { - print("compiling `", expr, "`; unexpected error: ", err.String(), "\n"); - sys.exit(1); - } - return re -} - -func MarkedLen(m *[] int) int { - if m == nil { - return 0 - } - var i int; - for i = 0; i < len(m) && m[i] != END; i = i+2 { - } - return i -} - -func PrintVec(m *[] int) { - l := MarkedLen(m); - if l == 0 { - print(""); - } else { - for i := 0; i < l && m[i] != END; i = i+2 { - print(m[i], ",", m[i+1], " ") - } - } -} - -func Equal(m1, m2 *[]int) bool { - l := MarkedLen(m1); - if l != MarkedLen(m2) { - return false - } - for i := 0; i < l; i++ { - if m1[i] != m2[i] { - return false - } - } - return true -} - -func Match(expr string, str string, match *[]int) { - re := Compile(expr, nil); - m := re.Execute(str); - if !Equal(m, match) { - print("failure on `", expr, "` matching `", str, "`:\n"); - PrintVec(m); - print("\nshould be:\n"); - PrintVec(match); - print("\n"); - sys.exit(1); - } -} - -func main() { - //regexp.debug = true; - if sys.argc() > 1 { - Compile(sys.argv(1), nil); - sys.exit(0); - } - for i := 0; i < len(good_re); i++ { - Compile(good_re[i], nil); - } - for i := 0; i < len(bad_re); i++ { - Compile(bad_re[i].re, bad_re[i].err) - } - for i := 0; i < len(matches); i++ { - t := &matches[i]; - Match(t.re, t.text, &t.match) - } -} diff --git a/src/lib/regexp/test.go b/src/lib/regexp/test.go new file mode 100644 index 000000000..223064c07 --- /dev/null +++ b/src/lib/regexp/test.go @@ -0,0 +1,160 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "os"; + "regexp"; +) + +var good_re = []string{ + ``, + `.`, + `^.$`, + `a`, + `a*`, + `a+`, + `a?`, + `a|b`, + `a*|b*`, + `(a*|b)(c*|d)`, + `[a-z]`, + `[a-abc-c\-\]\[]`, + `[a-z]+`, + `[]`, + `[abc]`, + `[^1234]`, +} + +// TODO: nice to do this with a map but we don't have an iterator +type StringError struct { + re string; + err *os.Error; +} +var bad_re = []StringError{ + StringError{ `*`, regexp.ErrBareClosure }, + StringError{ `(abc`, regexp.ErrUnmatchedLpar }, + StringError{ `abc)`, regexp.ErrUnmatchedRpar }, + StringError{ `x[a-z`, regexp.ErrUnmatchedLbkt }, + StringError{ `abc]`, regexp.ErrUnmatchedRbkt }, + StringError{ `[z-a]`, regexp.ErrBadRange }, + StringError{ `abc\`, regexp.ErrExtraneousBackslash }, + StringError{ `a**`, regexp.ErrBadClosure }, + StringError{ `a*+`, regexp.ErrBadClosure }, + StringError{ `a??`, regexp.ErrBadClosure }, + StringError{ `*`, regexp.ErrBareClosure }, + StringError{ `\x`, regexp.ErrBadBackslash }, +} + +type Vec [20]int; + +type Tester struct { + re string; + text string; + match Vec; +} + +const END = -1000 + +var matches = []Tester { + Tester{ ``, "", Vec{0,0, END} }, + Tester{ `a`, "a", Vec{0,1, END} }, + Tester{ `b`, "abc", Vec{1,2, END} }, + Tester{ `.`, "a", Vec{0,1, END} }, + Tester{ `.*`, "abcdef", Vec{0,6, END} }, + Tester{ `^abcd$`, "abcd", Vec{0,4, END} }, + Tester{ `^bcd'`, "abcdef", Vec{END} }, + Tester{ `^abcd$`, "abcde", Vec{END} }, + Tester{ `a+`, "baaab", Vec{1,4, END} }, + Tester{ `a*`, "baaab", Vec{0,0, END} }, + Tester{ `[a-z]+`, "abcd", Vec{0,4, END} }, + Tester{ `[^a-z]+`, "ab1234cd", Vec{2,6, END} }, + Tester{ `[a\-\]z]+`, "az]-bcz", Vec{0,4, END} }, + Tester{ `[日本語]+`, "日本語日本語", Vec{0,18, END} }, + Tester{ `()`, "", Vec{0,0, 0,0, END} }, + Tester{ `(a)`, "a", Vec{0,1, 0,1, END} }, + Tester{ `(.)(.)`, "日a", Vec{0,4, 0,3, 3,4, END} }, + Tester{ `(.*)`, "", Vec{0,0, 0,0, END} }, + Tester{ `(.*)`, "abcd", Vec{0,4, 0,4, END} }, + Tester{ `(..)(..)`, "abcd", Vec{0,4, 0,2, 2,4, END} }, + Tester{ `(([^xyz]*)(d))`, "abcd", Vec{0,4, 0,4, 0,3, 3,4, END} }, + Tester{ `((a|b|c)*(d))`, "abcd", Vec{0,4, 0,4, 2,3, 3,4, END} }, + Tester{ `(((a|b|c)*)(d))`, "abcd", Vec{0,4, 0,4, 0,3, 2,3, 3,4, END} }, + Tester{ `a*(|(b))c*`, "aacc", Vec{0,4, 2,2, -1,-1, END} }, +} + +func Compile(expr string, error *os.Error) regexp.Regexp { + re, err := regexp.Compile(expr); + if err != error { + print("compiling `", expr, "`; unexpected error: ", err.String(), "\n"); + sys.exit(1); + } + return re +} + +func MarkedLen(m *[] int) int { + if m == nil { + return 0 + } + var i int; + for i = 0; i < len(m) && m[i] != END; i = i+2 { + } + return i +} + +func PrintVec(m *[] int) { + l := MarkedLen(m); + if l == 0 { + print(""); + } else { + for i := 0; i < l && m[i] != END; i = i+2 { + print(m[i], ",", m[i+1], " ") + } + } +} + +func Equal(m1, m2 *[]int) bool { + l := MarkedLen(m1); + if l != MarkedLen(m2) { + return false + } + for i := 0; i < l; i++ { + if m1[i] != m2[i] { + return false + } + } + return true +} + +func Match(expr string, str string, match *[]int) { + re := Compile(expr, nil); + m := re.Execute(str); + if !Equal(m, match) { + print("failure on `", expr, "` matching `", str, "`:\n"); + PrintVec(m); + print("\nshould be:\n"); + PrintVec(match); + print("\n"); + sys.exit(1); + } +} + +func main() { + //regexp.debug = true; + if sys.argc() > 1 { + Compile(sys.argv(1), nil); + sys.exit(0); + } + for i := 0; i < len(good_re); i++ { + Compile(good_re[i], nil); + } + for i := 0; i < len(bad_re); i++ { + Compile(bad_re[i].re, bad_re[i].err) + } + for i := 0; i < len(matches); i++ { + t := &matches[i]; + Match(t.re, t.text, &t.match) + } +} diff --git a/src/run.bash b/src/run.bash index cd33b891e..72a540621 100755 --- a/src/run.bash +++ b/src/run.bash @@ -10,6 +10,12 @@ xcd() { echo --- cd $1 } +(xcd lib/regexp +make clean +time make +make test +) + (xcd ../usr/gri/gosrc make clean time make -- cgit v1.2.3