| 228 | } |
| 229 | |
| 230 | Regexp* CoalesceWalker::PostVisit(Regexp* re, |
| 231 | Regexp* parent_arg, |
| 232 | Regexp* pre_arg, |
| 233 | Regexp** child_args, |
| 234 | int nchild_args) { |
| 235 | if (re->nsub() == 0) |
| 236 | return re->Incref(); |
| 237 | |
| 238 | if (re->op() != kRegexpConcat) { |
| 239 | if (!ChildArgsChanged(re, child_args)) |
| 240 | return re->Incref(); |
| 241 | |
| 242 | // Something changed. Build a new op. |
| 243 | Regexp* nre = new Regexp(re->op(), re->parse_flags()); |
| 244 | nre->AllocSub(re->nsub()); |
| 245 | Regexp** nre_subs = nre->sub(); |
| 246 | for (int i = 0; i < re->nsub(); i++) |
| 247 | nre_subs[i] = child_args[i]; |
| 248 | // Repeats and Captures have additional data that must be copied. |
| 249 | if (re->op() == kRegexpRepeat) { |
| 250 | nre->min_ = re->min(); |
| 251 | nre->max_ = re->max(); |
| 252 | } else if (re->op() == kRegexpCapture) { |
| 253 | nre->cap_ = re->cap(); |
| 254 | } |
| 255 | return nre; |
| 256 | } |
| 257 | |
| 258 | bool can_coalesce = false; |
| 259 | for (int i = 0; i < re->nsub(); i++) { |
| 260 | if (i+1 < re->nsub() && |
| 261 | CanCoalesce(child_args[i], child_args[i+1])) { |
| 262 | can_coalesce = true; |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | if (!can_coalesce) { |
| 267 | if (!ChildArgsChanged(re, child_args)) |
| 268 | return re->Incref(); |
| 269 | |
| 270 | // Something changed. Build a new op. |
| 271 | Regexp* nre = new Regexp(re->op(), re->parse_flags()); |
| 272 | nre->AllocSub(re->nsub()); |
| 273 | Regexp** nre_subs = nre->sub(); |
| 274 | for (int i = 0; i < re->nsub(); i++) |
| 275 | nre_subs[i] = child_args[i]; |
| 276 | return nre; |
| 277 | } |
| 278 | |
| 279 | for (int i = 0; i < re->nsub(); i++) { |
| 280 | if (i+1 < re->nsub() && |
| 281 | CanCoalesce(child_args[i], child_args[i+1])) |
| 282 | DoCoalesce(&child_args[i], &child_args[i+1]); |
| 283 | } |
| 284 | // Determine how many empty matches were left by DoCoalesce. |
| 285 | int n = 0; |
| 286 | for (int i = n; i < re->nsub(); i++) { |
| 287 | if (child_args[i]->op() == kRegexpEmptyMatch) |