• Stars
    star
    182
  • Rank 211,154 (Top 5 %)
  • Language
    C
  • License
    MIT License
  • Created about 2 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A parser and compiler for the Gherkin language.

Gherkin

Gherkin is a parser and compiler for the Gherkin language.

Gherkin is currently implemented for the following platforms (in order of birthday):

The CI will run using the linked workflow when that specific language implementation is changed

The CI will also run for any/all linked workflows when any test data is modified (For example modifying one of the good or bad features / ndjson outputs)

Contributing Translations (i18n)

In order to allow Gherkin to be written in a number of languages, the keywords have been translated into multiple languages. To improve readability and flow, some languages may have more than one translation for any given keyword.

If you are looking to add, update or improve these translations please see CONTRIBUTING.md.

Contributing a Parser Implementation

See CONTRIBUTING.md if you want to contribute a parser for a new programming language. Our wish-list is (in no particular order):

  • Rust

Usage

Gherkin can be used either through its command line interface (CLI) or as a library.

It is designed to be used in conjunction with other tools such as Cucumber which consumes the output from the CLI or library as Cucumber Messages.

Library

Using the library is the preferred way to use Gherkin since it produces easily consumable AST and Pickle objects in-process without having to fork a CLI process or parse JSON.

The library itself provides a stream API, which is what the CLI is based on. This is the recommended way to use the library as it provides a high level API that is easy to use. See the CLI implementations to get an idea of how to use it.

Alternatively, you can use the lower level parser and compiler. Some usage examples are below:

// Java
Path path = Paths.get("../testdata/good/minimal.feature");
GherkinParser parser = GherkinParser.builder().build();
Stream<Envelope> pickles = parser.parse(envelope).filter(envelope -> envelope.getPickle().isPresent());
// C#
var parser = new Parser();
var gherkinDocument = parser.Parse(@"Drive:\PathToGherkinDocument\document.feature");
# Ruby
require 'gherkin/parser'
require 'gherkin/pickles/compiler'

source = {
  uri: 'uri_of_the_feature.feature',
  data: 'Feature: ...',
  mediaType: 'text/x.cucumber.gherkin+plain'
}

gherkin_document = Gherkin::Parser.new.parse(source[:data])
id_generator = Cucumber::Messages::IdGenerator::UUID.new

pickles = Gherkin::Pickles::Compiler.new(id_generator).compile(gherkin_document, source)
// JavaScript
var Gherkin = require('@cucumber/gherkin')
var Messages = require('@cucumber/messages')

var uuidFn = Messages.IdGenerator.uuid()
var builder = new Gherkin.AstBuilder(uuidFn)
var matcher = new Gherkin.GherkinClassicTokenMatcher() // or Gherkin.GherkinInMarkdownTokenMatcher()

var parser = new Gherkin.Parser(builder, matcher)
var gherkinDocument = parser.parse('Feature: ...')
var pickles = Gherkin.compile(gherkinDocument, 'uri_of_the_feature.feature', uuidFn)
// Go
// Download the package via: `go get github.com/cucumber/cucumber/gherkin/go`
import (
  "strings"
  gherkin "github.com/cucumber/cucumber/gherkin/go"
)
reader := strings.NewReader(`Feature: ...`)
gherkinDocument, err := gherkin.ParseGherkinDocument(reader)
# Python
from gherkin.parser import Parser
from gherkin.pickles.compiler import compile

parser = Parser()
gherkin_document = parser.parse("Feature: ...")
pickles = compile(gherkin_document)
// Objective-C
#import "GHParser+Extensions.h"

GHParser * parser = [[GHParser alloc] init];
NSString * featureFilePath; // Should refer to the place where we can get the content of the feature
NSString * content = [NSString stringWithContentsOfURL:featureFilePath encoding:NSUTF8StringEncoding error:nil];
if([content stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length == 0){
      // GHParser will throw an error if you passed empty content... handle this issue first.
}
GHGherkinDocument * result = [parser parseContent:content];
# Perl
use Gherkin::Parser;
use Gherkin::Pickles::Compiler;

my $parser = Gherkin::Parser->new();
my $gherkin_document = $parser->parse("Feature: ...");
my $pickles = Gherkin::Pickles::Compiler->compile($gherkin_document);
# PHP
use Cucumber\Gherkin\GherkinParser;

$path = '/path/to/my.feature';

$parser = new GherkinParser();
$pickles = $parser->parseString(uri: $path, data: file_get_contents($path));

CLI

The Gherkin CLI gherkin reads Gherkin source files (.feature files) and outputs ASTs and Pickles.

The gherkin program takes any number of files as arguments and prints the results to STDOUT as Newline Delimited JSON.

Each line is a JSON document that conforms to the Cucumber Event Protocol.

To try it out, just install Gherkin for your favourite language, and run it over the files in this repository:

gherkin testdata/**/*.feature

Ndjson is easy to read for programs, but hard for people. To pretty print each JSON document you can pipe it to the jq program:

gherkin testdata/**/*.feature | jq

Table cell escaping

If you want to use a newline character in a table cell, you can write this as \n. If you need a | as part of the cell, you can escape it as \|. And finally, if you need a \, you can escape that with \\.

Architecture

The following diagram outlines the architecture:

╔════════════╗   β”Œβ”€β”€β”€β”€β”€β”€β”€β”   ╔══════╗   β”Œβ”€β”€β”€β”€β”€β”€β”   ╔═══╗
β•‘Feature file║──>β”‚Scanner│──>β•‘Tokens║──>β”‚Parser│──>β•‘ASTβ•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•   β””β”€β”€β”€β”€β”€β”€β”€β”˜   β•šβ•β•β•β•β•β•β•   β””β”€β”€β”€β”€β”€β”€β”˜   β•šβ•β•β•β•

The scanner reads a gherkin doc (typically read from a .feature file) and creates a token for each line. The tokens are passed to the parser, which outputs an AST (Abstract Syntax Tree).

If the scanner sees a #language header, it will reconfigure itself dynamically to look for Gherkin keywords for the associated language. The keywords are defined in gherkin-languages.json.

The scanner is hand-written, but the parser is generated by the Berp parser generator as part of the build process.

Berp takes a grammar file (gherkin.berp) and a template file (gherkin-X.razor) as input and outputs a parser in language X:

╔════════════╗   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”   ╔═══════════════╗
β•‘gherkin.berp║──>β”‚berp.exeβ”‚<──║gherkin-X.razorβ•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•   β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
                      β”‚
                      V
                 ╔════════╗
                 β•‘Parser.xβ•‘
                 β•šβ•β•β•β•β•β•β•β•β•

Also see the wiki for some early design docs (which might be a little outdated, but mostly OK).

Abstract Syntax Tree (AST)

The AST produced by the parser can be described with the following class diagram:

Every class represents a node in the AST. Every node has a Location that describes the line number and column number in the input file. These numbers are 1-indexed.

All fields on nodes are strings (except for Location.line and Location.column).

The implementation is simple objects without behaviour, only data. It's up to the implementation to decide whether to use classes or just basic collections, but the AST must have a JSON representation (this is used for testing).

Each node in the JSON representation also has a type property with the name of the node type.

You can see some examples in the testdata/good directory.

Pickles

The AST isn't suitable for execution by Cucumber. It needs further processing into a simpler form called Pickles.

The compiler compiles the AST produced by the parser into pickles:

╔═══╗   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”   ╔═══════╗
β•‘AST║──>β”‚Compiler│──>β•‘Picklesβ•‘
β•šβ•β•β•β•   β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β•šβ•β•β•β•β•β•β•β•

The rationale is to decouple Gherkin from Cucumber so that Cucumber is open to support alternative formats to Gherkin (for example Markdown).

The simpler Pickles data structure also simplifies the internals of Cucumber. With the compilation logic maintained in the Gherkin library we can easily use the same test suite for all implementations to verify that compilation is behaving consistently between implementations.

Each Scenario will be compiled into a Pickle. A Pickle has a list of PickleStep, derived from the steps in a Scenario.

Each Examples row under Scenario Outline will also be compiled into a Pickle.

Any Background steps will also be compiled into a Pickle.

Every tag, like @a, will be compiled into a Pickle as well (inheriting tags from parent elements in the Gherkin AST).

Example:

@a
Feature:
  @b @c
  Scenario Outline:
    Given <x>

    Examples:
      | x |
      | y |

  @d @e
  Scenario Outline:
    Given <m>

    @f
    Examples:
      | m |
      | n |

Using the CLI we can compile this into several pickle objects:

gherkin testdata/good/readme_example.feature --no-source --no-ast | jq

Output:

{
  "type": "pickle",
  "uri": "testdata/good/readme_example.feature",
  "pickle": {
    "name": "",
    "steps": [
      {
        "text": "y",
        "arguments": [],
        "locations": [
          {
            "line": 9,
            "column": 7
          },
          {
            "line": 5,
            "column": 11
          }
        ]
      }
    ],
    "tags": [
      {
        "name": "@a",
        "location": {
          "line": 1,
          "column": 1
        }
      },
      {
        "name": "@b",
        "location": {
          "line": 3,
          "column": 3
        }
      },
      {
        "name": "@c",
        "location": {
          "line": 3,
          "column": 6
        }
      }
    ],
    "locations": [
      {
        "line": 9,
        "column": 7
      },
      {
        "line": 4,
        "column": 3
      }
    ]
  }
}
{
  "type": "pickle",
  "uri": "testdata/good/readme_example.feature",
  "pickle": {
    "name": "",
    "steps": [
      {
        "text": "n",
        "arguments": [],
        "locations": [
          {
            "line": 18,
            "column": 7
          },
          {
            "line": 13,
            "column": 11
          }
        ]
      }
    ],
    "tags": [
      {
        "name": "@a",
        "location": {
          "line": 1,
          "column": 1
        }
      },
      {
        "name": "@d",
        "location": {
          "line": 11,
          "column": 3
        }
      },
      {
        "name": "@e",
        "location": {
          "line": 11,
          "column": 6
        }
      },
      {
        "name": "@f",
        "location": {
          "line": 15,
          "column": 5
        }
      }
    ],
    "locations": [
      {
        "line": 18,
        "column": 7
      },
      {
        "line": 12,
        "column": 3
      }
    ]
  }
}

Each Pickle event also contains the path to the original source. This is useful for generating reports and stack traces when a Scenario fails.

Cucumber will further transform this list of Pickle objects to a list of TestCase objects. TestCase objects link to user code such as Hooks and Step Definitions.

Building Gherkin

See CONTRIBUTING.md

Markdown with Gherkin

See Markdown with Gherkin.

Projects using Gherkin

More Repositories

1

cucumber-ruby

Cucumber for Ruby. It's amazing!
Ruby
5,178
star
2

cucumber-js

Cucumber for JavaScript
TypeScript
5,053
star
3

common

A home for issues that are common to multiple cucumber repositories
3,363
star
4

cucumber-jvm

Cucumber for the JVM
Java
2,702
star
5

godog

Cucumber for golang
Go
2,307
star
6

cucumber-rails

Rails Generators for Cucumber with special support for Capybara and DatabaseCleaner
Ruby
1,021
star
7

aruba

Test command-line applications with Cucumber-Ruby, RSpec or Minitest.
Ruby
948
star
8

cucumber-java-skeleton

This is the simplest possible setup for Cucumber-JVM using Java.
Java
461
star
9

cucumber-cpp

Support for writing Cucumber step definitions in C++
C++
308
star
10

cucumber-eclipse

Eclipse plugin for Cucumber
Java
192
star
11

cucumber-expressions

Human friendly alternative to Regular Expressions
Java
155
star
12

docs

Cucumber user documentation
CSS
151
star
13

cucumber-android

Android support for Cucumber-JVM
Kotlin
135
star
14

cucumber-electron

Run cucumber.js in electron
JavaScript
118
star
15

gherkin-go

[READ-ONLY] Gherkin for Go - subtree of https://github.com/cucumber/gherkin -- moved to https://github.com/cucumber/gherkin
Go
84
star
16

gherkin-javascript

[READ-ONLY] Gherkin for JavaScript - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/gherkin
TypeScript
79
star
17

gherkin-python

[READ-ONLY] Gherkin for Python - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/gherkin
Python
76
star
18

vscode

Official Visual Studio Code Extension for Cucumber
TypeScript
66
star
19

screenplay.js

Library to ease implementation of the Screenplay pattern with CucumberJS
TypeScript
56
star
20

gherkin-java

[READ-ONLY] Gherkin for Java - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/gherkin
Java
49
star
21

cucumber-jvm-scala

Cucumber Scala
Scala
48
star
22

gherkin-dotnet

[READ-ONLY] Gherkin for Dotnet - subtree of monorepo https://github.com/cucumber/cucumber Gherkin parser/compiler for .NET
C#
46
star
23

language-server

Cucumber Language Server
TypeScript
36
star
24

cucumber-ruby-core

Core library for the Ruby flavour of Cucumber
Ruby
35
star
25

react-components

React components for Cucumber
TypeScript
34
star
26

cucumber-lua

A cucumber wire protocol implementation for Lua step definitions
Lua
29
star
27

cucumber.ml

Cucumber for OCaml
OCaml
27
star
28

cucumber-js-examples

Examples of using Cucumber-JS
Makefile
25
star
29

blockly

Gherkin Editor based on Blockly
TypeScript
24
star
30

cucumber-jvm-groovy

Cucumber Groovy
Java
23
star
31

microdata

Extract WHATWG microdata from a DOM
TypeScript
22
star
32

json-formatter

Provides a language-agnostic command-line tool to convert cucumber messages into a JSON document.
Go
21
star
33

messages

A message protocol for representing results and other information from Cucumber
C#
19
star
34

language-service

Cucumber Language Service
TypeScript
18
star
35

cucumber-js-pretty-formatter

Cucumber.js pretty formatter
TypeScript
17
star
36

monaco

Configure Monaco editor to use cucumber-language-service
TypeScript
16
star
37

html-formatter

HTML formatter for reporting Cucumber results
Java
14
star
38

gherkin-utils

API for working with Gherkin documents
TypeScript
12
star
39

ci-environment

Detect CI Environment from environment variables
Java
10
star
40

gherkin-c

[READ-ONLY] Gherkin for C - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/gherkin
C
10
star
41

tag-expressions

Cucumber tag expression parser
Python
9
star
42

screenplay.js.examples

Examples using @cucumber/screenplay
TypeScript
7
star
43

cucumber-eclipse-update-site-snapshot

Cucumber Eclipse Update Site Snapshots
CSS
7
star
44

gherkin-objective-c

[READ-ONLY] Gherkin for Objective C - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/gherkin
Objective-C
7
star
45

gherkin-ruby

[READ-ONLY] Gherkin for Ruby - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/gherkin
Ruby
7
star
46

cucumber-ruby-wire

Wire protocol plugin for Cucumber
Gherkin
7
star
47

cucumber-json-converter

Parse Cucumber JSON from most Cucumber implementations and versions
TypeScript
6
star
48

polyglot-release

Make polyglot releases with a single command
Shell
5
star
49

todo-react-typescript-subsecond

Tiny Todo app in React and TypeScript demonstrating sub-second test feedback
TypeScript
4
star
50

action-retire-inactive-contributors

Retire inactive contributors from one team to another
TypeScript
4
star
51

cucumber-json-schema

JSON Schemas for Cucumber JSON output
JavaScript
4
star
52

messages-go

[READ ONLY] Cucumber Messages for Go - subtree of monorepo https://github.com/cucumber/messages -- moved to https://github.com/cucumber/messages
Go
3
star
53

gherkin-streams

Stream utilities to read Gherkin parser output.
TypeScript
3
star
54

try-cucumber-expressions

Try Cucumber Expressions in your browser
TypeScript
3
star
55

cucumber-eclipse-update-site

Cucumber Eclipse Update Site
3
star
56

github-settings

Pulumi scripts to automatically configure our GitHub org/repo settings
TypeScript
2
star
57

gherkin-perl

[READ-ONLY] Gherkin for Perl - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/gherkin
Perl
2
star
58

gherkin-php

[READ ONLY] Cucumber Gherkin for PHP - subtree of https://github.com/cucumber/gherkin
PHP
2
star
59

junit-xml-formatter

JUnit XML formatter for reporting Cucumber results
Java
2
star
60

oselvar-github-metrics

Oselvar GitHub Metrics for the Cucumber Organisation
Shell
2
star
61

build

Docker image used to build the Cucumber Project
Shell
2
star
62

fake-cucumber

Tool to generate test data for cucumber
TypeScript
2
star
63

messages-javascript

[READ ONLY] Cucumber Messages for JavaScript (Protocol Buffers) - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/messages
TypeScript
2
star
64

commitbit

Microservice that hands out commit bit to everyone who gets a pull request merged
JavaScript
2
star
65

query

A query API for https://github.com/cucumber/messages
Java
2
star
66

aruba-getting-started

Getting started with aruba
Ruby
2
star
67

action-publish-cpan

GitHub Action to publish a Perl module to CPAN
Perl
1
star
68

community-calendar

Public calendar for community calls and events
1
star
69

action-changelog

GitHub Action for changelog tool
Shell
1
star
70

release-announcement-banner

For blog posts where we announce a new version of a Cucumber tool
JavaScript
1
star
71

split-java

A Cucumber plugin to toggle Split features from Cucumber scenarios
Java
1
star
72

cucumber-js-package-upgrade

package to point users to the new @cucumber/cucumber
JavaScript
1
star
73

messages-java

[READ ONLY] Cucumber Messages for Java (Protocol Buffers) - subtree of monorepo https://github.com/cucumber/cucumber
Java
1
star
74

action-publish-subrepo-test-monorepo-a-subfolder

target for tests for https://github.com/cucumber/action-publish-subrepo
1
star
75

cucumber-parent

Parent `pom.xml` for all Cucumber Java modules
1
star
76

action-publish-rubygem

GitHub Action to publish a Ruby Gem
Ruby
1
star
77

compatibility-kit

Platform-agnostic set of acceptance tests for validating cucumber implementations
TypeScript
1
star
78

action-publish-subrepo-test-monorepo

Test repo for testing the action-publish-subrepo GitHub Action
1
star
79

messages-ruby

[READ ONLY] Cucumber Messages for Ruby (Protocol Buffers) - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/messages
Ruby
1
star
80

renovate-config

Shareable Config Presets for Renovate in the Cucumber org
1
star
81

action-publish-mvn

GitHub Action to publish Maven artefacts
Java
1
star
82

message-streams

Stream utilities to read and write Cucumber Message objects to/from streams.
TypeScript
1
star
83

action-publish-npm

GitHub Action to publish an NPM module
1
star
84

messages-dotnet

[READ ONLY] Cucumber Messages for .NET (Protocol Buffers) - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/messages
Makefile
1
star
85

.github

πŸ‘©β€βš•οΈ Default community health files for the Cucumber organisation on GitHub.
Shell
1
star