Skip to content

Commit

Permalink
Merge pull request #52 from assertchris/feature/add-gdscript-support
Browse files Browse the repository at this point in the history
Add GDScript support
  • Loading branch information
brendt authored Mar 22, 2024
2 parents b64c646 + 1f97ea4 commit a95b69f
Show file tree
Hide file tree
Showing 22 changed files with 647 additions and 14 deletions.
18 changes: 10 additions & 8 deletions src/Highlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Tempest\Highlight\Languages\Blade\BladeLanguage;
use Tempest\Highlight\Languages\Css\CssLanguage;
use Tempest\Highlight\Languages\DocComment\DocCommentLanguage;
use Tempest\Highlight\Languages\Gdscript\GdscriptLanguage;
use Tempest\Highlight\Languages\Html\HtmlLanguage;
use Tempest\Highlight\Languages\JavaScript\JavaScriptLanguage;
use Tempest\Highlight\Languages\Json\JsonLanguage;
Expand All @@ -29,17 +30,18 @@ public function __construct(
private readonly Theme $theme = new CssTheme(),
) {
$this
->setLanguage('php', new PhpLanguage())
->setLanguage('xml', new XmlLanguage())
->setLanguage('blade', new BladeLanguage())
->setLanguage('css', new CssLanguage())
->setLanguage('doc', new DocCommentLanguage())
->setLanguage('gdscript', new GdscriptLanguage())
->setLanguage('html', new HtmlLanguage())
->setLanguage('js', new JavaScriptLanguage())
->setLanguage('json', new JsonLanguage())
->setLanguage('php', new PhpLanguage())
->setLanguage('sql', new SqlLanguage())
->setLanguage('blade', new BladeLanguage())
->setLanguage('xml', new XmlLanguage())
->setLanguage('yaml', new YamlLanguage())
->setLanguage('yml', new YamlLanguage())
->setLanguage('json', new JsonLanguage())
->setLanguage('js', new JavaScriptLanguage())
->setLanguage('doc', new DocCommentLanguage())
->setLanguage('css', new CssLanguage());
->setLanguage('yml', new YamlLanguage());
}

public function setLanguage(string $name, Language $language): self
Expand Down
124 changes: 124 additions & 0 deletions src/Languages/Gdscript/GdscriptLanguage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Gdscript;

use Tempest\Highlight\Languages\Base\BaseLanguage;
use Tempest\Highlight\Languages\Gdscript\Patterns\AnnotationPattern;
use Tempest\Highlight\Languages\Gdscript\Patterns\AsTypePattern;
use Tempest\Highlight\Languages\Gdscript\Patterns\ClassNamePattern;
use Tempest\Highlight\Languages\Gdscript\Patterns\DoubleQuoteValuePattern;
use Tempest\Highlight\Languages\Gdscript\Patterns\ExtendsPattern;
use Tempest\Highlight\Languages\Gdscript\Patterns\FunctionCallPattern;
use Tempest\Highlight\Languages\Gdscript\Patterns\FunctionNamePattern;
use Tempest\Highlight\Languages\Gdscript\Patterns\KeywordPattern;
use Tempest\Highlight\Languages\Gdscript\Patterns\OperatorPattern;
use Tempest\Highlight\Languages\Gdscript\Patterns\PropertyAccessPattern;
use Tempest\Highlight\Languages\Gdscript\Patterns\ReturnTypePattern;
use Tempest\Highlight\Languages\Gdscript\Patterns\SinglelineCommentPattern;
use Tempest\Highlight\Languages\Gdscript\Patterns\SingleQuoteValuePattern;
use Tempest\Highlight\Languages\Gdscript\Patterns\VarTypePattern;

class GdscriptLanguage extends BaseLanguage
{
public function getInjections(): array
{
return [
...parent::getInjections(),
];
}

public function getPatterns(): array
{
return [
...parent::getPatterns(),

new OperatorPattern('**='),
new OperatorPattern('<<='),
new OperatorPattern('>>='),
new OperatorPattern('**'),
new OperatorPattern('||'),
new OperatorPattern('&&'),
new OperatorPattern('<='),
new OperatorPattern('>='),
new OperatorPattern('=='),
new OperatorPattern('!='),
new OperatorPattern('<<'),
new OperatorPattern('>>'),
new OperatorPattern('+='),
new OperatorPattern('-='),
new OperatorPattern('*='),
new OperatorPattern('/='),
new OperatorPattern('%='),
new OperatorPattern('&='),
new OperatorPattern('|='),
new OperatorPattern('^='),
new OperatorPattern('->'),
new OperatorPattern('/'),
new OperatorPattern('%'),
new OperatorPattern('+'),
new OperatorPattern('-'),
new OperatorPattern('*'),
new OperatorPattern('/'),
new OperatorPattern('&'),
new OperatorPattern('^'),
new OperatorPattern('|'),
new OperatorPattern('<'),
new OperatorPattern('>'),
new OperatorPattern('!'),

new KeywordPattern('if'),
new KeywordPattern('elif'),
new KeywordPattern('else'),
new KeywordPattern('for'),
new KeywordPattern('while'),
new KeywordPattern('match'),
new KeywordPattern('break'),
new KeywordPattern('continue'),
new KeywordPattern('pass'),
new KeywordPattern('return'),
new KeywordPattern('class'),
new KeywordPattern('class_name'),
new KeywordPattern('extends'),
new KeywordPattern('is'),
new KeywordPattern('not'),
new KeywordPattern('and'),
new KeywordPattern('or'),
new KeywordPattern('in'),
new KeywordPattern('as'),
new KeywordPattern('self'),
new KeywordPattern('signal'),
new KeywordPattern('func'),
new KeywordPattern('static'),
new KeywordPattern('const'),
new KeywordPattern('enum'),
new KeywordPattern('var'),
new KeywordPattern('breakpoint'),
new KeywordPattern('preload'),
new KeywordPattern('await'),
new KeywordPattern('yield'),
new KeywordPattern('assert'),
new KeywordPattern('super'),
new KeywordPattern('PI'),
new KeywordPattern('INF'),
new KeywordPattern('TAU'),
new KeywordPattern('NAN'),

new SinglelineCommentPattern(),

new AnnotationPattern(),
new ExtendsPattern(),
new ClassNamePattern(),
new FunctionNamePattern(),
new FunctionCallPattern(),
new ReturnTypePattern(),
new AsTypePattern(),
new VarTypePattern(),
new PropertyAccessPattern(),

new SingleQuoteValuePattern(),
new DoubleQuoteValuePattern(),
];
}
}
49 changes: 49 additions & 0 deletions src/Languages/Gdscript/Patterns/AnnotationPattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Gdscript\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\Tokens\TokenType;

final readonly class AnnotationPattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
$variations = join('|@export_', [
'category',
'color_no_alpha',
'dir',
'enum',
'exp_easing',
'file',
'flags',
'flags_2d_navigation',
'flags_2d_physics',
'flags_2d_render',
'flags_3d_navigation',
'flags_3d_physics',
'flags_3d_render',
'flags_avoidance',
'global_dir',
'global_file',
'group',
'multiline',
'node_path',
'placeholder',
'range',
'subgroup',
]);

return "(?<match>(@onready|@icon|@export|@export_{$variations}|@rpc|@static_unload|@tool|@warning_ignore))";
}

public function getTokenType(): TokenType
{
return TokenType::KEYWORD;
}
}
26 changes: 26 additions & 0 deletions src/Languages/Gdscript/Patterns/AsTypePattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Gdscript\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\PatternTest;
use Tempest\Highlight\Tokens\TokenType;

#[PatternTest(input: 'as int', output: 'int')]
final readonly class AsTypePattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return 'as\s+\b(?<match>[a-zA-Z][a-zA-Z0-9]+)\b';
}

public function getTokenType(): TokenType
{
return TokenType::TYPE;
}
}
26 changes: 26 additions & 0 deletions src/Languages/Gdscript/Patterns/ClassNamePattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Gdscript\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\PatternTest;
use Tempest\Highlight\Tokens\TokenType;

#[PatternTest(input: 'class_name Foo', output: 'Foo')]
final readonly class ClassNamePattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return 'class_name\s(?<match>[\w]+)';
}

public function getTokenType(): TokenType
{
return TokenType::TYPE;
}
}
29 changes: 29 additions & 0 deletions src/Languages/Gdscript/Patterns/DoubleQuoteValuePattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Gdscript\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\PatternTest;
use Tempest\Highlight\Tokens\TokenType;

#[PatternTest(
input: 'return "hello"',
output: 'hello',
)]
final readonly class DoubleQuoteValuePattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '"(?<match>.*?)"';
}

public function getTokenType(): TokenType
{
return TokenType::VALUE;
}
}
26 changes: 26 additions & 0 deletions src/Languages/Gdscript/Patterns/ExtendsPattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Gdscript\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\PatternTest;
use Tempest\Highlight\Tokens\TokenType;

#[PatternTest(input: 'extends Foo', output: 'Foo')]
final readonly class ExtendsPattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return 'extends\s(?<match>[\w]+)';
}

public function getTokenType(): TokenType
{
return TokenType::TYPE;
}
}
27 changes: 27 additions & 0 deletions src/Languages/Gdscript/Patterns/FunctionCallPattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Gdscript\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\PatternTest;
use Tempest\Highlight\Tokens\TokenType;

#[PatternTest(input: 'foo()', output: 'foo')]
#[PatternTest(input: '.foo()', output: 'foo')]
final readonly class FunctionCallPattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '\b(?<match>[a-z][\w]+)\(';
}

public function getTokenType(): TokenType
{
return TokenType::PROPERTY;
}
}
26 changes: 26 additions & 0 deletions src/Languages/Gdscript/Patterns/FunctionNamePattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Gdscript\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\PatternTest;
use Tempest\Highlight\Tokens\TokenType;

#[PatternTest(input: 'func foo()', output: 'foo')]
final readonly class FunctionNamePattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return 'func (?<match>[\w]+)';
}

public function getTokenType(): TokenType
{
return TokenType::PROPERTY;
}
}
Loading

0 comments on commit a95b69f

Please sign in to comment.