Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintenance work on library #1

Merged
merged 19 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/php-tests.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: PHP Tests

on:
pull_request:
push:
branches: [ "master" ]

permissions:
contents: read
Expand All @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
operating-system: [ ubuntu-latest ]
php-version: [ '8.0', '8.1', '8.2' ]
php-version: ['8.2', '8.3']

name: PHP ${{ matrix.php-version }} test on ${{ matrix.operating-system }}

Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,22 @@
"php": "^8.2.0"
},
"require-dev": {
"symfony/console": ">=6.0",
"phpunit/phpunit": "^11.0.3"
"phpunit/phpunit": "^11.0.3",
"rector/rector": "^1.0",
"symfony/console": ">=6.0"
},
"suggest": {
"symfony/console": "for the command-line tool"
},
"autoload": {
"psr-0": {
"psr-4": {
"Dissect\\": [
"src/"
]
}
},
"autoload-dev": {
"psr-0": {
"psr-4": {
"Dissect\\": [
"tests/"
]
Expand Down
2 changes: 1 addition & 1 deletion docs/lexing.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ Now that we've demonstrated how to perform lexical analysis with
Dissect, we can move onto syntactical analysis, commonly known as
[parsing][parsing].

[tokenstream]: ../src/Dissect/Lexer/TokenStream/TokenStream.php
[tokenstream]: ../src/Lexer/TokenStream/TokenStream.php
[parsing]: parsing.md
[doctrinelexer]: https://github.com/doctrine/lexer/blob/master/lib/Doctrine/Common/Lexer/AbstractLexer.php
[doctrine]: https://github.com/doctrine/lexer
37 changes: 37 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
use Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector;
use Rector\Php71\Rector\ClassConst\PublicConstantVisibilityRector;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\TypeDeclaration\Rector\Class_\AddTestsVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
// uncomment to reach your current PHP version
// ->withPhpSets()
->withRules([
// Dead-code
RemoveUselessParamTagRector::class,
RemoveUselessReturnTagRector::class,
RemoveUselessVarTagRector::class,
PublicConstantVisibilityRector::class,
ClosureToArrowFunctionRector::class,
AddVoidReturnTypeWhereNoReturnRector::class,
AddTestsVoidReturnTypeWhereNoReturnRector::class,
// DeclareStrictTypesRector::class,
])
->withSets([
PHPUnitSetList::ANNOTATIONS_TO_ATTRIBUTES,
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
]);
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Dissect\Console;

use Symfony\Component\Console\Application as BaseApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Dissect\Console\Command;

use Dissect\Parser\LALR1\Analysis\Exception\ConflictException;
Expand Down Expand Up @@ -27,28 +29,28 @@ protected function configure()
->addOption('state', 's', InputOption::VALUE_REQUIRED, 'Exports only the specified state instead of the entire DFA.')
->addOption('output-dir', 'o', InputOption::VALUE_REQUIRED, 'Overrides the default output directory.')
->setHelp(<<<EOT
Analyzes the given grammar and, if successful, exports the parse table to a PHP
file.

By default, the output directory is taken to be the one in which the grammar is
defined. You can change that with the <info>--output-dir</info> option:

<info>--output-dir=../some/other/dir</info>

The parse table is by default written with minimal whitespace to make it compact.
If you wish to inspect the table manually, you can export it in a readable and
well-commented way with the <info>--debug</info> option.

If you wish to inspect the handle-finding automaton for your grammar (perhaps
to aid with grammar debugging), use the <info>--dfa</info> option. When in use, Dissect
will create a file with the automaton exported as a Graphviz graph
in the output directory.

Additionally, you can use the <info>--state</info> option to export only the specified
state and any relevant transitions:

<info>--dfa --state=5</info>
EOT
Analyzes the given grammar and, if successful, exports the parse table to a PHP
file.
By default, the output directory is taken to be the one in which the grammar is
defined. You can change that with the <info>--output-dir</info> option:
<info>--output-dir=../some/other/dir</info>
The parse table is by default written with minimal whitespace to make it compact.
If you wish to inspect the table manually, you can export it in a readable and
well-commented way with the <info>--debug</info> option.
If you wish to inspect the handle-finding automaton for your grammar (perhaps
to aid with grammar debugging), use the <info>--dfa</info> option. When in use, Dissect
will create a file with the automaton exported as a Graphviz graph
in the output directory.
Additionally, you can use the <info>--state</info> option to export only the specified
state and any relevant transitions:
<info>--dfa --state=5</info>
EOT
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

declare(strict_types=1);

namespace Dissect\Lexer;

use Dissect\Lexer\Exception\RecognitionException;
use Dissect\Lexer\TokenStream\ArrayTokenStream;
use Dissect\Lexer\TokenStream\TokenStream;
use Dissect\Parser\Parser;
use Dissect\Util\Util;

Expand All @@ -13,18 +16,14 @@
* SimpleLexer and StatefulLexer extend this class.
*
* @author Jakub Lédl <[email protected]>
* @see \Dissect\Lexer\AbstractLexerTest
*/
abstract class AbstractLexer implements Lexer
{
/**
* @var int
*/
private int $line = 1;

/**
* Returns the current line.
*
* @return int The current line.
*/
protected function getCurrentLine(): int
{
Expand All @@ -36,24 +35,18 @@ protected function getCurrentLine(): int
* Returns the token on success or null on failure.
*
* @param string $string The string to extract the token from.
*
* @return Token|null The extracted token or null.
*/
abstract protected function extractToken(string $string): ?Token;

/**
* Should given token be skipped?
*
* @param Token $token The token to evaluate.
*
* @return boolean Whether to skip the token.
*/
abstract protected function shouldSkipToken(Token $token): bool;

/**
* {@inheritDoc}
*/
public function lex(string $string): TokenStream\TokenStream|ArrayTokenStream
public function lex(string $string): TokenStream
{
// normalize line endings
$string = strtr($string, ["\r\n" => "\n", "\r" => "\n"]);
Expand Down Expand Up @@ -92,6 +85,6 @@ public function lex(string $string): TokenStream\TokenStream|ArrayTokenStream

$tokens[] = new CommonToken(Parser::EOF_TOKEN_TYPE, '', $this->line);

return new ArrayTokenStream($tokens);
return new ArrayTokenStream(...$tokens);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Dissect\Lexer;

/**
Expand All @@ -13,12 +15,12 @@ class CommonToken implements Token
* Constructor.
*
* @param mixed $type The type of the token.
* @param string $value The token value.
* @param int|string $value The token value.
* @param int $line The line.
*/
public function __construct(
protected mixed $type,
protected string $value,
protected int|string $value,
protected int $line
) {}

Expand All @@ -33,7 +35,7 @@ public function getType(): mixed
/**
* {@inheritDoc}
*/
public function getValue(): string
public function getValue(): int|string
{
return $this->value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Dissect\Lexer\Exception;

use RuntimeException;
Expand Down
4 changes: 2 additions & 2 deletions src/Dissect/Lexer/Lexer.php → src/Lexer/Lexer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Dissect\Lexer;

use Dissect\Lexer\Exception\RecognitionException;
Expand All @@ -19,8 +21,6 @@ interface Lexer
* @param string $string The string to lex.
*
* @throws RecognitionException When unable to extract more tokens from the string.
*
* @return TokenStream The resulting token stream.
*/
public function lex(string $string): TokenStream;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Dissect\Lexer\Recognizer;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php

declare(strict_types=1);

namespace Dissect\Lexer\Recognizer;

/**
* The RegexRecognizer matches a string using a
* regular expression.
*
* @author Jakub Lédl <[email protected]>
* @see \Dissect\Lexer\Recognizer\RegexRecognizerTest
*/
class RegexRecognizer implements Recognizer
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php

declare(strict_types=1);

namespace Dissect\Lexer\Recognizer;

/**
* SimpleRecognizer matches a string by a simple
* strpos match.
*
* @author Jakub Lédl <[email protected]>
* @see \Dissect\Lexer\Recognizer\SimpleRecognizerTest
*/
class SimpleRecognizer implements Recognizer
{
Expand Down
13 changes: 4 additions & 9 deletions src/Dissect/Lexer/RegexLexer.php → src/Lexer/RegexLexer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Dissect\Lexer;

use Dissect\Lexer\TokenStream\ArrayTokenStream;
Expand All @@ -13,6 +15,7 @@
* @author Jonathan Wage <[email protected]>
* @author Roman Borschel <[email protected]>
* @author Jakub Lédl <[email protected]>
* @see \Dissect\Lexer\RegexLexerTest
*/
abstract class RegexLexer implements Lexer
{
Expand Down Expand Up @@ -52,29 +55,21 @@ public function lex(string $string): TokenStream

$tokens[] = new CommonToken(Parser::EOF_TOKEN_TYPE, '', $line);

return new ArrayTokenStream($tokens);
return new ArrayTokenStream(...$tokens);
}

/**
* The patterns corresponding to tokens.
*
* @return array
*/
abstract protected function getCatchablePatterns(): array;

/**
* The patterns corresponding to tokens to be skipped.
*
* @return array
*/
abstract protected function getNonCatchablePatterns(): array;

/**
* Retrieves the token type.
*
* @param string $value
*
* @return string $type
*/
abstract protected function getType(string &$value): string;
}
Loading
Loading