Skip to content

Screenplay capture grammar (EBNF)

This document provides the complete formal grammar for the Change Data Capture Language (CDL) - the sub-language a capture block delegates to - using Extended Backus-Naur Form (EBNF) notation. It mirrors the Projection Declaration Language grammar, which documents PDL the same way.

EBNF is a notation for formally describing syntax. Here are the key elements used:

Notation Meaning Example
= Definition Rule = "value" ;
; End of rule Rule = "value" ;
{ } Zero or more { "item" } matches "", “item”, “itemitem”, etc.
[ ] Optional (zero or one) [ "optional" ] matches "" or “optional”
( ) Grouping ( "a" | "b" ) matches “a” or “b”
| Alternative (or) "a" | "b" matches either “a” or “b”
" " Terminal (literal) "keyword" matches the text “keyword”
, Sequence "a", "b" matches “a” followed by “b”

Special Symbols:

  • NL - Newline
  • INDENT - Increased indentation level
  • DEDENT - Decreased indentation level
  • Ident - Identifier (a letter or underscore, followed by letters, digits, or underscores)
  • Path - Dotted property path (Ident, optionally followed by ., Ident, repeated)
Document = Capture, { Capture } ;
Capture = "capture", Ident, NL,
[ INDENT,
{ Directive },
DEDENT ] ;
Directive = SourceBlock
| KeyDirective
| MapBlock
| AppendBlock
| ChildrenBlock
| NestedBlock ;
SourceBlock = "source", Ident, NL,
INDENT,
{ SourceSetting },
DEDENT ;
SourceSetting = Ident, LineValue, NL ;
KeyDirective = "key", LineValue, [ "is", "event", "source" ], NL ;
MapBlock = "map", NL,
INDENT,
{ MapOperation },
DEDENT ;
MapOperation = SplitOperation
| MapEntry ;
MapEntry = Ident, "=", Expr,
[ "translate", NL,
INDENT,
{ TranslateEntry },
DEDENT
], NL ;
TranslateEntry = StringLiteral, "=>", Ident, NL ;
SplitOperation = "split", Expr, "by", StringLiteral, NL,
INDENT,
{ Path, NL },
DEDENT ;
AppendBlock = "append", Ident, NL,
INDENT,
{ TagDecl },
( WhenClause, INDENT, { PropertyMapping }, DEDENT
| { PropertyMapping } ),
DEDENT ;
TagDecl = "tag", ( Ident | StringLiteral | MappingSource ), NL ;
WhenClause = "when", WhenExpr, NL ;
WhenExpr = "added"
| "removed"
| Template
| Path, [ "from", SimpleValue, "to", SimpleValue
| "or", Path, { "or", Path }
| "and", Path, { "and", Path } ] ;
PropertyMapping = Path, "=", MappingSource, NL ;
ChildrenBlock = "children", Ident, "identified", "by", Path, NL,
INDENT,
[ MapBlock ],
{ AppendBlock },
DEDENT ;
NestedBlock = "nested", Path, NL,
INDENT,
[ MapBlock ],
{ AppendBlock },
DEDENT ;
Expr = Template
| Path ;
MappingSource = SourceItemExpr
| ContextExpr
| EnvExpr
| Literal
| Path
| RawExpression ;
SourceItemExpr = "$.", Path ; (* a value from the current source item *)
ContextExpr = "$context.", Path ; (* a value from the capture context, such as $context.occurred *)
EnvExpr = "$env.", Ident ; (* an environment variable *)
Template = "`", { TemplateChar | "${", Expr, "}" }, "`" ;
TemplateChar = (* any character except ` *) ;
SimpleValue = StringLiteral | NumberLiteral | "true" | "false" | "null" | Ident ;
Literal = BoolLiteral
| StringLiteral
| NumberLiteral
| NullLiteral ;
BoolLiteral = "true" | "false" ;
StringLiteral = '"', { StringChar }, '"' ;
NumberLiteral = [ "-" ], Digit, { Digit }, [ ".", Digit, { Digit } ] ;
NullLiteral = "null" ;
RawExpression = (* freeform expression captured verbatim *) ;
LineValue = (* remainder of the line, captured verbatim *) ;
Path = Ident, { ".", Ident } ;
Ident = ( Letter | "_" ), { Letter | Digit | "_" } ;
Letter = "A" | "B" | ... | "Z" | "a" | "b" | ... | "z" ;
Digit = "0" | "1" | "2" | ... | "9" ;
StringChar = (* any character except " and newline *) ;

A document contains one or more captures:

Document = Capture, { Capture } ;

A capture names the construct and contains directives:

Capture = "capture", Ident, NL,
INDENT,
{ Directive },
DEDENT ;

Example:

capture LegacyInvoiceCapture
source api
api LegacyInvoicingApi
route /invoices
poll 5m
key id

Declares where the captured data comes from. The source kind and its settings are both free-form identifiers - api, webhook and message are the source kinds in active use, each with its own conventional settings (api/route/poll for api, path for webhook, topic for message), but the grammar itself does not restrict either:

SourceBlock = "source", Ident, NL,
INDENT,
{ SourceSetting },
DEDENT ;
SourceSetting = Ident, LineValue, NL ;

Example:

source webhook
path /invoices

source events is the one source kind with its own grammar: instead of free-form settings it takes one or more from <Event> lines, each naming a public event of another application. The captured item is the incoming event’s content, and $context is its event context.

EventsSource = "source", "events", NL,
INDENT,
"from", QualifiedName, NL,
{ "from", QualifiedName, NL },
DEDENT ;

The parser reports PLAY0620 for an empty block, any other setting, an invalid name or a repeated event. The assembled model additionally requires an explicitly inbound Translate slice (PLAY0619) whose consumed events are foreign public events (PLAY0610, PLAY0616) and whose append events are private local events (PLAY0612); all consumed events must come from one origin (PLAY0687). The form is admitted by ESM v9. Its key must be a required property of every locally declared from event (PLAY0700); context keys are not admitted. key <property> is event source is valid only here and on a single property key (PLAY0809); it selects ESM v33. Specifications present typed events through given <Event> and when append <Event>, not capture records (PLAY0701). The reference folds incoming properties into per-key state, then evaluates root appends; children and nested scopes over events are evaluated like those over other sources, though a composite value is still treated as a scalar. Only $context.occurred, supplied by given clock, is executable context. See capturing from public events for change detection and runtime limits.

Names the source property identifying an instance:

KeyDirective = "key", LineValue, NL ;

Reshapes source values before they reach an append - renaming, templating, translating and splitting:

MapBlock = "map", NL,
INDENT,
{ MapOperation },
DEDENT ;
MapOperation = SplitOperation
| MapEntry ;

A map block may appear at the capture level, or nested inside a children or nested block, where it applies to each child/nested instance.

Renames a source value onto a property, assigns a template built from source values, or both feed a translate block of value substitutions:

MapEntry = Ident, "=", Expr,
[ "translate", NL,
INDENT,
{ TranslateEntry },
DEDENT
], NL ;
TranslateEntry = StringLiteral, "=>", Ident, NL ;
Expr = Template
| Path ;

Examples:

map
status = status translate
"utkast" => draft
"sendt" => sent
summary = `${status} invoice`

status = status is a plain rename (the source property and target property happen to share a name); summary = `...` is a template - the same backtick/${...} syntax produces and PDL expressions use.

Splits one source value into several target properties by a separator:

SplitOperation = "split", Expr, "by", StringLiteral, NL,
INDENT,
{ Path, NL },
DEDENT ;

Example:

split contactName by ","
firstName
lastName

Declares the event to append and the trigger condition:

AppendBlock = "append", Ident, NL,
INDENT,
{ TagDecl },
( WhenClause, INDENT, { PropertyMapping }, DEDENT
| { PropertyMapping } ),
DEDENT ;
TagDecl = "tag", ( Ident | StringLiteral | MappingSource ), NL ;
PropertyMapping = Path, "=", MappingSource, NL ;

Note: when an append declares a when clause, its property mappings are indented one level deeper than when (the mappings belong to the trigger). An append may omit when entirely, in which case its property mappings sit directly under append.

tag lines apply tags to the appended event — static identifiers or string literals, or a $context. expression for tags resolved from context.

The trigger condition guarding an append:

WhenClause = "when", WhenExpr, NL ;
WhenExpr = "added"
| "removed"
| Template
| Path, [ "from", SimpleValue, "to", SimpleValue
| "or", Path, { "or", Path }
| "and", Path, { "and", Path } ] ;
Form Meaning
when added Appends when an item appears in the source.
when removed Appends when an item disappears from the source.
when `<expression>` Appends when a raw template-literal expression evaluates to true. The parser captures the text; the C# binder parses it (see below).
when <Path> Appends when the named property changes.
when <Path> from <value> to <value> Appends when the named property transitions from one specific value to another.
when <Path> or <Path> { or <Path> } Appends when any of the named properties change.
when <Path> and <Path> { and <Path> } Appends when all of the named properties change.

or and and cannot be mixed within a single property-change when clause - when a or b and c is a compile error. These forms test changes, not Boolean comparisons.

A backtick guard is not the and/or condition grammar used by produces when. For example, when `status == "sent" && overdue == true` is valid: the C# parser stores the guard text verbatim, including backticks, and the semantic binder then parses it. The binder admits fields, string, number, boolean and null literals, the comparisons ==, !=, <, <=, > and >=, and &&, ||, ! and parentheses. A field on its own is true when it holds the boolean true, and ordering compares numbers only. A guard outside that set reports PLAY0268. The TypeScript compiler keeps the guard as text and checks only that its closing backtick is present; it does not read the operators. Backtick guards are the only place &&, || and ! are admitted; see Notation conventions and the executable model rules.

Captures changes in a child collection, with an optional map applied to every child before its appends run:

ChildrenBlock = "children", Ident, "identified", "by", Path, NL,
INDENT,
[ MapBlock ],
{ AppendBlock },
DEDENT ;

Example:

children lineItems identified by lineNumber
map
productName = name
append InvoiceLineItemAdded
when added
invoiceId = $.id
lineNumber = $.lineNumber
append InvoiceLineItemRemoved
when removed
invoiceId = $.id
lineNumber = $.lineNumber

Captures changes to a single nullable child object - unlike children, which manages a collection - with the same optional map:

NestedBlock = "nested", Path, NL,
INDENT,
[ MapBlock ],
{ AppendBlock },
DEDENT ;

Example:

nested billingContact
map
contactName = name
append BillingContactUpdated
when email
invoiceId = $.id
email = $.email

The parser recognizes these mapping-source forms; parsing does not mean the capture binder and evaluator support executing them:

MappingSource = SourceItemExpr
| ContextExpr
| EnvExpr
| Literal
| Path
| RawExpression ;
SourceItemExpr = "$.", Path ;
ContextExpr = "$context.", Path ;
EnvExpr = "$env.", Ident ;

Executable append mappings support:

Expression Meaning
$.<field> A field from the current mapped source item, falling back to the enclosing record when absent. The reference evaluator does not traverse dotted field paths.
$context.occurred The supplied occurrence timestamp; a scenario must supply given clock.
"literal", 42, true, null A literal checked against the target property’s type.
<enum member> An enumeration member resolved against the target property’s type.

$env.<NAME>, other $context.<path> values, raw expressions and bare paths that are not enumeration members can parse, but are not supported executable capture append mappings. A bare path does not read a property from a command/event context. The preceding map block has a separate contract: source fields, templates of source fields, and field splitting.

The grammar uses indentation to define structure, the same as every other Screenplay sub-language:

  1. INDENT: Increase indentation by one level (typically 2 spaces)
  2. DEDENT: Decrease indentation by one level
  3. Consistent Spacing: All indentation must use spaces (no tabs)
  4. Block Structure: Each block’s content must be indented from its declaration

Beyond the grammar, the parser reports these as compile errors:

  1. capture <Name> must match a valid identifier.
  2. Each map entry must be <property> = <source> [translate].
  3. split <source> by "<separator>" requires a quoted separator; each indented line under it must be a valid property path.
  4. append <EventType> requires a PascalCase-looking event type, matching event naming elsewhere in Screenplay.
  5. A when clause must be added, removed, a backtick template with a matching closing backtick, or a property path optionally followed by a single from ... to ... transition or a chain of or/and properties - or and and cannot mix.
  6. children <collection> identified by <key> and nested <property> must match their header shape.
  7. An unrecognized construct inside a capture, children or nested body is reported as an error and the offending block is skipped.

This example demonstrates how the rules above combine:

capture LegacyInvoiceCapture
source api
api LegacyInvoicingApi
route /invoices
poll 5m
key id
map
status = status translate
"utkast" => draft
"sendt" => sent
"betalt" => paid
"kansellert" => cancelled
split contactName by ","
firstName
lastName
append InvoiceStatusChanged
when status
invoiceId = $.id
status = $.status
changedAt = $context.occurred
append InvoicePaidFromSent
when status from "sent" to "paid"
invoiceId = $.id
paidAt = $context.occurred
children lineItems identified by lineNumber
map
productName = name
append InvoiceLineItemAdded
when added
invoiceId = $.id
lineNumber = $.lineNumber
quantity = $.quantity
unitPrice = $.unitPrice
append InvoiceLineItemRemoved
when removed
invoiceId = $.id
lineNumber = $.lineNumber
nested billingContact
map
contactName = name
append BillingContactUpdated
when email
invoiceId = $.id
email = $.email

This capture uses:

  • A source block with api, route and poll settings
  • A map block with a translate mapping and a split operation
  • Two capture-level append blocks - a plain property-changed when and a value-transition when
  • A children block with its own map and two appends (when added / when removed)
  • A nested block with its own map and append