Skip to content

Commit

Permalink
[FEATURE] Make DocumentParserContext Availible in Text Roles
Browse files Browse the repository at this point in the history
The ParserContext is a Part of the DocumentParserContext, therefore information got lost that will be needed
  • Loading branch information
linawolf committed Aug 12, 2023
1 parent 72613f3 commit 6c21552
Show file tree
Hide file tree
Showing 25 changed files with 65 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Exception;
use phpDocumentor\Guides\Nodes\Inline\PlainTextInlineNode;
use phpDocumentor\Guides\Nodes\InlineCompoundNode;
use phpDocumentor\Guides\ParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules\InlineRule;

use function usort;
Expand All @@ -26,7 +25,7 @@ public function __construct(iterable $inlineRules)
});
}

public function parse(string $content, ParserContext $parserContext): InlineCompoundNode
public function parse(string $content, DocumentParserContext $documentParserContext): InlineCompoundNode
{
$lexer = new InlineLexer();
$lexer->setInput($content);
Expand All @@ -38,7 +37,7 @@ public function parse(string $content, ParserContext $parserContext): InlineComp
foreach ($this->rules as $inlineRule) {
$node = null;
if ($inlineRule->applies($lexer)) {
$node = $inlineRule->apply($parserContext, $lexer);
$node = $inlineRule->apply($documentParserContext, $lexer);
}

if ($node === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function apply(DocumentParserContext $documentParserContext, CompoundNode
$documentIterator = $documentParserContext->getDocumentIterator();
$buffer = $this->collectContent($documentIterator);

$node = $this->inlineTokenParser->parse($buffer->getLinesString(), $documentParserContext->getContext());
$node = $this->inlineTokenParser->parse($buffer->getLinesString(), $documentParserContext);

if ($on !== null) {
$on->setValue([$node]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use phpDocumentor\Guides\Nodes\Inline\CitationInlineNode;
use phpDocumentor\Guides\Nodes\Inline\FootnoteInlineNode;
use phpDocumentor\Guides\Nodes\Inline\InlineNode;
use phpDocumentor\Guides\ParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\AnnotationUtility;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer;

/**
Expand All @@ -21,7 +21,7 @@ public function applies(InlineLexer $lexer): bool
return $lexer->token?->type === InlineLexer::ANNOTATION_START;
}

public function apply(ParserContext $parserContext, InlineLexer $lexer): InlineNode|null
public function apply(DocumentParserContext $documentParserContext, InlineLexer $lexer): InlineNode|null
{
$startPosition = $lexer->token?->position;
$annotationName = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules;

use phpDocumentor\Guides\Nodes\Inline\HyperLinkNode;
use phpDocumentor\Guides\ParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer;

/**
Expand All @@ -25,7 +25,7 @@ public function applies(InlineLexer $lexer): bool
return $lexer->token?->type === InlineLexer::BACKTICK;
}

public function apply(ParserContext $parserContext, InlineLexer $lexer): HyperLinkNode|null
public function apply(DocumentParserContext $documentParserContext, InlineLexer $lexer): HyperLinkNode|null
{
$text = '';
$embeddedUrl = null;
Expand All @@ -36,7 +36,7 @@ public function apply(ParserContext $parserContext, InlineLexer $lexer): HyperLi
case InlineLexer::PHRASE_ANONYMOUS_END:
$lexer->moveNext();

return $this->createAnonymousReference($parserContext, $text, $embeddedUrl);
return $this->createAnonymousReference($documentParserContext, $text, $embeddedUrl);

case InlineLexer::EMBEDED_URL_START:
$embeddedUrl = $this->parseEmbeddedUrl($lexer);
Expand All @@ -57,11 +57,11 @@ public function apply(ParserContext $parserContext, InlineLexer $lexer): HyperLi
return null;
}

private function createAnonymousReference(ParserContext $parserContext, string $link, string|null $embeddedUrl): HyperLinkNode
private function createAnonymousReference(DocumentParserContext $documentParserContext, string $link, string|null $embeddedUrl): HyperLinkNode
{
$parserContext->resetAnonymousStack();
$node = $this->createReference($parserContext, $link, $embeddedUrl, false);
$parserContext->pushAnonymous($link);
$documentParserContext->getContext()->resetAnonymousStack();
$node = $this->createReference($documentParserContext, $link, $embeddedUrl, false);
$documentParserContext->getContext()->pushAnonymous($link);

return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules;

use phpDocumentor\Guides\Nodes\Inline\HyperLinkNode;
use phpDocumentor\Guides\ParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer;

use function trim;
Expand All @@ -26,22 +26,22 @@ public function applies(InlineLexer $lexer): bool
return $lexer->token?->type === InlineLexer::ANONYMOUSE_REFERENCE;
}

public function apply(ParserContext $parserContext, InlineLexer $lexer): HyperLinkNode|null
public function apply(DocumentParserContext $documentParserContext, InlineLexer $lexer): HyperLinkNode|null
{
$node = $this->createAnonymousReference(
$parserContext,
$documentParserContext,
trim((string) $lexer->token?->value, '_'),
);
$lexer->moveNext();

return $node;
}

private function createAnonymousReference(ParserContext $parserContext, string $link): HyperLinkNode
private function createAnonymousReference(DocumentParserContext $documentParserContext, string $link): HyperLinkNode
{
$parserContext->resetAnonymousStack();
$node = $this->createReference($parserContext, $link, null, false);
$parserContext->pushAnonymous($link);
$documentParserContext->getContext()->resetAnonymousStack();
$node = $this->createReference($documentParserContext, $link, null, false);
$documentParserContext->getContext()->pushAnonymous($link);

return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use phpDocumentor\Guides\Nodes\Inline\InlineNode;
use phpDocumentor\Guides\Nodes\Inline\LiteralInlineNode;
use phpDocumentor\Guides\ParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer;

/**
Expand All @@ -19,7 +19,7 @@ public function applies(InlineLexer $lexer): bool
return $lexer->token?->type === InlineLexer::BACKTICK;
}

public function apply(ParserContext $parserContext, InlineLexer $lexer): InlineNode|null
public function apply(DocumentParserContext $documentParserContext, InlineLexer $lexer): InlineNode|null
{
$text = '';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use phpDocumentor\Guides\Nodes\Inline\EmphasisInlineNode;
use phpDocumentor\Guides\Nodes\Inline\InlineNode;
use phpDocumentor\Guides\ParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer;

/**
Expand All @@ -19,7 +19,7 @@ public function applies(InlineLexer $lexer): bool
return $lexer->token?->type === InlineLexer::EMPHASIS_DELIMITER;
}

public function apply(ParserContext $parserContext, InlineLexer $lexer): InlineNode|null
public function apply(DocumentParserContext $documentParserContext, InlineLexer $lexer): InlineNode|null
{
$text = '';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules;

use phpDocumentor\Guides\Nodes\Inline\PlainTextInlineNode;
use phpDocumentor\Guides\ParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer;

use function preg_match;
Expand All @@ -21,7 +21,7 @@ public function applies(InlineLexer $lexer): bool
return $lexer->token?->type === InlineLexer::ESCAPED_SIGN;
}

public function apply(ParserContext $parserContext, InlineLexer $lexer): PlainTextInlineNode|null
public function apply(DocumentParserContext $documentParserContext, InlineLexer $lexer): PlainTextInlineNode|null
{
$char = $lexer->token?->value ?? '';
$char = substr($char, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
namespace phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules;

use phpDocumentor\Guides\Nodes\Inline\InlineNode;
use phpDocumentor\Guides\ParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer;

interface InlineRule
{
public function applies(InlineLexer $lexer): bool;

public function apply(ParserContext $parserContext, InlineLexer $lexer): InlineNode|null;
public function apply(DocumentParserContext $documentParserContext, InlineLexer $lexer): InlineNode|null;

public function getPriority(): int;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules;

use phpDocumentor\Guides\Nodes\Inline\InlineNode;
use phpDocumentor\Guides\ParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer;

class InternalReferenceRule extends ReferenceRule
Expand All @@ -15,7 +15,7 @@ public function applies(InlineLexer $lexer): bool
return $lexer->token?->type === InlineLexer::INTERNAL_REFERENCE_START;
}

public function apply(ParserContext $parserContext, InlineLexer $lexer): InlineNode|null
public function apply(DocumentParserContext $documentParserContext, InlineLexer $lexer): InlineNode|null
{
$text = '';
$initialPosition = $lexer->token?->position;
Expand All @@ -25,7 +25,7 @@ public function apply(ParserContext $parserContext, InlineLexer $lexer): InlineN
case InlineLexer::BACKTICK:
$lexer->moveNext();

return $this->createReference($parserContext, $text);
return $this->createReference($documentParserContext, $text);

default:
$text .= $lexer->token->value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules;

use phpDocumentor\Guides\Nodes\Inline\LiteralInlineNode;
use phpDocumentor\Guides\ParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer;

use function strlen;
Expand All @@ -21,7 +21,7 @@ public function applies(InlineLexer $lexer): bool
return $lexer->token?->type === InlineLexer::LITERAL;
}

public function apply(ParserContext $parserContext, InlineLexer $lexer): LiteralInlineNode
public function apply(DocumentParserContext $documentParserContext, InlineLexer $lexer): LiteralInlineNode
{
$literal = $lexer->token?->value ?? '';
if (strlen($literal) > 4) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules;

use phpDocumentor\Guides\Nodes\Inline\InlineNode;
use phpDocumentor\Guides\ParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer;

/**
Expand All @@ -25,7 +25,7 @@ public function applies(InlineLexer $lexer): bool
return $lexer->token?->type === InlineLexer::BACKTICK;
}

public function apply(ParserContext $parserContext, InlineLexer $lexer): InlineNode|null
public function apply(DocumentParserContext $documentParserContext, InlineLexer $lexer): InlineNode|null
{
$text = '';
$embeddedUrl = null;
Expand All @@ -39,7 +39,7 @@ public function apply(ParserContext $parserContext, InlineLexer $lexer): InlineN
$text = $embeddedUrl ?? '';
}

return $this->createReference($parserContext, $text, $embeddedUrl);
return $this->createReference($documentParserContext, $text, $embeddedUrl);

case InlineLexer::EMBEDED_URL_START:
$embeddedUrl = $this->parseEmbeddedUrl($lexer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules;

use phpDocumentor\Guides\Nodes\Inline\InlineNode;
use phpDocumentor\Guides\ParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer;

use function rtrim;
Expand All @@ -26,10 +26,10 @@ public function applies(InlineLexer $lexer): bool
return $lexer->token?->type === InlineLexer::NAMED_REFERENCE;
}

public function apply(ParserContext $parserContext, InlineLexer $lexer): InlineNode|null
public function apply(DocumentParserContext $documentParserContext, InlineLexer $lexer): InlineNode|null
{
$value = rtrim($lexer->token?->value ?? '', '_');
$node = $this->createReference($parserContext, $value);
$node = $this->createReference($documentParserContext, $value);

$lexer->moveNext();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules;

use phpDocumentor\Guides\Nodes\Inline\WhitespaceInlineNode;
use phpDocumentor\Guides\ParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer;

/**
Expand All @@ -18,7 +18,7 @@ public function applies(InlineLexer $lexer): bool
return $lexer->token?->type === InlineLexer::NBSP;
}

public function apply(ParserContext $parserContext, InlineLexer $lexer): WhitespaceInlineNode
public function apply(DocumentParserContext $documentParserContext, InlineLexer $lexer): WhitespaceInlineNode
{
$lexer->moveNext();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use phpDocumentor\Guides\Nodes\Inline\InlineNode;
use phpDocumentor\Guides\Nodes\Inline\PlainTextInlineNode;
use phpDocumentor\Guides\ParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer;

class PlainTextRule implements InlineRule
Expand All @@ -16,7 +16,7 @@ public function applies(InlineLexer $lexer): bool
return true;
}

public function apply(ParserContext $parserContext, InlineLexer $lexer): InlineNode|null
public function apply(DocumentParserContext $documentParserContext, InlineLexer $lexer): InlineNode|null
{
$node = new PlainTextInlineNode($lexer->token?->value ?? '');
$lexer->moveNext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules;

use phpDocumentor\Guides\Nodes\Inline\HyperLinkNode;
use phpDocumentor\Guides\ParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer;

use function preg_replace;
Expand All @@ -14,15 +14,15 @@

abstract class ReferenceRule extends AbstractInlineRule
{
protected function createReference(ParserContext $parserContext, string $link, string|null $embeddedUrl = null, bool $registerLink = true): HyperLinkNode
protected function createReference(DocumentParserContext $documentParserContext, string $link, string|null $embeddedUrl = null, bool $registerLink = true): HyperLinkNode
{
// the link may have a new line in it, so we need to strip it
// before setting the link and adding a token to be replaced
$link = str_replace("\n", ' ', $link);
$link = trim(preg_replace('/\s+/', ' ', $link) ?? '');

if ($registerLink && $embeddedUrl !== null) {
$parserContext->setLink($link, $embeddedUrl);
$documentParserContext->getContext()->setLink($link, $embeddedUrl);
}

return new HyperLinkNode($link, $embeddedUrl ?? $link);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules;

use phpDocumentor\Guides\Nodes\Inline\HyperLinkNode;
use phpDocumentor\Guides\ParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer;

/**
Expand All @@ -24,10 +24,10 @@ public function applies(InlineLexer $lexer): bool
return $lexer->token?->type === InlineLexer::EMAIL;
}

public function apply(ParserContext $parserContext, InlineLexer $lexer): HyperLinkNode|null
public function apply(DocumentParserContext $documentParserContext, InlineLexer $lexer): HyperLinkNode|null
{
$value = $lexer->token?->value ?? '';
$node = $this->createReference($parserContext, $value, $value, false);
$node = $this->createReference($documentParserContext, $value, $value, false);

$lexer->moveNext();

Expand Down
Loading

0 comments on commit 6c21552

Please sign in to comment.