2008-10-09 20:40:53 -06:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Regular expression library.
|
|
|
|
|
|
|
|
package regexp
|
|
|
|
|
2008-10-11 17:48:05 -06:00
|
|
|
import (
|
2008-11-19 16:16:20 -07:00
|
|
|
"array";
|
2009-01-16 15:58:14 -07:00
|
|
|
"os";
|
|
|
|
"utf8";
|
2008-10-11 17:48:05 -06:00
|
|
|
)
|
|
|
|
|
2009-01-15 18:22:15 -07:00
|
|
|
var debug = false;
|
2008-10-14 17:32:43 -06:00
|
|
|
|
2008-10-09 20:40:53 -06:00
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
var ErrInternal = os.NewError("internal error");
|
|
|
|
var ErrUnmatchedLpar = os.NewError("unmatched '('");
|
|
|
|
var ErrUnmatchedRpar = os.NewError("unmatched ')'");
|
|
|
|
var ErrUnmatchedLbkt = os.NewError("unmatched '['");
|
|
|
|
var ErrUnmatchedRbkt = os.NewError("unmatched ']'");
|
|
|
|
var ErrBadRange = os.NewError("bad range in character class");
|
|
|
|
var ErrExtraneousBackslash = os.NewError("extraneous backslash");
|
|
|
|
var ErrBadClosure = os.NewError("repeated closure (**, ++, etc.)");
|
|
|
|
var ErrBareClosure = os.NewError("closure applies to nothing");
|
|
|
|
var ErrBadBackslash = os.NewError("illegal backslash escape");
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// An instruction executed by the NFA
|
2009-01-15 18:22:15 -07:00
|
|
|
type instr interface {
|
2009-01-16 11:34:36 -07:00
|
|
|
Type() int; // the type of this instruction: _CHAR, _ANY, etc.
|
2009-01-15 18:22:15 -07:00
|
|
|
Next() instr; // the instruction to execute after this one
|
|
|
|
SetNext(i instr);
|
2008-10-11 17:48:05 -06:00
|
|
|
Index() int;
|
|
|
|
SetIndex(i int);
|
|
|
|
Print();
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
2008-10-26 09:28:33 -06:00
|
|
|
// Fields and methods common to all instructions
|
2009-01-16 11:34:36 -07:00
|
|
|
type _Common struct {
|
2009-01-15 18:22:15 -07:00
|
|
|
next instr;
|
2008-10-26 09:28:33 -06:00
|
|
|
index int;
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (c *_Common) Next() instr { return c.next }
|
|
|
|
func (c *_Common) SetNext(i instr) { c.next = i }
|
|
|
|
func (c *_Common) Index() int { return c.index }
|
|
|
|
func (c *_Common) SetIndex(i int) { c.index = i }
|
2008-10-26 09:28:33 -06:00
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
type _RE struct {
|
2008-10-09 20:40:53 -06:00
|
|
|
expr string; // the original expression
|
2009-01-16 11:34:36 -07:00
|
|
|
ch chan<- *_RE; // reply channel when we're done
|
2008-10-11 17:48:05 -06:00
|
|
|
error *os.Error; // compile- or run-time error; nil if OK
|
2008-11-19 16:16:20 -07:00
|
|
|
inst *array.Array;
|
2009-01-15 18:22:15 -07:00
|
|
|
start instr;
|
2008-10-14 17:32:43 -06:00
|
|
|
nbra int; // number of brackets in expression, for subexpressions
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2009-01-16 11:34:36 -07:00
|
|
|
_START // beginning of program
|
2008-10-09 20:40:53 -06:00
|
|
|
= iota;
|
2009-01-16 11:34:36 -07:00
|
|
|
_END; // end of program: success
|
|
|
|
_BOT; // '^' beginning of text
|
|
|
|
_EOT; // '$' end of text
|
|
|
|
_CHAR; // 'a' regular character
|
|
|
|
_CHARCLASS; // [a-z] character class
|
|
|
|
_ANY; // '.' any character
|
|
|
|
_BRA; // '(' parenthesized expression
|
|
|
|
_EBRA; // ')'; end of '(' parenthesized expression
|
|
|
|
_ALT; // '|' alternation
|
|
|
|
_NOP; // do nothing; makes it easy to link without patching
|
2008-10-09 20:40:53 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// --- START start of program
|
2009-01-16 11:34:36 -07:00
|
|
|
type _Start struct {
|
|
|
|
_Common
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (start *_Start) Type() int { return _START }
|
|
|
|
func (start *_Start) Print() { print("start") }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// --- END end of program
|
2009-01-16 11:34:36 -07:00
|
|
|
type _End struct {
|
|
|
|
_Common
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (end *_End) Type() int { return _END }
|
|
|
|
func (end *_End) Print() { print("end") }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// --- BOT beginning of text
|
2009-01-16 11:34:36 -07:00
|
|
|
type _Bot struct {
|
|
|
|
_Common
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (bot *_Bot) Type() int { return _BOT }
|
|
|
|
func (bot *_Bot) Print() { print("bot") }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// --- EOT end of text
|
2009-01-16 11:34:36 -07:00
|
|
|
type _Eot struct {
|
|
|
|
_Common
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (eot *_Eot) Type() int { return _EOT }
|
|
|
|
func (eot *_Eot) Print() { print("eot") }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// --- CHAR a regular character
|
2009-01-16 11:34:36 -07:00
|
|
|
type _Char struct {
|
|
|
|
_Common;
|
2008-10-14 17:32:43 -06:00
|
|
|
char int;
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (char *_Char) Type() int { return _CHAR }
|
|
|
|
func (char *_Char) Print() { print("char ", string(char.char)) }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func newChar(char int) *_Char {
|
|
|
|
c := new(_Char);
|
2008-10-09 20:40:53 -06:00
|
|
|
c.char = char;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2008-10-11 17:48:05 -06:00
|
|
|
// --- CHARCLASS [a-z]
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
type _CharClass struct {
|
|
|
|
_Common;
|
2008-10-11 17:48:05 -06:00
|
|
|
char int;
|
|
|
|
negate bool; // is character class negated? ([^a-z])
|
2008-11-19 16:16:20 -07:00
|
|
|
// array of int, stored pairwise: [a-z] is (a,z); x is (x,x):
|
|
|
|
ranges *array.IntArray;
|
2008-10-11 17:48:05 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (cclass *_CharClass) Type() int { return _CHARCLASS }
|
2008-10-26 09:28:33 -06:00
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (cclass *_CharClass) Print() {
|
2008-10-11 17:48:05 -06:00
|
|
|
print("charclass");
|
|
|
|
if cclass.negate {
|
|
|
|
print(" (negated)");
|
|
|
|
}
|
|
|
|
for i := 0; i < cclass.ranges.Len(); i += 2 {
|
2008-11-19 16:16:20 -07:00
|
|
|
l := cclass.ranges.At(i);
|
|
|
|
r := cclass.ranges.At(i+1);
|
2008-10-11 17:48:05 -06:00
|
|
|
if l == r {
|
|
|
|
print(" [", string(l), "]");
|
|
|
|
} else {
|
|
|
|
print(" [", string(l), "-", string(r), "]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (cclass *_CharClass) AddRange(a, b int) {
|
2008-10-11 17:48:05 -06:00
|
|
|
// range is a through b inclusive
|
2008-11-19 16:16:20 -07:00
|
|
|
cclass.ranges.Push(a);
|
|
|
|
cclass.ranges.Push(b);
|
2008-10-11 17:48:05 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (cclass *_CharClass) Matches(c int) bool {
|
2008-10-14 17:32:43 -06:00
|
|
|
for i := 0; i < cclass.ranges.Len(); i = i+2 {
|
2008-11-19 16:16:20 -07:00
|
|
|
min := cclass.ranges.At(i);
|
|
|
|
max := cclass.ranges.At(i+1);
|
2008-10-14 17:32:43 -06:00
|
|
|
if min <= c && c <= max {
|
|
|
|
return !cclass.negate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cclass.negate
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func newCharClass() *_CharClass {
|
|
|
|
c := new(_CharClass);
|
2008-11-19 16:16:20 -07:00
|
|
|
c.ranges = array.NewIntArray(0);
|
2008-10-11 17:48:05 -06:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2008-10-09 20:40:53 -06:00
|
|
|
// --- ANY any character
|
2009-01-16 11:34:36 -07:00
|
|
|
type _Any struct {
|
|
|
|
_Common
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (any *_Any) Type() int { return _ANY }
|
|
|
|
func (any *_Any) Print() { print("any") }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// --- BRA parenthesized expression
|
2009-01-16 11:34:36 -07:00
|
|
|
type _Bra struct {
|
|
|
|
_Common;
|
2008-10-10 19:42:19 -06:00
|
|
|
n int; // subexpression number
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (bra *_Bra) Type() int { return _BRA }
|
|
|
|
func (bra *_Bra) Print() { print("bra", bra.n); }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// --- EBRA end of parenthesized expression
|
2009-01-16 11:34:36 -07:00
|
|
|
type _Ebra struct {
|
|
|
|
_Common;
|
2008-10-09 20:40:53 -06:00
|
|
|
n int; // subexpression number
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (ebra *_Ebra) Type() int { return _EBRA }
|
|
|
|
func (ebra *_Ebra) Print() { print("ebra ", ebra.n); }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// --- ALT alternation
|
2009-01-16 11:34:36 -07:00
|
|
|
type _Alt struct {
|
|
|
|
_Common;
|
2009-01-15 18:22:15 -07:00
|
|
|
left instr; // other branch
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (alt *_Alt) Type() int { return _ALT }
|
|
|
|
func (alt *_Alt) Print() { print("alt(", alt.left.Index(), ")"); }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// --- NOP no operation
|
2009-01-16 11:34:36 -07:00
|
|
|
type _Nop struct {
|
|
|
|
_Common
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (nop *_Nop) Type() int { return _NOP }
|
|
|
|
func (nop *_Nop) Print() { print("nop") }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// report error and exit compiling/executing goroutine
|
2009-01-16 11:34:36 -07:00
|
|
|
func (re *_RE) Error(err *os.Error) {
|
2008-10-09 20:40:53 -06:00
|
|
|
re.error = err;
|
|
|
|
re.ch <- re;
|
2009-01-16 15:58:14 -07:00
|
|
|
sys.Goexit();
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (re *_RE) Add(i instr) instr {
|
2008-10-11 17:48:05 -06:00
|
|
|
i.SetIndex(re.inst.Len());
|
2008-11-19 16:16:20 -07:00
|
|
|
re.inst.Push(i);
|
2008-10-11 17:48:05 -06:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2009-01-15 18:22:15 -07:00
|
|
|
type parser struct {
|
2009-01-16 11:34:36 -07:00
|
|
|
re *_RE;
|
2008-10-10 19:42:19 -06:00
|
|
|
nlpar int; // number of unclosed lpars
|
2008-10-09 20:40:53 -06:00
|
|
|
pos int;
|
|
|
|
ch int;
|
|
|
|
}
|
|
|
|
|
2009-01-15 18:22:15 -07:00
|
|
|
const endOfFile = -1
|
2008-10-09 20:40:53 -06:00
|
|
|
|
2009-01-15 18:22:15 -07:00
|
|
|
func (p *parser) c() int {
|
2008-10-09 20:40:53 -06:00
|
|
|
return p.ch;
|
|
|
|
}
|
|
|
|
|
2009-01-15 18:22:15 -07:00
|
|
|
func (p *parser) nextc() int {
|
2008-10-09 20:40:53 -06:00
|
|
|
if p.pos >= len(p.re.expr) {
|
2009-01-15 18:22:15 -07:00
|
|
|
p.ch = endOfFile
|
2008-10-09 20:40:53 -06:00
|
|
|
} else {
|
2009-01-16 15:58:14 -07:00
|
|
|
c, w := utf8.DecodeRuneInString(p.re.expr, p.pos);
|
2008-10-09 20:40:53 -06:00
|
|
|
p.ch = c;
|
|
|
|
p.pos += w;
|
|
|
|
}
|
|
|
|
return p.ch;
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func newParser(re *_RE) *parser {
|
2009-01-15 18:22:15 -07:00
|
|
|
p := new(parser);
|
|
|
|
p.re = re;
|
|
|
|
p.nextc(); // load p.ch
|
|
|
|
return p;
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Grammar:
|
|
|
|
regexp:
|
|
|
|
concatenation { '|' concatenation }
|
|
|
|
concatenation:
|
|
|
|
{ closure }
|
|
|
|
closure:
|
2008-10-11 17:48:05 -06:00
|
|
|
term [ '*' | '+' | '?' ]
|
2008-10-09 20:40:53 -06:00
|
|
|
term:
|
2008-10-11 17:48:05 -06:00
|
|
|
'^'
|
|
|
|
'$'
|
2008-10-09 20:40:53 -06:00
|
|
|
'.'
|
|
|
|
character
|
2008-10-26 09:28:33 -06:00
|
|
|
'[' [ '^' ] character-ranges ']'
|
2008-10-09 20:40:53 -06:00
|
|
|
'(' regexp ')'
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2009-01-15 18:22:15 -07:00
|
|
|
func (p *parser) Regexp() (start, end instr)
|
2008-10-10 19:42:19 -06:00
|
|
|
|
2009-01-15 18:22:15 -07:00
|
|
|
var iNULL instr
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
func special(c int) bool {
|
2008-10-11 17:48:05 -06:00
|
|
|
s := `\.+*?()|[]`;
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
if c == int(s[i]) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func specialcclass(c int) bool {
|
|
|
|
s := `\-[]`;
|
2008-10-09 20:40:53 -06:00
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
if c == int(s[i]) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2009-01-15 18:22:15 -07:00
|
|
|
func (p *parser) CharClass() instr {
|
|
|
|
cc := newCharClass();
|
2008-10-11 17:48:05 -06:00
|
|
|
p.re.Add(cc);
|
|
|
|
if p.c() == '^' {
|
|
|
|
cc.negate = true;
|
|
|
|
p.nextc();
|
|
|
|
}
|
|
|
|
left := -1;
|
|
|
|
for {
|
|
|
|
switch c := p.c(); c {
|
2009-01-15 18:22:15 -07:00
|
|
|
case ']', endOfFile:
|
2008-10-11 17:48:05 -06:00
|
|
|
if left >= 0 {
|
|
|
|
p.re.Error(ErrBadRange);
|
|
|
|
}
|
|
|
|
return cc;
|
|
|
|
case '-': // do this before backslash processing
|
|
|
|
p.re.Error(ErrBadRange);
|
|
|
|
case '\\':
|
|
|
|
c = p.nextc();
|
|
|
|
switch {
|
2009-01-15 18:22:15 -07:00
|
|
|
case c == endOfFile:
|
2008-10-11 17:48:05 -06:00
|
|
|
p.re.Error(ErrExtraneousBackslash);
|
|
|
|
case c == 'n':
|
|
|
|
c = '\n';
|
|
|
|
case specialcclass(c):
|
|
|
|
// c is as delivered
|
|
|
|
default:
|
|
|
|
p.re.Error(ErrBadBackslash);
|
|
|
|
}
|
|
|
|
fallthrough;
|
|
|
|
default:
|
|
|
|
p.nextc();
|
|
|
|
switch {
|
|
|
|
case left < 0: // first of pair
|
|
|
|
if p.c() == '-' { // range
|
|
|
|
p.nextc();
|
|
|
|
left = c;
|
|
|
|
} else { // single char
|
|
|
|
cc.AddRange(c, c);
|
|
|
|
}
|
|
|
|
case left <= c: // second of pair
|
|
|
|
cc.AddRange(left, c);
|
|
|
|
left = -1;
|
|
|
|
default:
|
|
|
|
p.re.Error(ErrBadRange);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-15 18:22:15 -07:00
|
|
|
return iNULL
|
2008-10-11 17:48:05 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 18:22:15 -07:00
|
|
|
func (p *parser) Term() (start, end instr) {
|
2008-10-09 20:40:53 -06:00
|
|
|
switch c := p.c(); c {
|
2009-01-15 18:22:15 -07:00
|
|
|
case '|', endOfFile:
|
|
|
|
return iNULL, iNULL;
|
2008-10-21 18:18:34 -06:00
|
|
|
case '*', '+':
|
2008-10-09 20:40:53 -06:00
|
|
|
p.re.Error(ErrBareClosure);
|
2008-10-10 13:41:43 -06:00
|
|
|
case ')':
|
2008-10-10 19:42:19 -06:00
|
|
|
if p.nlpar == 0 {
|
|
|
|
p.re.Error(ErrUnmatchedRpar);
|
|
|
|
}
|
2009-01-15 18:22:15 -07:00
|
|
|
return iNULL, iNULL;
|
2008-10-11 17:48:05 -06:00
|
|
|
case ']':
|
|
|
|
p.re.Error(ErrUnmatchedRbkt);
|
|
|
|
case '^':
|
|
|
|
p.nextc();
|
2009-01-16 11:34:36 -07:00
|
|
|
start = p.re.Add(new(_Bot));
|
2008-10-11 17:48:05 -06:00
|
|
|
return start, start;
|
|
|
|
case '$':
|
|
|
|
p.nextc();
|
2009-01-16 11:34:36 -07:00
|
|
|
start = p.re.Add(new(_Eot));
|
2008-10-11 17:48:05 -06:00
|
|
|
return start, start;
|
2008-10-09 20:40:53 -06:00
|
|
|
case '.':
|
|
|
|
p.nextc();
|
2009-01-16 11:34:36 -07:00
|
|
|
start = p.re.Add(new(_Any));
|
2008-10-11 17:48:05 -06:00
|
|
|
return start, start;
|
|
|
|
case '[':
|
|
|
|
p.nextc();
|
|
|
|
start = p.CharClass();
|
|
|
|
if p.c() != ']' {
|
|
|
|
p.re.Error(ErrUnmatchedLbkt);
|
|
|
|
}
|
|
|
|
p.nextc();
|
2008-10-09 20:40:53 -06:00
|
|
|
return start, start;
|
|
|
|
case '(':
|
|
|
|
p.nextc();
|
2008-10-10 19:42:19 -06:00
|
|
|
p.nlpar++;
|
2008-10-14 18:45:49 -06:00
|
|
|
p.re.nbra++; // increment first so first subexpr is \1
|
|
|
|
nbra := p.re.nbra;
|
2008-10-09 20:40:53 -06:00
|
|
|
start, end = p.Regexp();
|
|
|
|
if p.c() != ')' {
|
|
|
|
p.re.Error(ErrUnmatchedLpar);
|
|
|
|
}
|
2008-10-10 19:42:19 -06:00
|
|
|
p.nlpar--;
|
2008-10-09 20:40:53 -06:00
|
|
|
p.nextc();
|
2009-01-16 11:34:36 -07:00
|
|
|
bra := new(_Bra);
|
2008-10-11 17:48:05 -06:00
|
|
|
p.re.Add(bra);
|
2009-01-16 11:34:36 -07:00
|
|
|
ebra := new(_Ebra);
|
2008-10-11 17:48:05 -06:00
|
|
|
p.re.Add(ebra);
|
2008-10-14 18:45:49 -06:00
|
|
|
bra.n = nbra;
|
|
|
|
ebra.n = nbra;
|
2009-01-15 18:22:15 -07:00
|
|
|
if start == iNULL {
|
|
|
|
if end == iNULL { p.re.Error(ErrInternal) }
|
2008-10-10 19:42:19 -06:00
|
|
|
start = ebra
|
2008-10-09 20:40:53 -06:00
|
|
|
} else {
|
2008-10-10 19:42:19 -06:00
|
|
|
end.SetNext(ebra);
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
bra.SetNext(start);
|
2008-10-10 19:42:19 -06:00
|
|
|
return bra, ebra;
|
2008-10-09 20:40:53 -06:00
|
|
|
case '\\':
|
|
|
|
c = p.nextc();
|
|
|
|
switch {
|
2009-01-15 18:22:15 -07:00
|
|
|
case c == endOfFile:
|
2008-10-09 20:40:53 -06:00
|
|
|
p.re.Error(ErrExtraneousBackslash);
|
|
|
|
case c == 'n':
|
|
|
|
c = '\n';
|
|
|
|
case special(c):
|
|
|
|
// c is as delivered
|
|
|
|
default:
|
|
|
|
p.re.Error(ErrBadBackslash);
|
|
|
|
}
|
|
|
|
fallthrough;
|
|
|
|
default:
|
|
|
|
p.nextc();
|
2009-01-15 18:22:15 -07:00
|
|
|
start = newChar(c);
|
2008-10-11 17:48:05 -06:00
|
|
|
p.re.Add(start);
|
2008-10-09 20:40:53 -06:00
|
|
|
return start, start
|
|
|
|
}
|
|
|
|
panic("unreachable");
|
|
|
|
}
|
|
|
|
|
2009-01-15 18:22:15 -07:00
|
|
|
func (p *parser) Closure() (start, end instr) {
|
2008-10-09 20:40:53 -06:00
|
|
|
start, end = p.Term();
|
2009-01-15 18:22:15 -07:00
|
|
|
if start == iNULL {
|
2008-10-14 18:45:49 -06:00
|
|
|
return
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
switch p.c() {
|
|
|
|
case '*':
|
|
|
|
// (start,end)*:
|
2009-01-16 11:34:36 -07:00
|
|
|
alt := new(_Alt);
|
2008-10-11 17:48:05 -06:00
|
|
|
p.re.Add(alt);
|
2008-10-10 19:42:19 -06:00
|
|
|
end.SetNext(alt); // after end, do alt
|
2008-10-09 20:40:53 -06:00
|
|
|
alt.left = start; // alternate brach: return to start
|
2008-10-10 19:42:19 -06:00
|
|
|
start = alt; // alt becomes new (start, end)
|
|
|
|
end = alt;
|
2008-10-09 20:40:53 -06:00
|
|
|
case '+':
|
|
|
|
// (start,end)+:
|
2009-01-16 11:34:36 -07:00
|
|
|
alt := new(_Alt);
|
2008-10-11 17:48:05 -06:00
|
|
|
p.re.Add(alt);
|
2008-10-10 19:42:19 -06:00
|
|
|
end.SetNext(alt); // after end, do alt
|
2008-10-09 20:40:53 -06:00
|
|
|
alt.left = start; // alternate brach: return to start
|
2008-10-10 19:42:19 -06:00
|
|
|
end = alt; // start is unchanged; end is alt
|
2008-10-09 20:40:53 -06:00
|
|
|
case '?':
|
|
|
|
// (start,end)?:
|
2009-01-16 11:34:36 -07:00
|
|
|
alt := new(_Alt);
|
2008-10-11 17:48:05 -06:00
|
|
|
p.re.Add(alt);
|
2009-01-16 11:34:36 -07:00
|
|
|
nop := new(_Nop);
|
2008-10-11 17:48:05 -06:00
|
|
|
p.re.Add(nop);
|
2008-10-09 20:40:53 -06:00
|
|
|
alt.left = start; // alternate branch is start
|
2008-10-10 19:42:19 -06:00
|
|
|
alt.next = nop; // follow on to nop
|
|
|
|
end.SetNext(nop); // after end, go to nop
|
|
|
|
start = alt; // start is now alt
|
|
|
|
end = nop; // end is nop pointed to by both branches
|
2008-10-09 20:40:53 -06:00
|
|
|
default:
|
2008-10-14 18:45:49 -06:00
|
|
|
return
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
switch p.nextc() {
|
|
|
|
case '*', '+', '?':
|
|
|
|
p.re.Error(ErrBadClosure);
|
|
|
|
}
|
2008-10-14 18:45:49 -06:00
|
|
|
return
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 18:22:15 -07:00
|
|
|
func (p *parser) Concatenation() (start, end instr) {
|
|
|
|
start, end = iNULL, iNULL;
|
2008-10-09 20:40:53 -06:00
|
|
|
for {
|
|
|
|
nstart, nend := p.Closure();
|
|
|
|
switch {
|
2009-01-15 18:22:15 -07:00
|
|
|
case nstart == iNULL: // end of this concatenation
|
|
|
|
if start == iNULL { // this is the empty string
|
2009-01-16 11:34:36 -07:00
|
|
|
nop := p.re.Add(new(_Nop));
|
2008-10-11 17:48:05 -06:00
|
|
|
return nop, nop;
|
|
|
|
}
|
2008-10-14 18:45:49 -06:00
|
|
|
return;
|
2009-01-15 18:22:15 -07:00
|
|
|
case start == iNULL: // this is first element of concatenation
|
2008-10-09 20:40:53 -06:00
|
|
|
start, end = nstart, nend;
|
|
|
|
default:
|
2008-10-10 19:42:19 -06:00
|
|
|
end.SetNext(nstart);
|
2008-10-09 20:40:53 -06:00
|
|
|
end = nend;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("unreachable");
|
|
|
|
}
|
|
|
|
|
2009-01-15 18:22:15 -07:00
|
|
|
func (p *parser) Regexp() (start, end instr) {
|
2008-10-09 20:40:53 -06:00
|
|
|
start, end = p.Concatenation();
|
|
|
|
for {
|
|
|
|
switch p.c() {
|
|
|
|
default:
|
2008-10-14 18:45:49 -06:00
|
|
|
return;
|
2008-10-09 20:40:53 -06:00
|
|
|
case '|':
|
|
|
|
p.nextc();
|
|
|
|
nstart, nend := p.Concatenation();
|
2009-01-16 11:34:36 -07:00
|
|
|
alt := new(_Alt);
|
2008-10-11 17:48:05 -06:00
|
|
|
p.re.Add(alt);
|
2008-10-09 20:40:53 -06:00
|
|
|
alt.left = start;
|
|
|
|
alt.next = nstart;
|
2009-01-16 11:34:36 -07:00
|
|
|
nop := new(_Nop);
|
2008-10-11 17:48:05 -06:00
|
|
|
p.re.Add(nop);
|
2008-10-10 19:42:19 -06:00
|
|
|
end.SetNext(nop);
|
|
|
|
nend.SetNext(nop);
|
|
|
|
start, end = alt, nop;
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("unreachable");
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func unNop(i instr) instr {
|
|
|
|
for i.Type() == _NOP {
|
2008-10-10 19:42:19 -06:00
|
|
|
i = i.Next()
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (re *_RE) EliminateNops() {
|
2008-10-11 17:48:05 -06:00
|
|
|
for i := 0; i < re.inst.Len(); i++ {
|
2009-01-15 18:22:15 -07:00
|
|
|
inst := re.inst.At(i).(instr);
|
2009-01-16 11:34:36 -07:00
|
|
|
if inst.Type() == _END {
|
2008-10-11 17:48:05 -06:00
|
|
|
continue
|
|
|
|
}
|
2009-01-16 11:34:36 -07:00
|
|
|
inst.SetNext(unNop(inst.Next()));
|
|
|
|
if inst.Type() == _ALT {
|
|
|
|
alt := inst.(*_Alt);
|
|
|
|
alt.left = unNop(alt.left);
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (re *_RE) Dump() {
|
2008-10-11 17:48:05 -06:00
|
|
|
for i := 0; i < re.inst.Len(); i++ {
|
2009-01-15 18:22:15 -07:00
|
|
|
inst := re.inst.At(i).(instr);
|
2008-10-11 17:48:05 -06:00
|
|
|
print(inst.Index(), ": ");
|
|
|
|
inst.Print();
|
2009-01-16 11:34:36 -07:00
|
|
|
if inst.Type() != _END {
|
2008-10-11 17:48:05 -06:00
|
|
|
print(" -> ", inst.Next().Index())
|
2008-10-10 19:42:19 -06:00
|
|
|
}
|
2008-10-11 17:48:05 -06:00
|
|
|
print("\n");
|
2008-10-10 19:42:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (re *_RE) DoParse() {
|
2009-01-15 18:22:15 -07:00
|
|
|
p := newParser(re);
|
2009-01-16 11:34:36 -07:00
|
|
|
start := new(_Start);
|
2008-10-11 17:48:05 -06:00
|
|
|
re.Add(start);
|
2009-01-15 18:22:15 -07:00
|
|
|
s, e := p.Regexp();
|
2008-10-09 20:40:53 -06:00
|
|
|
start.next = s;
|
2008-10-10 19:42:19 -06:00
|
|
|
re.start = start;
|
2009-01-16 11:34:36 -07:00
|
|
|
e.SetNext(re.Add(new(_End)));
|
2008-10-09 20:40:53 -06:00
|
|
|
|
2008-10-14 17:32:43 -06:00
|
|
|
if debug {
|
|
|
|
re.Dump();
|
|
|
|
println();
|
|
|
|
}
|
2008-10-09 20:40:53 -06:00
|
|
|
|
2008-10-11 17:48:05 -06:00
|
|
|
re.EliminateNops();
|
2008-10-09 20:40:53 -06:00
|
|
|
|
2008-10-14 17:32:43 -06:00
|
|
|
if debug {
|
|
|
|
re.Dump();
|
|
|
|
println();
|
|
|
|
}
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
2008-10-14 17:32:43 -06:00
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func compiler(str string, ch chan *_RE) {
|
|
|
|
re := new(_RE);
|
2008-10-09 20:40:53 -06:00
|
|
|
re.expr = str;
|
2008-11-19 16:16:20 -07:00
|
|
|
re.inst = array.New(0);
|
2008-10-09 20:40:53 -06:00
|
|
|
re.ch = ch;
|
|
|
|
re.DoParse();
|
|
|
|
ch <- re;
|
|
|
|
}
|
|
|
|
|
2009-01-06 14:54:53 -07:00
|
|
|
// Public interface has only execute functionality
|
2009-01-20 15:40:40 -07:00
|
|
|
type Regexp interface {
|
2009-01-06 14:54:53 -07:00
|
|
|
Execute(s string) []int;
|
|
|
|
Match(s string) bool;
|
|
|
|
MatchStrings(s string) []string;
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
2008-10-14 17:32:43 -06:00
|
|
|
// Compile in separate goroutine; wait for result
|
2009-01-20 15:40:40 -07:00
|
|
|
func Compile(str string) (regexp Regexp, error *os.Error) {
|
2009-01-16 11:34:36 -07:00
|
|
|
ch := make(chan *_RE);
|
|
|
|
go compiler(str, ch);
|
2008-10-09 20:40:53 -06:00
|
|
|
re := <-ch;
|
|
|
|
return re, re.error
|
|
|
|
}
|
2008-10-14 17:32:43 -06:00
|
|
|
|
2009-01-15 18:22:15 -07:00
|
|
|
type state struct {
|
|
|
|
inst instr; // next instruction to execute
|
2008-12-18 23:37:22 -07:00
|
|
|
match []int; // pairs of bracketing submatches. 0th is start,end
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Append new state to to-do list. Leftmost-longest wins so avoid
|
|
|
|
// adding a state that's already active.
|
2009-01-15 18:22:15 -07:00
|
|
|
func addState(s []state, inst instr, match []int) []state {
|
2008-10-14 17:32:43 -06:00
|
|
|
index := inst.Index();
|
|
|
|
l := len(s);
|
|
|
|
pos := match[0];
|
|
|
|
// TODO: Once the state is a vector and we can do insert, have inputs always
|
|
|
|
// go in order correctly and this "earlier" test is never necessary,
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
if s[i].inst.Index() == index && // same instruction
|
|
|
|
s[i].match[0] < pos { // earlier match already going; lefmost wins
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if l == cap(s) {
|
2009-01-15 18:22:15 -07:00
|
|
|
s1 := make([]state, 2*l)[0:l];
|
2008-10-14 17:32:43 -06:00
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
s1[i] = s[i];
|
|
|
|
}
|
|
|
|
s = s1;
|
|
|
|
}
|
|
|
|
s = s[0:l+1];
|
|
|
|
s[l].inst = inst;
|
|
|
|
s[l].match = match;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (re *_RE) DoExecute(str string, pos int) []int {
|
2009-01-15 18:22:15 -07:00
|
|
|
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[1] = make([]state, 10)[0:0];
|
2008-10-14 17:32:43 -06:00
|
|
|
in, out := 0, 1;
|
2009-01-15 18:22:15 -07:00
|
|
|
var final state;
|
2008-10-14 17:32:43 -06:00
|
|
|
found := false;
|
|
|
|
for pos <= len(str) {
|
|
|
|
if !found {
|
|
|
|
// prime the pump if we haven't seen a match yet
|
2009-01-06 16:19:02 -07:00
|
|
|
match := make([]int, 2*(re.nbra+1));
|
2008-10-14 18:45:49 -06:00
|
|
|
for i := 0; i < len(match); i++ {
|
|
|
|
match[i] = -1; // no match seen; catches cases like "a(b)?c" on "ac"
|
|
|
|
}
|
2008-10-14 17:32:43 -06:00
|
|
|
match[0] = pos;
|
2009-01-15 18:22:15 -07:00
|
|
|
s[out] = addState(s[out], re.start.Next(), match);
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
|
|
|
in, out = out, in; // old out state is new in state
|
|
|
|
s[out] = s[out][0:0]; // clear out state
|
|
|
|
if len(s[in]) == 0 {
|
|
|
|
// machine has completed
|
|
|
|
break;
|
|
|
|
}
|
2008-10-14 18:45:49 -06:00
|
|
|
charwidth := 1;
|
2009-01-15 18:22:15 -07:00
|
|
|
c := endOfFile;
|
2008-10-14 17:32:43 -06:00
|
|
|
if pos < len(str) {
|
2009-01-16 15:58:14 -07:00
|
|
|
c, charwidth = utf8.DecodeRuneInString(str, pos);
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
|
|
|
for i := 0; i < len(s[in]); i++ {
|
2009-01-15 18:22:15 -07:00
|
|
|
st := s[in][i];
|
2008-10-14 17:32:43 -06:00
|
|
|
switch s[in][i].inst.Type() {
|
2009-01-16 11:34:36 -07:00
|
|
|
case _BOT:
|
2008-10-14 17:32:43 -06:00
|
|
|
if pos == 0 {
|
2009-01-15 18:22:15 -07:00
|
|
|
s[in] = addState(s[in], st.inst.Next(), st.match)
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
2009-01-16 11:34:36 -07:00
|
|
|
case _EOT:
|
2008-10-14 17:32:43 -06:00
|
|
|
if pos == len(str) {
|
2009-01-15 18:22:15 -07:00
|
|
|
s[in] = addState(s[in], st.inst.Next(), st.match)
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
2009-01-16 11:34:36 -07:00
|
|
|
case _CHAR:
|
|
|
|
if c == st.inst.(*_Char).char {
|
2009-01-15 18:22:15 -07:00
|
|
|
s[out] = addState(s[out], st.inst.Next(), st.match)
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
2009-01-16 11:34:36 -07:00
|
|
|
case _CHARCLASS:
|
|
|
|
if st.inst.(*_CharClass).Matches(c) {
|
2009-01-15 18:22:15 -07:00
|
|
|
s[out] = addState(s[out], st.inst.Next(), st.match)
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
2009-01-16 11:34:36 -07:00
|
|
|
case _ANY:
|
2009-01-15 18:22:15 -07:00
|
|
|
if c != endOfFile {
|
|
|
|
s[out] = addState(s[out], st.inst.Next(), st.match)
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
2009-01-16 11:34:36 -07:00
|
|
|
case _BRA:
|
|
|
|
n := st.inst.(*_Bra).n;
|
2009-01-15 18:22:15 -07:00
|
|
|
st.match[2*n] = pos;
|
|
|
|
s[in] = addState(s[in], st.inst.Next(), st.match);
|
2009-01-16 11:34:36 -07:00
|
|
|
case _EBRA:
|
|
|
|
n := st.inst.(*_Ebra).n;
|
2009-01-15 18:22:15 -07:00
|
|
|
st.match[2*n+1] = pos;
|
|
|
|
s[in] = addState(s[in], st.inst.Next(), st.match);
|
2009-01-16 11:34:36 -07:00
|
|
|
case _ALT:
|
|
|
|
s[in] = addState(s[in], st.inst.(*_Alt).left, st.match);
|
2008-10-14 17:32:43 -06:00
|
|
|
// give other branch a copy of this match vector
|
2009-01-06 16:19:02 -07:00
|
|
|
s1 := make([]int, 2*(re.nbra+1));
|
2008-10-14 17:32:43 -06:00
|
|
|
for i := 0; i < len(s1); i++ {
|
2009-01-15 18:22:15 -07:00
|
|
|
s1[i] = st.match[i]
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
2009-01-15 18:22:15 -07:00
|
|
|
s[in] = addState(s[in], st.inst.Next(), s1);
|
2009-01-16 11:34:36 -07:00
|
|
|
case _END:
|
2008-10-14 17:32:43 -06:00
|
|
|
// choose leftmost longest
|
|
|
|
if !found || // first
|
2009-01-15 18:22:15 -07:00
|
|
|
st.match[0] < final.match[0] || // leftmost
|
|
|
|
(st.match[0] == final.match[0] && pos > final.match[1]) { // longest
|
|
|
|
final = st;
|
2008-10-14 17:32:43 -06:00
|
|
|
final.match[1] = pos;
|
|
|
|
}
|
|
|
|
found = true;
|
|
|
|
default:
|
2009-01-15 18:22:15 -07:00
|
|
|
st.inst.Print();
|
2008-10-14 17:32:43 -06:00
|
|
|
panic("unknown instruction in execute");
|
|
|
|
}
|
|
|
|
}
|
2008-10-14 18:45:49 -06:00
|
|
|
pos += charwidth;
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
|
|
|
return final.match;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (re *_RE) Execute(s string) []int {
|
2008-10-14 17:32:43 -06:00
|
|
|
return re.DoExecute(s, 0)
|
|
|
|
}
|
2009-01-06 14:54:53 -07:00
|
|
|
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (re *_RE) Match(s string) bool {
|
2009-01-06 14:54:53 -07:00
|
|
|
return len(re.DoExecute(s, 0)) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func (re *_RE) MatchStrings(s string) []string {
|
2009-01-06 14:54:53 -07:00
|
|
|
r := re.DoExecute(s, 0);
|
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2009-01-06 16:19:02 -07:00
|
|
|
a := make([]string, len(r)/2);
|
2009-01-06 14:54:53 -07:00
|
|
|
for i := 0; i < len(r); i += 2 {
|
|
|
|
a[i/2] = s[r[i] : r[i+1]]
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exported function for simple boolean check. Anything more fancy
|
|
|
|
// needs a call to Compile.
|
2009-01-20 15:40:40 -07:00
|
|
|
func Match(pattern string, s string) (matched bool, error *os.Error) {
|
2009-01-06 14:54:53 -07:00
|
|
|
re, err := Compile(pattern);
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return re.Match(s), nil
|
|
|
|
}
|