Syntax
Lexical syntax
Comments
Single line comments start with // and block comments are enclosed in /*
and */ and can be nested.
Keywords
contract include let switch type record datatype if elif else function
stateful payable true false mod public entrypoint private indexed namespace
interface main using as for hidingTokens
Id = [a-z_][A-Za-z0-9_']*identifiers start with a lower case letter.Con = [A-Z][A-Za-z0-9_']*constructors start with an upper case letter.QId = (Con\.)+Idqualified identifiers (e.g.Map.member)QCon = (Con\.)+Conqualified constructorTVar = 'Idtype variable (e.g'a,'b)Int = [0-9]+(_[0-9]+)*|0x[0-9A-Fa-f]+(_[0-9A-Fa-f]+)*integer literal with optional_separatorsBytes = #[0-9A-Fa-f]+(_[0-9A-Fa-f]+)*byte array literal with optional_separatorsStringstring literal enclosed in"with escape character\Charcharacter literal enclosed in'with escape character\AccountAddressbase58-encoded 32 byte account pubkey withak_prefixContractAddressbase58-encoded 32 byte contract address withct_prefixOracleAddressbase58-encoded 32 byte oracle address withok_prefixOracleQueryIdbase58-encoded 32 byte oracle query id withoq_prefixSignaturebase58-encoded 64 byte cryptographic signature withsg_prefix
Valid string escape codes are
\b
8
9
10
\v
11
\f
12
13
\e
27
\xHexDigits
HexDigits
See the identifier encoding scheme for the details on the base58 literals.
Layout blocks
Sophia uses Python-style layout rules to group declarations and statements. A layout block with more than one element must start on a separate line and be indented more than the currently enclosing layout block. Blocks with a single element can be written on the same line as the previous token.
Each element of the block must share the same indentation and no part of an element may be indented less than the indentation of the block. For instance
Notation
In describing the syntax below, we use the following conventions:
Upper-case identifiers denote non-terminals (like
Expr) or terminals with some associated value (likeId).Keywords and symbols are enclosed in single quotes:
'let'or'='.Choices are separated by vertical bars:
|.Optional elements are enclosed in
[square brackets].(Parentheses)are used for grouping.Zero or more repetitions are denoted by a postfix
*, and one or more repetitions by a+.Block(X)denotes a layout block ofXs.Sep(X, S)is short for[X (S X)*], i.e. a possibly empty sequence ofXs separated bySs.Sep1(X, S)is short forX (S X)*, i.e. same asSep, but must not be empty.
Declarations
A Sophia file consists of a sequence of declarations in a layout block.
Contract declarations must appear at the top-level.
For example,
There are three forms of type declarations: type aliases (declared with thetype keyword), record type definitions (record) and data type definitions
(datatype):
For example,
Types
The function type arrow associates to the right.
Example,
Statements
Function bodies are blocks of statements, where a statement is one of the following
if statements can be followed by zero or more elif statements and an optional final else statement. For example,
Expressions
Operators types
- + * / mod ^
arithmetic operators
! && ||
logical operators
band bor bxor bnot << >>
bitwise operators
== != < > =< >=
comparison operators
:: ++
list operators
|>
functional operators
Operator precedence
In order of highest to lowest precedence.
! bnot
right
^
left
* / mod
left
- (unary)
right
+ -
left
<< >>
left
:: ++
right
< > =< >= == !=
none
band
left
bxor
left
bor
left
&&
right
||
right
|>
left
Last updated
Was this helpful?