Sway Language Grammar v0.66.5

Sway is a domain-specific programming language for implementing smart contracts on blockchain platforms, most notably for the Fuel Virtual Machine (Fuel VM).

Parser Grammar

module

On top level, Sway allows module kind and definitions of submodule, storage, contracts, traits, libraries, structs, enums and constants. (AST: Module)

Rule module

submodule

The mod keyword registers a submodule, making its items (such as functions and structs) accessible from the parent library. If used at the top level it will refer to a file in the src folder and in other cases in a folder named after the library in which it is defined. (AST: Submodule)

Rule submodule

use

Provides several utility types and methods we can use in our contract. (AST: ItemUse)

Rule use

use-tree

Path of a types or methods to be imported. (AST: UseTree)

Rule use-tree

struct

Definition of a struct. Can occur at top-level within a source unit or within a contract, library or interface. (AST: ItemStruct)

Rule struct

type-field

Definition of TypeField. (AST: TypeField)

Rule type-field

type-name

Definition of TypeName. (AST: Ty)

Rule type-name

enum

Definition of an enum. Can occur at top-level within a source unit or within a contract, library or interface. (AST: ItemEnum)

Rule enum

traits

Definition of traits. Can be used for abi and trait.(AST: Traits)

Rule traits

trait-item

Define an item of an trait. (AST: ItemTraitItem).

Rule trait-item

abi

ABI stands for Application Binary Interface.(AST: ItemAbi, doc: The ABI Declaration).

Rule abi

const

Define constant items. (AST: ItemConst)

Rule const

configurable

Configurable constants are special constants that behave like regular constants in the sense that they cannot change during program execution, but they can be configured after the Sway program has been built. (AST: ItemConfigurable, doc: Configurable Constants)

Rule configurable

type-alias

Definition of type-alias. To declare a type alias to give an existing type another name. (AST: ItemTypeAlias)

Rule type-alias

fn

Functions in Sway are declared with the fn keyword. (AST: ItemFn, doc: Functions)

Rule fn

fn-signature

fn-signature declares signature for function. (AST: FnSignature, doc: Functions)

Rule fn-signature

generic-params

Generic types are a way to refer to types in general, meaning without specifying a single type. (AST: GenericParams, doc: Generic Types).

Rule generic-params

where-clause

Where clause can be used to specify the required traits for the generic argument. (AST: WhereClause, doc: Trait Constraints).

Rule where-clause

code-block-contents

A CodeBlockContents represents the body of a code block, which contains a sequence of statements and expressions. (AST: CodeBlockContents, doc: Functions). Rule code-block-contents

fn-arg

A fn-arg represents function arguments. (AST: FnArg, doc: Functions). Rule fn-arg

pattern

A pattern is a construct used to match and destructure data. (AST: Pattern, doc: match expressions). Rule pattern

trait

A trait opts a type into a certain type of behavior or functionality that can be shared among types. This allows for easy reuse of code and generic programming. (AST: ItemTrait, doc: Declaring a Trait). Rule trait

impl

Implement inherent or trait functionality. (AST: ItemImpl, doc: Implementing a Trait)

Rule impl

storage

In smart contract development, persistent storage (storage) is used to store values that persist after the contract exits, unlike regular memory values that disappear. (AST: ItemStorage, doc: Declaring storage)

Rule storage

storage-entry

Define storage entry. (AST: StorageEntry, doc: Declaring storage)

Rule storage-entry

expr

Define types of expressions. (AST: Expr, doc: If Expressions)

Rule expr