Skip to content

Commit

Permalink
Remove phplrt dependency and speedup comment lexing
Browse files Browse the repository at this point in the history
  • Loading branch information
SerafimArts committed Mar 30, 2024
1 parent 4933040 commit dda2004
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 168 deletions.
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
"issues": "https://github.com/php-type-language/phpdoc/issues"
},
"require": {
"php": "^8.1",
"phplrt/lexer": "^3.6",
"phplrt/source": "^3.6"
"php": "^8.1"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 2 additions & 4 deletions src/Exception/InvalidTagNameException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace TypeLang\PHPDoc\Exception;

use Phplrt\Contracts\Source\ReadableInterface;

class InvalidTagNameException extends InvalidTagException
{
final public const ERROR_CODE_EMPTY = 0x01 + parent::CODE_LAST;
Expand Down Expand Up @@ -33,7 +31,7 @@ public static function fromEmptyTag(int $offset = 0): static
*
* @param int<0, max> $offset
*/
public static function fromEmptyTagName(ReadableInterface|string $source, int $offset = 0): static
public static function fromEmptyTagName(string $source, int $offset = 0): static
{
$message = 'Tag name cannot be empty';

Expand All @@ -45,7 +43,7 @@ public static function fromEmptyTagName(ReadableInterface|string $source, int $o
*
* @param int<0, max> $offset
*/
public static function fromInvalidTagPrefix(ReadableInterface|string $source, int $offset = 0): static
public static function fromInvalidTagPrefix(string $source, int $offset = 0): static
{
$message = 'The tag name must starts with the "@" character';

Expand Down
17 changes: 5 additions & 12 deletions src/Exception/ParsingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,24 @@

namespace TypeLang\PHPDoc\Exception;

use Phplrt\Contracts\Source\ReadableInterface;
use Phplrt\Source\Source;

class ParsingException extends \RuntimeException implements RuntimeExceptionInterface
{
final public const ERROR_CODE_INTERNAL = 0x01;

protected const CODE_LAST = self::ERROR_CODE_INTERNAL;

public readonly ReadableInterface $source;
public readonly string $source;

/**
* @param int<0, max> $offset
*/
final public function __construct(
ReadableInterface|string $source,
string $source,
public readonly int $offset = 0,
string $message = "",
int $code = 0,
?\Throwable $previous = null
) {
if (\is_string($source)) {
$source = new Source($source);
}

$this->source = $source;

parent::__construct($message, $code, $previous);
Expand All @@ -37,7 +30,7 @@ final public function __construct(
/**
* @param int<0, max> $offset
*/
public static function fromInternalError(ReadableInterface|string $source, int $offset, \Throwable $e): self
public static function fromInternalError(string $source, int $offset, \Throwable $e): self
{
return new static(
source: $source,
Expand All @@ -51,7 +44,7 @@ public static function fromInternalError(ReadableInterface|string $source, int $
/**
* @param int<0, max> $offset
*/
public function withSource(ReadableInterface|string $source, int $offset): self
public function withSource(string $source, int $offset): self
{
return new static(
source: $source,
Expand All @@ -62,7 +55,7 @@ public function withSource(ReadableInterface|string $source, int $offset): self
);
}

public function getSource(): ReadableInterface
public function getSource(): string
{
return $this->source;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Exception/RuntimeExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace TypeLang\PHPDoc\Exception;

use Phplrt\Contracts\Source\ReadableInterface;

/**
* Error occurring while processing phpdoc content.
*/
Expand All @@ -16,12 +14,12 @@ interface RuntimeExceptionInterface extends PHPDocExceptionInterface
*
* @param int<0, max> $offset
*/
public function withSource(ReadableInterface|string $source, int $offset): self;
public function withSource(string $source, int $offset): self;

/**
* Returns the full content in which the error occurred.
*/
public function getSource(): ReadableInterface;
public function getSource(): string;

/**
* Returns the byte offset at the location where the error occurs.
Expand Down
14 changes: 2 additions & 12 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
namespace TypeLang\PHPDoc;

use JetBrains\PhpStorm\Language;
use Phplrt\Contracts\Lexer\LexerExceptionInterface;
use Phplrt\Contracts\Lexer\LexerRuntimeExceptionInterface;
use Phplrt\Contracts\Source\SourceExceptionInterface;
use TypeLang\PHPDoc\Exception\ParsingException;
use TypeLang\PHPDoc\Exception\RuntimeExceptionInterface;
use TypeLang\PHPDoc\Parser\Comment\CommentParserInterface;
use TypeLang\PHPDoc\Parser\Comment\LexerAwareCommentParser;
use TypeLang\PHPDoc\Parser\Comment\RegexCommentParser;
use TypeLang\PHPDoc\Parser\Comment\Segment;
use TypeLang\PHPDoc\Parser\Description\DescriptionParserInterface;
use TypeLang\PHPDoc\Parser\Description\SprintfDescriptionReader;
Expand All @@ -25,7 +22,7 @@
class Parser implements ParserInterface
{
public function __construct(
private readonly CommentParserInterface $comments = new LexerAwareCommentParser(),
private readonly CommentParserInterface $comments = new RegexCommentParser(),
private readonly DescriptionParserInterface $descriptions = new SprintfDescriptionReader(),
private readonly TagParserInterface $tags = new TagParser(),
) {}
Expand Down Expand Up @@ -64,10 +61,7 @@ public function parse(#[Language('PHP')] string $docblock): DocBlock
/**
* @return \Generator<array-key, Segment, void, DocBlock>
*
* @throws LexerExceptionInterface
* @throws LexerRuntimeExceptionInterface
* @throws RuntimeExceptionInterface
* @throws SourceExceptionInterface
*/
private function analyze(string $docblock): \Generator
{
Expand Down Expand Up @@ -106,10 +100,6 @@ private function analyze(string $docblock): \Generator

/**
* @return \Generator<array-key, Segment, void, non-empty-list<string>>
*
* @throws LexerExceptionInterface
* @throws LexerRuntimeExceptionInterface
* @throws SourceExceptionInterface
*/
private function groupByCommentSections(string $docblock): \Generator
{
Expand Down
128 changes: 0 additions & 128 deletions src/Parser/Comment/LexerAwareCommentParser.php

This file was deleted.

Loading

0 comments on commit dda2004

Please sign in to comment.