This chapter presents the complete grammar of the Scaly programming language in a formal notation. Each production defines a syntactic construct, with nonterminals linking to their definitions.
Notation:
identifier, literal — terminal symbols from the lexer
'keyword' — keyword terminals
X | Y — alternatives
X? — optional
X+ — one or more
X* — zero or more
| Program | ::= | File Statement* |
| File | ::= | Package* Use* Declaration* |
| Declaration | ::= | Symbol ':'? |
| Symbol | ::= | Private | Definition | Function | Operator | Trait | Macro | Module |
| Private | ::= | 'private' Export |
| Export | ::= | Definition | Function | Operator | Trait | Module |
| Member | ::= | Constituent ':'? |
| Constituent | ::= | Definition | Function | Procedure | Operator | Implement | Trait | Macro | Module |
| Definition | ::= | 'define' IDENTIFIER GenericParameters? ':'? Attribute* Concept |
| GenericParameters | ::= | '[' GenericParameter+ ']' |
| GenericParameter | ::= | IDENTIFIER Attribute* ',' |
| Concept | ::= | Class | Namespace | Union | Constant | Delegate | Intrinsic |
| Class | ::= | Structure ':'? Body? ':'? |
| Body | ::= | '{' Use* Init* DeInit? Member* '}' ':'? |
| Namespace | ::= | '{' Use* Declaration* '}' ':'? |
| Union | ::= | 'union' ':'? '(' Variant+ ')' ':'? Body? ':'? |
| Variant | ::= | IDENTIFIER Attribute* TypeAnnotation? ',' ':'? |
| Constant | ::= | Type Operand* ':'? |
| Delegate | ::= | 'delegate' ParameterSet? Attribute* Returns? Throws? |
| GenericArguments | ::= | '[' GenericArgument* ']' |
| GenericArgument | ::= | Type ',' |
| Optional | ::= | '?' |
| ParameterSet | ::= | Parameters | Type |
| Parameters | ::= | '(' Item* ')' |
| Item | ::= | IDENTIFIER TypeAnnotation? ',' ':'? Attribute* ':'? |
| Returns | ::= | 'returns' Type Attribute* |
| Throws | ::= | 'throws' Type Attribute* |
| Use | ::= | 'use' Name ':'? |
| Implement | ::= | 'implement' Type Attribute* ':'? '{' Use* Method* '}' ':'? |
| Trait | ::= | 'trait' Name Extends? Attribute* '{' Use* Method* '}' ':'? |
| Method | ::= | Function | Procedure | Operator |
| Init | ::= | 'init' ParameterSet? ':'? Action ':'? |
| DeInit | ::= | 'deinit' ':'? Action ':'? |
| Function | ::= | 'function' Target |
| Procedure | ::= | 'procedure' Target |
| Operator | ::= | 'operator' Target |
| Target | ::= | Named | Routine |
| Named | ::= | IDENTIFIER Routine |
| Routine | ::= | GenericArguments? Lifetime? ParameterSet? Attribute* Returns? ':'? Throws? ':'? Implementation |
| Implementation | ::= | Action | Extern | Instruction | Intrinsic |
| Extern | ::= | 'extern' |
| Instruction | ::= | 'instruction' ':'? |
| Intrinsic | ::= | 'intrinsic' ':'? |
| Extends | ::= | 'extends' Extend* |
| Extend | ::= | Type ',' |
| Macro | ::= | 'macro' IDENTIFIER Model Operand+ |
| Attribute | ::= | ATTRIBUTE Model ':'? |
| Model | ::= | Literal | Name | Object | Vector |
| Module | ::= | 'module' IDENTIFIER ':'? |
| Package | ::= | 'package' Name ':'? |
| Initializer | ::= | '(' Operand+ ')' |
| Operand | ::= | Expression MemberAccess* |
| MemberAccess | ::= | '.' Name |
| Expression | ::= | Literal | Type | Object | Vector | Block | If | Match | Lambda | For | While | Choose | Try | Repeat | SizeOf | Is |
| Literal | ::= | LITERAL |
| Object | ::= | '(' Component* ')' |
| Component | ::= | Operand+ Attribute* Value? ',' |
| Value | ::= | ':' Operand+ Attribute* |
| Vector | ::= | '[' Element+ ']' Lifetime? |
| Element | ::= | Operand+ Attribute* ',' |
| Block | ::= | '{' Use* Statement* '}' |
| If | ::= | 'if' Operand+ ':' Command Else? |
| Else | ::= | 'else' ':'? Command |
| Match | ::= | 'match' Operand+ ':' Branch+ Else? |
| Branch | ::= | Case+ ':' Statement |
| Case | ::= | 'case' Operand+ |
| Statement | ::= | Command ':'? |
| While | ::= | 'while' Condition ':' Label? Action |
| Choose | ::= | 'choose' Operand+ ':' When* Else? |
| Try | ::= | 'try' Condition ':' When* Else? |
| Condition | ::= | Operation | Let |
| When | ::= | 'when' IDENTIFIER ':' Name ':'? Command ':'? |
| Command | ::= | Operation | Let | Var | Mutable | Set | Continue | Break | Return | Throw |
| Let | ::= | 'let' Binding |
| Var | ::= | 'var' Binding |
| Mutable | ::= | 'mutable' Binding |
| Binding | ::= | IDENTIFIER BindingAnnotation? Operand+ |
| BindingAnnotation | ::= | ':' BindingSpec |
| BindingSpec | ::= | Structure | Type | Array |
| Array | ::= | '[' Type* ']' |
| Structure | ::= | '(' Part* ')' |
| Part | ::= | Field | Property |
| Field | ::= | 'private' Property |
| Property | ::= | IDENTIFIER TypeAnnotation Initializer? ',' ':'? Attribute* ':'? |
| TypeAnnotation | ::= | ':' Type |
| Continue | ::= | 'continue' Loop? ':'? |
| Break | ::= | 'break' Loop? Operand* ':'? |
| Loop | ::= | 'loop' IDENTIFIER |
| Return | ::= | 'return' Operand* |
| Throw | ::= | 'throw' Operand* |
| Lambda | ::= | 'lambda' Operand+ ':' Action |
| For | ::= | 'for' IDENTIFIER TypeAnnotation? 'in' Operand+ ':' Label? Action |
| Label | ::= | 'label' IDENTIFIER ':'? |
| Repeat | ::= | 'repeat' Label? Action |
| Action | ::= | Operation | Set |
| Operation | ::= | Operand+ |
| Set | ::= | 'set' Operand+ ':' Operand+ |
| SizeOf | ::= | 'sizeof' Type |
| Is | ::= | 'is' Name |
| Type | ::= | Name GenericArguments? Optional? Lifetime? |
| Name | ::= | IDENTIFIER Extension* |
| Extension | ::= | '.' IDENTIFIER |
| Lifetime | ::= | Call | Local | Reference | Thrown |
| Call | ::= | '#' |
| Local | ::= | '$' |
| Reference | ::= | '^' IDENTIFIER |
| Thrown | ::= | '!' |