<?php
declare(strict_types=1);
namespace League\CommonMark\Parser;
use League\CommonMark\Environment\EnvironmentInterface;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Event\DocumentPreParsedEvent;
use League\CommonMark\Exception\CommonMarkException;
use League\CommonMark\Input\MarkdownInput;
use League\CommonMark\Node\Block\Document;
use League\CommonMark\Node\Block\Paragraph;
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
use League\CommonMark\Parser\Block\BlockContinueParserWithInlinesInterface;
use League\CommonMark\Parser\Block\BlockStart;
use League\CommonMark\Parser\Block\BlockStartParserInterface;
use League\CommonMark\Parser\Block\DocumentBlockParser;
use League\CommonMark\Parser\Block\ParagraphParser;
use League\CommonMark\Reference\MemoryLimitedReferenceMap;
use League\CommonMark\Reference\ReferenceInterface;
use League\CommonMark\Reference\ReferenceMap;
final class MarkdownParser implements MarkdownParserInterface
{
private EnvironmentInterface $environment;
private int $maxNestingLevel;
private ReferenceMap $referenceMap;
private int $lineNumber = 0;
private Cursor $cursor;
private array $activeBlockParsers = [];
private array $closedBlockParsers = [];
public function __construct(EnvironmentInterface $environment)
{
$this->environment = $environment;
}
private function initialize(): void
{
$this->referenceMap = new ReferenceMap();
$this->lineNumber = 0;
$this->activeBlockParsers = [];
$this->closedBlockParsers = [];
$this->maxNestingLevel = $this->environment->getConfiguration()->get('max_nesting_level');
}
public function parse(string $input): Document
{
$this->initialize();
$documentParser = new DocumentBlockParser($this->referenceMap);
$this->activateBlockParser($documentParser);
$preParsedEvent = new DocumentPreParsedEvent($documentParser->getBlock(), new MarkdownInput($input));
$this->environment->dispatch($preParsedEvent);
$markdownInput = $preParsedEvent->getMarkdown();
foreach ($markdownInput->getLines() as $lineNumber => $line) {
$this->lineNumber = $lineNumber;
$this->parseLine($line);
}
$this->closeBlockParsers(\count($this->activeBlockParsers), $this->lineNumber);
$this->processInlines(\strlen($input));
$this->environment->dispatch(new DocumentParsedEvent($documentParser->getBlock()));
return $documentParser->getBlock();
}
private function parseLine(string $line): void
{
$line = \str_replace("\0", "\u{FFFD}", $line);
$this->cursor = new Cursor($line);
$matches = $this->parseBlockContinuation();
if ($matches === null) {
return;
}
$unmatchedBlocks = \count($this->activeBlockParsers) - $matches;
$blockParser = $this->activeBlockParsers[$matches - 1];
$startedNewBlock = false;
$tryBlockStarts = $blockParser->getBlock() instanceof Paragraph || $blockParser->isContainer();
while ($tryBlockStarts) {
if ($this->cursor->isBlank()) {
$this->cursor->advanceToEnd();
break;
}
if ($blockParser->getBlock()->getDepth() >= $this->maxNestingLevel) {
break;
}
$blockStart = $this->findBlockStart($blockParser);
if ($blockStart === null || $blockStart->isAborting()) {
$this->cursor->advanceToNextNonSpaceOrTab();
break;
}
if (($state = $blockStart->getCursorState()) !== null) {
$this->cursor->restoreState($state);
}
$startedNewBlock = true;
if ($unmatchedBlocks > 0) {
$this->closeBlockParsers($unmatchedBlocks, $this->lineNumber - 1);
$unmatchedBlocks = 0;
}
$oldBlockLineStart = null;
if ($blockStart->isReplaceActiveBlockParser()) {
$oldBlockLineStart = $this->prepareActiveBlockParserForReplacement();
}
foreach ($blockStart->getBlockParsers() as $newBlockParser) {
$blockParser = $this->addChild($newBlockParser, $oldBlockLineStart);
$tryBlockStarts = $newBlockParser->isContainer();
}
}
if (! $startedNewBlock && ! $this->cursor->isBlank() && $this->getActiveBlockParser()->canHaveLazyContinuationLines()) {
$this->getActiveBlockParser()->addLine($this->cursor->getRemainder());
} else {
if ($unmatchedBlocks > 0) {
$this->closeBlockParsers($unmatchedBlocks, $this->lineNumber - 1);
}
if (! $blockParser->isContainer()) {
$this->getActiveBlockParser()->addLine($this->cursor->getRemainder());
} elseif (! $this->cursor->isBlank()) {
$this->addChild(new ParagraphParser());
$this->getActiveBlockParser()->addLine($this->cursor->getRemainder());
}
}
}
private function parseBlockContinuation(): ?int
{
$matches = 1;
for ($i = 1; $i < \count($this->activeBlockParsers); $i++) {
$blockParser = $this->activeBlockParsers[$i];
$blockContinue = $blockParser->tryContinue(clone $this->cursor, $this->getActiveBlockParser());
if ($blockContinue === null) {
break;
}
if ($blockContinue->isFinalize()) {
$this->closeBlockParsers(\count($this->activeBlockParsers) - $i, $this->lineNumber);
return null;
}
if (($state = $blockContinue->getCursorState()) !== null) {
$this->cursor->restoreState($state);
}
$matches++;
}
return $matches;
}
private function findBlockStart(BlockContinueParserInterface $lastMatchedBlockParser): ?BlockStart
{
$matchedBlockParser = new MarkdownParserState($this->getActiveBlockParser(), $lastMatchedBlockParser);
foreach ($this->environment->getBlockStartParsers() as $blockStartParser) {
\assert($blockStartParser instanceof BlockStartParserInterface);
if (($result = $blockStartParser->tryStart(clone $this->cursor, $matchedBlockParser)) !== null) {
return $result;
}
}
return null;
}
private function closeBlockParsers(int $count, int $endLineNumber): void
{
for ($i = 0; $i < $count; $i++) {
$blockParser = $this->deactivateBlockParser();
$this->finalize($blockParser, $endLineNumber);
if ($blockParser instanceof BlockContinueParserWithInlinesInterface) {
$this->closedBlockParsers[] = $blockParser;
}
}
}
private function finalize(BlockContinueParserInterface $blockParser, int $endLineNumber): void
{
if ($blockParser instanceof ParagraphParser) {
$this->updateReferenceMap($blockParser->getReferences());
}
$blockParser->getBlock()->setEndLine($endLineNumber);
$blockParser->closeBlock();
}
private function processInlines(int $inputSize): void
{
$p = new InlineParserEngine($this->environment, new MemoryLimitedReferenceMap($this->referenceMap, $inputSize));
foreach ($this->closedBlockParsers as $blockParser) {
$blockParser->parseInlines($p);
}
}
private function addChild(BlockContinueParserInterface $blockParser, ?int $startLineNumber = null): BlockContinueParserInterface
{
$blockParser->getBlock()->setStartLine($startLineNumber ?? $this->lineNumber);
while (! $this->getActiveBlockParser()->canContain($blockParser->getBlock())) {
$this->closeBlockParsers(1, ($startLineNumber ?? $this->lineNumber) - 1);
}
$this->getActiveBlockParser()->getBlock()->appendChild($blockParser->getBlock());
$this->activateBlockParser($blockParser);
return $blockParser;
}
private function activateBlockParser(BlockContinueParserInterface $blockParser): void
{
$this->activeBlockParsers[] = $blockParser;
}
private function deactivateBlockParser(): BlockContinueParserInterface
{
$popped = \array_pop($this->activeBlockParsers);
if ($popped === null) {
throw new ParserLogicException('The last block parser should not be deactivated');
}
return $popped;
}
private function prepareActiveBlockParserForReplacement(): ?int
{
$old = $this->deactivateBlockParser();
if ($old instanceof ParagraphParser) {
$this->updateReferenceMap($old->getReferences());
}
$old->getBlock()->detach();
return $old->getBlock()->getStartLine();
}
private function updateReferenceMap(iterable $references): void
{
foreach ($references as $reference) {
if (! $this->referenceMap->contains($reference->getLabel())) {
$this->referenceMap->add($reference);
}
}
}
public function getActiveBlockParser(): BlockContinueParserInterface
{
$active = \end($this->activeBlockParsers);
if ($active === false) {
throw new ParserLogicException('No active block parsers are available');
}
return $active;
}
}