8
8
*
9
9
* New BSD License
10
10
*
11
- * Copyright © 2007-2015, Ivan Enderlin . All rights reserved.
11
+ * Copyright © 2007-2015, Hoa community . All rights reserved.
12
12
*
13
13
* Redistribution and use in source and binary forms, with or without
14
14
* modification, are permitted provided that the following conditions are met:
46
46
*
47
47
* Isotropic walk on the AST to generate a data.
48
48
*
49
- * @author Ivan Enderlin <[email protected] >
50
- * @copyright Copyright © 2007-2015 Ivan Enderlin.
49
+ * @copyright Copyright © 2007-2015 Hoa community
51
50
* @license New BSD License
52
51
*/
53
-
54
- class Isotropic implements Visitor \Visit {
55
-
52
+ class Isotropic implements Visitor \Visit
53
+ {
56
54
/**
57
55
* Numeric-sampler.
58
56
*
59
- * @var \Hoa\Math\Sampler object
57
+ * @var \Hoa\Math\Sampler
60
58
*/
61
59
protected $ _sampler = null ;
62
60
@@ -65,12 +63,11 @@ class Isotropic implements Visitor\Visit {
65
63
/**
66
64
* Initialize numeric-sampler.
67
65
*
68
- * @access public
69
66
* @param \Hoa\Math\Sampler $sampler Numeric-sampler.
70
67
* @return void
71
68
*/
72
- public function __construct ( Math \Sampler $ sampler ) {
73
-
69
+ public function __construct ( Math \Sampler $ sampler)
70
+ {
74
71
$ this ->_sampler = $ sampler ;
75
72
76
73
return ;
@@ -79,128 +76,133 @@ public function __construct ( Math\Sampler $sampler ) {
79
76
/**
80
77
* Visit an element.
81
78
*
82
- * @access public
83
79
* @param \Hoa\Visitor\Element $element Element to visit.
84
80
* @param mixed &$handle Handle (reference).
85
81
* @param mixed $eldnah Handle (not reference).
86
82
* @return mixed
87
83
*/
88
- public function visit ( Visitor \Element $ element ,
89
- &$ handle = null , $ eldnah = null ) {
90
-
91
- switch ($ element ->getId ()) {
92
-
84
+ public function visit (
85
+ Visitor \Element $ element ,
86
+ &$ handle = null ,
87
+ $ eldnah = null
88
+ ) {
89
+ switch ($ element ->getId ()) {
93
90
case '#expression ' :
94
91
case '#capturing ' :
95
92
case '#noncapturing ' :
96
93
case '#namedcapturing ' :
97
94
return $ element ->getChild (0 )->accept ($ this , $ handle , $ eldnah );
98
- break ;
99
95
100
96
case '#alternation ' :
101
97
case '#class ' :
102
98
return $ element ->getChild ($ this ->_sampler ->getInteger (
103
99
0 ,
104
100
$ element ->getChildrenNumber () - 1
105
101
))->accept ($ this , $ handle , $ eldnah );
106
- break ;
107
102
108
103
case '#concatenation ' :
109
104
$ out = null ;
110
105
111
- foreach ($ element ->getChildren () as $ child )
106
+ foreach ($ element ->getChildren () as $ child ) {
112
107
$ out .= $ child ->accept ($ this , $ handle , $ eldnah );
108
+ }
113
109
114
110
return $ out ;
115
- break ;
116
111
117
112
case '#quantification ' :
118
113
$ out = null ;
119
114
$ xy = $ element ->getChild (1 )->getValueValue ();
120
115
$ x = 0 ;
121
116
$ y = 0 ;
122
117
123
- switch ($ element ->getChild (1 )->getValueToken ()) {
124
-
118
+ switch ($ element ->getChild (1 )->getValueToken ()) {
125
119
case 'zero_or_one ' :
126
120
$ y = 1 ;
127
- break ;
121
+
122
+ break ;
128
123
129
124
case 'zero_or_more ' :
130
125
$ y = mt_rand (5 , 8 ); // why not?
131
- break ;
126
+
127
+ break ;
132
128
133
129
case 'one_or_more ' :
134
130
$ x = 1 ;
135
131
$ y = mt_rand (5 , 8 ); // why not?
136
- break ;
132
+
133
+ break ;
137
134
138
135
case 'exactly_n ' :
139
136
$ x = $ y = (int ) substr ($ xy , 1 , -1 );
140
- break ;
137
+
138
+ break ;
141
139
142
140
case 'n_to_m ' :
143
141
$ xy = explode (', ' , substr ($ xy , 1 , -1 ));
144
142
$ x = (int ) trim ($ xy [0 ]);
145
143
$ y = (int ) trim ($ xy [1 ]);
146
- break ;
144
+
145
+ break ;
147
146
148
147
case 'n_or_more ' :
149
148
$ xy = explode (', ' , substr ($ xy , 1 , -1 ));
150
149
$ x = (int ) trim ($ xy [0 ]);
151
150
$ y = mt_rand ($ x + 5 , $ x + 8 ); // why not?
152
- break ;
151
+
152
+ break ;
153
153
}
154
154
155
- for ($ i = 0 , $ max = $ this ->_sampler ->getInteger ($ x , $ y );
156
- $ i < $ max ; ++$ i )
155
+ for (
156
+ $ i = 0 , $ max = $ this ->_sampler ->getInteger ($ x , $ y );
157
+ $ i < $ max ;
158
+ ++$ i
159
+ ) {
157
160
$ out .= $ element ->getChild (0 )->accept (
158
161
$ this ,
159
162
$ handle ,
160
163
$ eldnah
161
164
);
165
+ }
162
166
163
167
return $ out ;
164
- break ;
165
168
166
169
case '#negativeclass ' :
167
170
$ c = [];
168
171
169
- foreach ($ element ->getChildren () as $ child )
172
+ foreach ($ element ->getChildren () as $ child ) {
170
173
$ c [String::toCode (
171
174
$ child ->accept ($ this , $ handle , $ eldnah )
172
175
)] = true ;
176
+ }
173
177
174
178
do {
175
-
176
179
// all printable ASCII.
177
180
$ i = $ this ->_sampler ->getInteger (32 , 126 );
178
- } while (isset ($ c [$ i ]));
181
+ } while (isset ($ c [$ i ]));
179
182
180
183
return String::fromCode ($ i );
181
- break ;
182
184
183
185
case '#range ' :
184
186
$ out = null ;
185
187
$ left = $ element ->getChild (0 )->accept ($ this , $ handle , $ eldnah );
186
188
$ right = $ element ->getChild (1 )->accept ($ this , $ handle , $ eldnah );
187
189
188
- return String::fromCode ($ this ->_sampler ->getInteger (
189
- String::toCode ($ left ),
190
- String::toCode ($ right )
191
- ));
192
- break ;
190
+ return
191
+ String::fromCode (
192
+ $ this ->_sampler ->getInteger (
193
+ String::toCode ($ left ),
194
+ String::toCode ($ right )
195
+ )
196
+ );
193
197
194
198
case 'token ' :
195
199
$ value = $ element ->getValueValue ();
196
200
197
- switch ($ element ->getValueToken ()) {
198
-
201
+ switch ($ element ->getValueToken ()) {
199
202
case 'character ' :
200
203
$ value = ltrim ($ value , '\\' );
201
204
202
- switch ($ value ) {
203
-
205
+ switch ($ value ) {
204
206
case 'a ' :
205
207
return "\a " ;
206
208
@@ -220,44 +222,45 @@ public function visit ( Visitor\Element $element,
220
222
return "\t" ;
221
223
222
224
default :
223
- return String::fromCode (intval (
224
- substr ($ value , 1 )
225
- ));
225
+ return
226
+ String::fromCode (
227
+ intval (
228
+ substr ($ value , 1 )
229
+ )
230
+ );
226
231
}
227
- break ;
232
+
233
+ break ;
228
234
229
235
case 'dynamic_character ' :
230
236
$ value = ltrim ($ value , '\\' );
231
237
232
- switch ($ value [0 ]) {
233
-
238
+ switch ($ value [0 ]) {
234
239
case 'x ' :
235
240
$ value = trim ($ value , 'x{} ' );
241
+
236
242
return String::fromCode (
237
243
hexdec ($ value )
238
244
);
239
- break ;
240
245
241
246
default :
242
247
return String::fromCode (octdec ($ value ));
243
248
}
244
- break ;
249
+
250
+ break ;
245
251
246
252
case 'character_type ' :
247
253
$ value = ltrim ($ value , '\\' );
248
254
249
- switch ($ value ) {
250
-
255
+ switch ($ value ) {
251
256
case 'C ' :
252
257
return $ this ->_sampler ->getInteger (0 , 127 );
253
258
254
259
case 'd ' :
255
260
return $ this ->_sampler ->getInteger (0 , 9 );
256
261
257
262
case 's ' :
258
- $ value = $ this ->_sampler ->getInteger (0 , 1 )
259
- ? 'h '
260
- : 'v ' ;
263
+ $ value = $ this ->_sampler ->getInteger (0 , 1 ) ? 'h ' : 'v ' ;
261
264
262
265
case 'h ' :
263
266
$ h = [
@@ -266,10 +269,7 @@ public function visit ( Visitor\Element $element,
266
269
chr (0x00a0 )
267
270
];
268
271
269
- return $ h [$ this ->_sampler ->getInteger (
270
- 0 ,
271
- count ($ h ) -1
272
- )];
272
+ return $ h [$ this ->_sampler ->getInteger (0 , count ($ h ) -1 )];
273
273
274
274
case 'v ' :
275
275
$ v = [
@@ -279,10 +279,7 @@ public function visit ( Visitor\Element $element,
279
279
chr (0x000d )
280
280
];
281
281
282
- return $ v [$ this ->_sampler ->getInteger (
283
- 0 ,
284
- count ($ v ) -1
285
- )];
282
+ return $ v [$ this ->_sampler ->getInteger (0 , count ($ v ) -1 )];
286
283
287
284
case 'w ' :
288
285
$ w = array_merge (
@@ -291,50 +288,48 @@ public function visit ( Visitor\Element $element,
291
288
[0x5f ]
292
289
);
293
290
294
- return chr ($ w [
295
- $ this ->_sampler ->getInteger (
296
- 0 ,
297
- count ($ w ) - 1
298
- )
299
- ]);
291
+ return chr ($ w [$ this ->_sampler ->getInteger (0 , count ($ w ) - 1 )]);
300
292
301
293
default :
302
294
return '? ' ;
303
295
}
304
- break ;
305
296
306
- case 'literal ' :
307
- if ('. ' === $ value ) {
297
+ break ;
308
298
299
+ case 'literal ' :
300
+ if ('. ' === $ value ) {
309
301
$ w = array_merge (
310
302
range (0x41 , 0x5a ),
311
303
range (0x61 , 0x7a ),
312
304
[0x5f ]
313
305
);
314
306
315
- return chr ($ w [
316
- $ this ->_sampler ->getInteger (
317
- 0 ,
318
- count ($ w ) - 1
319
- )
320
- ]);
307
+ return chr ($ w [$ this ->_sampler ->getInteger (0 , count ($ w ) - 1 )]);
321
308
}
322
309
323
- return str_replace ('\\\\' , '\\' , preg_replace (
324
- '# \\\(?! \\\)# ' ,
325
- '' ,
326
- $ value
327
- ));
310
+ return
311
+ str_replace (
312
+ '\\\\' ,
313
+ '\\' ,
314
+ preg_replace (
315
+ '# \\\(?! \\\)# ' ,
316
+ '' ,
317
+ $ value
318
+ )
319
+ );
328
320
}
329
321
330
- break ;
322
+ break ;
331
323
332
324
case '#internal_options ' :
333
- break ;
325
+ break ;
334
326
335
327
default :
336
328
throw new Regex \Exception (
337
- 'Unsupported node: %s. ' , 0 , $ element ->getId ());
329
+ 'Unsupported node: %s. ' ,
330
+ 0 ,
331
+ $ element ->getId ()
332
+ );
338
333
}
339
334
340
335
return ;
0 commit comments