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 (
|
|
|
|
"os";
|
2008-11-19 16:16:20 -07:00
|
|
|
"array";
|
2008-10-11 17:48:05 -06:00
|
|
|
)
|
|
|
|
|
2008-10-14 17:32:43 -06:00
|
|
|
export var debug = false;
|
|
|
|
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
export var ErrInternal = os.NewError("internal error");
|
|
|
|
export var ErrUnmatchedLpar = os.NewError("unmatched '('");
|
|
|
|
export var ErrUnmatchedRpar = os.NewError("unmatched ')'");
|
2008-10-11 17:48:05 -06:00
|
|
|
export var ErrUnmatchedLbkt = os.NewError("unmatched '['");
|
|
|
|
export var ErrUnmatchedRbkt = os.NewError("unmatched ']'");
|
|
|
|
export var ErrBadRange = os.NewError("bad range in character class");
|
2008-10-09 20:40:53 -06:00
|
|
|
export var ErrExtraneousBackslash = os.NewError("extraneous backslash");
|
|
|
|
export var ErrBadClosure = os.NewError("repeated closure (**, ++, etc.)");
|
|
|
|
export var ErrBareClosure = os.NewError("closure applies to nothing");
|
|
|
|
export var ErrBadBackslash = os.NewError("illegal backslash escape");
|
|
|
|
|
|
|
|
// An instruction executed by the NFA
|
|
|
|
type Inst interface {
|
|
|
|
Type() int; // the type of this instruction: CHAR, ANY, etc.
|
2008-10-10 19:42:19 -06:00
|
|
|
Next() Inst; // the instruction to execute after this one
|
|
|
|
SetNext(i Inst);
|
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
|
|
|
|
type Common struct {
|
|
|
|
next Inst;
|
|
|
|
index int;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Common) Next() Inst { return c.next }
|
|
|
|
func (c *Common) SetNext(i Inst) { c.next = i }
|
|
|
|
func (c *Common) Index() int { return c.index }
|
|
|
|
func (c *Common) SetIndex(i int) { c.index = i }
|
|
|
|
|
2008-10-09 20:40:53 -06:00
|
|
|
type RE struct {
|
|
|
|
expr string; // the original expression
|
2008-12-19 04:05:37 -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;
|
2008-10-09 20:40:53 -06:00
|
|
|
start Inst;
|
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 (
|
2008-10-14 17:32:43 -06:00
|
|
|
START // beginning of program
|
2008-10-09 20:40:53 -06:00
|
|
|
= iota;
|
|
|
|
END; // end of program: success
|
|
|
|
BOT; // '^' beginning of text
|
|
|
|
EOT; // '$' end of text
|
|
|
|
CHAR; // 'a' regular character
|
2008-10-11 17:48:05 -06:00
|
|
|
CHARCLASS; // [a-z] character class
|
2008-10-09 20:40:53 -06:00
|
|
|
ANY; // '.' any character
|
|
|
|
BRA; // '(' parenthesized expression
|
|
|
|
EBRA; // ')'; end of '(' parenthesized expression
|
|
|
|
ALT; // '|' alternation
|
|
|
|
NOP; // do nothing; makes it easy to link without patching
|
|
|
|
)
|
|
|
|
|
|
|
|
// --- START start of program
|
|
|
|
type Start struct {
|
2008-10-26 09:28:33 -06:00
|
|
|
Common
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (start *Start) Type() int { return START }
|
2008-10-11 17:48:05 -06:00
|
|
|
func (start *Start) Print() { print("start") }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// --- END end of program
|
|
|
|
type End struct {
|
2008-10-26 09:28:33 -06:00
|
|
|
Common
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (end *End) Type() int { return END }
|
2008-10-11 17:48:05 -06:00
|
|
|
func (end *End) Print() { print("end") }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// --- BOT beginning of text
|
|
|
|
type Bot struct {
|
2008-10-26 09:28:33 -06:00
|
|
|
Common
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bot *Bot) Type() int { return BOT }
|
2008-10-11 17:48:05 -06:00
|
|
|
func (bot *Bot) Print() { print("bot") }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// --- EOT end of text
|
|
|
|
type Eot struct {
|
2008-10-26 09:28:33 -06:00
|
|
|
Common
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (eot *Eot) Type() int { return EOT }
|
2008-10-11 17:48:05 -06:00
|
|
|
func (eot *Eot) Print() { print("eot") }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// --- CHAR a regular character
|
|
|
|
type Char struct {
|
2008-10-26 09:28:33 -06:00
|
|
|
Common;
|
2008-10-14 17:32:43 -06:00
|
|
|
char int;
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (char *Char) Type() int { return CHAR }
|
2008-10-11 17:48:05 -06:00
|
|
|
func (char *Char) Print() { print("char ", string(char.char)) }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
func NewChar(char int) *Char {
|
2008-12-19 04:05:37 -07:00
|
|
|
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]
|
|
|
|
|
|
|
|
type CharClass struct {
|
2008-10-26 09:28:33 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-10-14 17:32:43 -06:00
|
|
|
func (cclass *CharClass) Type() int { return CHARCLASS }
|
2008-10-26 09:28:33 -06:00
|
|
|
|
2008-10-11 17:48:05 -06:00
|
|
|
func (cclass *CharClass) Print() {
|
|
|
|
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), "]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-15 18:20:37 -06: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
|
|
|
}
|
|
|
|
|
2008-10-14 17:32:43 -06:00
|
|
|
func (cclass *CharClass) Matches(c int) bool {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2008-10-11 17:48:05 -06:00
|
|
|
func NewCharClass() *CharClass {
|
2008-12-19 04:05:37 -07:00
|
|
|
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
|
|
|
|
type Any struct {
|
2008-10-26 09:28:33 -06:00
|
|
|
Common
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *Any) Type() int { return ANY }
|
2008-10-11 17:48:05 -06:00
|
|
|
func (any *Any) Print() { print("any") }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// --- BRA parenthesized expression
|
|
|
|
type Bra struct {
|
2008-10-26 09:28:33 -06:00
|
|
|
Common;
|
2008-10-10 19:42:19 -06:00
|
|
|
n int; // subexpression number
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bra *Bra) Type() int { return BRA }
|
2008-10-26 09:28:33 -06:00
|
|
|
func (bra *Bra) Print() { print("bra", bra.n); }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// --- EBRA end of parenthesized expression
|
|
|
|
type Ebra struct {
|
2008-10-26 09:28:33 -06:00
|
|
|
Common;
|
2008-10-09 20:40:53 -06:00
|
|
|
n int; // subexpression number
|
|
|
|
}
|
|
|
|
|
2008-10-14 17:32:43 -06:00
|
|
|
func (ebra *Ebra) Type() int { return EBRA }
|
2008-10-11 17:48:05 -06:00
|
|
|
func (ebra *Ebra) Print() { print("ebra ", ebra.n); }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// --- ALT alternation
|
|
|
|
type Alt struct {
|
2008-10-26 09:28:33 -06:00
|
|
|
Common;
|
2008-10-10 19:42:19 -06:00
|
|
|
left Inst; // other branch
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (alt *Alt) Type() int { return ALT }
|
2008-10-11 17:48:05 -06:00
|
|
|
func (alt *Alt) Print() { print("alt(", alt.left.Index(), ")"); }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// --- NOP no operation
|
|
|
|
type Nop struct {
|
2008-10-26 09:28:33 -06:00
|
|
|
Common
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (nop *Nop) Type() int { return NOP }
|
2008-10-11 17:48:05 -06:00
|
|
|
func (nop *Nop) Print() { print("nop") }
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
// report error and exit compiling/executing goroutine
|
|
|
|
func (re *RE) Error(err *os.Error) {
|
|
|
|
re.error = err;
|
|
|
|
re.ch <- re;
|
|
|
|
sys.goexit();
|
|
|
|
}
|
|
|
|
|
2008-10-11 17:48:05 -06:00
|
|
|
func (re *RE) Add(i Inst) Inst {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-10-09 20:40:53 -06:00
|
|
|
type Parser struct {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const EOF = -1
|
|
|
|
|
|
|
|
func (p *Parser) c() int {
|
|
|
|
return p.ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) nextc() int {
|
|
|
|
if p.pos >= len(p.re.expr) {
|
|
|
|
p.ch = EOF
|
|
|
|
} else {
|
2008-10-11 17:48:05 -06:00
|
|
|
c, w := sys.stringtorune(p.re.expr, p.pos);
|
2008-10-09 20:40:53 -06:00
|
|
|
p.ch = c;
|
|
|
|
p.pos += w;
|
|
|
|
}
|
|
|
|
return p.ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewParser(re *RE) *Parser {
|
2008-12-19 04:05:37 -07:00
|
|
|
parser := new(*Parser);
|
2008-10-09 20:40:53 -06:00
|
|
|
parser.re = re;
|
|
|
|
parser.nextc(); // load p.ch
|
|
|
|
return parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
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 ')'
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2008-10-10 19:42:19 -06:00
|
|
|
func (p *Parser) Regexp() (start, end Inst)
|
|
|
|
|
|
|
|
var NULL Inst
|
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
|
|
|
|
}
|
|
|
|
|
2008-10-11 17:48:05 -06:00
|
|
|
func (p *Parser) CharClass() Inst {
|
|
|
|
cc := NewCharClass();
|
|
|
|
p.re.Add(cc);
|
|
|
|
if p.c() == '^' {
|
|
|
|
cc.negate = true;
|
|
|
|
p.nextc();
|
|
|
|
}
|
|
|
|
left := -1;
|
|
|
|
for {
|
|
|
|
switch c := p.c(); c {
|
|
|
|
case ']', EOF:
|
|
|
|
if left >= 0 {
|
|
|
|
p.re.Error(ErrBadRange);
|
|
|
|
}
|
|
|
|
return cc;
|
|
|
|
case '-': // do this before backslash processing
|
|
|
|
p.re.Error(ErrBadRange);
|
|
|
|
case '\\':
|
|
|
|
c = p.nextc();
|
|
|
|
switch {
|
|
|
|
case c == EOF:
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL
|
|
|
|
}
|
|
|
|
|
2008-10-10 19:42:19 -06:00
|
|
|
func (p *Parser) Term() (start, end Inst) {
|
2008-10-09 20:40:53 -06:00
|
|
|
switch c := p.c(); c {
|
2008-10-10 13:41:43 -06:00
|
|
|
case '|', EOF:
|
2008-10-09 20:40:53 -06:00
|
|
|
return NULL, NULL;
|
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);
|
|
|
|
}
|
|
|
|
return NULL, NULL;
|
2008-10-11 17:48:05 -06:00
|
|
|
case ']':
|
|
|
|
p.re.Error(ErrUnmatchedRbkt);
|
|
|
|
case '^':
|
|
|
|
p.nextc();
|
2008-12-19 04:05:37 -07:00
|
|
|
start = p.re.Add(new(*Bot));
|
2008-10-11 17:48:05 -06:00
|
|
|
return start, start;
|
|
|
|
case '$':
|
|
|
|
p.nextc();
|
2008-12-19 04:05:37 -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();
|
2008-12-19 04:05:37 -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();
|
2008-12-19 04:05:37 -07:00
|
|
|
bra := new(*Bra);
|
2008-10-11 17:48:05 -06:00
|
|
|
p.re.Add(bra);
|
2008-12-19 04:05:37 -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;
|
2008-10-14 17:32:43 -06:00
|
|
|
if start == NULL {
|
|
|
|
if end == NULL { 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 {
|
|
|
|
case c == EOF:
|
|
|
|
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();
|
2008-10-10 19:42:19 -06: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");
|
|
|
|
}
|
|
|
|
|
2008-10-10 19:42:19 -06:00
|
|
|
func (p *Parser) Closure() (start, end Inst) {
|
2008-10-09 20:40:53 -06:00
|
|
|
start, end = p.Term();
|
2008-10-14 17:32:43 -06:00
|
|
|
if start == NULL {
|
2008-10-14 18:45:49 -06:00
|
|
|
return
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
switch p.c() {
|
|
|
|
case '*':
|
|
|
|
// (start,end)*:
|
2008-12-19 04:05:37 -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)+:
|
2008-12-19 04:05:37 -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)?:
|
2008-12-19 04:05:37 -07:00
|
|
|
alt := new(*Alt);
|
2008-10-11 17:48:05 -06:00
|
|
|
p.re.Add(alt);
|
2008-12-19 04:05:37 -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
|
|
|
}
|
|
|
|
|
2008-10-10 19:42:19 -06:00
|
|
|
func (p *Parser) Concatenation() (start, end Inst) {
|
2008-10-09 20:40:53 -06:00
|
|
|
start, end = NULL, NULL;
|
|
|
|
for {
|
|
|
|
nstart, nend := p.Closure();
|
|
|
|
switch {
|
2008-10-14 17:32:43 -06:00
|
|
|
case nstart == NULL: // end of this concatenation
|
|
|
|
if start == NULL { // this is the empty string
|
2008-12-19 04:05:37 -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;
|
2008-10-14 17:32:43 -06:00
|
|
|
case start == NULL: // 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");
|
|
|
|
}
|
|
|
|
|
2008-10-10 19:42:19 -06:00
|
|
|
func (p *Parser) Regexp() (start, end Inst) {
|
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();
|
2008-12-19 04:05:37 -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;
|
2008-12-19 04:05:37 -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");
|
|
|
|
}
|
|
|
|
|
2008-10-10 19:42:19 -06:00
|
|
|
func UnNop(i Inst) Inst {
|
|
|
|
for i.Type() == NOP {
|
|
|
|
i = i.Next()
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2008-10-11 17:48:05 -06:00
|
|
|
func (re *RE) EliminateNops() {
|
|
|
|
for i := 0; i < re.inst.Len(); i++ {
|
|
|
|
inst := re.inst.At(i).(Inst);
|
|
|
|
if inst.Type() == END {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
inst.SetNext(UnNop(inst.Next()));
|
|
|
|
if inst.Type() == ALT {
|
|
|
|
alt := inst.(*Alt);
|
2008-10-10 19:42:19 -06:00
|
|
|
alt.left = UnNop(alt.left);
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-11 17:48:05 -06:00
|
|
|
func (re *RE) Dump() {
|
|
|
|
for i := 0; i < re.inst.Len(); i++ {
|
|
|
|
inst := re.inst.At(i).(Inst);
|
|
|
|
print(inst.Index(), ": ");
|
|
|
|
inst.Print();
|
|
|
|
if inst.Type() != END {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-09 20:40:53 -06:00
|
|
|
func (re *RE) DoParse() {
|
|
|
|
parser := NewParser(re);
|
2008-12-19 04:05:37 -07:00
|
|
|
start := new(*Start);
|
2008-10-11 17:48:05 -06:00
|
|
|
re.Add(start);
|
2008-10-09 20:40:53 -06:00
|
|
|
s, e := parser.Regexp();
|
|
|
|
start.next = s;
|
2008-10-10 19:42:19 -06:00
|
|
|
re.start = start;
|
2008-12-19 04:05:37 -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
|
|
|
|
2008-12-19 04:05:37 -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
|
2008-10-09 20:40:53 -06:00
|
|
|
export 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
|
2008-10-09 20:40:53 -06:00
|
|
|
export func Compile(str string) (regexp Regexp, error *os.Error) {
|
|
|
|
ch := new(chan *RE);
|
|
|
|
go Compiler(str, ch);
|
|
|
|
re := <-ch;
|
|
|
|
return re, re.error
|
|
|
|
}
|
2008-10-14 17:32:43 -06:00
|
|
|
|
|
|
|
type State struct {
|
|
|
|
inst Inst; // 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.
|
2008-12-18 23:37:22 -07:00
|
|
|
func AddState(s []State, inst Inst, 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) {
|
|
|
|
s1 := new([]State, 2*l)[0:l];
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-12-18 23:37:22 -07:00
|
|
|
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
|
2008-10-14 17:32:43 -06:00
|
|
|
s[0] = new([]State, 10)[0:0];
|
|
|
|
s[1] = new([]State, 10)[0:0];
|
|
|
|
in, out := 0, 1;
|
|
|
|
var final State;
|
|
|
|
found := false;
|
|
|
|
for pos <= len(str) {
|
|
|
|
if !found {
|
|
|
|
// prime the pump if we haven't seen a match yet
|
|
|
|
match := new([]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;
|
|
|
|
s[out] = AddState(s[out], re.start.Next(), match);
|
|
|
|
}
|
|
|
|
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;
|
2008-10-14 17:32:43 -06:00
|
|
|
c := EOF;
|
|
|
|
if pos < len(str) {
|
2008-10-14 18:45:49 -06:00
|
|
|
c, charwidth = sys.stringtorune(str, pos);
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
|
|
|
for i := 0; i < len(s[in]); i++ {
|
|
|
|
state := s[in][i];
|
|
|
|
switch s[in][i].inst.Type() {
|
|
|
|
case BOT:
|
|
|
|
if pos == 0 {
|
|
|
|
s[in] = AddState(s[in], state.inst.Next(), state.match)
|
|
|
|
}
|
|
|
|
case EOT:
|
|
|
|
if pos == len(str) {
|
|
|
|
s[in] = AddState(s[in], state.inst.Next(), state.match)
|
|
|
|
}
|
|
|
|
case CHAR:
|
|
|
|
if c == state.inst.(*Char).char {
|
|
|
|
s[out] = AddState(s[out], state.inst.Next(), state.match)
|
|
|
|
}
|
|
|
|
case CHARCLASS:
|
|
|
|
if state.inst.(*CharClass).Matches(c) {
|
|
|
|
s[out] = AddState(s[out], state.inst.Next(), state.match)
|
|
|
|
}
|
|
|
|
case ANY:
|
|
|
|
if c != EOF {
|
|
|
|
s[out] = AddState(s[out], state.inst.Next(), state.match)
|
|
|
|
}
|
|
|
|
case BRA:
|
|
|
|
n := state.inst.(*Bra).n;
|
|
|
|
state.match[2*n] = pos;
|
|
|
|
s[in] = AddState(s[in], state.inst.Next(), state.match);
|
|
|
|
case EBRA:
|
|
|
|
n := state.inst.(*Ebra).n;
|
|
|
|
state.match[2*n+1] = pos;
|
|
|
|
s[in] = AddState(s[in], state.inst.Next(), state.match);
|
|
|
|
case ALT:
|
|
|
|
s[in] = AddState(s[in], state.inst.(*Alt).left, state.match);
|
|
|
|
// give other branch a copy of this match vector
|
|
|
|
s1 := new([]int, 2*(re.nbra+1));
|
|
|
|
for i := 0; i < len(s1); i++ {
|
|
|
|
s1[i] = state.match[i]
|
|
|
|
}
|
|
|
|
s[in] = AddState(s[in], state.inst.Next(), s1);
|
|
|
|
case END:
|
|
|
|
// choose leftmost longest
|
|
|
|
if !found || // first
|
|
|
|
state.match[0] < final.match[0] || // leftmost
|
|
|
|
(state.match[0] == final.match[0] && pos > final.match[1]) { // longest
|
|
|
|
final = state;
|
|
|
|
final.match[1] = pos;
|
|
|
|
}
|
|
|
|
found = true;
|
|
|
|
default:
|
|
|
|
state.inst.Print();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-18 23:37:22 -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
|
|
|
|
|
|
|
|
|
|
|
func (re *RE) Match(s string) bool {
|
|
|
|
return len(re.DoExecute(s, 0)) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (re *RE) MatchStrings(s string) []string {
|
|
|
|
r := re.DoExecute(s, 0);
|
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
a := new([]string, len(r)/2);
|
|
|
|
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.
|
|
|
|
export func Match(pattern string, s string) (matched bool, error *os.Error) {
|
|
|
|
re, err := Compile(pattern);
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return re.Match(s), nil
|
|
|
|
}
|