MCPcopy Create free account
hub / github.com/OpenEndedGroup/Field2 / Production

Class Production

src/main/java/trace/util/Production.kt:3–65  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1package trace.util
2
3import java.util.regex.Matcher
4
5class Production {
6 inner class Rule(val left: String, val right: String) {
7 fun apply(a: String): String {
8 val r = Regex(Regex.escape(left))
9 return r.replace(a, right)
10 }
11 }
12
13 inner class Action(val m: String, val r: () -> Unit) {
14 fun apply(a: String): String {
15 val r = Regex("^" + Regex.escape(m))
16 print(r)
17 return r.replace(a, {mr ->
18 r()
19 ""})
20 }
21 }
22
23 val rules = mutableListOf<Rule>()
24 val actions = mutableListOf<Action>()
25
26 fun addRule(left: String, right: String) {
27 rules.add(Rule(left, right))
28 }
29
30 fun addAction(matches: String, action: () -> Unit) {
31 actions.add(Action(matches, action))
32 }
33
34 fun clearRules() {
35 rules.clear()
36 }
37
38 fun apply(a: String): String {
39 var aa = a
40 rules.forEach {
41 println("aa => $aa")
42 aa = it.apply(aa)
43 if (aa.length > 1000) return aa
44 }
45 return aa
46 }
47
48 fun act(a: String) {
49 var aa = a
50 var prev = a
51 while (aa.length > 0) {
52 actions.forEach {
53 aa = it.apply(aa)
54 }
55
56 if (aa.equals(prev)) {
57 if (aa.length == 0)
58 return
59
60 aa = aa.substring(1);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected