README.md

# ExGherkin

This Gherkin Parser is meant to parse gherkin files. In order to provide an official parser that can be maintained by the Cucumber community, the parser is generated by the Berp parser generator, which takes the gherkin grammar file. This way, when new gherkin syntax is supported, it is only a matter of generating a new parser (and making slight changes to the code base).

## Usage tokenizer

For now the Gherkin Parser is meant to be used internally and not as a command line tool. There are 2 main functionalities, tokenizing and actually parsing to an AST. Sample usage:

```elixir
# For the tokenizer
iex> ExGherkin.tokenize("testdata/good/background.feature") |> IO.puts
(1:1)FeatureLine:Feature/Background/
(2:1)Empty://
(3:3)BackgroundLine:Background/a simple background/
...
```

## Usage parser

The other functionality is parsing feature files to an actual AST / envelopes. You can specify the following options:

* `:no_source`
* `:no_ast`
* `:no_pickles`

You always get a list back with the non-ignored elements. Keep in mind that there can be more than one executable pickle!

```elixir
# You can parse path(s) that export Cucumber Envelopes
#   The second parameter are options, supported options are listed below
ExGherkin.parse_path("testdata/good/background.feature", [:no_ast, :no_pickles])
[
  %CucumberMessages.Envelope{
    __uf__: [],
    message: {:source,
     %CucumberMessages.Source{
       # ...
     }}
  }
]
# or only the executable pickles
ExGherkin.parse_path("testdata/good/background.feature", [:no_ast, :no_source])
[
  %CucumberMessages.Envelope{
    __uf__: [],
    message: {:pickle,
     %CucumberMessages.Pickle{
       # ...
    }}
  },
  %CucumberMessages.Envelope{
    __uf__: [],
    message: {:pickle,
     %CucumberMessages.Pickle{
       # ...
    }}
  }
]
# Or the AST
ExGherkin.parse_path("testdata/good/background.feature", [:no_pickles, :no_source])
[
  %CucumberMessages.Envelope{
    __uf__: [],
    message: {:gherkin_document,
     %CucumberMessages.GherkinDocument{
       # ...
    }}
  }
]
```

As you've probably already surmised, the structs that you get back are from the [cucumber messages](https://github.com/WannesFransen1994/ex_cucumber_messages) library.

## Pretty printing to ndjson

If you want the ndjson output, you can print this with:

```elixir
ExGherkin.parse_path("testdata/good/background.feature", []) |> ExGherkin.print_messages(:ndjson) |> IO.puts
{"source":{"data": .......... }}
{"gherkinDocument":{"feature": ........... }}
{"pickle":{ .......... }}
{"pickle":{ .......... }}
```

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `ex_gherkin` to your list of dependencies in `mix.exs`:

```elixir
def deps do
  [
    {:ex_gherkin, "~> 0.1.0"}
  ]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/ex_gherkin](https://hexdocs.pm/ex_gherkin).

## Extra info

testdata from cucumber gherkin monorepo commit hash 27e0b8a7d9102b83f7f2100cd85f46ef211133a4