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).
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:
# 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/
...
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!
# 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 library.
If you want the ndjson output, you can print this with:
ExGherkin.parse_path("testdata/good/background.feature", []) |> ExGherkin.print_messages(:ndjson) |> IO.puts
{"source":{"data": .......... }}
{"gherkinDocument":{"feature": ........... }}
{"pickle":{ .......... }}
{"pickle":{ .......... }}
If available in Hex, the package can be installed
by adding ex_gherkin
to your list of dependencies in mix.exs
:
def deps do
[
{:ex_gherkin, "~> 0.1.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/ex_gherkin.
testdata from cucumber gherkin monorepo commit hash 27e0b8a7d9102b83f7f2100cd85f46ef211133a4