Grammar notes#

Meta: not a full BNF — the source of truth is pkg/dang/dang.peg. This page captures the user-visible regularities so people don't have to read PEG.

The dang examples on this page are live: the docs build runs them and bakes in the results. The grammar sketches are plain text.

Source of truth#

Top-level structure#

The start rule is Dang, not Module. Import and Reassignment are siblings of Decl/Form, not members of Decl.

Dang         := (Expr FormSep)* Expr?     # FormSep = newline or ';' (forms in sequence); Sep = newline or ',' (collection elements)
Expr         := Import | Decl | Reassignment | Form
Import       := 'import' Symbol
Reassignment := Term AssignOp Form
Decl         := DocString? ( InterfaceDecl | UnionDecl | EnumDecl | ScalarDecl
                           | ObjectDecl | NewConstructorDecl | FieldDecl | DirectiveDecl )
Form         := Return | RescueExpr | Raise | Conditional
              | Case | Break | Continue | DefaultExpr | TypeHint | Term
Term         := UnaryExpr | NonNullAssert | IndexOrCall | SelectOrCall | Literal
              | MapLiteral | List | ObjectLiteral | Block | ParenForm | SymbolOrCall
NonNullAssert := Term '!'
RescueExpr   := (RescueExpr | Term) 'rescue' (RescueClauses | Fallback)   # left-associative chaining; Fallback is any Term
RescueClauses := '{' RescueClause+ '}'    # a bare '{' after 'rescue' always starts clauses; a record fallback is '{{ ... }}'
RescueClause := (Symbol ':' Type '!' | 'else') '=>' Form   # a type pattern, as in a case clause: any type, written non-null

Separators#

Expression form (precedence)#

Meta: keep this in sync with Operators precedence table — duplicating it isn't ideal but it's the kind of thing readers expect on a grammar page.

(JSON.decode("null") :: Int) rescue null ?? -1
=> -1

Read the other way, as … rescue (null ?? -1), this would be null: the decode succeeds, so the fallback never applies.

Type syntax#

Type dispatches to one of these; non-null is a suffix ! wrapping any inner type. See Types and nullability.

Type         := NonNull | AppliedType | NamedType | ListType | ObjectType | TypeVariable
NonNull      := Type '!'
AppliedType  := NamedType '[' (Type Sep)* Type? ']'   # generic application, e.g. List[a], Map[a]
NamedType    := (NamedType '.')? UpperIdent     # qualifier is itself a NamedType
ListType     := '[' Type ']'                    # shorthand for List[...]
ObjectType   := '{{' (ObjectTypeField Sep)* ObjectTypeField? '}}'
TypeVariable := [a-z]                           # single lowercase letter

[a] is shorthand for List[a]. Map[a] is a string-keyed map of a values (only the built-in List and Map accept type arguments today).

A function or method declaration may list its type parameters right after its name (see Type parameters, Ordered and Numeric):

TypeParamList := '[' TypeParam (Sep TypeParam)* Sep? ']'   # immediately after the name: f[a, k: Ordered](...), and on an interface member header
TypeParam     := Word (':' Bound ('&' Bound)*)?           # a single lowercase letter
Bound         := NamedType                                # Ordered or Numeric

The grammar reads a parameter as a word and a bound as a type name; a parameter must be one lowercase letter, and the bounds are Ordered and Numeric.

Each form in use:

import Demo

let grid: List[[Int!]!]! = [[1, 2], [3]]
let tags: Map[[String!]!]! = ["fruit": ["apple"], "veg": []]
let origin: {{x: Int!, y: Int!}}! = {{x: 0, y: 0}}
let viewer: Demo.User = null
pick(first: a, second: a): a { first }
largest[a: Ordered](xs: [a!]!): a { xs.sort(descending: true)[0] }

[grid.length, tags["fruit"]!.length, origin.x, pick(1, 2), largest([2, 7, 3])]
=> [2, 1, 0, 1, 7]

Lexical#

Reserved words#

Notable productions#