From 6f7a5574b474bd48742f93484ecdc2f84dfe67aa Mon Sep 17 00:00:00 2001 From: Ciaran McNulty Date: Fri, 5 Feb 2021 21:55:19 +0000 Subject: [PATCH 1/4] Cast table node exceptions into ParserExceptions when throwing --- src/Behat/Gherkin/Parser.php | 12 +++++++++++- tests/Behat/Gherkin/Cucumber/CompatibilityTest.php | 1 - 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Behat/Gherkin/Parser.php b/src/Behat/Gherkin/Parser.php index 66b0e596..a88ac271 100644 --- a/src/Behat/Gherkin/Parser.php +++ b/src/Behat/Gherkin/Parser.php @@ -11,6 +11,7 @@ namespace Behat\Gherkin; use Behat\Gherkin\Exception\LexerException; +use Behat\Gherkin\Exception\NodeException; use Behat\Gherkin\Exception\ParserException; use Behat\Gherkin\Node\BackgroundNode; use Behat\Gherkin\Node\ExampleTableNode; @@ -565,7 +566,16 @@ protected function parseExamples() */ protected function parseTable() { - return new TableNode($this->parseTableRows()); + try { + return new TableNode($this->parseTableRows()); + } + catch(NodeException $e) { + throw new ParserException( + $e->getMessage() . $this->file ? ' in file '.$this->file : '', + 0, + $e + ); + } } /** diff --git a/tests/Behat/Gherkin/Cucumber/CompatibilityTest.php b/tests/Behat/Gherkin/Cucumber/CompatibilityTest.php index bd7d74fb..76d7a0c7 100644 --- a/tests/Behat/Gherkin/Cucumber/CompatibilityTest.php +++ b/tests/Behat/Gherkin/Cucumber/CompatibilityTest.php @@ -43,7 +43,6 @@ class CompatibilityTest extends TestCase ]; private $parsedButShouldNotBe = [ - 'inconsistent_cell_count.feature' => 'Inconsistent cells count throws low level exception not ParserException', 'invalid_language.feature' => 'Invalid language is silently ignored', 'whitespace_in_tags.feature' => 'Whitespace in tags is tolerated', ]; From 9373a130d651b050ac8a0a38b354f3afce29d1c4 Mon Sep 17 00:00:00 2001 From: Ciaran McNulty Date: Wed, 10 Feb 2021 14:15:14 +0000 Subject: [PATCH 2/4] Whitespace fix --- src/Behat/Gherkin/Parser.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Behat/Gherkin/Parser.php b/src/Behat/Gherkin/Parser.php index a88ac271..decd52eb 100644 --- a/src/Behat/Gherkin/Parser.php +++ b/src/Behat/Gherkin/Parser.php @@ -568,8 +568,7 @@ protected function parseTable() { try { return new TableNode($this->parseTableRows()); - } - catch(NodeException $e) { + } catch(NodeException $e) { throw new ParserException( $e->getMessage() . $this->file ? ' in file '.$this->file : '', 0, From 363dd69b9136846a1d4db1d3d47eaf5f0bc7e69d Mon Sep 17 00:00:00 2001 From: Ciaran McNulty Date: Wed, 10 Feb 2021 14:16:14 +0000 Subject: [PATCH 3/4] Fix error message and reduce scope of try block --- src/Behat/Gherkin/Parser.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Behat/Gherkin/Parser.php b/src/Behat/Gherkin/Parser.php index decd52eb..101f324f 100644 --- a/src/Behat/Gherkin/Parser.php +++ b/src/Behat/Gherkin/Parser.php @@ -566,11 +566,13 @@ protected function parseExamples() */ protected function parseTable() { + $table = $this->parseTableRows(); + try { - return new TableNode($this->parseTableRows()); + return new TableNode($table); } catch(NodeException $e) { throw new ParserException( - $e->getMessage() . $this->file ? ' in file '.$this->file : '', + $e->getMessage() . ($this->file ? ' in file '.$this->file : ''), 0, $e ); From a79bb0a17d72f48f7c9e5ed9a4b2d5dcc61ad6aa Mon Sep 17 00:00:00 2001 From: Ciaran McNulty Date: Wed, 10 Feb 2021 14:22:36 +0000 Subject: [PATCH 4/4] Also handle error when Examples table throws NodeException --- src/Behat/Gherkin/Parser.php | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/Behat/Gherkin/Parser.php b/src/Behat/Gherkin/Parser.php index 101f324f..c290a9f2 100644 --- a/src/Behat/Gherkin/Parser.php +++ b/src/Behat/Gherkin/Parser.php @@ -550,13 +550,15 @@ protected function parseStep() */ protected function parseExamples() { - $token = $this->expectTokenType('Examples'); - - $keyword = $token['keyword']; - + $keyword = ($this->expectTokenType('Examples'))['keyword']; $tags = empty($this->tags) ? array() : $this->popTags(); + $table = $this->parseTableRows(); - return new ExampleTableNode($this->parseTableRows(), $keyword, $tags); + try { + return new ExampleTableNode($table, $keyword, $tags); + } catch(NodeException $e) { + $this->rethrowNodeException($e); + } } /** @@ -571,11 +573,7 @@ protected function parseTable() try { return new TableNode($table); } catch(NodeException $e) { - throw new ParserException( - $e->getMessage() . ($this->file ? ' in file '.$this->file : ''), - 0, - $e - ); + $this->rethrowNodeException($e); } } @@ -744,4 +742,13 @@ private function normalizeStepNodeKeywordType(StepNode $node, array $steps = arr } return $node; } + + private function rethrowNodeException(NodeException $e): void + { + throw new ParserException( + $e->getMessage() . ($this->file ? ' in file ' . $this->file : ''), + 0, + $e + ); + } }