From e2cc9adbd89b3fa33a47e7edeec422d77cbf1980 Mon Sep 17 00:00:00 2001 From: Dustin Wilson Date: Wed, 30 Jun 2021 16:48:27 -0500 Subject: [PATCH] Working on Grammars --- lib/Data.php | 7 ++- lib/FauxReadOnly.php | 18 ++++++ lib/Grammar.php | 92 ++++++++++++++++++++++++++++ lib/Grammar/ImmutableList.php | 37 +++++++++++ lib/Grammar/Include.php | 32 ++++++++++ lib/Grammar/InjectionList.php | 9 +++ lib/Grammar/NamedPatternListList.php | 18 ++++++ lib/Grammar/Pattern.php | 38 ++++++++++++ lib/Grammar/PatternList.php | 13 ++++ lib/Grammar/Registry.php | 4 +- lib/Grammar/Repository.php | 9 +++ lib/Tokenizer.php | 14 ++++- 12 files changed, 284 insertions(+), 7 deletions(-) create mode 100644 lib/FauxReadOnly.php create mode 100644 lib/Grammar.php create mode 100644 lib/Grammar/ImmutableList.php create mode 100644 lib/Grammar/Include.php create mode 100644 lib/Grammar/InjectionList.php create mode 100644 lib/Grammar/NamedPatternListList.php create mode 100644 lib/Grammar/Pattern.php create mode 100644 lib/Grammar/PatternList.php create mode 100644 lib/Grammar/Repository.php diff --git a/lib/Data.php b/lib/Data.php index 6250bb9..9121209 100644 --- a/lib/Data.php +++ b/lib/Data.php @@ -8,10 +8,11 @@ namespace dW\Lit; class Data { public static function fileToGenerator(string $filepath): \Generator { + $lineNumber = 0; $fp = fopen($filepath, 'r'); try { while ($line = fgets($fp)) { - yield $line; + yield ++$lineNumber => $line; } } finally { fclose($fp); @@ -20,8 +21,8 @@ class Data { public static function stringToGenerator(string $string): \Generator { $string = explode("\n", $string); - foreach ($string as $s) { - yield $s; + foreach ($string as $lineNumber => $line) { + yield $lineNumber + 1 => $line; } } } \ No newline at end of file diff --git a/lib/FauxReadOnly.php b/lib/FauxReadOnly.php new file mode 100644 index 0000000..8cd977e --- /dev/null +++ b/lib/FauxReadOnly.php @@ -0,0 +1,18 @@ +$name; + } +} \ No newline at end of file diff --git a/lib/Grammar.php b/lib/Grammar.php new file mode 100644 index 0000000..ba09ff7 --- /dev/null +++ b/lib/Grammar.php @@ -0,0 +1,92 @@ +_name = $name; + $this->_scopeName = $scopeName; + $this->_patterns = $patterns; + $this->_contentRegex = $contentRegex; + $this->_firstLineMatch = $firstLineMatch; + $this->_injections = $injections; + $this->_repository = $repository; + } + + + public static function fromJSON(string $jsonPath): self { + assert(is_file($jsonPath), new \Exception("\"$jsonPath\" is either not a file or you do not have permission to read the file\n")); + + $json = json_decode($jsonPath, true); + assert($json, new \Exception("\"$jsonPath\" is not a valid JSON file.\n")); + + assert(isset($json['name']), new \Exception("\"$jsonPath\" does not have the required name property")); + assert(isset($json['scopeName']), new \Exception("\"$jsonPath\" does not have the required scopeName property")); + assert(isset($json['patterns']), new \Exception("\"$jsonPath\" does not have the required patterns property")); + + $name = $json['name']; + $scopeName = $json['scopeName']; + $contentRegex = (isset($json['contentRegex'])) ? "/{$json['contentRegex']}/" : null; + $firstLineMatch = (isset($json['firstLineMatch'])) ? "/{$json['firstLineMatch']}/" : null; + + $patterns = []; + foreach ($json['patterns'] as $pattern) { + foreach ($pattern as $key => $p) { + + } + } + + if (count($patterns) > 0) { + $patterns = new PatternList(...$patterns); + } else { + $patterns = null; + } + + if (isset($json['injections'])) { + $injections = []; + foreach ($json['injections'] as $injection) { + + } + + if (count($injections) > 0) { + $injections = new InjectionList($injections); + } else { + $patterns = null; + } + } + + + if (isset($json['repository'])) { + $respository = []; + foreach ($json['repository'] as $r) { + + } + + if (count($repository) > 0) { + $repository = new InjectionList($repository); + } else { + $repository = null; + } + } + + return new self($name, $scopeName, $patterns, $contentRegex, $firstLineMatch, $injections, $repository); + } +} \ No newline at end of file diff --git a/lib/Grammar/ImmutableList.php b/lib/Grammar/ImmutableList.php new file mode 100644 index 0000000..6801ac4 --- /dev/null +++ b/lib/Grammar/ImmutableList.php @@ -0,0 +1,37 @@ +storage = $values; + } + + public function offsetSet($offset, $value) { + throw new \Exception(__CLASS__ . "s are immutable\n"); + } + + public function offsetExists($offset) { + return isset($this->storage[$offset]); + } + + public function offsetUnset($offset) { + throw new \Exception(__CLASS__ . "s are immutable\n"); + } + + public function offsetGet($offset) { + assert(isset($this->storage[$offset]), new \Exception("Invalid ImmutableList index at $offset\n")); + return $this->storage[$offset]; + } + + public function count(): int { + return $this->count; + } +} diff --git a/lib/Grammar/Include.php b/lib/Grammar/Include.php new file mode 100644 index 0000000..4eb07ef --- /dev/null +++ b/lib/Grammar/Include.php @@ -0,0 +1,32 @@ +type = self::REPOSITORY_TYPE; + $this->name = substr($string, 1); + } elseif ($string === '$self') { + $this->type = self::SELF_TYPE; + } + + $this->type = self::SCOPE_TYPE; + $this->name = $string; + } +} \ No newline at end of file diff --git a/lib/Grammar/InjectionList.php b/lib/Grammar/InjectionList.php new file mode 100644 index 0000000..60ffb72 --- /dev/null +++ b/lib/Grammar/InjectionList.php @@ -0,0 +1,9 @@ + $v) { + assert(is_string($k), new \Exception('String index expected for supplied array, found ' . gettype($k) . "\n")); + assert($v instanceof PatternList, new \Exception(__NAMESPACE__ . '\PatternList value expected for supplied array, found ' . gettype($v) . "\n")); + } + + $this->storage = $array; + } +} \ No newline at end of file diff --git a/lib/Grammar/Pattern.php b/lib/Grammar/Pattern.php new file mode 100644 index 0000000..7e3aacc --- /dev/null +++ b/lib/Grammar/Pattern.php @@ -0,0 +1,38 @@ +_name = $name; + $this->_contentName = $contentName; + $this->_begin = $begin; + $this->_end = $end; + $this->_match = $match; + + $this->_patterns = $patterns; + $this->_captures = $captures; + $this->_beginCaptures = $beginCaptures; + $this->_endCaptures = $endCaptures; + } +} \ No newline at end of file diff --git a/lib/Grammar/PatternList.php b/lib/Grammar/PatternList.php new file mode 100644 index 0000000..4561595 --- /dev/null +++ b/lib/Grammar/PatternList.php @@ -0,0 +1,13 @@ +data = $data; + } + + public function tokenize(): \Generator { + foreach ($this->data as $lineNumber => $line) { + + } + } +} \ No newline at end of file