From 7a8d6d5f1fb7c5c61c8fbe158d13a9817593bcb7 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Wed, 7 Jun 2017 22:23:44 -0500 Subject: [PATCH] Remove Parser Moved to a separate repository at amphp/parser. --- docs/index.md | 4 -- docs/parser.md | 5 -- examples/simple-parser.php | 42 ------------- lib/Parser.php | 119 ------------------------------------- test/ParserTest.php | 40 ------------- 5 files changed, 210 deletions(-) delete mode 100644 docs/parser.md delete mode 100644 examples/simple-parser.php delete mode 100644 lib/Parser.php delete mode 100644 test/ParserTest.php diff --git a/docs/index.md b/docs/index.md index 48aa8d5..b0bd3ab 100644 --- a/docs/index.md +++ b/docs/index.md @@ -75,7 +75,3 @@ This package offers some basic implementations, other libraries might provide ev * [`ResourceOutputStream`](./resource-streams.md) * [`ZlibOutputStream`](./compression-streams.md) - -## Parser - - * [`Parser`](./parser.md) diff --git a/docs/parser.md b/docs/parser.md deleted file mode 100644 index 4873393..0000000 --- a/docs/parser.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -title: Parser -permalink: /parser ---- -TBD. diff --git a/examples/simple-parser.php b/examples/simple-parser.php deleted file mode 100644 index 81ef6cb..0000000 --- a/examples/simple-parser.php +++ /dev/null @@ -1,42 +0,0 @@ -write("This\nis\n"); - - Loop::delay(1000, function () use ($parser) { - $parser->write("an\nexample\nof\n"); - }); - - Loop::delay(2000, function () use ($parser) { - $parser->write("a\nsimple\n"); - }); - - Loop::delay(3000, function () use ($parser) { - $parser->write("incremental\nstream\nparser\n"); - }); - - Loop::delay(4000, function () use ($parser) { - $parser->end(); // Marks the end of data. - }); -}); diff --git a/lib/Parser.php b/lib/Parser.php deleted file mode 100644 index d7ba85c..0000000 --- a/lib/Parser.php +++ /dev/null @@ -1,119 +0,0 @@ -generator = $generator; - - $this->delimiter = $this->generator->current(); - - if (!$this->generator->valid()) { - $this->generator = null; - return; - } - - if ($this->delimiter !== null - && (!\is_int($this->delimiter) || $this->delimiter <= 0) - && (!\is_string($this->delimiter) || !\strlen($this->delimiter)) - ) { - throw new InvalidYieldError( - $generator, - \sprintf( - "Unexpected yield; Expected NULL, an int greater than 0, or a non-empty string; %s given", - \is_object($this->delimiter) ? \sprintf("instance of %s", \get_class($this->delimiter)) : \gettype($this->delimiter) - ) - ); - } - } - - /** - * Cancels the generator parser and returns any remaining data in the internal buffer. Writing data after calling - * this method will result in an error. - * - * @return string - */ - final public function cancel(): string { - $this->generator = null; - return $this->buffer; - } - - final public function push(string $data) { - if ($this->generator === null) { - throw new StreamException("The parser is no longer writable"); - } - - $this->buffer .= $data; - $end = false; - - try { - while ($this->buffer !== "") { - if (\is_int($this->delimiter)) { - if (\strlen($this->buffer) < $this->delimiter) { - break; // Too few bytes in buffer. - } - - $send = \substr($this->buffer, 0, $this->delimiter); - $this->buffer = \substr($this->buffer, $this->delimiter); - } elseif (\is_string($this->delimiter)) { - if (($position = \strpos($this->buffer, $this->delimiter)) === false) { - break; - } - - $send = \substr($this->buffer, 0, $position); - $this->buffer = \substr($this->buffer, $position + \strlen($this->delimiter)); - } else { - $send = $this->buffer; - $this->buffer = ""; - } - - try { - $this->delimiter = $this->generator->send($send); - } catch (\Exception $exception) { // Wrap Exception instances into a StreamException. - throw new StreamException("The generator parser threw an exception", 0, $exception); - } - - if (!$this->generator->valid()) { - $end = true; - break; - } - - if ($this->delimiter !== null - && (!\is_int($this->delimiter) || $this->delimiter <= 0) - && (!\is_string($this->delimiter) || !\strlen($this->delimiter)) - ) { - throw new InvalidYieldError( - $this->generator, - \sprintf( - "Unexpected yield; Expected NULL, an int greater than 0, or a non-empty string; %s given", - \is_object($this->delimiter) ? \sprintf("instance of %s", \get_class($this->delimiter)) : \gettype($this->delimiter) - ) - ); - } - } - } catch (\Throwable $exception) { - $end = true; - throw $exception; - } finally { - if ($end) { - $this->generator = null; - } - } - } -} diff --git a/test/ParserTest.php b/test/ParserTest.php deleted file mode 100644 index 20b3484..0000000 --- a/test/ParserTest.php +++ /dev/null @@ -1,40 +0,0 @@ -push("foobarfoo\r\n"); - - $this->assertSame("foobar", $value); - } - - public function testStringDelimiter() { - $parser = new Parser((function () use (&$value1, &$value2) { - $value1 = yield "bar"; - $value2 = yield "\r\n"; - })()); - - $parser->push("foobarbaz\r\n"); - - $this->assertSame("foo", $value1); - $this->assertSame("baz", $value2); - } - - public function testUndelimited() { - $parser = new Parser((function () use (&$value) { - $value = yield; - })()); - - $parser->push("foobarbaz\r\n"); - - $this->assertSame("foobarbaz\r\n", $value); - } -}