public function testGenerateWithAlternatingStrategy() { $scope = new Scope('scope1'); $gen = new MersenneRandom(700); $result = ''; $scope->setMinOccurances(7); $scope->setMaxOccurances(7); for ($i = 1; $i <= 6; $i++) { $lit = new LiteralScope('label_' . $i); $lit->addLiteral($i); $scope->attach($lit); $lit = null; } $scope->useAlternatingStrategy(); $scope->generate($result, $gen); $this->assertRegExp('/[1-6]{7}/', $result); }
/** * Parse the current token for closers : {###} { ## } {##,##} * * @access public * @return ReverseRegex\Generator\Scope a new head * @param ReverseRegex\Generator\Scope $head * @param ReverseRegex\Generator\Scope $result * @param ReverseRegex\Lexer $lexer */ public function quantifyClosure(Scope $head, Scope $result, Lexer $lexer) { $tokens = array(); $min = $head->getMinOccurances(); $max = $head->getMaxOccurances(); # move to the first token inside the quantifer. # parse for the minimum , move lookahead until read end of the closure or the `,` while ($lexer->moveNext() === true && !$lexer->isNextToken(Lexer::T_QUANTIFIER_CLOSE) && $lexer->lookahead['value'] !== ',') { if ($lexer->isNextToken(Lexer::T_QUANTIFIER_OPEN)) { throw new ParserException('Nesting Quantifiers is not allowed'); } $tokens[] = $lexer->lookahead; } $min = $this->convertInteger($tokens); # do we have a maximum after the comma? if ($lexer->lookahead['value'] === ',') { # make sure we have values to gather ie not {778,} $tokens = array(); # move to the first token after the `,` character # grab the remaining numbers while ($lexer->moveNext() && !$lexer->isNextToken(Lexer::T_QUANTIFIER_CLOSE)) { if ($lexer->isNextToken(Lexer::T_QUANTIFIER_OPEN)) { throw new ParserException('Nesting Quantifiers is not allowed'); } $tokens[] = $lexer->lookahead; } $max = $this->convertInteger($tokens); } else { $max = $min; } $head->setMaxOccurances($max); $head->setMinOccurances($min); # skip the lexer to the closing token $lexer->skipUntil(Lexer::T_QUANTIFIER_CLOSE); # check if the last matched token was the closing bracket # not going to stop errors like {#####,###{[a-z]} {#####{[a-z]} if (!$lexer->isNextToken(Lexer::T_QUANTIFIER_CLOSE)) { throw new ParserException('Closing quantifier token `}` not found'); } return $head; }