-
Notifications
You must be signed in to change notification settings - Fork 5
/
CRegexTests.scala
149 lines (140 loc) · 4.09 KB
/
CRegexTests.scala
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
144
145
146
147
148
149
package lms.verify
// see lms-verify/src/out/re.c for original interpreter
trait CRegexpMatcher extends SimulatedReader {
def matchsearch(r: Input, s: Input): Boolean = {
if (r.first == '^')
matchhere(r.rest, s)
else {
var found = false
var continue = true
var cur_s = s
while (!found && continue) {
found = matchhere(r, cur_s)
if (!found) {
continue = !cur_s.atEnd
if (continue) cur_s = cur_s.rest
}
}
found
}
}
def matchhere(r: Input, s: Input): Boolean = {
if (r.atEnd)
true
else if (r.second == '*')
matchstar(r.first, r.rest.rest, s)
else if (r.first == '$' && r.rest.atEnd)
s.atEnd
else if (!s.atEnd && (r.first == '.' || r.first == s.first))
matchhere(r.rest, s.rest)
else
false
}
def matchstar(c: Char, r: Input, s: Input): Boolean = {
var found = false
var continue = true
var cur_s = s
while (!found && continue) {
found = matchhere(r, cur_s)
if (!found) {
continue = !cur_s.atEnd
if (continue) {
continue = cur_s.first == c || c == '.'
cur_s = cur_s.rest
}
}
}
found
}
}
class CRegexpMatcherTest extends TestSuite with CRegexpMatcher {
val under = "cre_"
def testmatch(regexp: String, text: String, expected: Boolean) {
test(s"""matchsearch("$regexp", "$text") == $expected""") {
assertResult(expected){matchsearch(fromString(regexp), fromString(text))}
}
}
testmatch("^hello$", "hello", true)
testmatch("^hello$", "hell", false)
testmatch("hell", "hello", true);
testmatch("hell", "hell", true);
testmatch("hel*", "he", true);
testmatch("hel*$", "hello", false);
testmatch("hel*", "yo hello", true);
testmatch("ab", "hello ab hello", true);
testmatch("^ab", "hello ab hello", false);
testmatch("a*b", "hello aab hello", true);
testmatch("^ab*", "abcd", true);
testmatch("^ab*", "a", true);
testmatch("^ab*", "ac", true);
testmatch("^ab*", "bac", false);
testmatch("^ab*$", "ac", false);
}
trait StagedCRegexpMatcher extends Dsl with Reader with SimulatedReader {
def matchsearch(r: Input, s: Rep[Input]): Rep[Boolean] = {
if (r.first == '^')
matchhere(r.rest, s)
else {
var found = unit(false)
var continue = unit(true)
var cur_s = s
loop (valid_input(cur_s), List[Any](found, continue, cur_s), cur_s.length+(if (!found) 1 else 0)+(if (continue) 1 else 0)) {
while (!found && continue) {
found = matchhere(r, cur_s)
if (!found) {
continue = !cur_s.atEnd
if (continue) cur_s = cur_s.rest
}
}}
found
}
}
def matchhere(r: Input, s: Rep[Input]): Rep[Boolean] = {
if (r.atEnd)
true
else if (r.second == '*')
matchstar(r.first, r.rest.rest, s)
else if (r.first == '$' && r.rest.atEnd)
s.atEnd
else if (!s.atEnd && (r.first == '.' || r.first == s.first))
matchhere(r.rest, s.rest)
else
false
}
def matchstar(c: Char, r: Input, s: Rep[Input]): Rep[Boolean] = {
var found = unit(false)
var continue = unit(true)
var cur_s = s
loop (valid_input(cur_s), List[Any](found, continue, cur_s), cur_s.length+(if (!found) 1 else 0)+(if (continue) 1 else 0)) {
while (!found && continue) {
found = matchhere(r, cur_s)
if (!found) {
continue = !cur_s.atEnd
if (continue) {
continue = cur_s.first == c || c == '.'
cur_s = cur_s.rest
}
}
}}
found
}
}
class CRegexTests extends TestSuite {
val under = "cre_"
def gen(msg: String, re: String) {
test(msg) {
trait RegexProg extends StagedCRegexpMatcher {
override def autoAssignNothing = false
toplevel("matcher_"+msg,
{ (s: Rep[Input]) => matchsearch(fromString(re), s) },
{ (s: Rep[Input]) => valid_input(s) },
{ (s: Rep[Input]) => (r: Rep[Boolean]) => true })
}
check(msg, (new RegexProg with Impl).code)
}
}
gen("begin_a", "^a")
gen("a_end", "a$")
gen("a", "a")
gen("ab_dot_star_ab", "ab.*ab")
}