-
Notifications
You must be signed in to change notification settings - Fork 0
/
re.go
143 lines (130 loc) · 3.12 KB
/
re.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package builder
import (
"fmt"
"regexp"
"strings"
)
// Expression defines a regexp that evolves
type Expression struct {
atoms []CharacterExpression
pattern *regexp.Regexp
}
// AddLine inserts candidates into an Expression
func (e *Expression) AddLine(line string) {
if !e.Match(line) {
for i, c := range []rune(line) {
if i >= len(e.atoms) {
e.atoms = append(e.atoms, newCharacterExpression(c))
}
e.atoms[i].AddRune(c)
}
var s []string
for _, a := range e.atoms {
s = append(s, a.Pattern())
}
e.pattern = regexp.MustCompile(strings.Join(s, ""))
}
}
// String returns a printable form of the Expression
func (e *Expression) String() string {
if e.pattern == nil {
return fmt.Sprintf("%+v", e.atoms)
}
return e.pattern.String()
}
// Match checks whether the input satisfies the Expression
func (e *Expression) Match(input string) bool {
if e.pattern != nil && e.pattern.MatchString(input) {
return true
}
for i, a := range []rune(input) {
if len(e.atoms) <= i || !e.atoms[i].Match(a) {
return false
}
}
return true
}
// CharacterExpression defines a regexp that matches against
// a single-character input
type CharacterExpression struct {
pattern *regexp.Regexp
literals []rune
atoms []rune
}
func newCharacterExpression(c rune) CharacterExpression {
return CharacterExpression{
pattern: nil,
literals: nil,
atoms: []rune{c},
}
}
// Match checks whether the input satisfies the CharacterExpression
func (e *CharacterExpression) Match(input rune) bool {
if e.pattern != nil && allMatch(e.pattern, []rune{input}) {
return true
}
for _, c := range e.literals {
if c == input {
return true
}
}
return false
}
// AddRune adds a candidate to a CharacterExpression
func (e *CharacterExpression) AddRune(r rune) {
if e.Match(r) {
return
}
e.literals = append(e.literals, r)
if len(append(e.atoms, e.literals...)) > 4 {
var highest int
for ex, w := range knownTypes {
if allMatch(ex, append(e.atoms, e.literals...)) && w > highest {
e.pattern = ex
e.atoms = append(e.atoms, e.literals...)
e.literals = nil
highest = w
}
}
}
}
// Pattern returns a regexp representing the CharacterExpression in string form
func (e *CharacterExpression) Pattern() string {
var k []string
if e.pattern != nil {
k = append(k, e.pattern.String())
}
for _, a := range e.literals {
k = append(k, string(a))
}
return "(" + strings.Join(k, "|") + ")"
}
func combine(a, b *regexp.Regexp) *regexp.Regexp {
if a == nil {
return b
}
if b == nil {
return a
}
return regexp.MustCompile(
a.String() + "|" + b.String(),
)
}
func allMatch(ex *regexp.Regexp, patterns []rune) bool {
bounded := regexp.MustCompile("^" + ex.String() + "$")
for _, p := range patterns {
if !bounded.Match([]byte{byte(p)}) {
return false
}
}
return true
}
var knownTypes = map[*regexp.Regexp]int{
regexp.MustCompile(`[A-Z]`): 200,
regexp.MustCompile(`[a-z]`): 200,
regexp.MustCompile(`[[:digit:]]`): 200,
regexp.MustCompile(`[[:alpha:]]`): 100,
regexp.MustCompile(`[[:xdigit:]]`): 100,
regexp.MustCompile(`\w`): 50,
regexp.MustCompile(`.`): 1,
}