mirror of
https://github.com/golang/go.git
synced 2025-05-30 11:51:34 +00:00
recasify regexp to use underscores and clean up the tests more
R=rsc DELTA=174 (0 added, 0 deleted, 174 changed) OCL=22917 CL=22942
This commit is contained in:
parent
4b590bf985
commit
794efd7e78
@ -85,7 +85,7 @@ var matches = []tester {
|
|||||||
tester{ `a*(|(b))c*`, "aacc", vec{0,4, 2,2, -1,-1} },
|
tester{ `a*(|(b))c*`, "aacc", vec{0,4, 2,2, -1,-1} },
|
||||||
}
|
}
|
||||||
|
|
||||||
func CompileTest(t *testing.T, expr string, error *os.Error) regexp.Regexp {
|
func compileTest(t *testing.T, expr string, error *os.Error) regexp.Regexp {
|
||||||
re, err := regexp.Compile(expr);
|
re, err := regexp.Compile(expr);
|
||||||
if err != error {
|
if err != error {
|
||||||
t.Error("compiling `", expr, "`; unexpected error: ", err.String());
|
t.Error("compiling `", expr, "`; unexpected error: ", err.String());
|
||||||
@ -93,7 +93,7 @@ func CompileTest(t *testing.T, expr string, error *os.Error) regexp.Regexp {
|
|||||||
return re
|
return re
|
||||||
}
|
}
|
||||||
|
|
||||||
func Printvec(t *testing.T, m []int) {
|
func printVec(t *testing.T, m []int) {
|
||||||
l := len(m);
|
l := len(m);
|
||||||
if l == 0 {
|
if l == 0 {
|
||||||
t.Log("\t<no match>");
|
t.Log("\t<no match>");
|
||||||
@ -104,7 +104,7 @@ func Printvec(t *testing.T, m []int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func PrintStrings(t *testing.T, m []string) {
|
func printStrings(t *testing.T, m []string) {
|
||||||
l := len(m);
|
l := len(m);
|
||||||
if l == 0 {
|
if l == 0 {
|
||||||
t.Log("\t<no match>");
|
t.Log("\t<no match>");
|
||||||
@ -115,7 +115,7 @@ func PrintStrings(t *testing.T, m []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Equal(m1, m2 []int) bool {
|
func equal(m1, m2 []int) bool {
|
||||||
l := len(m1);
|
l := len(m1);
|
||||||
if l != len(m2) {
|
if l != len(m2) {
|
||||||
return false
|
return false
|
||||||
@ -128,7 +128,7 @@ func Equal(m1, m2 []int) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func EqualStrings(m1, m2 []string) bool {
|
func equalStrings(m1, m2 []string) bool {
|
||||||
l := len(m1);
|
l := len(m1);
|
||||||
if l != len(m2) {
|
if l != len(m2) {
|
||||||
return false
|
return false
|
||||||
@ -141,41 +141,41 @@ func EqualStrings(m1, m2 []string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExecuteTest(t *testing.T, expr string, str string, match []int) {
|
func executeTest(t *testing.T, expr string, str string, match []int) {
|
||||||
re := CompileTest(t, expr, nil);
|
re := compileTest(t, expr, nil);
|
||||||
if re == nil {
|
if re == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
m := re.Execute(str);
|
m := re.Execute(str);
|
||||||
if !Equal(m, match) {
|
if !equal(m, match) {
|
||||||
t.Error("Execute failure on `", expr, "` matching `", str, "`:");
|
t.Error("Execute failure on `", expr, "` matching `", str, "`:");
|
||||||
Printvec(t, m);
|
printVec(t, m);
|
||||||
t.Log("should be:");
|
t.Log("should be:");
|
||||||
Printvec(t, match);
|
printVec(t, match);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export func TestGoodCompile(t *testing.T) {
|
export func TestGoodCompile(t *testing.T) {
|
||||||
for i := 0; i < len(good_re); i++ {
|
for i := 0; i < len(good_re); i++ {
|
||||||
CompileTest(t, good_re[i], nil);
|
compileTest(t, good_re[i], nil);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export func TestBadCompile(t *testing.T) {
|
export func TestBadCompile(t *testing.T) {
|
||||||
for i := 0; i < len(bad_re); i++ {
|
for i := 0; i < len(bad_re); i++ {
|
||||||
CompileTest(t, bad_re[i].re, bad_re[i].err)
|
compileTest(t, bad_re[i].re, bad_re[i].err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export func TestExecute(t *testing.T) {
|
export func TestExecute(t *testing.T) {
|
||||||
for i := 0; i < len(matches); i++ {
|
for i := 0; i < len(matches); i++ {
|
||||||
test := &matches[i];
|
test := &matches[i];
|
||||||
ExecuteTest(t, test.re, test.text, test.match)
|
executeTest(t, test.re, test.text, test.match)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func MatchTest(t *testing.T, expr string, str string, match []int) {
|
func matchTest(t *testing.T, expr string, str string, match []int) {
|
||||||
re := CompileTest(t, expr, nil);
|
re := compileTest(t, expr, nil);
|
||||||
if re == nil {
|
if re == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -188,12 +188,12 @@ func MatchTest(t *testing.T, expr string, str string, match []int) {
|
|||||||
export func TestMatch(t *testing.T) {
|
export func TestMatch(t *testing.T) {
|
||||||
for i := 0; i < len(matches); i++ {
|
for i := 0; i < len(matches); i++ {
|
||||||
test := &matches[i];
|
test := &matches[i];
|
||||||
MatchTest(t, test.re, test.text, test.match)
|
matchTest(t, test.re, test.text, test.match)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func MatchStringsTest(t *testing.T, expr string, str string, match []int) {
|
func matchStringsTest(t *testing.T, expr string, str string, match []int) {
|
||||||
re := CompileTest(t, expr, nil);
|
re := compileTest(t, expr, nil);
|
||||||
if re == nil {
|
if re == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -202,22 +202,22 @@ func MatchStringsTest(t *testing.T, expr string, str string, match []int) {
|
|||||||
strs[i/2] = str[match[i] : match[i+1]]
|
strs[i/2] = str[match[i] : match[i+1]]
|
||||||
}
|
}
|
||||||
m := re.MatchStrings(str);
|
m := re.MatchStrings(str);
|
||||||
if !EqualStrings(m, strs) {
|
if !equalStrings(m, strs) {
|
||||||
t.Error("MatchStrings failure on `", expr, "` matching `", str, "`:");
|
t.Error("MatchStrings failure on `", expr, "` matching `", str, "`:");
|
||||||
PrintStrings(t, m);
|
printStrings(t, m);
|
||||||
t.Log("should be:");
|
t.Log("should be:");
|
||||||
PrintStrings(t, strs);
|
printStrings(t, strs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export func TestMatchStrings(t *testing.T) {
|
export func TestMatchStrings(t *testing.T) {
|
||||||
for i := 0; i < len(matches); i++ {
|
for i := 0; i < len(matches); i++ {
|
||||||
test := &matches[i];
|
test := &matches[i];
|
||||||
MatchTest(t, test.re, test.text, test.match)
|
matchTest(t, test.re, test.text, test.match)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func MatchFunctionTest(t *testing.T, expr string, str string, match []int) {
|
func matchFunctionTest(t *testing.T, expr string, str string, match []int) {
|
||||||
m, err := Match(expr, str);
|
m, err := Match(expr, str);
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return
|
return
|
||||||
@ -230,6 +230,6 @@ func MatchFunctionTest(t *testing.T, expr string, str string, match []int) {
|
|||||||
export func TestMatchFunction(t *testing.T) {
|
export func TestMatchFunction(t *testing.T) {
|
||||||
for i := 0; i < len(matches); i++ {
|
for i := 0; i < len(matches); i++ {
|
||||||
test := &matches[i];
|
test := &matches[i];
|
||||||
MatchFunctionTest(t, test.re, test.text, test.match)
|
matchFunctionTest(t, test.re, test.text, test.match)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ export var ErrBadBackslash = os.NewError("illegal backslash escape");
|
|||||||
|
|
||||||
// An instruction executed by the NFA
|
// An instruction executed by the NFA
|
||||||
type instr interface {
|
type instr interface {
|
||||||
Type() int; // the type of this instruction: cCHAR, cANY, etc.
|
Type() int; // the type of this instruction: _CHAR, _ANY, etc.
|
||||||
Next() instr; // the instruction to execute after this one
|
Next() instr; // the instruction to execute after this one
|
||||||
SetNext(i instr);
|
SetNext(i instr);
|
||||||
Index() int;
|
Index() int;
|
||||||
@ -36,19 +36,19 @@ type instr interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fields and methods common to all instructions
|
// Fields and methods common to all instructions
|
||||||
type iCommon struct {
|
type _Common struct {
|
||||||
next instr;
|
next instr;
|
||||||
index int;
|
index int;
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *iCommon) Next() instr { return c.next }
|
func (c *_Common) Next() instr { return c.next }
|
||||||
func (c *iCommon) SetNext(i instr) { c.next = i }
|
func (c *_Common) SetNext(i instr) { c.next = i }
|
||||||
func (c *iCommon) Index() int { return c.index }
|
func (c *_Common) Index() int { return c.index }
|
||||||
func (c *iCommon) SetIndex(i int) { c.index = i }
|
func (c *_Common) SetIndex(i int) { c.index = i }
|
||||||
|
|
||||||
type regExp struct {
|
type _RE struct {
|
||||||
expr string; // the original expression
|
expr string; // the original expression
|
||||||
ch chan<- *regExp; // reply channel when we're done
|
ch chan<- *_RE; // reply channel when we're done
|
||||||
error *os.Error; // compile- or run-time error; nil if OK
|
error *os.Error; // compile- or run-time error; nil if OK
|
||||||
inst *array.Array;
|
inst *array.Array;
|
||||||
start instr;
|
start instr;
|
||||||
@ -56,80 +56,80 @@ type regExp struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
cSTART // beginning of program
|
_START // beginning of program
|
||||||
= iota;
|
= iota;
|
||||||
cEND; // end of program: success
|
_END; // end of program: success
|
||||||
cBOT; // '^' beginning of text
|
_BOT; // '^' beginning of text
|
||||||
cEOT; // '$' end of text
|
_EOT; // '$' end of text
|
||||||
cCHAR; // 'a' regular character
|
_CHAR; // 'a' regular character
|
||||||
cCHARCLASS; // [a-z] character class
|
_CHARCLASS; // [a-z] character class
|
||||||
cANY; // '.' any character
|
_ANY; // '.' any character
|
||||||
cBRA; // '(' parenthesized expression
|
_BRA; // '(' parenthesized expression
|
||||||
cEBRA; // ')'; end of '(' parenthesized expression
|
_EBRA; // ')'; end of '(' parenthesized expression
|
||||||
cALT; // '|' alternation
|
_ALT; // '|' alternation
|
||||||
cNOP; // do nothing; makes it easy to link without patching
|
_NOP; // do nothing; makes it easy to link without patching
|
||||||
)
|
)
|
||||||
|
|
||||||
// --- START start of program
|
// --- START start of program
|
||||||
type iStart struct {
|
type _Start struct {
|
||||||
iCommon
|
_Common
|
||||||
}
|
}
|
||||||
|
|
||||||
func (start *iStart) Type() int { return cSTART }
|
func (start *_Start) Type() int { return _START }
|
||||||
func (start *iStart) Print() { print("start") }
|
func (start *_Start) Print() { print("start") }
|
||||||
|
|
||||||
// --- END end of program
|
// --- END end of program
|
||||||
type iEnd struct {
|
type _End struct {
|
||||||
iCommon
|
_Common
|
||||||
}
|
}
|
||||||
|
|
||||||
func (end *iEnd) Type() int { return cEND }
|
func (end *_End) Type() int { return _END }
|
||||||
func (end *iEnd) Print() { print("end") }
|
func (end *_End) Print() { print("end") }
|
||||||
|
|
||||||
// --- BOT beginning of text
|
// --- BOT beginning of text
|
||||||
type iBot struct {
|
type _Bot struct {
|
||||||
iCommon
|
_Common
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bot *iBot) Type() int { return cBOT }
|
func (bot *_Bot) Type() int { return _BOT }
|
||||||
func (bot *iBot) Print() { print("bot") }
|
func (bot *_Bot) Print() { print("bot") }
|
||||||
|
|
||||||
// --- EOT end of text
|
// --- EOT end of text
|
||||||
type iEot struct {
|
type _Eot struct {
|
||||||
iCommon
|
_Common
|
||||||
}
|
}
|
||||||
|
|
||||||
func (eot *iEot) Type() int { return cEOT }
|
func (eot *_Eot) Type() int { return _EOT }
|
||||||
func (eot *iEot) Print() { print("eot") }
|
func (eot *_Eot) Print() { print("eot") }
|
||||||
|
|
||||||
// --- CHAR a regular character
|
// --- CHAR a regular character
|
||||||
type iChar struct {
|
type _Char struct {
|
||||||
iCommon;
|
_Common;
|
||||||
char int;
|
char int;
|
||||||
}
|
}
|
||||||
|
|
||||||
func (char *iChar) Type() int { return cCHAR }
|
func (char *_Char) Type() int { return _CHAR }
|
||||||
func (char *iChar) Print() { print("char ", string(char.char)) }
|
func (char *_Char) Print() { print("char ", string(char.char)) }
|
||||||
|
|
||||||
func newChar(char int) *iChar {
|
func newChar(char int) *_Char {
|
||||||
c := new(iChar);
|
c := new(_Char);
|
||||||
c.char = char;
|
c.char = char;
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- CHARCLASS [a-z]
|
// --- CHARCLASS [a-z]
|
||||||
|
|
||||||
type iCharClass struct {
|
type _CharClass struct {
|
||||||
iCommon;
|
_Common;
|
||||||
char int;
|
char int;
|
||||||
negate bool; // is character class negated? ([^a-z])
|
negate bool; // is character class negated? ([^a-z])
|
||||||
// array of int, stored pairwise: [a-z] is (a,z); x is (x,x):
|
// array of int, stored pairwise: [a-z] is (a,z); x is (x,x):
|
||||||
ranges *array.IntArray;
|
ranges *array.IntArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cclass *iCharClass) Type() int { return cCHARCLASS }
|
func (cclass *_CharClass) Type() int { return _CHARCLASS }
|
||||||
|
|
||||||
func (cclass *iCharClass) Print() {
|
func (cclass *_CharClass) Print() {
|
||||||
print("charclass");
|
print("charclass");
|
||||||
if cclass.negate {
|
if cclass.negate {
|
||||||
print(" (negated)");
|
print(" (negated)");
|
||||||
@ -145,13 +145,13 @@ func (cclass *iCharClass) Print() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cclass *iCharClass) AddRange(a, b int) {
|
func (cclass *_CharClass) AddRange(a, b int) {
|
||||||
// range is a through b inclusive
|
// range is a through b inclusive
|
||||||
cclass.ranges.Push(a);
|
cclass.ranges.Push(a);
|
||||||
cclass.ranges.Push(b);
|
cclass.ranges.Push(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cclass *iCharClass) Matches(c int) bool {
|
func (cclass *_CharClass) Matches(c int) bool {
|
||||||
for i := 0; i < cclass.ranges.Len(); i = i+2 {
|
for i := 0; i < cclass.ranges.Len(); i = i+2 {
|
||||||
min := cclass.ranges.At(i);
|
min := cclass.ranges.At(i);
|
||||||
max := cclass.ranges.At(i+1);
|
max := cclass.ranges.At(i+1);
|
||||||
@ -162,70 +162,70 @@ func (cclass *iCharClass) Matches(c int) bool {
|
|||||||
return cclass.negate
|
return cclass.negate
|
||||||
}
|
}
|
||||||
|
|
||||||
func newCharClass() *iCharClass {
|
func newCharClass() *_CharClass {
|
||||||
c := new(iCharClass);
|
c := new(_CharClass);
|
||||||
c.ranges = array.NewIntArray(0);
|
c.ranges = array.NewIntArray(0);
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- ANY any character
|
// --- ANY any character
|
||||||
type iAny struct {
|
type _Any struct {
|
||||||
iCommon
|
_Common
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *iAny) Type() int { return cANY }
|
func (any *_Any) Type() int { return _ANY }
|
||||||
func (any *iAny) Print() { print("any") }
|
func (any *_Any) Print() { print("any") }
|
||||||
|
|
||||||
// --- BRA parenthesized expression
|
// --- BRA parenthesized expression
|
||||||
type iBra struct {
|
type _Bra struct {
|
||||||
iCommon;
|
_Common;
|
||||||
n int; // subexpression number
|
n int; // subexpression number
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bra *iBra) Type() int { return cBRA }
|
func (bra *_Bra) Type() int { return _BRA }
|
||||||
func (bra *iBra) Print() { print("bra", bra.n); }
|
func (bra *_Bra) Print() { print("bra", bra.n); }
|
||||||
|
|
||||||
// --- EBRA end of parenthesized expression
|
// --- EBRA end of parenthesized expression
|
||||||
type iEbra struct {
|
type _Ebra struct {
|
||||||
iCommon;
|
_Common;
|
||||||
n int; // subexpression number
|
n int; // subexpression number
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ebra *iEbra) Type() int { return cEBRA }
|
func (ebra *_Ebra) Type() int { return _EBRA }
|
||||||
func (ebra *iEbra) Print() { print("ebra ", ebra.n); }
|
func (ebra *_Ebra) Print() { print("ebra ", ebra.n); }
|
||||||
|
|
||||||
// --- ALT alternation
|
// --- ALT alternation
|
||||||
type iAlt struct {
|
type _Alt struct {
|
||||||
iCommon;
|
_Common;
|
||||||
left instr; // other branch
|
left instr; // other branch
|
||||||
}
|
}
|
||||||
|
|
||||||
func (alt *iAlt) Type() int { return cALT }
|
func (alt *_Alt) Type() int { return _ALT }
|
||||||
func (alt *iAlt) Print() { print("alt(", alt.left.Index(), ")"); }
|
func (alt *_Alt) Print() { print("alt(", alt.left.Index(), ")"); }
|
||||||
|
|
||||||
// --- NOP no operation
|
// --- NOP no operation
|
||||||
type iNop struct {
|
type _Nop struct {
|
||||||
iCommon
|
_Common
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nop *iNop) Type() int { return cNOP }
|
func (nop *_Nop) Type() int { return _NOP }
|
||||||
func (nop *iNop) Print() { print("nop") }
|
func (nop *_Nop) Print() { print("nop") }
|
||||||
|
|
||||||
// report error and exit compiling/executing goroutine
|
// report error and exit compiling/executing goroutine
|
||||||
func (re *regExp) Error(err *os.Error) {
|
func (re *_RE) Error(err *os.Error) {
|
||||||
re.error = err;
|
re.error = err;
|
||||||
re.ch <- re;
|
re.ch <- re;
|
||||||
sys.goexit();
|
sys.goexit();
|
||||||
}
|
}
|
||||||
|
|
||||||
func (re *regExp) Add(i instr) instr {
|
func (re *_RE) Add(i instr) instr {
|
||||||
i.SetIndex(re.inst.Len());
|
i.SetIndex(re.inst.Len());
|
||||||
re.inst.Push(i);
|
re.inst.Push(i);
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
type parser struct {
|
type parser struct {
|
||||||
re *regExp;
|
re *_RE;
|
||||||
nlpar int; // number of unclosed lpars
|
nlpar int; // number of unclosed lpars
|
||||||
pos int;
|
pos int;
|
||||||
ch int;
|
ch int;
|
||||||
@ -248,7 +248,7 @@ func (p *parser) nextc() int {
|
|||||||
return p.ch;
|
return p.ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
func newParser(re *regExp) *parser {
|
func newParser(re *_RE) *parser {
|
||||||
p := new(parser);
|
p := new(parser);
|
||||||
p.re = re;
|
p.re = re;
|
||||||
p.nextc(); // load p.ch
|
p.nextc(); // load p.ch
|
||||||
@ -364,15 +364,15 @@ func (p *parser) Term() (start, end instr) {
|
|||||||
p.re.Error(ErrUnmatchedRbkt);
|
p.re.Error(ErrUnmatchedRbkt);
|
||||||
case '^':
|
case '^':
|
||||||
p.nextc();
|
p.nextc();
|
||||||
start = p.re.Add(new(iBot));
|
start = p.re.Add(new(_Bot));
|
||||||
return start, start;
|
return start, start;
|
||||||
case '$':
|
case '$':
|
||||||
p.nextc();
|
p.nextc();
|
||||||
start = p.re.Add(new(iEot));
|
start = p.re.Add(new(_Eot));
|
||||||
return start, start;
|
return start, start;
|
||||||
case '.':
|
case '.':
|
||||||
p.nextc();
|
p.nextc();
|
||||||
start = p.re.Add(new(iAny));
|
start = p.re.Add(new(_Any));
|
||||||
return start, start;
|
return start, start;
|
||||||
case '[':
|
case '[':
|
||||||
p.nextc();
|
p.nextc();
|
||||||
@ -393,9 +393,9 @@ func (p *parser) Term() (start, end instr) {
|
|||||||
}
|
}
|
||||||
p.nlpar--;
|
p.nlpar--;
|
||||||
p.nextc();
|
p.nextc();
|
||||||
bra := new(iBra);
|
bra := new(_Bra);
|
||||||
p.re.Add(bra);
|
p.re.Add(bra);
|
||||||
ebra := new(iEbra);
|
ebra := new(_Ebra);
|
||||||
p.re.Add(ebra);
|
p.re.Add(ebra);
|
||||||
bra.n = nbra;
|
bra.n = nbra;
|
||||||
ebra.n = nbra;
|
ebra.n = nbra;
|
||||||
@ -437,7 +437,7 @@ func (p *parser) Closure() (start, end instr) {
|
|||||||
switch p.c() {
|
switch p.c() {
|
||||||
case '*':
|
case '*':
|
||||||
// (start,end)*:
|
// (start,end)*:
|
||||||
alt := new(iAlt);
|
alt := new(_Alt);
|
||||||
p.re.Add(alt);
|
p.re.Add(alt);
|
||||||
end.SetNext(alt); // after end, do alt
|
end.SetNext(alt); // after end, do alt
|
||||||
alt.left = start; // alternate brach: return to start
|
alt.left = start; // alternate brach: return to start
|
||||||
@ -445,16 +445,16 @@ func (p *parser) Closure() (start, end instr) {
|
|||||||
end = alt;
|
end = alt;
|
||||||
case '+':
|
case '+':
|
||||||
// (start,end)+:
|
// (start,end)+:
|
||||||
alt := new(iAlt);
|
alt := new(_Alt);
|
||||||
p.re.Add(alt);
|
p.re.Add(alt);
|
||||||
end.SetNext(alt); // after end, do alt
|
end.SetNext(alt); // after end, do alt
|
||||||
alt.left = start; // alternate brach: return to start
|
alt.left = start; // alternate brach: return to start
|
||||||
end = alt; // start is unchanged; end is alt
|
end = alt; // start is unchanged; end is alt
|
||||||
case '?':
|
case '?':
|
||||||
// (start,end)?:
|
// (start,end)?:
|
||||||
alt := new(iAlt);
|
alt := new(_Alt);
|
||||||
p.re.Add(alt);
|
p.re.Add(alt);
|
||||||
nop := new(iNop);
|
nop := new(_Nop);
|
||||||
p.re.Add(nop);
|
p.re.Add(nop);
|
||||||
alt.left = start; // alternate branch is start
|
alt.left = start; // alternate branch is start
|
||||||
alt.next = nop; // follow on to nop
|
alt.next = nop; // follow on to nop
|
||||||
@ -478,7 +478,7 @@ func (p *parser) Concatenation() (start, end instr) {
|
|||||||
switch {
|
switch {
|
||||||
case nstart == iNULL: // end of this concatenation
|
case nstart == iNULL: // end of this concatenation
|
||||||
if start == iNULL { // this is the empty string
|
if start == iNULL { // this is the empty string
|
||||||
nop := p.re.Add(new(iNop));
|
nop := p.re.Add(new(_Nop));
|
||||||
return nop, nop;
|
return nop, nop;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -501,11 +501,11 @@ func (p *parser) Regexp() (start, end instr) {
|
|||||||
case '|':
|
case '|':
|
||||||
p.nextc();
|
p.nextc();
|
||||||
nstart, nend := p.Concatenation();
|
nstart, nend := p.Concatenation();
|
||||||
alt := new(iAlt);
|
alt := new(_Alt);
|
||||||
p.re.Add(alt);
|
p.re.Add(alt);
|
||||||
alt.left = start;
|
alt.left = start;
|
||||||
alt.next = nstart;
|
alt.next = nstart;
|
||||||
nop := new(iNop);
|
nop := new(_Nop);
|
||||||
p.re.Add(nop);
|
p.re.Add(nop);
|
||||||
end.SetNext(nop);
|
end.SetNext(nop);
|
||||||
nend.SetNext(nop);
|
nend.SetNext(nop);
|
||||||
@ -515,47 +515,47 @@ func (p *parser) Regexp() (start, end instr) {
|
|||||||
panic("unreachable");
|
panic("unreachable");
|
||||||
}
|
}
|
||||||
|
|
||||||
func UnNop(i instr) instr {
|
func unNop(i instr) instr {
|
||||||
for i.Type() == cNOP {
|
for i.Type() == _NOP {
|
||||||
i = i.Next()
|
i = i.Next()
|
||||||
}
|
}
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
func (re *regExp) EliminateNops() {
|
func (re *_RE) EliminateNops() {
|
||||||
for i := 0; i < re.inst.Len(); i++ {
|
for i := 0; i < re.inst.Len(); i++ {
|
||||||
inst := re.inst.At(i).(instr);
|
inst := re.inst.At(i).(instr);
|
||||||
if inst.Type() == cEND {
|
if inst.Type() == _END {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
inst.SetNext(UnNop(inst.Next()));
|
inst.SetNext(unNop(inst.Next()));
|
||||||
if inst.Type() == cALT {
|
if inst.Type() == _ALT {
|
||||||
alt := inst.(*iAlt);
|
alt := inst.(*_Alt);
|
||||||
alt.left = UnNop(alt.left);
|
alt.left = unNop(alt.left);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (re *regExp) Dump() {
|
func (re *_RE) Dump() {
|
||||||
for i := 0; i < re.inst.Len(); i++ {
|
for i := 0; i < re.inst.Len(); i++ {
|
||||||
inst := re.inst.At(i).(instr);
|
inst := re.inst.At(i).(instr);
|
||||||
print(inst.Index(), ": ");
|
print(inst.Index(), ": ");
|
||||||
inst.Print();
|
inst.Print();
|
||||||
if inst.Type() != cEND {
|
if inst.Type() != _END {
|
||||||
print(" -> ", inst.Next().Index())
|
print(" -> ", inst.Next().Index())
|
||||||
}
|
}
|
||||||
print("\n");
|
print("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (re *regExp) DoParse() {
|
func (re *_RE) DoParse() {
|
||||||
p := newParser(re);
|
p := newParser(re);
|
||||||
start := new(iStart);
|
start := new(_Start);
|
||||||
re.Add(start);
|
re.Add(start);
|
||||||
s, e := p.Regexp();
|
s, e := p.Regexp();
|
||||||
start.next = s;
|
start.next = s;
|
||||||
re.start = start;
|
re.start = start;
|
||||||
e.SetNext(re.Add(new(iEnd)));
|
e.SetNext(re.Add(new(_End)));
|
||||||
|
|
||||||
if debug {
|
if debug {
|
||||||
re.Dump();
|
re.Dump();
|
||||||
@ -571,8 +571,8 @@ func (re *regExp) DoParse() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func Compiler(str string, ch chan *regExp) {
|
func compiler(str string, ch chan *_RE) {
|
||||||
re := new(regExp);
|
re := new(_RE);
|
||||||
re.expr = str;
|
re.expr = str;
|
||||||
re.inst = array.New(0);
|
re.inst = array.New(0);
|
||||||
re.ch = ch;
|
re.ch = ch;
|
||||||
@ -589,8 +589,8 @@ export type Regexp interface {
|
|||||||
|
|
||||||
// Compile in separate goroutine; wait for result
|
// Compile in separate goroutine; wait for result
|
||||||
export func Compile(str string) (regexp Regexp, error *os.Error) {
|
export func Compile(str string) (regexp Regexp, error *os.Error) {
|
||||||
ch := make(chan *regExp);
|
ch := make(chan *_RE);
|
||||||
go Compiler(str, ch);
|
go compiler(str, ch);
|
||||||
re := <-ch;
|
re := <-ch;
|
||||||
return re, re.error
|
return re, re.error
|
||||||
}
|
}
|
||||||
@ -627,7 +627,7 @@ func addState(s []state, inst instr, match []int) []state {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
func (re *regExp) DoExecute(str string, pos int) []int {
|
func (re *_RE) DoExecute(str string, pos int) []int {
|
||||||
var s [2][]state; // TODO: use a vector when state values (not ptrs) can be vector elements
|
var s [2][]state; // TODO: use a vector when state values (not ptrs) can be vector elements
|
||||||
s[0] = make([]state, 10)[0:0];
|
s[0] = make([]state, 10)[0:0];
|
||||||
s[1] = make([]state, 10)[0:0];
|
s[1] = make([]state, 10)[0:0];
|
||||||
@ -658,43 +658,43 @@ func (re *regExp) DoExecute(str string, pos int) []int {
|
|||||||
for i := 0; i < len(s[in]); i++ {
|
for i := 0; i < len(s[in]); i++ {
|
||||||
st := s[in][i];
|
st := s[in][i];
|
||||||
switch s[in][i].inst.Type() {
|
switch s[in][i].inst.Type() {
|
||||||
case cBOT:
|
case _BOT:
|
||||||
if pos == 0 {
|
if pos == 0 {
|
||||||
s[in] = addState(s[in], st.inst.Next(), st.match)
|
s[in] = addState(s[in], st.inst.Next(), st.match)
|
||||||
}
|
}
|
||||||
case cEOT:
|
case _EOT:
|
||||||
if pos == len(str) {
|
if pos == len(str) {
|
||||||
s[in] = addState(s[in], st.inst.Next(), st.match)
|
s[in] = addState(s[in], st.inst.Next(), st.match)
|
||||||
}
|
}
|
||||||
case cCHAR:
|
case _CHAR:
|
||||||
if c == st.inst.(*iChar).char {
|
if c == st.inst.(*_Char).char {
|
||||||
s[out] = addState(s[out], st.inst.Next(), st.match)
|
s[out] = addState(s[out], st.inst.Next(), st.match)
|
||||||
}
|
}
|
||||||
case cCHARCLASS:
|
case _CHARCLASS:
|
||||||
if st.inst.(*iCharClass).Matches(c) {
|
if st.inst.(*_CharClass).Matches(c) {
|
||||||
s[out] = addState(s[out], st.inst.Next(), st.match)
|
s[out] = addState(s[out], st.inst.Next(), st.match)
|
||||||
}
|
}
|
||||||
case cANY:
|
case _ANY:
|
||||||
if c != endOfFile {
|
if c != endOfFile {
|
||||||
s[out] = addState(s[out], st.inst.Next(), st.match)
|
s[out] = addState(s[out], st.inst.Next(), st.match)
|
||||||
}
|
}
|
||||||
case cBRA:
|
case _BRA:
|
||||||
n := st.inst.(*iBra).n;
|
n := st.inst.(*_Bra).n;
|
||||||
st.match[2*n] = pos;
|
st.match[2*n] = pos;
|
||||||
s[in] = addState(s[in], st.inst.Next(), st.match);
|
s[in] = addState(s[in], st.inst.Next(), st.match);
|
||||||
case cEBRA:
|
case _EBRA:
|
||||||
n := st.inst.(*iEbra).n;
|
n := st.inst.(*_Ebra).n;
|
||||||
st.match[2*n+1] = pos;
|
st.match[2*n+1] = pos;
|
||||||
s[in] = addState(s[in], st.inst.Next(), st.match);
|
s[in] = addState(s[in], st.inst.Next(), st.match);
|
||||||
case cALT:
|
case _ALT:
|
||||||
s[in] = addState(s[in], st.inst.(*iAlt).left, st.match);
|
s[in] = addState(s[in], st.inst.(*_Alt).left, st.match);
|
||||||
// give other branch a copy of this match vector
|
// give other branch a copy of this match vector
|
||||||
s1 := make([]int, 2*(re.nbra+1));
|
s1 := make([]int, 2*(re.nbra+1));
|
||||||
for i := 0; i < len(s1); i++ {
|
for i := 0; i < len(s1); i++ {
|
||||||
s1[i] = st.match[i]
|
s1[i] = st.match[i]
|
||||||
}
|
}
|
||||||
s[in] = addState(s[in], st.inst.Next(), s1);
|
s[in] = addState(s[in], st.inst.Next(), s1);
|
||||||
case cEND:
|
case _END:
|
||||||
// choose leftmost longest
|
// choose leftmost longest
|
||||||
if !found || // first
|
if !found || // first
|
||||||
st.match[0] < final.match[0] || // leftmost
|
st.match[0] < final.match[0] || // leftmost
|
||||||
@ -714,17 +714,17 @@ func (re *regExp) DoExecute(str string, pos int) []int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (re *regExp) Execute(s string) []int {
|
func (re *_RE) Execute(s string) []int {
|
||||||
return re.DoExecute(s, 0)
|
return re.DoExecute(s, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (re *regExp) Match(s string) bool {
|
func (re *_RE) Match(s string) bool {
|
||||||
return len(re.DoExecute(s, 0)) > 0
|
return len(re.DoExecute(s, 0)) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (re *regExp) MatchStrings(s string) []string {
|
func (re *_RE) MatchStrings(s string) []string {
|
||||||
r := re.DoExecute(s, 0);
|
r := re.DoExecute(s, 0);
|
||||||
if r == nil {
|
if r == nil {
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user