[[!meta title="Typesetting scenario steps based on matched bindings"]] [[!tag pondering]] [[!meta date="2019-11-30 09:34"]] We're working on typesetting scenarios. The first iteration of this aims at the following: * Subplot reads the markdown input, and a YAML file with bindings. The YAML file is named in the markdown's metadata block (a Pandoc extension to markdown). * The bindings may capture parts of a step using regular expressions. * Each scenario snippet is typeset separately. * Each step in a snippet is typeset separately. * There is no support for "and" or "but". The keyword must be one of "given", "when", or "then". * The keyword of a step is typeset in italics. * Captured parts of a step are typeset in bold face. * The rest of the step is typeset in a normal font. * Steps that do not match a binding, or have an unknown keyword, or otherwise are problematic, are typeset in monospace and have a question mark prepended to them. A descriptive error message is added to the document as well. To implement this, I'm going to want a couple of abstractions: * A snippet parser, as an iterator over unparsed steps (string slices). * A step parser, which takes an unparsed step (a string slice), and returns a parsed step. A parsed step is matched against all known bindings. It can either be a successful match, or an unsuccessful one. A successful match consists of a step kind (keyword), and a list of partial steps. A partial step is either captured or uncaptured parts of the step. Given a binding file like this: ```yaml - given: I am (?P\S+) - when: I declare myself (king|queen) - then: everyone agrees ``` And a scenario snippet like this: ``` given I am Tomjon when I declare myself king then there is applause ``` This would result in the following parse results: * A matched step, with keyword _given_, an uncaptured part "I am " and a captured part "Tomjon" * A matched step, with keyword _when_, and an uncaptured part "I declare myself king" * An unmatched step, with text "then there is applause"