Overview
Leo’s syntax is formally defined using Augmented Backus-Naur Form (ABNF) notation. The complete grammar specification is maintained in a separate repository and serves as the authoritative reference for the language’s syntax.Official Grammar
The Leo ABNF grammar is available on GitHub:Leo ABNF Grammar
View the complete ABNF grammar specification
Grammar Structure
The Leo grammar is organized into several key sections:Lexical Elements
- Keywords: Reserved words like
program,fn,struct,let,if,else,return,final - Literals: Integer, address, boolean, field, group, scalar, string literals
- Identifiers: Variable names, function names, type names
- Operators: Arithmetic, logical, comparison, and assignment operators
- Delimiters: Parentheses, braces, brackets, semicolons, commas
Program Structure
Type Declarations
- Structs: Composite types with named fields
- Records: Private data structures with owner field
- Mappings: On-chain key-value storage
Statements and Expressions
- Let bindings: Variable declarations
- Assignments: Variable updates
- Conditionals:
if/elseexpressions - Loops: (Note: Leo unrolls loops at compile time)
- Function calls: Both regular and finalizer functions
- Return statements: Early and final returns
Parser Implementation
Leo uses a two-stage parsing approach:- Lexer (using the
logoscrate): Tokenizes the source code - Parser (using
rowan): Builds a concrete syntax tree, then converts to AST
crates/parser-rowan/- Rowan-based parser with grammar ingrammar.rscrates/parser/- Converts rowan parse tree to typed AST
Syntax Highlighting
For syntax highlighting in your editor:VS Code Extension
Install the Leo language extension for syntax highlighting
TextMate Grammar
Available for editors supporting TextMate grammars
Grammar Features
Comments
Type Annotations
Leo requires explicit type annotations for function parameters and return types:Visibility Modifiers
Thepublic keyword marks function parameters as publicly visible:
Related Resources
Built-in Types
Complete reference of Leo’s type system
Aleo Instructions
How Leo compiles to Aleo bytecode