Browse Source

Cleaning up a bit

main
Dustin Wilson 3 years ago
parent
commit
5ae2d256d3
  1. 2
      lib/Data.php
  2. 16
      lib/Grammar.php
  3. 42
      lib/Grammar/Registry.php
  4. 2
      lib/Highlighter.php
  5. 2
      lib/Scope/Data.php
  6. 4
      lib/Scope/Exception.php
  7. 2
      lib/Scope/Matcher.php
  8. 2
      lib/Scope/Matchers/AndMatcher.php
  9. 2
      lib/Scope/Matchers/GroupMatcher.php
  10. 2
      lib/Scope/Matchers/NegateMatcher.php
  11. 2
      lib/Scope/Matchers/OrMatcher.php
  12. 2
      lib/Scope/Matchers/PathMatcher.php
  13. 2
      lib/Scope/Matchers/ScopeMatcher.php
  14. 2
      lib/Scope/Parser.php
  15. 2
      lib/Tokenizer.php

2
lib/Data.php

@ -1,7 +1,7 @@
<?php
/** @license MIT
* Copyright 2021 Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
* See LICENSE file for details */
declare(strict_types=1);
namespace dW\Lit;

16
lib/Grammar.php

@ -1,16 +0,0 @@
<?php
/** @license MIT
* Copyright 2021 Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace dW\Lit;
interface Grammar {
public static array $fileTypes;
public static string $firstLineMatch;
public static string $name;
public static GrammarPattern $patterns;
public static GrammarRepository $repository;
public static string $scopeName;
}

42
lib/Grammar/Registry.php

@ -1,7 +1,7 @@
<?php
/** @license MIT
* Copyright 2021 Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
* See LICENSE file for details */
declare(strict_types=1);
namespace dW\Lit\Grammar;
@ -34,25 +34,43 @@ class Registry {
return false;
}
public static function import(string $jsonPath, bool $force = false): bool {
if (!file_exists($jsonPath)) {
throw new \Exception("Path \"$jsonPath\" either does not exist or you do not have permission to view the file.");
}
public static function import(string $jsonPath) {}
$grammar = json_decode(file_get_contents($jsonPath), true);
public static function validate(string $grammar): bool {
if ($grammar === null) {
throw new \Exception("\"$jsonPath\" is not a valid grammar JSON file.");
throw new \Exception("\"$jsonPath\" is not a valid grammar JSON file.".\PHP_EOL);
}
if (!isset($grammar['scopeName'])) {
throw new \Exception("\"$jsonPath\" is missing the required scopeName property.");
$requiredProperties = [
'name',
'patterns',
'scopeName'
];
$missing = [];
foreach ($requiredProperties as $r) {
if (!array_key_exists($r, $grammar))) {
$missing = $r;
}
}
if (!$force && isset(self::$grammars[$grammar['scopeName']])) {
throw new \Exception("Grammar with the \"{$grammar['scopeName']}\" scope already exists.");
$missingLen = count($missing);
if ($missingLen > 0) {
if ($missingLen > 1) {
if ($missingLen > 2) {
$last = array_pop($missing);
$missing = implode(', ', $missing);
$missing .= ", and $last";
} else {
$missing = implode(' and ', $missing);
}
throw new \Exception("\"$jsonPath\" is missing the required $missing properties.".\PHP_EOL);
}
throw new \Exception("\"$jsonPath\" is missing the required {$missing[0]} property.".\PHP_EOL);
}
self::$grammars[$grammar['scopeName']] = $grammar;
return true;
}
}

2
lib/Highlighter.php

@ -1,7 +1,7 @@
<?php
/** @license MIT
* Copyright 2021 Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
* See LICENSE file for details */
declare(strict_types=1);
namespace dW\Lit;

2
lib/Scope/Data.php

@ -1,7 +1,7 @@
<?php
/** @license MIT
* Copyright 2021 Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
* See LICENSE file for details */
declare(strict_types=1);
namespace dW\Lit\Scope;

4
lib/Scope/Exception.php

@ -1,13 +1,13 @@
<?php
/** @license MIT
* Copyright 2021 Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
* See LICENSE file for details */
declare(strict_types=1);
namespace dW\Lit\Scope;
class Exception extends \Exception {
const MESSAGE = "%s expected; found %s at offset %s\n";
const MESSAGE = '%s expected; found %s at offset %s'.\PHP_EOL;
public function __construct(array|string $expected, string|bool $found, int $offset) {
if (!is_string($expected)) {

2
lib/Scope/Matcher.php

@ -1,7 +1,7 @@
<?php
/** @license MIT
* Copyright 2021 Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
* See LICENSE file for details */
declare(strict_types=1);
namespace dW\Lit\Scope;

2
lib/Scope/Matchers/AndMatcher.php

@ -1,7 +1,7 @@
<?php
/** @license MIT
* Copyright 2021 Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
* See LICENSE file for details */
declare(strict_types=1);
namespace dW\Lit\Scope;

2
lib/Scope/Matchers/GroupMatcher.php

@ -1,7 +1,7 @@
<?php
/** @license MIT
* Copyright 2021 Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
* See LICENSE file for details */
declare(strict_types=1);
namespace dW\Lit\Scope;

2
lib/Scope/Matchers/NegateMatcher.php

@ -1,7 +1,7 @@
<?php
/** @license MIT
* Copyright 2021 Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
* See LICENSE file for details */
declare(strict_types=1);
namespace dW\Lit\Scope;

2
lib/Scope/Matchers/OrMatcher.php

@ -1,7 +1,7 @@
<?php
/** @license MIT
* Copyright 2021 Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
* See LICENSE file for details */
declare(strict_types=1);
namespace dW\Lit\Scope;

2
lib/Scope/Matchers/PathMatcher.php

@ -1,7 +1,7 @@
<?php
/** @license MIT
* Copyright 2021 Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
* See LICENSE file for details */
declare(strict_types=1);
namespace dW\Lit\Scope;

2
lib/Scope/Matchers/ScopeMatcher.php

@ -1,7 +1,7 @@
<?php
/** @license MIT
* Copyright 2021 Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
* See LICENSE file for details */
declare(strict_types=1);
namespace dW\Lit\Scope;

2
lib/Scope/Parser.php

@ -1,7 +1,7 @@
<?php
/** @license MIT
* Copyright 2021 Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
* See LICENSE file for details */
declare(strict_types=1);
namespace dW\Lit\Scope;

2
lib/Tokenizer.php

@ -1,7 +1,7 @@
<?php
/** @license MIT
* Copyright 2021 Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
* See LICENSE file for details */
declare(strict_types=1);
namespace dW\Lit;

Loading…
Cancel
Save