From e7386950570ac539e2199a556af90f6dd8bb0984 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Fri, 19 Feb 2021 11:18:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=9F=E5=A7=8B=E8=BE=93=E5=87=BA=E7=B1=BB?= =?UTF-8?q?=E5=8F=8A=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Output/CnTextOutput.php | 21 --------------------- src/Output/OutputInterface.php | 8 ++++++++ src/Output/RawOutput.php | 29 +++++++++++++++++++++++++++++ tests/Output/RawOutputTest.php | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 21 deletions(-) delete mode 100644 src/Output/CnTextOutput.php create mode 100644 src/Output/RawOutput.php create mode 100644 tests/Output/RawOutputTest.php diff --git a/src/Output/CnTextOutput.php b/src/Output/CnTextOutput.php deleted file mode 100644 index 7eab8eb..0000000 --- a/src/Output/CnTextOutput.php +++ /dev/null @@ -1,21 +0,0 @@ - - * @date 2020/12/17 15:33 - */ - - -namespace JerryYan\DSL\Output; - - -/** - * 又以中文形式重新输出一遍 - * @package JerryYan\DSL\Output - * @author Jerry Yan <792602257@qq.com> - * @date 2020/12/17 16:12 - */ -class CnTextOutput extends OutputInterface -{ - -} \ No newline at end of file diff --git a/src/Output/OutputInterface.php b/src/Output/OutputInterface.php index 7eeab86..5e3bf77 100644 --- a/src/Output/OutputInterface.php +++ b/src/Output/OutputInterface.php @@ -9,7 +9,15 @@ namespace JerryYan\DSL\Output; +use JerryYan\DSL\Token\TokenInterface; + abstract class OutputInterface { + protected $tokenChain; + public function __construct(TokenInterface $tokenChain) + { + $this->tokenChain = $tokenChain->getFirstToken(); + } + abstract public function output(); } \ No newline at end of file diff --git a/src/Output/RawOutput.php b/src/Output/RawOutput.php new file mode 100644 index 0000000..ca00a5c --- /dev/null +++ b/src/Output/RawOutput.php @@ -0,0 +1,29 @@ + + * @date 2020/12/17 15:33 + */ + + +namespace JerryYan\DSL\Output; + + +/** + * 又以中文形式重新输出一遍 + * @package JerryYan\DSL\Output + * @author Jerry Yan <792602257@qq.com> + * @date 2020/12/17 16:12 + */ +class RawOutput extends OutputInterface +{ + public function output(): string + { + $output = []; + $token = $this->tokenChain; + do { + array_push($output, $token->getRawString()); + } while ($token = $token->getNextToken()); + return implode(' ', $output); + } +} \ No newline at end of file diff --git a/tests/Output/RawOutputTest.php b/tests/Output/RawOutputTest.php new file mode 100644 index 0000000..3d33293 --- /dev/null +++ b/tests/Output/RawOutputTest.php @@ -0,0 +1,33 @@ + + * @date 2021/2/19 11:10 + */ + +namespace JerryYan\DSL\Test\Output; + +use JerryYan\DSL\Grammar\DefaultGrammar; +use JerryYan\DSL\Output\RawOutput; +use JerryYan\DSL\Reader\StringReader; +use JerryYan\DSL\Tokenizer\Tokenizer; +use PHPUnit\Framework\TestCase; + +class RawOutputTest extends TestCase +{ + private $text = "当 另外那个 与 另外一个 不相等时 或者 那个 和 这个 等于 -0.5 的时候"; + private $output; + + protected function setUp(): void + { + $tokenizer = new Tokenizer(new DefaultGrammar()); + $reader = new StringReader($this->text); + $token = $tokenizer->tokenize($reader); + $this->output = new RawOutput($token); + } + + public function testOutput() + { + $this->assertEquals($this->text, $this->output->output(), '输出与预期不一致'); + } +}