-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextStringRewrite.sc
201 lines (156 loc) · 4.51 KB
/
extStringRewrite.sc
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// stream implementation of lindenmeyer systems, depth first traversal
// a stream of strings, symbols or characters is recursively rewrittenten
// into a stream of characters
// version 2.0 03/2006
// context-free as well as context-sensitive generative grammars.
// the rules can use strings, strings, chars or functions as values
+ Stream {
// a stream of strings or characters
rewriteString { arg rules, level=1, contextSize, parseRules=true, contextFree;
var routine;
if(level <= 0) { ^this };
contextFree = contextFree ? true;
if(parseRules) {
rules = rules.collect { arg assoc;
assoc = assoc.copy;
assoc.key = assoc.key.asRewritingRule;
contextFree = contextFree and: { assoc.key.rewritingContextFree };
assoc
};
parseRules = false;
};
if(contextSize.isNil) {
contextSize = 1;
rules.do { |assoc|
contextSize = max(contextSize, assoc.key.rewritingRuleSize);
};
};
// "contextFree: % \n".postf(contextFree);
if(contextFree) {
// for context-free generative grammar
routine = Routine { arg inval;
var outval, dropSize, current, rewritten, count=0;
current = "";
loop {
outval = this.next(inval);
count = count + 1;
if(outval.notNil) {
current = current ++ outval;
} {
if(current.size == 0) { inval = nil.yield };
};
if( current.size >= contextSize or: { outval.isNil } ) {
#rewritten, dropSize
= current.matchRules(rules, current, level, rules, count);
if(rewritten.notNil) {
rewritten.do { |el| inval = el.yield };
current = current.drop(dropSize);
} {
if(current.size == 0) {
inval = nil.yield
} {
inval = current.removeAt(0).yield
}
}
};
};
}
} {
// for context-sensitive generative grammar
routine = Routine { arg inval;
var outval;
var keySize, count=0, offset=0;
var current="", rewritten, last;
loop {
outval = this.next(inval);
count = count + 1;
if(outval.notNil) {
current = current ++ outval;
} {
if(current.size == 0) { inval = nil.yield };
};
if( current.size >= contextSize or: { outval.isNil } ) {
#rewritten, keySize
= current.matchRules(rules, count, level, rules);
if(rewritten.isNil) {
if(offset == 0) {Ê
inval = current[0].yield;
}
} {
rewritten.drop(offset).do { |el| inval = el.yield };
offset = 0;
};
offset = offset + keySize;
current = current.drop(1);
offset = max(0, offset - 1);
};
};
};
};
^routine.rewriteString(rules, level - 1, contextSize, parseRules, contextFree)
}
}
+ String {
matchRules { arg rules ... args; // returns [value, dropSize] pair
rules.do { |assoc|
var res = assoc.key.matchForRewriting(this, assoc.value, args);
if(res.notNil) {
^res
}
}
^[nil, 0]
}
matchForRewriting { arg string, value, args;
^if(string.beginsWith(this)) {
[value.valueArray(args), this.size] // 'this' is key
} {
nil
}
}
rewriteString { arg rules, level=1, contextSize; // the whole string is rewrittenten at once
^all(this.iter.rewriteString(rules, level, contextSize)).join
}
asRewritingRule {
var x, a, b, key, rewriteMask, dropSize;
a = this.indexOf($<);
b = this.indexOf($>);
if(a.isNil and: { b.isNil }) { ^this }; // contextFree
x = this.delimit { |x| x === $< or: {Êx === $> } };
if(a.isNil) { // only >
rewriteMask = "%";
key = x.join;
dropSize = b;
}{
if(b.isNil) { // only <
rewriteMask = x[0]Ê++ "%";
key = x.join;
dropSize = key.size;
}{
if(b < a) {
Error("illegal syntax. \"<\" must precede \">\".").throw
} { // both < >
rewriteMask = x[0] ++ "%" ++ x[2];
key = x.join;
dropSize = b - 1; // because one preceeding char (<) has been removed
}
}
};
^[key, rewriteMask, dropSize] // for format method
}
rewritingRuleSize { ^this.size }
rewritingContextFree {Ê^true }
}
+ Array { // array is an object with a context.
matchForRewriting { arg string, value, args;
var key = this.first;
var res;
^if(string.beginsWith(key)) {
res = this[1].format(value.valueArray(args)); [res, this[2]]
} {
nil
};
}
asRewritingRule { ^this }
rewritingRuleSize { ^this.first.size }
rewritingContextFree {Ê^false }
}