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 hiding

Tokens

  • 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\.)+Id qualified identifiers (e.g. Map.member)

  • QCon = (Con\.)+Con qualified constructor

  • TVar = 'Id type variable (e.g 'a, 'b)

  • Int = [0-9]+(_[0-9]+)*|0x[0-9A-Fa-f]+(_[0-9A-Fa-f]+)* integer literal with optional _ separators

  • Bytes = #[0-9A-Fa-f]+(_[0-9A-Fa-f]+)* byte array literal with optional _ separators

  • String string literal enclosed in " with escape character \

  • Char character literal enclosed in ' with escape character \

  • AccountAddress base58-encoded 32 byte account pubkey with ak_ prefix

  • ContractAddress base58-encoded 32 byte contract address with ct_ prefix

  • OracleAddress base58-encoded 32 byte oracle address with ok_ prefix

  • OracleQueryId base58-encoded 32 byte oracle query id with oq_ prefix

  • Signature base58-encoded 64 byte cryptographic signature with sg_ prefix

Valid string escape codes are

Escape
ASCII

\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 (like Id).

  • 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 of Xs.

  • Sep(X, S) is short for [X (S X)*], i.e. a possibly empty sequence of Xs separated by Ss.

  • Sep1(X, S) is short for X (S X)*, i.e. same as Sep, 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

Operators
Type

- + * / 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.

Operators
Associativity

! 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?