-
-
Notifications
You must be signed in to change notification settings - Fork 40
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
Add python support #121
Merged
Merged
Add python support #121
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,40 @@ | ||
<?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,35 @@ | ||
<?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', | ||
]) | ||
{ | ||
} | ||
Comment on lines
+15
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I chose to add the keywords as an array directly within the class constructor for simplicity. For me this offers more efficiency and clarity, especially considering that the list of keywords is unlikely to change frequently. |
||
|
||
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; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same goes for the built-in functions, adding all of this individually inside the PythonLanguage just gave me anxiety 😂