Flex models

Check Flex sources

tenon flex check checks your Flex sources for errors and reports each one with the source line it’s on. Give it the folders that hold your .flex files: Tenon scans each one, subfolders included, and checks them together as one model, so imports between files resolve. Warnings are reported but don’t fail the check, so it exits 0 unless there’s an error.

Each problem comes with a code that names it. If AcceptedThermalBand sends a Reading that the model never declares, for example, tenon flex check reports unresolved-reference at that line, and message-data-type, because a protocol can only send a declared message.

To see the results for one file while still checking it against the rest of your model, add --file. The default output shows each problem as a source snippet, while --format short prints one path:line:col line per problem, and --json gives a scripts-friendly report instead.

Like every tenon flex command apart from tenon flex docs, it takes a license seat. The Flex language itself is documented at flexlang.org.

Command: tenon flex check [options] [<dir>...]

Arguments

<dir>...
The folders of .flex files to check, as one model. You can also give a single file. Optional when you give --file.

Options

--file <file>
Show results for this file only. With a folder, Tenon still checks the whole model, so imports resolve.
--json
Print a machine-readable JSON report instead.
--format <graphical|short>
How to print problems: graphical shows each one as a source snippet, and short as a single path:line:col: code: message line. Defaults to graphical. Ignored with --json.

Exit codes

Code Meaning
0 No errors; warnings alone pass
1 Errors found, or an input couldn’t be read
Check Flex sources
tenon flex check spectenon flex check spec --file spec/thermal.flextenon flex check spec --format short
A protocol that sends an undeclared message
global protocol AcceptedThermalBand {
  exch any ReadTemperature from CommandPost to Radar;
  exch any Reading from Radar to CommandPost;
}

Draw diagrams

tenon flex diagram draws your model’s protocols. A sequence diagram, the default, shows one protocol’s messages in the order they’re sent, and Tenon draws one for each protocol. A block diagram, with --kind block, joins the protocols you select into a single picture of which components talk to which, without the order; --messages adds a table of the messages each connection carries.

The diagrams follow your protocols closely. In the sequence diagram of RejectedThermalBand, CommandPost and Radar each have a lifeline, the ReadTemperature request comes first, and Radar’s choice is a box with one section for each branch: Temperature and Accept in one, OutOfBand and Retry in the other. In a block diagram of the whole model, CommandPost and Radar are two blocks joined in both directions.

Without --format, Tenon draws sequence diagrams in the terminal. To save them, choose a format and, optionally, a place with --out: a folder gets one file per protocol, named after it (a block diagram is block.<format>), while a .md file collects every diagram in one Markdown page. A block diagram always needs a format.

Tenon draws every protocol in the model unless you narrow it down, with --protocol for particular protocols or --global and --local for one kind. A workflow can draw the diagrams on every run.

Command: tenon flex diagram [options] [<path>...]

Arguments

<path>...
The .flex files, or folders of them, to draw from, as one model. Defaults to the current folder.

Options

--kind <sequence|block>
The kind of diagram: sequence draws one diagram per protocol, and block draws one diagram of all the selected protocols. Defaults to sequence.
--protocol <pattern>
Draw only the protocols whose full name matches: a substring, or a glob with * or ?, which must match the whole name. Case-sensitive. Separate several with commas, or repeat the option.
--global
Draw global protocols only.
--local
Draw local protocols only.
--format <mmd|md|svg|png|pdf>
Save the diagrams in this format: Mermaid source (mmd), Markdown with Mermaid blocks (md), or an image. Without it, Tenon draws sequence diagrams in the terminal.
--out <path>
Where to save them: a folder for one file per diagram, a .md file for every diagram in one file, or a file named for one diagram. Needs --format. Defaults to printing mmd and md to the terminal, and to saving images in the current folder.
--ascii
Draw terminal diagrams in plain ASCII instead of box-drawing characters.
--messages
List the messages each connection carries, in a table under a block diagram. Needs --kind block, and a format other than mmd.

Exit codes

Code Meaning
0 Success
1 An input couldn’t be read, or a diagram couldn’t be written
Draw diagrams
tenon flex diagram spec --protocol RejectedThermalBandtenon flex diagram spec --format svg --out diagramstenon flex diagram spec --kind block --messages \    --format svg --out diagrams
A protocol with a choice
global protocol RejectedThermalBand {
  exch any ReadTemperature from CommandPost to Radar;
  choice in Radar
  | true =>
    exch any Temperature from Radar to CommandPost;
    exch any Accept from CommandPost to Radar;
  | else =>
    exch any OutOfBand from Radar to CommandPost;
    exch any Retry from CommandPost to Radar;
  end
}

Inspect the syntax tree

tenon flex ast prints the syntax tree Tenon reads from your Flex sources, with any syntax errors beside it. It’s for seeing exactly how Tenon reads a construct, or for feeding your model to another tool, with --json. As with tenon flex check, you give it folders of .flex files and can narrow the output to one of them with --file.

Command: tenon flex ast [options] [<dir>...]

Arguments

<dir>...
The folders of .flex files to read, as one model. You can also give a single file. Optional when you give --file.

Options

--file <file>
Show only this file’s tree. With a folder, Tenon still reads the whole model, so imports resolve.
--json
Print the tree as JSON.

Exit codes

Code Meaning
0 Success
1 An input couldn’t be read, or has syntax errors
Inspect the syntax tree
tenon flex ast spec --file spec/thermal.flextenon flex ast spec --json > ast.json

List elements

tenon flex elements lists every top-level declaration in your Flex sources, such as components, connections, messages and protocols, with where each one is, followed by every annotation use. It’s a quick way to find a name in a large model, or to check what a requirement annotation is attached to.

--filter narrows the list to the kinds you name, and hides annotation uses unless you include @annotation. As with tenon flex check, you give it folders of .flex files and can narrow the output to one of them with --file.

Command: tenon flex elements [options] [<dir>...]

Arguments

<dir>...
The folders of .flex files to list, as one model. You can also give a single file. Optional when you give --file.

Options

--file <file>
List only this file’s elements. With a folder, Tenon still reads the whole model, so imports resolve.
--json
Print the list as JSON.
--filter <kinds>
List only these kinds, separated by commas: annotation, const, enum, function, transform, contract, struct, newtype, variant, type_alias, component, connection, group, system, global_protocol and local_protocol. The two-word kinds can also be written with a space (local protocol), and message struct, extensible struct or abstract struct narrows struct to those structs. Add @annotation to keep annotation uses.

Exit codes

Code Meaning
0 Success
1 An input couldn’t be read or parsed
List elements
tenon flex elements spectenon flex elements spec --filter "component,local protocol"tenon flex elements spec --filter "global protocol,@annotation"

Evaluate an expression

tenon flex eval evaluates one Flex expression and prints its value, which makes it a quick way to check how Flex treats a calculation, such as how a value wraps when you cast it to a smaller type. Quote the expression, so your shell passes it as one argument.

The expression can use the declarations in your own files, too: --load loads a .flex file and imports everything it declares, so you can refer to those names directly. For more than one expression, work interactively instead.

Command: tenon flex eval [options] <expr>

Arguments

<expr>
The Flex expression to evaluate.

Options

--load <file>
Load this .flex file and import all its declarations, so the expression can use them by name. Repeat it to load several files.

Exit codes

Code Meaning
0 Success
1 The expression or a loaded file has errors
Evaluate an expression
tenon flex eval '(200 + 100) : uint8'tenon flex eval --load spec/thermal.flex 'Thermal.T1'

Work interactively

tenon flex, on its own, starts an interactive Flex session. You type expressions and Tenon prints their values, as tenon flex eval does, but you can also enter declarations, such as a const, a struct or a function, and use them in what you type next.

Session commands start with a colon. :load <file> loads a .flex file and imports its declarations, :load-dir <dir> makes every file in a folder known so that you can import from them, and :decls lists what you’ve declared so far. :reset clears the session, :help lists every command, and :quit ends the session.

Command: tenon flex

Work interactively
tenon flex

Read the Flex docs

tenon flex docs prints the Flex language documentation: every construct, its rules, and what the checker rejects. With no arguments it lists the pages, grouped by category. Name a page, such as struct, to print it, or a category, such as protocols, to print every page in it.

The documentation comes with Tenon, so it works offline and needs no license seat. It’s also published at flexlang.org.

Command: tenon flex docs [options] [<page|category>...]

Arguments

<page|category>...
The pages to print, by name, or the categories whose pages you want. Without one, Tenon lists every page.

Options

--all
Print every page, in reading order.
Read the Flex docs
tenon flex docstenon flex docs structtenon flex docs protocols | less