Skip to main content

Grammar

note

This grammar is subject to change before D2 1.0.

Structural rule

File

A D2 file is a map. Write its entries directly at the top level.

FileFile = MapBody .MapBody
Literal textAnother rule
EBNF notation
File     = MapBody .
D2 example
api.v1: API
db
api.v1 -> db: reads
Structural rule

Map

Braces enclose a map of child objects, properties, connections, and comments.

MapMap = "{" MapBody "}" ."{"MapBody"}"
Literal textAnother rule
EBNF notation
Map      = "{" MapBody "}" .
D2 example
service: {
  api
  db
  api -> db
}
Structural rule

MapBody

A map contains zero or more entries separated by newlines, semicolons, or line comments.

MapBodyMapBody = Padding [ MapEntry spaces { Boundary Padding MapEntry spaces } [ Boundary Padding ] ] .PaddingMapEntryspacesBoundaryPaddingMapEntryspacesBoundaryPadding
Literal textAnother rule
EBNF notation
MapBody  = Padding [ MapEntry spaces { Boundary Padding MapEntry spaces } [ Boundary Padding ] ] .
D2 example
api; db; api -> db
Structural rule

Array

Square brackets hold values in order. A semicolon or newline separates adjacent items.

ArrayArray = "[" Padding [ ArrayEntry spaces { Boundary Padding ArrayEntry spaces } [ Boundary Padding ] ] "]" ."["PaddingArrayEntryspacesBoundaryPaddingArrayEntryspacesBoundaryPadding"]"
Literal textAnother rule
EBNF notation
Array    = "[" Padding [ ArrayEntry spaces { Boundary Padding ArrayEntry spaces } [ Boundary Padding ] ] "]" .
D2 example
colors: [red; green; blue]
Structural rule

Padding

Whitespace, comments, and extra semicolons can appear before an entry.

PaddingPadding = { horizontalSpace | newline | ";" | lineComment | blockComment } .horizontalSpacenewline";"lineCommentblockComment
Literal textAnother rule
EBNF notation
Padding  = { horizontalSpace | newline | ";" | lineComment | blockComment } .
Structural rule

Boundary

A newline, semicolon, or line comment separates adjacent entries.

BoundaryBoundary = newline | ";" | lineComment .newline";"lineComment
Literal textAnother rule
EBNF notation
Boundary = newline | ";" | lineComment .
D2 example
api; db # Two entries
Structural rule

spaces

Zero or more whitespace characters on the same line.

spacesspaces = { horizontalSpace } .horizontalSpace
Literal textAnother rule
EBNF notation
spaces   = { horizontalSpace } .
Structural rule

MapEntry

A map entry declares a key, describes a connection, or spreads an import or substitution.

MapEntryMapEntry = Spread | [ Filter spaces ] EntryKey [ spaces Assignment ] .SpreadFilterspacesEntryKeyspacesAssignment
Literal textAnother rule
EBNF notation
MapEntry   = Spread | [ Filter spaces ] EntryKey [ spaces Assignment ] .
D2 example
api -> db: reads
Structural rule

ArrayEntry

An array item holds a value or spreads an import or substitution.

ArrayEntryArrayEntry = Spread | Value .SpreadValue
Literal textAnother rule
EBNF notation
ArrayEntry = Spread | Value .
D2 example
colors: [red; ...${more}]
Structural rule

Filter

An ampersand adds a condition to a glob. Use !& to negate it.

FilterFilter = "&" | "!&" ."&""!&"
Literal textAnother rule
EBNF notation
Filter     = "&" | "!&" .
D2 example
api1: preview
api2: stable
api*: { &label: pre*; style.fill: blue }
Structural rule

EntryKey

The subject of an entry is a key path, a chain of connections, or a grouped connection.

EntryKeyEntryKey = keyPath | EdgeChain | [ keyPath spaces "." spaces ] EdgeGroup .keyPathEdgeChainkeyPathspaces"."spacesEdgeGroup
Literal textAnother rule
EBNF notation
EntryKey    = keyPath | EdgeChain | [ keyPath spaces "." spaces ] EdgeGroup .
D2 example
(api -> db)[0].style.stroke: blue
Structural rule

keyPath

Dots separate key segments. Quote or escape a dot to keep it in the name.

keyPathkeyPath = keyString { spaces "." spaces keyString } .keyStringspaces"."spaceskeyString
Literal textAnother rule
EBNF notation
keyPath     = keyString { spaces "." spaces keyString } .
D2 example
api.v1."read.me"
Structural rule

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.

AssignmentAssignment = Map | ":" spaces ( Scalar [ spaces Map ] | Map | Array | Import ) .Map":"spacesScalarspacesMapMapArrayImport
Literal textAnother rule
EBNF notation
Assignment  = Map | ":" spaces ( Scalar [ spaces Map ] | Map | Array | Import ) .
D2 example
api: Public API { shape: cloud }
Structural rule

Value

A value can be a scalar, a map, an array, or an import.

ValueValue = Scalar | Map | Array | Import .ScalarMapArrayImport
Literal textAnother rule
EBNF notation
Value       = Scalar | Map | Array | Import .
D2 example
theme: @palette.colors
Structural rule

Scalar

An unquoted scalar can be text, a number, a boolean, null, suspend, or unsuspend. Quotes keep it a string.

ScalarScalar = valueString .valueString
Literal textAnother rule
EBNF notation
Scalar      = valueString .
D2 example
enabled: "true"
Structural rule

EdgeChain

An arrow connects each pair of successive endpoints. Here, both connections share the endpoint api.

EdgeChainEdgeChain = keyPath spaces FirstArrow EdgeTargets .keyPathspacesFirstArrowEdgeTargets
Literal textAnother rule
EBNF notation
EdgeChain   = keyPath spaces FirstArrow EdgeTargets .
D2 example
client -> api -> db
Structural rule

EdgeGroup

Parentheses group a connection so you can select an index, a property, or both.

EdgeGroupEdgeGroup = "(" spaces keyPath spaces Arrow EdgeTargets spaces ")" [ spaces "[" spaces EdgeIndex spaces "]" ] [ spaces "." spaces keyPath ] ."("spaceskeyPathspacesArrowEdgeTargetsspaces")"spaces"["spacesEdgeIndexspaces"]"spaces"."spaceskeyPath
Literal textAnother rule
EBNF notation
EdgeGroup   = "(" spaces keyPath spaces Arrow EdgeTargets spaces ")"
              [ spaces "[" spaces EdgeIndex spaces "]" ] [ spaces "." spaces keyPath ] .
D2 example
(api -> db)[0].style.stroke: blue
Structural rule

EdgeTargets

A chain has a destination and can continue with more arrows and destinations.

EdgeTargetsEdgeTargets = spaces keyPath { spaces Arrow spaces keyPath } .spaceskeyPathspacesArrowspaceskeyPath
Literal textAnother rule
EBNF notation
EdgeTargets = spaces keyPath { spaces Arrow spaces keyPath } .
D2 example
a -> b -> c -> d
Structural rule

EdgeIndex

An index counts matching connections from zero. An asterisk selects all matches.

EdgeIndexEdgeIndex = "*" | decimalDigit { decimalDigit } ."*"decimalDigitdecimalDigit
Literal textAnother rule
EBNF notation
EdgeIndex   = "*" | decimalDigit { decimalDigit } .
D2 example
(api -> db)[*].style.stroke: blue
Structural rule

FirstArrow

The first arrow in an ungrouped chain starts with a hyphen or a less-than sign.

FirstArrowFirstArrow = "<" ArrowTail | "-" { EdgeContinuation } ( "-" ArrowTail | ">" | "*" ) ."<"ArrowTail"-"EdgeContinuation"-"ArrowTail">""*"
Literal textAnother rule
EBNF notation
FirstArrow       = "<" ArrowTail | "-" { EdgeContinuation } ( "-" ArrowTail | ">" | "*" ) .
D2 example
client <- api
Structural rule

Arrow

An arrow inside a group or after the first link in a chain. Common forms are --, ->, <-, and <->.

ArrowArrow = FirstArrow | "*" ArrowTail .FirstArrow"*"ArrowTail
Literal textAnother rule
EBNF notation
Arrow            = FirstArrow | "*" ArrowTail .
D2 example
client -> api <-> db
Structural rule

ArrowTail

Hyphens extend an arrow’s shaft. A greater-than sign or asterisk can mark its end.

ArrowTailArrowTail = { "-" | EdgeContinuation } [ ">" | "*" ] ."-"EdgeContinuation">""*"
Literal textAnother rule
EBNF notation
ArrowTail        = { "-" | EdgeContinuation } [ ">" | "*" ] .
D2 example
api ---> db
Structural rule

EdgeContinuation

A backslash continues the shaft of an arrow onto the next line.

EdgeContinuationEdgeContinuation = "\\" spaces newline spaces ."\\"spacesnewlinespaces
Literal textAnother rule
EBNF notation
EdgeContinuation = "\\" spaces newline spaces .
D2 example
api --\
  > db
Structural rule

Spread

Three dots directly before an import or substitution expand its contents into the surrounding map or array.

SpreadSpread = "..." ( Import | substitution ) ."..."Importsubstitution
Literal textAnother rule
EBNF notation
Spread       = "..." ( Import | substitution ) .
D2 example
...@theme
Structural rule

Import

An at sign introduces a file path. Further key segments select a value from that file.

ImportImport = "@" { "." | "/" } spaces keyPath ."@"".""/"spaceskeyPath
Literal textAnother rule
EBNF notation
Import       = "@" { "." | "/" } spaces keyPath .
D2 example
colors: @../theme.d2.colors
Structural rule

substitution

A dollar sign and braces insert a variable by key path into an unquoted or double-quoted value.

substitutionsubstitution = "$" spaces "{" spaces keyPath spaces "}" ."$"spaces"{"spaceskeyPathspaces"}"
Literal textAnother rule
EBNF notation
substitution = "$" spaces "{" spaces keyPath spaces "}" .
D2 example
label: "Hello ${user.name}"
Lexical rule

sourceChar

One Unicode scalar value from the decoded source file.

sourceCharsourceChar = "\u0000" … "\uD7FF" | "\uE000" … "\U0010FFFF" .U+0000 … U+D7FFU+E000 … U+10FFFF
Literal textAnother ruleCharacter range
EBNF notation
sourceChar = "\u0000" … "\uD7FF" | "\uE000" … "\U0010FFFF" .
Lexical rule

nonLF

Any Unicode scalar value except a line feed.

nonLFnonLF = "\u0000" … "\t" | "\v" … "\uD7FF" | "\uE000" … "\U0010FFFF" .U+0000 … "\t""\v" … U+D7FFU+E000 … U+10FFFF
Literal textAnother ruleCharacter range
EBNF notation
nonLF = "\u0000" … "\t" | "\v" … "\uD7FF" | "\uE000" … "\U0010FFFF" .
Lexical rule

newline

A line feed (U+000A) separates lines. A CRLF pair contains one line feed.

newlinenewline = "\n" ."\n"
Literal textAnother rule
EBNF notation
newline = "\n" .
Lexical rule

horizontalSpace

Any Unicode whitespace character except line feed, including a space, tab, or carriage return.

horizontalSpacehorizontalSpace = "\t" | "\v" … "\r" | " " | "\u0085" | "\u00A0" | "\u1680" | "\u2000" … "\u200A" | "\u2028" | "\u2029" | "\u202F" | "\u205F" | "\u3000" ."\t""\v" … "\r"" ""\u0085""\u00A0""\u1680"U+2000 … U+200A"\u2028""\u2029""\u202F""\u205F""\u3000"
Literal textAnother ruleCharacter range
EBNF notation
horizontalSpace = "\t" | "\v" … "\r" | " " | "\u0085" | "\u00A0" | "\u1680"
                | "\u2000" … "\u200A" | "\u2028" | "\u2029" | "\u202F"
                | "\u205F" | "\u3000" .
Lexical rule

decimalDigit

One ASCII digit from 0 to 9.

decimalDigitdecimalDigit = "0" … "9" ."0" … "9"
Literal textAnother ruleCharacter range
EBNF notation
decimalDigit = "0" … "9" .
D2 example
(api -> db)[2]
Lexical rule

lineComment

A hash begins a comment that runs to the end of the line.

lineCommentlineComment = "#" { nonLF } ."#"nonLF
Literal textAnother rule
EBNF notation
lineComment = "#" { nonLF } .
D2 example
api -> db # reads
Lexical rule

blockComment

At the start of an entry, three double quotes open a comment. The next triple closes it.

blockCommentblockComment = "\"\"\"" { [ "\"" [ "\"" ] ] nonQuote } "\"\"\"" ."\"\"\"""\"""\""nonQuote"\"\"\""
Literal textAnother rule
EBNF notation
blockComment = "\"\"\"" { [ "\"" [ "\"" ] ] nonQuote } "\"\"\"" .
D2 example
""" Notes about this diagram """
api
Lexical rule

nonQuote

Any Unicode scalar value except a double quote.

nonQuotenonQuote = "\u0000" … "!" | "#" … "\uD7FF" | "\uE000" … "\U0010FFFF" .U+0000 … "!""#" … U+D7FFU+E000 … U+10FFFF
Literal textAnother ruleCharacter range
EBNF notation
nonQuote = "\u0000" … "!" | "#" … "\uD7FF" | "\uE000" … "\U0010FFFF" .
Lexical rule

keyString

One key segment can be bare text, a single-quoted or double-quoted string, or a block string.

keyStringkeyString = singleQuotedString | doubleQuotedKey | blockString | unquotedKey .singleQuotedStringdoubleQuotedKeyblockStringunquotedKey
Literal textAnother rule
EBNF notation
keyString = singleQuotedString | doubleQuotedKey | blockString | unquotedKey .
D2 example
"api.v1": Service
Lexical rule

valueString

A scalar can be written as bare text, a single-quoted or double-quoted string, or a block string.

valueStringvalueString = singleQuotedString | doubleQuotedValue | blockString | unquotedValue .singleQuotedStringdoubleQuotedValueblockStringunquotedValue
Literal textAnother rule
EBNF notation
valueString = singleQuotedString | doubleQuotedValue | blockString | unquotedValue .
D2 example
api: serves v1.0
Lexical rule

unquotedKey

Spaces inside a bare key stay in the name. An unescaped dot separates segments, and an unescaped star marks a wildcard.

unquotedKeyunquotedKey = keyStart { keyUnit } .keyStartkeyUnit
Literal textAnother rule
EBNF notation
unquotedKey = keyStart { keyUnit } .
D2 example
edge service.v1
Lexical rule

keyStart

The first character, escape, or line continuation of a bare key.

keyStartkeyStart = commonBareStart | "$" | "*" | "-" | escape | continuedLine .commonBareStart"$""*""-"escapecontinuedLine
Literal textAnother rule
EBNF notation
keyStart = commonBareStart | "$" | "*" | "-" | escape | continuedLine .
Lexical rule

keyUnit

A character, escape, or line continuation inside a bare key. An interior quote can be literal.

keyUnitkeyUnit = commonBareChar | "$" | "*" | "-" | escape | continuedLine .commonBareChar"$""*""-"escapecontinuedLine
Literal textAnother rule
EBNF notation
keyUnit = commonBareChar | "$" | "*" | "-" | escape | continuedLine .
Lexical rule

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.

unquotedValueunquotedValue = valueStart { valueUnit } .valueStartvalueUnit
Literal textAnother rule
EBNF notation
unquotedValue = valueStart { valueUnit } .
D2 example
caption: pre*
api*: { &label: pre*; style.fill: blue }
Lexical rule

valueStart

The first part of a bare value can be a character, substitution, escape, or line continuation.

valueStartvalueStart = commonBareStart | "(" | "&" | "*" | "-" | "." | ":" | "<" | ">" | substitution | escape | continuedLine .commonBareStart"(""&""*""-""."":""<"">"substitutionescapecontinuedLine
Literal textAnother rule
EBNF notation
valueStart = commonBareStart | "(" | "&" | "*" | "-" | "." | ":" | "<" | ">"
           | substitution | escape | continuedLine .
Lexical rule

valueUnit

One more part of a bare value. A star can act as a wildcard in a filter.

valueUnitvalueUnit = commonBareChar | "&" | "*" | "-" | "." | ":" | "<" | ">" | substitution | escape | continuedLine .commonBareChar"&""*""-""."":""<"">"substitutionescapecontinuedLine
Literal textAnother rule
EBNF notation
valueUnit = commonBareChar | "&" | "*" | "-" | "." | ":" | "<" | ">"
          | substitution | escape | continuedLine .
Lexical rule

commonBareChar

Characters shared by the interiors of bare keys and values, including spaces and quotes.

commonBareCharcommonBareChar = commonBareStart | horizontalSpace | "\"" | "'" | "(" | "@" | "|" .commonBareStarthorizontalSpace"\"""'""(""@""|"
Literal textAnother rule
EBNF notation
commonBareChar = commonBareStart | horizontalSpace | "\"" | "'" | "(" | "@" | "|" .
Lexical rule

commonBareStart

Characters that can start both a bare key and a bare value.

commonBareStartcommonBareStart = "\u0000" … "\b" | "\u000E" … "\u001F" | "!" | "%" | ")" | "+" … "," | "/" … "9" | "=" | "?" | "A" … "Z" | "^" … "z" | "~" … "\u007F" | nonASCIINonSpace .U+0000 … "\b"U+000E … U+001F"!""%"")""+" … ",""/" … "9""=""?""A" … "Z""^" … "z""~" … U+007FnonASCIINonSpace
Literal textAnother ruleCharacter range
EBNF notation
commonBareStart = "\u0000" … "\b" | "\u000E" … "\u001F" | "!" | "%" | ")"
                | "+" … "," | "/" … "9" | "=" | "?" | "A" … "Z" | "^" … "z"
                | "~" … "\u007F" | nonASCIINonSpace .
Lexical rule

nonASCIINonSpace

Any non-ASCII Unicode scalar value except whitespace.

nonASCIINonSpacenonASCIINonSpace = "\u0080" … "\u0084" | "\u0086" … "\u009F" | "\u00A1" … "\u167F" | "\u1681" … "\u1FFF" | "\u200B" … "\u2027" | "\u202A" … "\u202E" | "\u2030" … "\u205E" | "\u2060" … "\u2FFF" | "\u3001" … "\uD7FF" | "\uE000" … "\U0010FFFF" .U+0080 … U+0084U+0086 … U+009FU+00A1 … U+167FU+1681 … U+1FFFU+200B … U+2027U+202A … U+202EU+2030 … U+205EU+2060 … U+2FFFU+3001 … U+D7FFU+E000 … U+10FFFF
Literal textAnother ruleCharacter range
EBNF notation
nonASCIINonSpace = "\u0080" … "\u0084" | "\u0086" … "\u009F" | "\u00A1" … "\u167F"
                 | "\u1681" … "\u1FFF" | "\u200B" … "\u2027" | "\u202A" … "\u202E"
                 | "\u2030" … "\u205E" | "\u2060" … "\u2FFF" | "\u3001" … "\uD7FF"
                 | "\uE000" … "\U0010FFFF" .
Lexical rule

escape

A backslash escapes the next character. An escaped dot stays in the same key segment.

escapeescape = "\\" nonLF ."\\"nonLF
Literal textAnother rule
EBNF notation
escape = "\\" nonLF .
D2 example
api\.v1: Service
Lexical rule

continuedLine

In bare text, a backslash immediately followed by a line feed removes the break and the next line’s indentation.

continuedLinecontinuedLine = "\\" newline { horizontalSpace } ."\\"newlinehorizontalSpace
Literal textAnother rule
EBNF notation
continuedLine = "\\" newline { horizontalSpace } .
D2 example
api: first \
  second
Lexical rule

singleQuotedString

Single quotes keep dollar signs and backslashes literal, except for a backslash before a line feed. Double an apostrophe to write one.

singleQuotedStringsingleQuotedString = "'" { singleChar | "''" | "\\" newline } "'" ."'"singleChar"''""\\"newline"'"
Literal textAnother rule
EBNF notation
singleQuotedString = "'" { singleChar | "''" | "\\" newline } "'" .
D2 example
owner: 'Ada''s ${service}'
Lexical rule

singleChar

Any Unicode scalar value except an apostrophe or a line feed.

singleCharsingleChar = "\u0000" … "\t" | "\v" … "&" | "(" … "\uD7FF" | "\uE000" … "\U0010FFFF" .U+0000 … "\t""\v" … "&""(" … U+D7FFU+E000 … U+10FFFF
Literal textAnother ruleCharacter range
EBNF notation
singleChar = "\u0000" … "\t" | "\v" … "&" | "(" … "\uD7FF"
           | "\uE000" … "\U0010FFFF" .
Lexical rule

doubleQuotedKey

Double quotes keep punctuation in one key segment. Dollar signs and braces stay literal.

doubleQuotedKeydoubleQuotedKey = "\"" { doubleChar | "$" | escape | "\\" newline } "\"" ."\""doubleChar"$"escape"\\"newline"\""
Literal textAnother rule
EBNF notation
doubleQuotedKey = "\"" { doubleChar | "$" | escape | "\\" newline } "\"" .
D2 example
"api.v1": Service
Lexical rule

doubleQuotedValue

Double quotes preserve spaces and allow escapes and substitutions.

doubleQuotedValuedoubleQuotedValue = "\"" { doubleChar | substitution | escape | "\\" newline } "\"" ."\""doubleCharsubstitutionescape"\\"newline"\""
Literal textAnother rule
EBNF notation
doubleQuotedValue = "\"" { doubleChar | substitution | escape | "\\" newline } "\"" .
D2 example
api: "Hello ${name}"
Lexical rule

doubleChar

An ordinary character inside double quotes. Quotes, backslashes, dollar signs, and line feeds have separate rules.

doubleChardoubleChar = "\u0000" … "\t" | "\v" … "!" | "#" | "%" … "[" | "]" … "\uD7FF" | "\uE000" … "\U0010FFFF" .U+0000 … "\t""\v" … "!""#""%" … "[""]" … U+D7FFU+E000 … U+10FFFF
Literal textAnother ruleCharacter range
EBNF notation
doubleChar = "\u0000" … "\t" | "\v" … "!" | "#" | "%" … "["
           | "]" … "\uD7FF" | "\uE000" … "\U0010FFFF" .
Lexical rule

blockString

A pipe opens a block of literal text. An optional fence sets its closing delimiter, and a tag identifies the content.

blockStringblockString = "|" { fenceChar } [ blockTag ] ( horizontalSpace | newline ) { sourceChar } { fenceChar } "|" ."|"fenceCharblockTaghorizontalSpacenewlinesourceCharfenceChar"|"
Literal textAnother rule
EBNF notation
blockString = "|" { fenceChar } [ blockTag ] ( horizontalSpace | newline )
              { sourceChar } { fenceChar } "|" .
D2 example
code: |||ts
  const value = a || b
|||
Lexical rule

fenceChar

A fence character can follow the opening pipe. Whitespace, letters, decimal digits, and underscores end the fence.

fenceCharfenceChar = "\u0000" … "\b" | "\u000E" … "\u001F" | "!" … "/" | ":" … "@" | "[" … "^" | "\u0060" | "{" … "\u007F" | nonASCIIFenceChar .U+0000 … "\b"U+000E … U+001F"!" … "/"":" … "@""[" … "^""\u0060""{" … U+007FnonASCIIFenceChar
Literal textAnother ruleCharacter range
EBNF notation
fenceChar = "\u0000" … "\b" | "\u000E" … "\u001F" | "!" … "/" | ":" … "@"
          | "[" … "^" | "\u0060" | "{" … "\u007F" | nonASCIIFenceChar .
Lexical rule

nonASCIIFenceChar

Any non-ASCII Unicode scalar value except whitespace, letters, and decimal digits.

nonASCIIFenceCharnonASCIIFenceChar = nonASCIINonSpace .nonASCIINonSpace
Literal textAnother rule
EBNF notation
nonASCIIFenceChar = nonASCIINonSpace .
Lexical rule

blockTag

A tag identifies the contents of a block and ends at the first whitespace character.

blockTagblockTag = tagStart { tagChar } .tagStarttagChar
Literal textAnother rule
EBNF notation
blockTag = tagStart { tagChar } .
D2 example
label: |md
  **Public API**
|
Lexical rule

tagStart

A block tag starts with a Unicode letter, a decimal digit, or an underscore.

tagStarttagStart = "A" … "Z" | "a" … "z" | decimalDigit | "_" | nonASCIITagStart ."A" … "Z""a" … "z"decimalDigit"_"nonASCIITagStart
Literal textAnother ruleCharacter range
EBNF notation
tagStart = "A" … "Z" | "a" … "z" | decimalDigit | "_" | nonASCIITagStart .
Lexical rule

nonASCIITagStart

Any non-ASCII Unicode letter or decimal digit.

nonASCIITagStartnonASCIITagStart = nonASCIINonSpace .nonASCIINonSpace
Literal textAnother rule
EBNF notation
nonASCIITagStart = nonASCIINonSpace .
Lexical rule

tagChar

After the first character, a block tag accepts any non-whitespace character.

tagChartagChar = "\u0000" … "\b" | "\u000E" … "\u001F" | "!" … "\u007F" | nonASCIINonSpace .U+0000 … "\b"U+000E … U+001F"!" … U+007FnonASCIINonSpace
Literal textAnother ruleCharacter range
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.

structure.ebnf26 rules
// 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 "}" .
lexical.ebnf33 rules
// 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 .