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#
pkg/dang/dang.peg— Pigeon-PEG grammar; generatespkg/dang/dang.peg.go- the tree-sitter grammar at
treesitter/is derived from the same source
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#
- forms (top-level, block bodies) are separated by newlines or
;—FormSep - newlines and commas are interchangeable inside arg lists, lists, records —
Sep
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.
rescuesits between??andor: it binds tighter than??and looser thanor, sox rescue null ?? "default"is(x rescue null) ?? "default"(see Errors:raiseandrescue)
(JSON.decode("null") :: Int) rescue null ?? -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 NumericThe 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])]
Lexical#
- identifiers:
[a-zA-Z_][a-zA-Z0-9_]*(lowercase-leading = value, uppercase-leading = type) - comments:
#to end of line - strings:
"..."and"""...""": escapes and${expr}interpolation;\$escapes a dollar sign, so\${is a literal${(before Dang 2.5,"..."takes${literally and"""is raw)'...'and'''...''': verbatim, with no escapes or interpolation;'...'can't hold a'('''...'''can, even inline:'''it's''')`...`backtick templates: raw text with${expr}interpolation;\${escapes a literal${. Longer backtick fences (```) nest- a tripled delimiter (
""",''',```) makes a multi-line, dedented string whose opening line holds at most a language tag ("""sql), unless it closes on the same line - docstrings are triple-quoted but always verbatim, and may start on the opening line. See Literals and Strings.
- numbers:
Intdecimal;Floatrequires a fraction (1.0) or exponent (scientific notation)
Reserved words#
- keyword tokens (each
!WordChar-terminated):and,break,case,continue,directive,else,enum,false,if,implements,import,interface,let,new,null,on,or,pub,raise,rescue,return,scalar,self,true,type,union(tryandcatchare ordinary identifiers now; legacytry/catchblocks still parse but warn at type-check — see Errors:raiseandrescue) - of these,
nullandrescueare never identifiers (WordTokenexcludes them). Where only a name can go (FieldId,KeyValuekeys,FieldSelection, object type keys),rescueis a name (ReservedName). Elsewhere, the rarely needed escape$rescue(RawWordToken) spells the identifierrescue;dang migratewrites it for names a newer version reserves pubis optional — the visibility keyword is parsed but no longer required; declarations are public by default (see Fields)- see Syntax and literals
Notable productions#
SelectOrCall:Term '.' (ObjectSelection | FieldId ArgValues? BlockArg?)— the field path; zero-arg fields auto-call. See Fields.BlockArg:'{' (BlockParams '=>')? Expr (Sep Expr)* '}'— trailing block attached to a call; params are optional. See Blocks.ObjectSelection:"{{" ... "}}"after a.— two forms: aFieldSelectionlist (user.{{name, posts.{{title}}}}), or a list ofInlineFragments for unions/interfaces. The double braces mirror record literals{{ }}. See Objects (type).DotBlock:'.' BlockArg— a single-brace block applied to the receiver (foo.{ bar(_) }≡bar(foo)), a sibling ofObjectSelectionand method calls at.precedence. See Blocks's Dot-block application (piping).FieldSelection:(Id ':')? Id ArgValues? ('.' ObjectSelection)?— a field in a selection, optionally with args and a nested{{ }}selection. A leadingId ':'is a GraphQL-style alias that renames the field in the result (user.{{ full: name }}); a bare field is shorthand forname: name.InlineFragment:'...' 'on' Symbol ('{{' FieldSelection* '}}' | '!'?)— type-narrowing in a selection; the field set uses the same double braces as any selection. See Interfaces and unions.