Skip to content

Commit

Permalink
Add python #123 support
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdancondorachi committed May 23, 2024
1 parent 116da0e commit dbde1c2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Languages/Python/Patterns/PyBooleanPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public function getPattern(): string

public function getTokenType(): TokenTypeEnum
{
return TokenTypeEnum::TYPE;
return TokenTypeEnum::BOOLEAN;
}
}
24 changes: 24 additions & 0 deletions src/Languages/Python/Patterns/PyNumberPattern.php
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 PyNumberPattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '(?<match>\b0(?:[bB](?:_?[01])+|[oO](?:_?[0-7])+|[xX](?:_?[a-fA-F0-9])+)\b|(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+(?:_\d+)*)(?:[eE][+-]?\d+(?:_\d+)*)?j?(?!\w))';
}

public function getTokenType(): TokenTypeEnum
{
return TokenTypeEnum::NUMBER;
}
}
8 changes: 7 additions & 1 deletion src/Languages/Python/PythonLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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\PyNumberPattern;
use Tempest\Highlight\Languages\Python\Patterns\PyOperatorPattern;
use Tempest\Highlight\Languages\Python\Patterns\PyTripleDoubleQuoteStringPattern;
use Tempest\Highlight\Languages\Python\Patterns\PyTripleSingleQuoteStringPattern;
Expand Down Expand Up @@ -52,12 +53,17 @@ public function getPatterns(): array
new PyFunctionPattern(),

// TYPES
new PyBooleanPattern(),
new PyBuiltinPattern(),

// COMMENTS
new PyCommentPattern(),

// NUMBERS
new PyNumberPattern(),

// BOOLEANS
new PyBooleanPattern(),

// OPERATORS
new PyOperatorPattern(),

Expand Down
4 changes: 2 additions & 2 deletions tests/Languages/Python/PythonLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public static function data(): array
<<<TXT
<span class="hl-keyword">def</span> <span class="hl-property">fib</span>(n): <span class="hl-comment"># write Fibonacci series up to n</span>
<span class="hl-value">&quot;&quot;&quot;Print a Fibonacci series up to n.&quot;&quot;&quot;</span>
a, b <span class="hl-operator">=</span> 0, 1
a, b <span class="hl-operator">=</span> <span class="hl-number">0</span>, <span class="hl-number">1</span>
<span class="hl-keyword">while</span> a <span class="hl-operator">&lt;</span> n:
<span class="hl-type">print</span>(a, <span class="hl-variable">end</span><span class="hl-operator">=</span>'<span class="hl-value"> </span>')
a, b <span class="hl-operator">=</span> b, a<span class="hl-operator">+</span>b
<span class="hl-type">print</span>()
<span class="hl-comment"># Now call the function we just defined:</span>
fib(2000)
fib(<span class="hl-number">2000</span>)
TXT],
];
}
Expand Down

0 comments on commit dbde1c2

Please sign in to comment.