Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
184 changes: 184 additions & 0 deletions docs/language-spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
{
"name": "php-script",
"version": "1.0.0",
"keywords": [
"if",
"else",
"for",
"foreach",
"as",
"echo",
"return",
"true",
"false",
"null",
"LINEBREAK"
],
"operators": [
"==",
"===",
"!=",
"!==",
"=",
".",
"+",
"-",
"*",
"/",
">",
"<"
],
"symbols": "[=><!~?&|+\\-*\\/^%.,;()\\{}\\[\\]]",
"tokenizer": {
"root": [
[
"[a-zA-Z_]\\\\w*",
{
"cases": {
"@keywords": "keyword",
"@allowedFunctions": "predefined",
"@contextVariables": "variable.predefined",
"@default": "identifier"
}
}
],
[
"include",
"@whitespace"
],
[
"[{}()\\[\\]]",
"@brackets"
],
[
"[<>](?!@symbols)",
"@brackets"
],
[
"@symbols",
{
"cases": {
"@operators": "operator",
"@default": ""
}
}
],
[
"\\\\d+(\\\\.\\\\d+)?",
"number"
],
[
"\\"",
"string.quote",
"@string_double"
],
[
"'",
"string.quote",
"@string_single"
]
],
"whitespace": [
[
"[ \\t\\r\\n]+",
""
],
[
"\\\\/\\\\/.*$",
"comment"
]
],
"string_double": [
[
"[^\\\\\\\\"]+",
"string"
],
[
"\\\\\\\\.",
"string.escape"
],
[
"\\"",
"string.quote",
"@pop"
]
],
"string_single": [
[
"[^\\\\\\\\']+",
"string"
],
[
"\\\\\\\\.",
"string.escape"
],
[
"'",
"string.quote",
"@pop"
]
]
},
"completionItems": {
"globalFunctions": [],
"globalVariables": [],
"classes": [],
"controlFlows": [
{
"label": "for",
"kind": "Snippet",
"snippet": "for (${1:i} = ${2:0}; ${1} < ${3:count}(${4}); ${1}++) {\\n $0\\n}",
"doc": "For-Loop",
"detail": "for (int $i = 0; $i < count($items); $i++) {"
},
{
"label": "foreach",
"kind": "Snippet",
"snippet": "foreach (${1:items} as ${2:item}) {\\n $0\\n}",
"doc": "Foreach-Loop",
"detail": "foreach ($items as $item) {"
},
{
"label": "foreach (key, value)",
"kind": "Snippet",
"snippet": "foreach (${1:map} as ${2:key}, ${3:value}) {\\n $0\\n}",
"doc": "Foreach-Loop for Key/Value Pairs",
"detail": "foreach ($map as $key => $value) {"
},
{
"label": "if",
"kind": "Snippet",
"snippet": "if (${1:condition}) {\\n $0\\n}",
"doc": "If-Condition",
"detail": "if ($condition) {"
},
{
"label": "ifelse",
"kind": "Snippet",
"snippet": "if (${1:condition}) {\\n $2\\n} else {\\n $0\\n}",
"doc": "If-Else-Condition",
"detail": "if ($condition) { ... } else { ... }"
}
],
"loopControls": [
{
"label": "break 2",
"kind": "Snippet",
"snippet": "break ${1:2};",
"doc": "Break from nested loops",
"detail": "break N;"
},
{
"label": "continue 2",
"kind": "Snippet",
"snippet": "continue ${1:2};",
"doc": "Continue outer loop from nested loops",
"detail": "continue N;"
}
],
"loopKeywords": [
"break",
"continue"
]
}
}
32 changes: 32 additions & 0 deletions src/Monarch/LanguageSpecificationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace PhpScript\Monarch;

final class LanguageSpecificationService
{
public function __construct(
private readonly MonarchLanguageDefinitionService $monarchLanguageDefinitionService
) {
}

/**
* @return array<string, mixed>
*/
public function getSpecification(): array
{
$definition = $this->monarchLanguageDefinitionService->getDefinition();
$completionItems = $this->monarchLanguageDefinitionService->getCompletionItems();

return [
'name' => 'php-script',
'version' => '1.0.0',
'keywords' => $definition['keywords'],
'operators' => $definition['operators'],
'symbols' => $definition['symbols'],
'tokenizer' => $definition['tokenizer'],
'completionItems' => $completionItems,
];
}
}
19 changes: 19 additions & 0 deletions tools/generate-language-spec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

use PhpScript\Monarch\LanguageSpecificationService;
use PhpScript\Monarch\MonarchLanguageDefinitionService;

require __DIR__.'/../vendor/autoload.php';

$monarchLanguageDefinitionService = new MonarchLanguageDefinitionService();
$languageSpecificationService = new LanguageSpecificationService($monarchLanguageDefinitionService);

$specification = $languageSpecificationService->getSpecification();

$json = json_encode($specification, JSON_PRETTY_PRINT);

file_put_contents(__DIR__.'/../docs/language-spec.json', $json);

echo "Language specification generated successfully.\n";
Loading