Structure of Programs
A semi-formal representation of Polylang
programs is shown below.
💡
For the official grammar for Polylang
, please refer to this grammar file (opens in a new tab).
Grammar Notation
{x}
means zero or more occurrences ofx
.[x]
means zero or one occurrences ofx
, meaning thatx
is optional.(one of)
followed by space-separated items indicates a choice of one of the items.x
indicates a literal of valuex
.
Simplified Grammar for Polylang
A polylang
program ("schema") consists of one or more contract (opens in a new tab) declarations:
Schema:
[ContractDeclaration]
where each contract consists of field declarations, a constructor declaration, and function declarations:
ContractDeclaration:
[(one of)`@public` `@private`]
`contract` ContractName ContractBody
ContractName:
alphanumeric starting with a letter
ContractBody:
{FieldDeclaration}
[ConstructorDeclaration]
{FunctionDeclaration}
FieldDeclaration:
(one of)
RequiredFieldDeclaration OptionalFieldDeclaration
RequiredFieldDeclaration:
FieldName: FieldType ';'
OptionalFieldDeclaration:
FieldName`?` : FieldTyoe ';'
FieldName:
alphanumeric starting with a letter
where each field can have the following types:
FieldType:
(one of)
PrimitiveType `bytes` `PublicKey` ContractType ArrayType MapType ObjectType
PrimitiveType:
(one of)
`string` `number` `boolean`
ContractType:
a reference to a contract
ArrayType:
`string`[] `number`[] `boolean`[] `PublicKey`[] ContractType[]
MapType:
`map` `<`MapKeyType : FieldType `>`
MapKeyType:
(one of)
`string` `number`
ObjectType:
'{' [FieldName ':' FieldType] '}'
Field types are defined in more detail here.
Constructors and functions are similar in structure. Note that the constructor
keyword is mandatory for a constructor declaration:
ConstructorDeclaration:
`constructor` ConstructorName ConstructorArgs ConstructorBody
ConstructorName:
alphanumeric starting with a letter
ConstructorArgs:
'(' [ArgPair] {`,` ArgPair} ')'
ArgPair:
ArgName ':' ArgType
ArgName:
alphanumeric starting with a letter
ArgType:
FieldType
Note that the function
keyword in a function declaration is optional:
FunctionDeclaration:
[`function`] FunctionName FunctionArgs FunctionBody
FunctionName:
alphanumeric starting with a letter
FunctionArgs:
ConstructorArgs
FunctionBody:
ConstructorBody