Skip to content

Commit

Permalink
Use recursive lookup as a last resort to avoid throwing
Browse files Browse the repository at this point in the history
  • Loading branch information
colinodell committed Dec 7, 2024
1 parent b61bbd4 commit e1cfa8d
Show file tree
Hide file tree
Showing 7 changed files with 2 additions and 32 deletions.
15 changes: 1 addition & 14 deletions src/Delimiter/DelimiterStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

use League\CommonMark\Delimiter\Processor\CacheableDelimiterProcessorInterface;
use League\CommonMark\Delimiter\Processor\DelimiterProcessorCollection;
use League\CommonMark\Exception\LogicException;
use League\CommonMark\Node\Inline\AdjacentTextMerger;
use League\CommonMark\Node\Node;

Expand Down Expand Up @@ -81,9 +80,6 @@ public function getLastBracket(): ?Bracket
return $this->brackets;
}

/**
* @throws LogicException
*/
private function findEarliest(int $stackBottom): ?DelimiterInterface
{
// Move back to first relevant delim.
Expand Down Expand Up @@ -149,9 +145,6 @@ private function removeDelimiterAndNode(DelimiterInterface $delimiter): void
$this->removeDelimiter($delimiter);
}

/**
* @throws LogicException
*/
private function removeDelimitersBetween(DelimiterInterface $opener, DelimiterInterface $closer): void
{
$delimiter = $closer->getPrevious();
Expand All @@ -165,8 +158,6 @@ private function removeDelimitersBetween(DelimiterInterface $opener, DelimiterIn

/**
* @param DelimiterInterface|int|null $stackBottom
*
* @throws LogicException if the index/position cannot be determined for some delimiter
*/
public function removeAll($stackBottom = null): void
{
Expand Down Expand Up @@ -230,8 +221,6 @@ public function searchByCharacter($characters): ?DelimiterInterface
/**
* @param DelimiterInterface|int|null $stackBottom
*
* @throws LogicException if the index/position cannot be determined for any delimiter
*
* @todo change $stackBottom to an int in 3.0
*/
public function processDelimiters($stackBottom, DelimiterProcessorCollection $processors): void
Expand Down Expand Up @@ -350,8 +339,6 @@ public function __destruct()

/**
* @deprecated This method will be dropped in 3.0 once all delimiters MUST have an index/position
*
* @throws LogicException if no index was defined on this delimiter, and no reasonable guess could be made based on its neighbors
*/
private function getIndex(?DelimiterInterface $delimiter): int
{
Expand Down Expand Up @@ -395,6 +382,6 @@ private function getIndex(?DelimiterInterface $delimiter): int
} while ($next = $next->getNext());

// No index was defined on this delimiter, and none could be guesstimated based on the stack.
throw new LogicException('No index was defined on this delimiter, and none could be guessed based on the stack. Ensure you are passing the index when instantiating the Delimiter.');
return $this->missingIndexCache[$delimiter] = $this->getIndex($delimiter->getPrevious()) + 1;
}
}
4 changes: 0 additions & 4 deletions src/Extension/Table/TableParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

namespace League\CommonMark\Extension\Table;

use League\CommonMark\Exception\LogicException;
use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
use League\CommonMark\Parser\Block\BlockContinue;
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
Expand Down Expand Up @@ -151,9 +150,6 @@ public function parseInlines(InlineParserEngineInterface $inlineParser): void
}
}

/**
* @throws LogicException
*/
private function parseCell(string $cell, int $column, InlineParserEngineInterface $inlineParser): TableCell
{
$tableCell = new TableCell(TableCell::TYPE_DATA, $this->columns[$column] ?? null);
Expand Down
3 changes: 0 additions & 3 deletions src/Parser/Block/BlockContinueParserWithInlinesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@

namespace League\CommonMark\Parser\Block;

use League\CommonMark\Exception\LogicException;
use League\CommonMark\Parser\InlineParserEngineInterface;

interface BlockContinueParserWithInlinesInterface extends BlockContinueParserInterface
{
/**
* Parse any inlines inside of the current block
*
* @throws LogicException
*/
public function parseInlines(InlineParserEngineInterface $inlineParser): void;
}
4 changes: 0 additions & 4 deletions src/Parser/Inline/InlineParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@

namespace League\CommonMark\Parser\Inline;

use League\CommonMark\Exception\LogicException;
use League\CommonMark\Parser\InlineParserContext;

interface InlineParserInterface
{
public function getMatchDefinition(): InlineParserMatch;

/**
* @throws LogicException
*/
public function parse(InlineParserContext $inlineContext): bool;
}
3 changes: 0 additions & 3 deletions src/Parser/InlineParserEngineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace League\CommonMark\Parser;

use League\CommonMark\Exception\LogicException;
use League\CommonMark\Node\Block\AbstractBlock;

/**
Expand All @@ -23,8 +22,6 @@ interface InlineParserEngineInterface
{
/**
* Parse the given contents as inlines and insert them into the given block
*
* @throws LogicException
*/
public function parse(string $contents, AbstractBlock $block): void;
}
3 changes: 0 additions & 3 deletions src/Parser/MarkdownParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Event\DocumentPreParsedEvent;
use League\CommonMark\Exception\CommonMarkException;
use League\CommonMark\Exception\LogicException;
use League\CommonMark\Input\MarkdownInput;
use League\CommonMark\Node\Block\Document;
use League\CommonMark\Node\Block\Paragraph;
Expand Down Expand Up @@ -267,8 +266,6 @@ private function finalize(BlockContinueParserInterface $blockParser, int $endLin

/**
* Walk through a block & children recursively, parsing string content into inline content where appropriate.
*
* @throws LogicException
*/
private function processInlines(int $inputSize): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Delimiter/DelimiterStackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public function testGetIndex(): void
$method = $reflection->getMethod('getIndex');
$method->setAccessible(true);

$this->assertSame(0, $method->invoke($stack, $delim1));
$this->assertSame(9, $method->invoke($stack, $delim1));
$this->assertSame(10, $method->invoke($stack, $delim2));
$this->assertSame(20, $method->invoke($stack, $delim3));
$this->assertSame(21, $method->invoke($stack, $delim4));
Expand Down

0 comments on commit e1cfa8d

Please sign in to comment.