Grammar
This grammar is subject to change before D2 1.0.
File
A D2 file is a map. Write its entries directly at the top level.
EBNF notation
File = MapBody .api.v1: API
db
api.v1 -> db: readsMap
Braces enclose a map of child objects, properties, connections, and comments.
EBNF notation
Map = "{" MapBody "}" .service: {
api
db
api -> db
}MapBody
A map contains zero or more entries separated by newlines, semicolons, or line comments.
EBNF notation
MapBody = Padding [ MapEntry spaces { Boundary Padding MapEntry spaces } [ Boundary Padding ] ] .api; db; api -> dbArray
Square brackets hold values in order. A semicolon or newline separates adjacent items.
EBNF notation
Array = "[" Padding [ ArrayEntry spaces { Boundary Padding ArrayEntry spaces } [ Boundary Padding ] ] "]" .colors: [red; green; blue]Padding
Whitespace, comments, and extra semicolons can appear before an entry.
EBNF notation
Padding = { horizontalSpace | newline | ";" | lineComment | blockComment } .Boundary
A newline, semicolon, or line comment separates adjacent entries.
EBNF notation
Boundary = newline | ";" | lineComment .api; db # Two entriesspaces
Zero or more whitespace characters on the same line.
EBNF notation
spaces = { horizontalSpace } .MapEntry
A map entry declares a key, describes a connection, or spreads an import or substitution.
EBNF notation
MapEntry = Spread | [ Filter spaces ] EntryKey [ spaces Assignment ] .api -> db: readsArrayEntry
An array item holds a value or spreads an import or substitution.
EBNF notation
ArrayEntry = Spread | Value .colors: [red; ...${more}]Filter
An ampersand adds a condition to a glob. Use !& to negate it.
EBNF notation
Filter = "&" | "!&" .api1: preview
api2: stable
api*: { &label: pre*; style.fill: blue }EntryKey
The subject of an entry is a key path, a chain of connections, or a grouped connection.
EBNF notation
EntryKey = keyPath | EdgeChain | [ keyPath spaces "." spaces ] EdgeGroup .(api -> db)[0].style.stroke: bluekeyPath
Dots separate key segments. Quote or escape a dot to keep it in the name.
EBNF notation
keyPath = keyString { spaces "." spaces keyString } .api.v1."read.me"Assignment
A key can take a map directly. A colon introduces a value, and a scalar can have a map of properties on the same line.
EBNF notation
Assignment = Map | ":" spaces ( Scalar [ spaces Map ] | Map | Array | Import ) .api: Public API { shape: cloud }Value
A value can be a scalar, a map, an array, or an import.
EBNF notation
Value = Scalar | Map | Array | Import .theme: @palette.colorsScalar
An unquoted scalar can be text, a number, a boolean, null, suspend, or unsuspend. Quotes keep it a string.
EBNF notation
Scalar = valueString .enabled: "true"EdgeChain
An arrow connects each pair of successive endpoints. Here, both connections share the endpoint api.
EBNF notation
EdgeChain = keyPath spaces FirstArrow EdgeTargets .client -> api -> dbEdgeGroup
Parentheses group a connection so you can select an index, a property, or both.
EBNF notation
EdgeGroup = "(" spaces keyPath spaces Arrow EdgeTargets spaces ")"
[ spaces "[" spaces EdgeIndex spaces "]" ] [ spaces "." spaces keyPath ] .(api -> db)[0].style.stroke: blueEdgeTargets
A chain has a destination and can continue with more arrows and destinations.
EBNF notation
EdgeTargets = spaces keyPath { spaces Arrow spaces keyPath } .a -> b -> c -> dEdgeIndex
An index counts matching connections from zero. An asterisk selects all matches.
EBNF notation
EdgeIndex = "*" | decimalDigit { decimalDigit } .(api -> db)[*].style.stroke: blueFirstArrow
The first arrow in an ungrouped chain starts with a hyphen or a less-than sign.
EBNF notation
FirstArrow = "<" ArrowTail | "-" { EdgeContinuation } ( "-" ArrowTail | ">" | "*" ) .client <- apiArrow
An arrow inside a group or after the first link in a chain. Common forms are --, ->, <-, and <->.
EBNF notation
Arrow = FirstArrow | "*" ArrowTail .client -> api <-> dbArrowTail
Hyphens extend an arrow’s shaft. A greater-than sign or asterisk can mark its end.
EBNF notation
ArrowTail = { "-" | EdgeContinuation } [ ">" | "*" ] .api ---> dbEdgeContinuation
A backslash continues the shaft of an arrow onto the next line.
EBNF notation
EdgeContinuation = "\\" spaces newline spaces .api --\
> dbSpread
Three dots directly before an import or substitution expand its contents into the surrounding map or array.
EBNF notation
Spread = "..." ( Import | substitution ) ....@themeImport
An at sign introduces a file path. Further key segments select a value from that file.
EBNF notation
Import = "@" { "." | "/" } spaces keyPath .colors: @../theme.d2.colorssubstitution
A dollar sign and braces insert a variable by key path into an unquoted or double-quoted value.
EBNF notation
substitution = "$" spaces "{" spaces keyPath spaces "}" .label: "Hello ${user.name}"sourceChar
One Unicode scalar value from the decoded source file.
EBNF notation
sourceChar = "\u0000" … "\uD7FF" | "\uE000" … "\U0010FFFF" .nonLF
Any Unicode scalar value except a line feed.
EBNF notation
nonLF = "\u0000" … "\t" | "\v" … "\uD7FF" | "\uE000" … "\U0010FFFF" .newline
A line feed (U+000A) separates lines. A CRLF pair contains one line feed.
EBNF notation
newline = "\n" .horizontalSpace
Any Unicode whitespace character except line feed, including a space, tab, or carriage return.
EBNF notation
horizontalSpace = "\t" | "\v" … "\r" | " " | "\u0085" | "\u00A0" | "\u1680"
| "\u2000" … "\u200A" | "\u2028" | "\u2029" | "\u202F"
| "\u205F" | "\u3000" .decimalDigit
One ASCII digit from 0 to 9.
EBNF notation
decimalDigit = "0" … "9" .(api -> db)[2]lineComment
A hash begins a comment that runs to the end of the line.
EBNF notation
lineComment = "#" { nonLF } .api -> db # readsblockComment
At the start of an entry, three double quotes open a comment. The next triple closes it.
EBNF notation
blockComment = "\"\"\"" { [ "\"" [ "\"" ] ] nonQuote } "\"\"\"" .""" Notes about this diagram """
apinonQuote
Any Unicode scalar value except a double quote.
EBNF notation
nonQuote = "\u0000" … "!" | "#" … "\uD7FF" | "\uE000" … "\U0010FFFF" .keyString
One key segment can be bare text, a single-quoted or double-quoted string, or a block string.
EBNF notation
keyString = singleQuotedString | doubleQuotedKey | blockString | unquotedKey ."api.v1": ServicevalueString
A scalar can be written as bare text, a single-quoted or double-quoted string, or a block string.
EBNF notation
valueString = singleQuotedString | doubleQuotedValue | blockString | unquotedValue .api: serves v1.0unquotedKey
Spaces inside a bare key stay in the name. An unescaped dot separates segments, and an unescaped star marks a wildcard.
EBNF notation
unquotedKey = keyStart { keyUnit } .edge service.v1keyStart
The first character, escape, or line continuation of a bare key.
EBNF notation
keyStart = commonBareStart | "$" | "*" | "-" | escape | continuedLine .keyUnit
A character, escape, or line continuation inside a bare key. An interior quote can be literal.
EBNF notation
keyUnit = commonBareChar | "$" | "*" | "-" | escape | continuedLine .unquotedValue
Dots, colons, and arrows are literal in a bare value. A dollar sign starts a substitution, and a star can match labels in a filter.
EBNF notation
unquotedValue = valueStart { valueUnit } .caption: pre*
api*: { &label: pre*; style.fill: blue }valueStart
The first part of a bare value can be a character, substitution, escape, or line continuation.
EBNF notation
valueStart = commonBareStart | "(" | "&" | "*" | "-" | "." | ":" | "<" | ">"
| substitution | escape | continuedLine .valueUnit
One more part of a bare value. A star can act as a wildcard in a filter.
EBNF notation
valueUnit = commonBareChar | "&" | "*" | "-" | "." | ":" | "<" | ">"
| substitution | escape | continuedLine .commonBareChar
Characters shared by the interiors of bare keys and values, including spaces and quotes.
EBNF notation
commonBareChar = commonBareStart | horizontalSpace | "\"" | "'" | "(" | "@" | "|" .commonBareStart
Characters that can start both a bare key and a bare value.
EBNF notation
commonBareStart = "\u0000" … "\b" | "\u000E" … "\u001F" | "!" | "%" | ")"
| "+" … "," | "/" … "9" | "=" | "?" | "A" … "Z" | "^" … "z"
| "~" … "\u007F" | nonASCIINonSpace .nonASCIINonSpace
Any non-ASCII Unicode scalar value except whitespace.
EBNF notation
nonASCIINonSpace = "\u0080" … "\u0084" | "\u0086" … "\u009F" | "\u00A1" … "\u167F"
| "\u1681" … "\u1FFF" | "\u200B" … "\u2027" | "\u202A" … "\u202E"
| "\u2030" … "\u205E" | "\u2060" … "\u2FFF" | "\u3001" … "\uD7FF"
| "\uE000" … "\U0010FFFF" .escape
A backslash escapes the next character. An escaped dot stays in the same key segment.
EBNF notation
escape = "\\" nonLF .api\.v1: ServicecontinuedLine
In bare text, a backslash immediately followed by a line feed removes the break and the next line’s indentation.
EBNF notation
continuedLine = "\\" newline { horizontalSpace } .api: first \
secondsingleQuotedString
Single quotes keep dollar signs and backslashes literal, except for a backslash before a line feed. Double an apostrophe to write one.
EBNF notation
singleQuotedString = "'" { singleChar | "''" | "\\" newline } "'" .owner: 'Ada''s ${service}'singleChar
Any Unicode scalar value except an apostrophe or a line feed.
EBNF notation
singleChar = "\u0000" … "\t" | "\v" … "&" | "(" … "\uD7FF"
| "\uE000" … "\U0010FFFF" .doubleQuotedKey
Double quotes keep punctuation in one key segment. Dollar signs and braces stay literal.
EBNF notation
doubleQuotedKey = "\"" { doubleChar | "$" | escape | "\\" newline } "\"" ."api.v1": ServicedoubleQuotedValue
Double quotes preserve spaces and allow escapes and substitutions.
EBNF notation
doubleQuotedValue = "\"" { doubleChar | substitution | escape | "\\" newline } "\"" .api: "Hello ${name}"doubleChar
An ordinary character inside double quotes. Quotes, backslashes, dollar signs, and line feeds have separate rules.
EBNF notation
doubleChar = "\u0000" … "\t" | "\v" … "!" | "#" | "%" … "["
| "]" … "\uD7FF" | "\uE000" … "\U0010FFFF" .blockString
A pipe opens a block of literal text. An optional fence sets its closing delimiter, and a tag identifies the content.
EBNF notation
blockString = "|" { fenceChar } [ blockTag ] ( horizontalSpace | newline )
{ sourceChar } { fenceChar } "|" .code: |||ts
const value = a || b
|||fenceChar
A fence character can follow the opening pipe. Whitespace, letters, decimal digits, and underscores end the fence.
EBNF notation
fenceChar = "\u0000" … "\b" | "\u000E" … "\u001F" | "!" … "/" | ":" … "@"
| "[" … "^" | "\u0060" | "{" … "\u007F" | nonASCIIFenceChar .nonASCIIFenceChar
Any non-ASCII Unicode scalar value except whitespace, letters, and decimal digits.
EBNF notation
nonASCIIFenceChar = nonASCIINonSpace .blockTag
A tag identifies the contents of a block and ends at the first whitespace character.
EBNF notation
blockTag = tagStart { tagChar } .label: |md
**Public API**
|tagStart
A block tag starts with a Unicode letter, a decimal digit, or an underscore.
EBNF notation
tagStart = "A" … "Z" | "a" … "z" | decimalDigit | "_" | nonASCIITagStart .nonASCIITagStart
Any non-ASCII Unicode letter or decimal digit.
EBNF notation
nonASCIITagStart = nonASCIINonSpace .tagChar
After the first character, a block tag accepts any non-whitespace character.
EBNF notation
tagChar = "\u0000" … "\b" | "\u000E" … "\u001F" | "!" … "\u007F" | nonASCIINonSpace .Full grammar
In Extended Backus–Naur Form (EBNF), read | as or, [ ] as optional, and { } as zero or more.
// A file is an implicit map. Braces delimit nested maps.
File = MapBody .
Map = "{" MapBody "}" .
MapBody = Padding [ MapEntry spaces { Boundary Padding MapEntry spaces } [ Boundary Padding ] ] .
Array = "[" Padding [ ArrayEntry spaces { Boundary Padding ArrayEntry spaces } [ Boundary Padding ] ] "]" .
// A line comment includes the remainder of its line. A block comment can
// precede an entry without a separator. After an entry it needs a boundary.
Padding = { horizontalSpace | newline | ";" | lineComment | blockComment } .
Boundary = newline | ";" | lineComment .
spaces = { horizontalSpace } .
MapEntry = Spread | [ Filter spaces ] EntryKey [ spaces Assignment ] .
ArrayEntry = Spread | Value .
Filter = "&" | "!&" .
EntryKey = keyPath | EdgeChain | [ keyPath spaces "." spaces ] EdgeGroup .
keyPath = keyString { spaces "." spaces keyString } .
Assignment = Map | ":" spaces ( Scalar [ spaces Map ] | Map | Array | Import ) .
Value = Scalar | Map | Array | Import .
Scalar = valueString .
// Inside an EdgeGroup, ")" ends an unquoted endpoint when followed, after any
// horizontal space, by LF, end of input, or one of ";#{}[]:.".
EdgeChain = keyPath spaces FirstArrow EdgeTargets .
EdgeGroup = "(" spaces keyPath spaces Arrow EdgeTargets spaces ")"
[ spaces "[" spaces EdgeIndex spaces "]" ] [ spaces "." spaces keyPath ] .
EdgeTargets = spaces keyPath { spaces Arrow spaces keyPath } .
// Numeric indices must fit the reference implementation's nonnegative int range.
EdgeIndex = "*" | decimalDigit { decimalDigit } .
FirstArrow = "<" ArrowTail | "-" { EdgeContinuation } ( "-" ArrowTail | ">" | "*" ) .
Arrow = FirstArrow | "*" ArrowTail .
// Consume the full shaft before reading the destination mark or key.
ArrowTail = { "-" | EdgeContinuation } [ ">" | "*" ] .
EdgeContinuation = "\\" spaces newline spaces .
// At entry start, "...@" and "...$" introduce a spread.
Spread = "..." ( Import | substitution ) .
// Consume the longest run of dots and slashes after "@" before reading keyPath.
Import = "@" { "." | "/" } spaces keyPath .
substitution = "$" spaces "{" spaces keyPath spaces "}" .
// Productions match decoded Unicode scalar values. Whitespace is explicit.
sourceChar = "\u0000" … "\uD7FF" | "\uE000" … "\U0010FFFF" .
nonLF = "\u0000" … "\t" | "\v" … "\uD7FF" | "\uE000" … "\U0010FFFF" .
newline = "\n" .
horizontalSpace = "\t" | "\v" … "\r" | " " | "\u0085" | "\u00A0" | "\u1680"
| "\u2000" … "\u200A" | "\u2028" | "\u2029" | "\u202F"
| "\u205F" | "\u3000" .
decimalDigit = "0" … "9" .
// Consume line comments through the last character before LF or EOF.
lineComment = "#" { nonLF } .
// At entry start, three quotes begin a block comment. The next triple closes it.
blockComment = "\"\"\"" { [ "\"" [ "\"" ] ] nonQuote } "\"\"\"" .
nonQuote = "\u0000" … "!" | "#" … "\uD7FF" | "\uE000" … "\U0010FFFF" .
keyString = singleQuotedString | doubleQuotedKey | blockString | unquotedKey .
valueString = singleQuotedString | doubleQuotedValue | blockString | unquotedValue .
// Scan the longest bare string, trimming trailing whitespace from its final text
// segment. It must yield text or a substitution. An unescaped "--", "->" or "-*"
// ends a bare key before the hyphen. A decoded bare key cannot start with "@".
unquotedKey = keyStart { keyUnit } .
keyStart = commonBareStart | "$" | "*" | "-" | escape | continuedLine .
keyUnit = commonBareChar | "$" | "*" | "-" | escape | continuedLine .
// The raw prefix "...@" is reserved for a spread and cannot start a bare value.
unquotedValue = valueStart { valueUnit } .
valueStart = commonBareStart | "(" | "&" | "*" | "-" | "." | ":" | "<" | ">"
| substitution | escape | continuedLine .
valueUnit = commonBareChar | "&" | "*" | "-" | "." | ":" | "<" | ">"
| substitution | escape | continuedLine .
// Space, quotes, "(", "@", and "|" may occur within a bare string. At the
// beginning they either introduce another construct or depend on key/value mode.
commonBareChar = commonBareStart | horizontalSpace | "\"" | "'" | "(" | "@" | "|" .
commonBareStart = "\u0000" … "\b" | "\u000E" … "\u001F" | "!" | "%" | ")"
| "+" … "," | "/" … "9" | "=" | "?" | "A" … "Z" | "^" … "z"
| "~" … "\u007F" | nonASCIINonSpace .
// U+0080 and higher, excluding Unicode whitespace and surrogate code points.
nonASCIINonSpace = "\u0080" … "\u0084" | "\u0086" … "\u009F" | "\u00A1" … "\u167F"
| "\u1681" … "\u1FFF" | "\u200B" … "\u2027" | "\u202A" … "\u202E"
| "\u2030" … "\u205E" | "\u2060" … "\u2FFF" | "\u3001" … "\uD7FF"
| "\uE000" … "\U0010FFFF" .
escape = "\\" nonLF .
continuedLine = "\\" newline { horizontalSpace } .
singleQuotedString = "'" { singleChar | "''" | "\\" newline } "'" .
singleChar = "\u0000" … "\t" | "\v" … "&" | "(" … "\uD7FF"
| "\uE000" … "\U0010FFFF" .
doubleQuotedKey = "\"" { doubleChar | "$" | escape | "\\" newline } "\"" .
doubleQuotedValue = "\"" { doubleChar | substitution | escape | "\\" newline } "\"" .
doubleChar = "\u0000" … "\t" | "\v" … "!" | "#" | "%" … "["
| "]" … "\uD7FF" | "\uE000" … "\U0010FFFF" .
// Capture the longest opening fence. The first copy of that fence followed
// immediately by "|" closes the block, even mid-line.
blockString = "|" { fenceChar } [ blockTag ] ( horizontalSpace | newline )
{ sourceChar } { fenceChar } "|" .
fenceChar = "\u0000" … "\b" | "\u000E" … "\u001F" | "!" … "/" | ":" … "@"
| "[" … "^" | "\u0060" | "{" … "\u007F" | nonASCIIFenceChar .
nonASCIIFenceChar = nonASCIINonSpace . // Excludes Unicode letters and decimal digits.
blockTag = tagStart { tagChar } .
tagStart = "A" … "Z" | "a" … "z" | decimalDigit | "_" | nonASCIITagStart .
nonASCIITagStart = nonASCIINonSpace . // A Unicode letter or decimal digit.
tagChar = "\u0000" … "\b" | "\u000E" … "\u001F" | "!" … "\u007F" | nonASCIINonSpace .