public function removeFromConcatenation(Concatenation $expression, ArrayObject $accumulator) { $items = $expression->toArray(); $cleaned = $this->remove($items); $expression = new Concatenation(array_shift($cleaned), $cleaned); $accumulator->append($expression); return 1; }
/** * a b (1 | { 2 } | 3) => a b (1 | generatedNonTerminal1 | 3) */ public function testGroupWithRepeatedSymbol() { // $this->markTestSkipped('s'); $exprTestUtils = $this->getExpressionTestUtils(); $initialList = $exprTestUtils->createListOfExpressions(array('a', 'b')); $initialList[] = $exprTestUtils->getGroupUtils()->createAlternationFromSymbols(array($exprTestUtils->createTerminal('1'), new Repetition($exprTestUtils->createTerminal('2')), $exprTestUtils->createTerminal('3'))); $initialExpression = new Concatenation(array_shift($initialList), $initialList); $namingStrategy = $exprTestUtils->createNonTerminalNamingStrategy(); $actualExpression = $this->getDepthFirstWalkResult($initialExpression, $namingStrategy); $this->assertInstanceOf(get_class($initialExpression), $actualExpression, 'there should have been some expressions'); $expectedList = $exprTestUtils->createListOfExpressions(array('a', 'b')); $namingStrategy = $exprTestUtils->createNonTerminalNamingStrategy(); $expectedList[] = $exprTestUtils->getGroupUtils()->createAlternationFromSymbols(array($exprTestUtils->createTerminal('1'), $exprTestUtils->createNonTerminal($namingStrategy->getName()), $exprTestUtils->createTerminal('3'))); $expectedExpression = new Concatenation(array_shift($expectedList), $expectedList); $assertFailMsgTpl = 'Expected the following sequence: %s'; $this->assertEquals($expectedExpression->toArray(), $actualExpression->toArray(), sprintf($assertFailMsgTpl, $exprTestUtils->serializeExpressionIterable($expectedExpression))); }
/** * @param array|Expression[] $items * * @return array| array[] * @throws \Exception */ public function removeFromConcatenation(Concatenation $expression, ArrayObject $accumulator) { $items = $expression->toArray(); $successors = new SplStack(); $successors->setIteratorMode(SplStack::IT_MODE_KEEP | SplStack::IT_MODE_LIFO); $choice = $this->findGroupedChoice($items, $successors); $items = array_slice($items, 0, count($items) - 1 - $successors->count()); $total = 0; foreach ($choice as $expression) { $cleaned = []; foreach ($items as $item) { $cleaned[] = $item; } $cleaned[] = $expression; foreach ($successors as $successor) { $cleaned[] = $successor; } $expression = new Concatenation(array_shift($cleaned), $cleaned); $accumulator->append($expression); $total++; } return $total; }