-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac52f93
commit 3c7d63b
Showing
13 changed files
with
371 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Highlight\Languages\Python\Patterns; | ||
|
||
use Tempest\Highlight\IsPattern; | ||
use Tempest\Highlight\Pattern; | ||
use Tempest\Highlight\Tokens\TokenTypeEnum; | ||
|
||
final readonly class PyArgumentPattern implements Pattern | ||
{ | ||
use IsPattern; | ||
|
||
public function getPattern(): string | ||
{ | ||
return '(?<match>\w+)(s*=)'; | ||
} | ||
|
||
public function getTokenType(): TokenTypeEnum | ||
{ | ||
return TokenTypeEnum::VARIABLE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Highlight\Languages\Python\Patterns; | ||
|
||
use Tempest\Highlight\IsPattern; | ||
use Tempest\Highlight\Pattern; | ||
use Tempest\Highlight\Tokens\TokenTypeEnum; | ||
|
||
final readonly class PyBooleanPattern implements Pattern | ||
{ | ||
use IsPattern; | ||
|
||
public function getPattern(): string | ||
{ | ||
return '\b(?<match>(?:False|None|True))\b'; | ||
} | ||
|
||
public function getTokenType(): TokenTypeEnum | ||
{ | ||
return TokenTypeEnum::TYPE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Highlight\Languages\Python\Patterns; | ||
|
||
use Tempest\Highlight\IsPattern; | ||
use Tempest\Highlight\Pattern; | ||
use Tempest\Highlight\Tokens\TokenTypeEnum; | ||
|
||
final readonly class PyBuiltinPattern implements Pattern | ||
{ | ||
use IsPattern; | ||
|
||
public function __construct(private array $builtinFunctions = [ | ||
'__import__', 'abs', 'aiter', 'all', 'any', 'anext', 'ascii', 'bin', 'bool', | ||
'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', | ||
'complex', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', | ||
'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', | ||
'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', | ||
'len', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', | ||
'oct', 'open', 'ord', 'pow', 'print', 'property', 'range', 'repr', 'reversed', | ||
'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', | ||
'super', 'tuple', 'type', 'vars', 'zip', | ||
]) {} | ||
|
||
public function getPattern(): string | ||
{ | ||
$builtinFunctions = implode('|', $this->builtinFunctions); | ||
|
||
return "\b(?<match>(?:{$builtinFunctions}))\b"; | ||
} | ||
|
||
public function getTokenType(): TokenTypeEnum | ||
{ | ||
return TokenTypeEnum::TYPE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Highlight\Languages\Python\Patterns; | ||
|
||
use Tempest\Highlight\IsPattern; | ||
use Tempest\Highlight\Pattern; | ||
use Tempest\Highlight\PatternTest; | ||
use Tempest\Highlight\Tokens\TokenTypeEnum; | ||
|
||
#[PatternTest(input: 'class MyClass:', output: 'MyClass')] | ||
#[PatternTest(input: 'class HisClass(MyClass):', output: 'HisClass')] | ||
final readonly class PyClassNamePattern implements Pattern | ||
{ | ||
use IsPattern; | ||
|
||
public function getPattern(): string | ||
{ | ||
return '\bclass\s+(?<match>\w*)(?=[\s*\:(])'; | ||
} | ||
|
||
public function getTokenType(): TokenTypeEnum | ||
{ | ||
return TokenTypeEnum::PROPERTY; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Highlight\Languages\Python\Patterns; | ||
|
||
use Tempest\Highlight\IsPattern; | ||
use Tempest\Highlight\Pattern; | ||
use Tempest\Highlight\Tokens\TokenTypeEnum; | ||
|
||
final readonly class PyCommentPattern implements Pattern | ||
{ | ||
use IsPattern; | ||
|
||
public function getPattern(): string | ||
{ | ||
return '(^|[^\\\\])(?<match>#.*)'; | ||
} | ||
|
||
public function getTokenType(): TokenTypeEnum | ||
{ | ||
return TokenTypeEnum::COMMENT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Highlight\Languages\Python\Patterns; | ||
|
||
use Tempest\Highlight\IsPattern; | ||
use Tempest\Highlight\Pattern; | ||
use Tempest\Highlight\PatternTest; | ||
use Tempest\Highlight\Tokens\TokenTypeEnum; | ||
|
||
#[PatternTest(input: '@decorator', output: '@decorator')] | ||
#[PatternTest(input: '@decorator.chained', output: '@decorator.chained')] | ||
final readonly class PyDecoratorPattern implements Pattern | ||
{ | ||
use IsPattern; | ||
|
||
public function getPattern(): string | ||
{ | ||
return '(^|\n)\s*(?<match>@\s*\w*(?:\.\w+)*)'; | ||
} | ||
|
||
public function getTokenType(): TokenTypeEnum | ||
{ | ||
return TokenTypeEnum::PROPERTY; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Highlight\Languages\Python\Patterns; | ||
|
||
use Tempest\Highlight\IsPattern; | ||
use Tempest\Highlight\Pattern; | ||
use Tempest\Highlight\PatternTest; | ||
use Tempest\Highlight\Tokens\TokenTypeEnum; | ||
|
||
#[PatternTest(input: 'def fibonacci(n)', output: 'fibonacci')] | ||
final readonly class PyFunctionPattern implements Pattern | ||
{ | ||
use IsPattern; | ||
|
||
public function getPattern(): string | ||
{ | ||
return '\bdef\s+(?<match>\w*)(?=\s*\()'; | ||
} | ||
|
||
public function getTokenType(): TokenTypeEnum | ||
{ | ||
return TokenTypeEnum::PROPERTY; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Highlight\Languages\Python\Patterns; | ||
|
||
use Tempest\Highlight\IsPattern; | ||
use Tempest\Highlight\Pattern; | ||
use Tempest\Highlight\Tokens\TokenTypeEnum; | ||
|
||
final readonly class PyKeywordPattern implements Pattern | ||
{ | ||
use IsPattern; | ||
|
||
public function __construct(private array $keywords = [ | ||
'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', | ||
'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', | ||
'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', | ||
'raise', 'return', 'try', 'while', 'with', 'yield', | ||
]) {} | ||
|
||
public function getPattern(): string | ||
{ | ||
$keywords = implode('|', $this->keywords); | ||
|
||
return "\b(?<match>(?:_(?=\s*:){$keywords}))\b"; | ||
} | ||
|
||
public function getTokenType(): TokenTypeEnum | ||
{ | ||
return TokenTypeEnum::KEYWORD; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Highlight\Languages\Python\Patterns; | ||
|
||
use Tempest\Highlight\IsPattern; | ||
use Tempest\Highlight\Pattern; | ||
use Tempest\Highlight\Tokens\TokenTypeEnum; | ||
|
||
final readonly class PyOperatorPattern implements Pattern | ||
{ | ||
use IsPattern; | ||
|
||
public function getPattern(): string | ||
{ | ||
return "(?<match>([-+&%=]=?|!=|:=|>>=|<<=|\|=|\^=|\*\*?=?|\/\/?=?|<[<=]?|>[=>]?|[\|^~]))"; | ||
} | ||
|
||
public function getTokenType(): TokenTypeEnum | ||
{ | ||
return TokenTypeEnum::OPERATOR; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Languages/Python/Patterns/PyTripleDoubleQuoteStringPattern.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Highlight\Languages\Python\Patterns; | ||
|
||
use Tempest\Highlight\IsPattern; | ||
use Tempest\Highlight\Pattern; | ||
use Tempest\Highlight\Tokens\TokenTypeEnum; | ||
|
||
final readonly class PyTripleDoubleQuoteStringPattern implements Pattern | ||
{ | ||
use IsPattern; | ||
|
||
public function getPattern(): string | ||
{ | ||
return '/(?<match>"""(.|\n)*?""")/m'; | ||
} | ||
|
||
public function getTokenType(): TokenTypeEnum | ||
{ | ||
return TokenTypeEnum::VALUE; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Languages/Python/Patterns/PyTripleSingleQuoteStringPattern.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Highlight\Languages\Python\Patterns; | ||
|
||
use Tempest\Highlight\IsPattern; | ||
use Tempest\Highlight\Pattern; | ||
use Tempest\Highlight\Tokens\TokenTypeEnum; | ||
|
||
final readonly class PyTripleSingleQuoteStringPattern implements Pattern | ||
{ | ||
use IsPattern; | ||
|
||
public function getPattern(): string | ||
{ | ||
return '/(?<match>\'\'\'(.|\n)*?\'\'\')/m'; | ||
} | ||
|
||
public function getTokenType(): TokenTypeEnum | ||
{ | ||
return TokenTypeEnum::VALUE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Highlight\Languages\Python; | ||
|
||
use Tempest\Highlight\Languages\Base\BaseLanguage; | ||
use Tempest\Highlight\Languages\Php\Patterns\DoubleQuoteValuePattern; | ||
use Tempest\Highlight\Languages\Php\Patterns\SingleQuoteValuePattern; | ||
use Tempest\Highlight\Languages\Python\Patterns\PyArgumentPattern; | ||
use Tempest\Highlight\Languages\Python\Patterns\PyBooleanPattern; | ||
use Tempest\Highlight\Languages\Python\Patterns\PyBuiltinPattern; | ||
use Tempest\Highlight\Languages\Python\Patterns\PyClassNamePattern; | ||
use Tempest\Highlight\Languages\Python\Patterns\PyCommentPattern; | ||
use Tempest\Highlight\Languages\Python\Patterns\PyDecoratorPattern; | ||
use Tempest\Highlight\Languages\Python\Patterns\PyFunctionPattern; | ||
use Tempest\Highlight\Languages\Python\Patterns\PyKeywordPattern; | ||
use Tempest\Highlight\Languages\Python\Patterns\PyOperatorPattern; | ||
use Tempest\Highlight\Languages\Python\Patterns\PyTripleDoubleQuoteStringPattern; | ||
use Tempest\Highlight\Languages\Python\Patterns\PyTripleSingleQuoteStringPattern; | ||
|
||
class PythonLanguage extends BaseLanguage | ||
{ | ||
public function getName(): string | ||
{ | ||
return 'python'; | ||
} | ||
|
||
public function getAliases(): array | ||
{ | ||
return ['py']; | ||
} | ||
|
||
public function getInjections(): array | ||
{ | ||
return [ | ||
...parent::getInjections(), | ||
]; | ||
} | ||
|
||
public function getPatterns(): array | ||
{ | ||
return [ | ||
...parent::getPatterns(), | ||
|
||
// KEYWORDS | ||
new PyKeywordPattern(), | ||
|
||
// PROPERTY | ||
new PyClassNamePattern(), | ||
new PyDecoratorPattern(), | ||
new PyFunctionPattern(), | ||
|
||
// TYPES | ||
new PyBooleanPattern(), | ||
new PyBuiltinPattern(), | ||
|
||
// COMMENTS | ||
new PyCommentPattern(), | ||
|
||
// OPERATORS | ||
new PyOperatorPattern(), | ||
|
||
// VALUES | ||
new SingleQuoteValuePattern(), | ||
new DoubleQuoteValuePattern(), | ||
new PyTripleDoubleQuoteStringPattern(), | ||
new PyTripleSingleQuoteStringPattern(), | ||
|
||
// VARIABLES | ||
new PyArgumentPattern(), | ||
]; | ||
} | ||
} |