/** * Optimize regular expression * * Tries to optimize the given regular expression. Returns true if the AST * has been modified, and false otherwise. * * @param slRegularExpression $regularExpression * @return bool */ public function optimize(slRegularExpression &$regularExpression) { if ((($op1 = $regularExpression instanceof slRegularExpressionOptional) || $regularExpression instanceof slRegularExpressionRepeated) && (($op2 = $regularExpression->getChild() instanceof slRegularExpressionOptional) || $regularExpression->getChild() instanceof slRegularExpressionRepeated)) { $class = $op1 && $op2 ? 'slRegularExpressionOptional' : 'slRegularExpressionRepeated'; $regularExpression = new $class($regularExpression->getChild()->getChild()); return true; } return $this->recurse($regularExpression); }
/** * Recursively filters out start and end markers * * Recursively filter out start and end markers from the regular expression * structure, since they do not have any real meaning, but were required to * create correct automatons. * * @param slRegularExpression $regularExpression * @return slRegularExpression */ protected function filterStartEndMarkers(slRegularExpression $regularExpression) { if ($regularExpression instanceof slRegularExpressionMultiple) { foreach ($regularExpression->getChildren() as $nr => $child) { if ($child instanceof slRegularExpressionElement && ($child->getContent() === 0 || $child->getContent() === 1)) { $regularExpression[$nr] = new slRegularExpressionEmpty(); } else { $this->filterStartEndMarkers($child); } } } if ($regularExpression instanceof slRegularExpressionSingular) { $child = $regularExpression->getChild(); if ($child instanceof slRegularExpressionElement && ($child->getContent() === 0 || $child->getContent() === 1)) { $regularExpression->setChild(new slRegularExpressionEmpty()); } else { $this->filterStartEndMarkers($child); } } return $regularExpression; }