Behat\Gherkin\Parser::parse PHP Method

parse() public method

Parses input & returns features array.
public parse ( string $input, string $file = null ) : Behat\Gherkin\Node\FeatureNode | null
$input string Gherkin string document
$file string File name
return Behat\Gherkin\Node\FeatureNode | null
    public function parse($input, $file = null)
    {
        $this->languageSpecifierLine = null;
        $this->input = $input;
        $this->file = $file;
        $this->tags = array();
        try {
            $this->lexer->analyse($this->input, 'en');
        } catch (LexerException $e) {
            throw new ParserException(sprintf('Lexer exception "%s" thrown for file %s', $e->getMessage(), $file), 0, $e);
        }
        $feature = null;
        while ('EOS' !== ($predicted = $this->predictTokenType())) {
            $node = $this->parseExpression();
            if (null === $node || "\n" === $node) {
                continue;
            }
            if (!$feature && $node instanceof FeatureNode) {
                $feature = $node;
                continue;
            }
            if ($feature && $node instanceof FeatureNode) {
                throw new ParserException(sprintf('Only one feature is allowed per feature file. But %s got multiple.', $this->file));
            }
            if (is_string($node)) {
                throw new ParserException(sprintf('Expected Feature, but got text: "%s"%s', $node, $this->file ? ' in file: ' . $this->file : ''));
            }
            if (!$node instanceof FeatureNode) {
                throw new ParserException(sprintf('Expected Feature, but got %s on line: %d%s', $node->getKeyword(), $node->getLine(), $this->file ? ' in file: ' . $this->file : ''));
            }
        }
        return $feature;
    }

Usage Example

コード例 #1
0
 /**
  * {@inheritDoc}
  *
  * @throws \InvalidArgumentException
  * @throws \RuntimeException
  * @throws ParserException
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $align = $input->getOption('align') === Step::ALIGN_TO_LEFT ? Step::ALIGN_TO_LEFT : Step::ALIGN_TO_RIGHT;
     $directory = $input->getArgument('directory');
     $finder = (new FeatureResolve($directory))->__invoke();
     $output->writeln("\nFinding files on <info>" . $directory . "</info>\n");
     $tagFormatter = new Tags();
     $featureDescription = new FeatureDescription();
     $background = new Background($align);
     $scenario = new Scenario($align);
     /* @var $file \Symfony\Component\Finder\SplFileInfo */
     foreach ($finder as $file) {
         $fileContent = $file->getContents();
         $contentWithoutComments = $this->removeComments($fileContent);
         $feature = $this->parser->parse($fileContent);
         $formatted = $feature->hasTags() ? $tagFormatter->format($feature->getTags()) . "\n" : '';
         $formatted .= $featureDescription->format($feature) . "\n\n";
         $formatted .= $feature->hasBackground() ? $background->format($feature->getBackground()) . "\n" : '';
         $formatted .= $feature->hasScenarios() ? $scenario->format($feature->getScenarios()) : '';
         if ($formatted !== $contentWithoutComments) {
             if (!defined('FAILED')) {
                 define('FAILED', true);
             }
             $diff = new Differ("--- Original\n+++ Expected\n", false);
             $output->writeln('<error>Wrong style: ' . $file->getRealPath() . '</error>');
             $output->writeln($diff->diff($contentWithoutComments, $formatted));
         }
     }
     if (defined('FAILED')) {
         return 1;
     }
     $output->writeln('<bg=green;fg=white>     Everything is OK!     </>');
 }
All Usage Examples Of Behat\Gherkin\Parser::parse