Tern

Tern grammar

The exact syntax accepted by the compiler (compiler/syntax/lexer.tn, compiler/syntax/parser.tn), in EBNF. { x } means zero or more, [ x ] optional, | alternatives, quoted text is literal. For what the constructs mean, see reference.md.

Tokens

NEWLINE   end of a line (not inside ( ) [ ] { }, and not after a trailing `\`)
INDENT    a line indented more than the one before; DEDENT: less (must match an earlier level)
          (a tab counts as 4 spaces; blank lines and comment-only lines are ignored)
comment   "#" up to the end of the line
NAME      letter or "_", then letters, digits, "_"   (not a keyword)
INT       digit { digit | "_" }                       e.g. 42  1_000_000  (at most 9223372036854775807)
          | "0" ("x"|"X") hexdigit { hexdigit | "_" }  e.g. 0xff  (hex, binary, octal: up to 64 bits;
          | "0" ("b"|"B") bindigit { bindigit | "_" }       above the largest int they are negative)
          | "0" ("o"|"O") octdigit { octdigit | "_" }
FLOAT     INT [ "." digit { digit } ] [ exponent ]    with a "." or an exponent: 3.14  1e9  2.5e-3
exponent  ("e"|"E") [ "+" | "-" ] digit { digit }     (not "1." or ".5")
STR       '"' { char | escape } '"'                   one line; escapes \n \t \r \0 \" \\
                                                      \xHH (a byte), \uHHHH and \u{H..} (UTF-8)
          | '"""' { char | newline | escape } '"""'    over lines; drops a blank first and last line
                                                      and the indentation the lines share
          | "`" { char | newline } "`"                 raw: every character as written, no escapes
FSTR      'f"' { char | "{{" | "}}" | "{" expr "}" } '"'
          | 'f"""' ... '"""'                           an f-string over lines, like """
keywords  and break const continue do elif else enum export extern false fn for from if import
          in mut not null or parallel pass return struct take true while
soft      match (a keyword only at the start of a statement followed by an expression),
          is (only after an expression)
symbols   -> == != <= >= .. += -= *= /= %= &= |= ^= <<= >>= >>>= << >> >>>
          ( ) [ ] { } , : . = + - * / % < > & | ^ ~ ?

Program

program     = { import | struct | enum | alias | const | fn | "export" fn | extern | NEWLINE } EOF
alias       = "type" NAME "=" type NEWLINE                      ("type" is a soft keyword here)
import      = "import" NAME NEWLINE
            | "from" NAME "import" NAME { "," NAME } NEWLINE
struct      = "struct" NAME [ tparams ] ":" NEWLINE INDENT field_line { field_line } DEDENT
field_line  = NAME { "," NAME } ":" type NEWLINE
enum        = "enum" NAME [ tparams ] ":" NEWLINE INDENT variant { variant } DEDENT
variant     = NAME [ "(" [ NAME ":" type { "," NAME ":" type } ] ")" ] NEWLINE
const       = "const" NAME [ ":" type ] "=" expr NEWLINE           (int, float, str, or bool, computed
                                                                  from literals, constants, operators)
fn          = "fn" NAME [ tparams ] "(" [ param { "," param } ] ")" [ [ "->" ] type ] block
param       = NAME ":" [ "mut" | "take" ] type
tparams     = "[" tparam { "," tparam } "]"                  (generic: fn first[T], struct Pair[A, B])
tparam      = NAME [ ":" NAME { "+" NAME } ]                  (constraints: Eq, Ord, Hash)
extern      = "extern" [ STR ] { ( "source" | "link" ) STR } ":" NEWLINE
              INDENT { "fn" NAME "(" [ param { "," param } ] ")" [ type ] [ "=" STR ] NEWLINE } DEDENT

Types

type        = "int" | "float" | "bool" | "str" | "byte" | "ptr"
            | NAME [ "." NAME ] [ "[" type { "," type } "]" ]   (a struct or enum, maybe from a module:
                                                 http.Request; with type arguments: Pair[int, str])
            | "[" type "]"                         (array)
            | "{" type ":" type "}"                (map; keys: int, str, bool, or arrays, structs, enums,
                                                 tuples of those)
            | "(" type "," type { "," type } ")"   (tuple)
            | "fn" "(" [ fparam { "," fparam } ] ")" [ type ]    (function; no type = returns nothing)
fparam      = [ "mut" | "take" ] type

Statements

block       = ":" ( NEWLINE INDENT stmt { stmt } DEDENT | simple NEWLINE )
stmt        = if | while | for | parallel | match | "do" block | fn | simple NEWLINE
if          = "if" expr block { "elif" expr block } [ "else" block ]
while       = "while" expr block
for         = "for" NAME "in" expr ".." expr block              (range: start inclusive, end exclusive)
            | "for" NAME [ "," NAME ] "in" expr block             (array: element or index, element;
                                                                  map: key or key, value; str: bytes)
parallel    = "parallel" block                                   (each statement is a task; `do` groups)
            | "parallel" "for" NAME "in" expr ".." expr block
match       = "match" expr ":" NEWLINE INDENT arm { arm } DEDENT
arm         = pattern { "|" pattern } block                  (alternatives bind no names)
pattern     = "_" | INT | FLOAT | STR | "-" INT | "true" | "false"
            | NAME { "." NAME } [ "(" [ NAME { "," NAME } ] ")" ]    (variant; "_" skips a value)
simple      = "return" [ exprs ] | "break" | "continue" | "pass"
            | NAME ":" type "=" expr                              (typed declaration)
            | place "=" exprs                                     (declaration or assignment)
            | target "," target { "," target } "=" exprs          (unpacks a tuple)
            | place ( "+=" | "-=" | "*=" | "/=" | "%=" | "&=" | "|=" | "^=" | "<<=" | ">>=" | ">>>=" ) expr
            | expr                                                (a call, for its effect)
place       = NAME { "." ( NAME | INT ) | "[" expr "]" }
target      = place | "_"
exprs       = expr { "," expr }                                   (two or more make a tuple)

Expressions

Lowest to highest precedence; all binary operators are left-associative.

expr        = or_expr [ "if" or_expr "else" expr ]              (conditional: only one side runs)
or_expr     = and_expr { "or" and_expr }
and_expr    = cmp { "and" cmp }
cmp         = bor { ( "==" | "!=" | "<" | "<=" | ">" | ">=" | "in" ) bor | "is" pattern }
                                                          (`is` binds names only in if/while conditions)
bor         = bxor { "|" bxor }
bxor        = band { "^" band }
band        = shift { "&" shift }                            (binary `&`: only after an operand)
shift       = sum { ( "<<" | ">>" | ">>>" ) sum }
sum         = term { ( "+" | "-" ) term }
term        = unary { ( "*" | "/" | "%" ) unary }
unary       = "-" unary | "not" unary | "~" unary | "&" place | postfix
postfix     = primary { "." NAME [ args ] | "." INT | "[" index "]" | args | "?" }
                                              (t.0: a tuple's value; x?: unwrap an Option / Result or return early)
index       = expr | expr ".." [ expr ] | ".." [ expr ]
            | expr "," expr { "," expr }              (type arguments: f[int, str](...), after a generic name)
args        = "(" [ arg { "," arg } ] ")"
arg         = expr | NAME "=" expr                               (named: only when building a struct/variant)
primary     = INT | FLOAT | STR | FSTR | "true" | "false" | "null"
            | NAME
            | "[" [ expr { "," expr } ] "]"                      ([v] * n repeats v n times)
            | "{" [ expr ":" expr { "," expr ":" expr } ] "}"
            | "(" expr ")" | "(" expr "," expr { "," expr } [ "," ] ")"    (a tuple)
            | "fn" "(" [ lparam { "," lparam } ] ")" expr        (lambda: one expression)
lparam      = NAME [ ":" [ "mut" | "take" ] type ]

Notes:

  • not binds tighter than in and ==, so write not (k in m) and not (a == b).
  • Name(...) where Name is a struct builds a struct; Enum.Variant(...) builds a variant.
  • x.f(args) calls f(x, args) (or a function-valued field f of struct x).
  • & before an operand is the mut-argument marker (f(&x)); between two operands it is bitwise and.
  • There is no c ? a : b (write a if c else b), no **, no //, no not in, no is not.
Source: docs/grammar.md. Every example on this page is compiled and run by tern x test docs.