A Mica source is one unit, a program, a library or an object file, made of declarations, routines and a body. Its block declares, in any order and as often as it likes, its type parameters, imports, constants, types and variables; then its routines; then the statement that is its body. A routine has a block of its own, so routines nest.

compilation-unit

compilation-unitcompilation-unit = unit-kind identifier ";" block "." .unit-kindunit-kindidentifieridentifier;blockblock.
compilation-unit = unit-kind identifier ";" block "." .

A unit: its kind, its name, its block and a final period.

unit-kind

unit-kindunit-kind = "program" | "library" | "object" .programlibraryobject
unit-kind = "program" | "library" | "object" .

What the unit becomes when it is linked.

Used in compilation-unit.

block

blockblock = { declaration-section } { routine-declaration } action .declaration-sectiondeclaration-sectionroutine-declarationroutine-declarationactionaction
block = { declaration-section } { routine-declaration } action .

Declarations, then routines, then the statement the block runs.

A section of declarations goes on as long as a line begins with a name, so a body that follows a section directly and begins with a name is read as one more declaration. Write the body as begin ... end.

Routines nest at most sixteen deep. A callback’s body counts as the first level again, since a C library calls it directly.

Used in compilation-unit, routine-declaration.

declaration-section

declaration-sectiondeclaration-section = generic-section | import-section | constant-section | type-section | variable-section .generic-sectiongeneric-sectionimport-sectionimport-sectionconstant-sectionconstant-sectiontype-sectiontype-sectionvariable-sectionvariable-section
declaration-section = generic-section | import-section | constant-section | type-section | variable-section .

A section of declarations. No section follows a routine of the same block.

Used in block.

generic-section

generic-sectiongeneric-section = "gen" generic-parameter { generic-parameter } .gengeneric-parametergeneric-parametergeneric-parametergeneric-parameter
generic-section = "gen" generic-parameter { generic-parameter } .

The type parameters and value parameters that make the block’s routines generic.

Used in declaration-section.

generic-parameter

generic-parametergeneric-parameter = identifier ( "is" identifier { "," identifier } | ":" identifier ) ";" .identifieridentifierisidentifieridentifier,identifieridentifier:identifieridentifier;
generic-parameter = identifier ( "is" identifier { "," identifier } | ":" identifier ) ";" .

A type parameter with the capabilities it must offer, or a value parameter with its type.

Used in generic-section.

import-section

import-sectionimport-section = "imp" import-group { import-group } .impimport-groupimport-groupimport-groupimport-group
import-section = "imp" import-group { import-group } .

Names taken from other units: the standard library std, the C surface cstd, a library by its name.

Used in declaration-section.

import-group

import-groupimport-group = identifier ";" | ( "*" | import-member { "," import-member } ) ":" identifier ";" .identifieridentifier;*import-memberimport-member,import-memberimport-member:identifieridentifier;
import-group = identifier ";" | ( "*" | import-member { "," import-member } ) ":" identifier ";" .

A whole namespace, every name of a unit with *, or the listed names, each optionally under a name of its own.

* stands alone in its group and takes no other name.

Used in import-section.

import-member

import-memberimport-member = identifier [ "=" identifier ] .identifieridentifier=identifieridentifier
import-member = identifier [ "=" identifier ] .

A name to import, written Alias = Name to bind it under another name.

Used in import-group.

constant-section

constant-sectionconstant-section = "const" constant-group { constant-group } .constconstant-groupconstant-groupconstant-groupconstant-group
constant-section = "const" constant-group { constant-group } .

Named values.

Used in declaration-section.

constant-group

constant-groupconstant-group = constant-entry { "," constant-entry } ";" .constant-entryconstant-entry,constant-entryconstant-entry;
constant-group = constant-entry { "," constant-entry } ";" .

One or more constants on a line.

Used in constant-section.

constant-entry

constant-entryconstant-entry = [ "exp" ] identifier ( "=" expression | ":" identifier "=" typed-constant-value ) .expidentifieridentifier=expressionexpression:identifieridentifier=typed-constant-valuetyped-constant-value
constant-entry = [ "exp" ] identifier ( "=" expression | ":" identifier "=" typed-constant-value ) .

A constant, optionally exported with exp and optionally pinned to a named type.

Used in constant-group.

typed-constant-value

typed-constant-valuetyped-constant-value = "(" expression { "," expression } ")" | (?! "(" ) expression .(expressionexpression,expressionexpression)not followed by "("expressionexpression
typed-constant-value = "(" expression { "," expression } ")" | (?! "(" ) expression .

A typed constant’s value, or the list of an array constant in parentheses.

A typed constant’s value that opens with ( is read as a list, so it holds expressions only.

Used in constant-entry.

type-section

type-sectiontype-section = "type" type-declaration { type-declaration } .typetype-declarationtype-declarationtype-declarationtype-declaration
type-section = "type" type-declaration { type-declaration } .

Named types.

Used in declaration-section.

type-declaration

type-declarationtype-declaration = [ "exp" ] identifier [ type-parameters ] "=" type-denoter ";" .expidentifieridentifiertype-parameterstype-parameters=type-denotertype-denoter;
type-declaration = [ "exp" ] identifier [ type-parameters ] "=" type-denoter ";" .

A type, optionally exported, optionally generic over the type parameters behind of.

Used in type-section.

type-parameters

type-parameterstype-parameters = "of" ( identifier | "(" identifier { "," identifier } ")" ) .ofidentifieridentifier(identifieridentifier,identifieridentifier)
type-parameters = "of" ( identifier | "(" identifier { "," identifier } ")" ) .

One type parameter, or several in parentheses.

Used in type-declaration.

variable-section

variable-sectionvariable-section = "var" variable-group { variable-group } .varvariable-groupvariable-groupvariable-groupvariable-group
variable-section = "var" variable-group { variable-group } .

Variables.

Used in declaration-section.

variable-group

variable-groupvariable-group = identifier ":" value-type [ "absolute" expression | ":=" expression ] ";" | identifier "," identifier { "," identifier } ":" value-type ";" .identifieridentifier:value-typevalue-typeabsoluteexpressionexpression:=expressionexpression;identifieridentifier,identifieridentifier,identifieridentifier:value-typevalue-type;
variable-group = identifier ":" value-type [ "absolute" expression | ":=" expression ] ";"
               | identifier "," identifier { "," identifier } ":" value-type ";" .

Variables of one type; a single variable may carry an initial value or the fixed address of a device register.

An initial value and an absolute address each belong to one variable, and they exclude each other.

Used in variable-section, inline-variable-section.

routine-declaration

routine-declarationroutine-declaration = [ "exp" ] ( intrinsic-declaration | [ "heapless" ] routine-heading ";" block ";" ) .expintrinsic-declarationintrinsic-declarationheaplessroutine-headingroutine-heading;blockblock;
routine-declaration = [ "exp" ] ( intrinsic-declaration | [ "heapless" ] routine-heading ";" block ";" ) .

A routine with its block, or the declaration of an intrinsic; exp exports it and heapless promises that it never uses the heap.

exp stands in front of heapless.

Used in block.

intrinsic-declaration

intrinsic-declarationintrinsic-declaration = "intrinsic" identifier ";" .intrinsicidentifieridentifier;
intrinsic-declaration = "intrinsic" identifier ";" .

A word of the language’s own math vocabulary, declared by a unit that provides it.

Used in routine-declaration.

routine-heading

routine-headingroutine-heading = function-heading | procedure-heading | task-heading | generator-heading | callback-heading .function-headingfunction-headingprocedure-headingprocedure-headingtask-headingtask-headinggenerator-headinggenerator-headingcallback-headingcallback-heading
routine-heading = function-heading | procedure-heading | task-heading | generator-heading | callback-heading .

The heading of one of the five kinds of routine.

Used in routine-declaration.

function-heading

function-headingfunction-heading = "function" identifier formal-parameters ":" result-type [ fails-clause ] .functionidentifieridentifierformal-parametersformal-parameters:result-typeresult-typefails-clausefails-clause
function-heading = "function" identifier formal-parameters ":" result-type [ fails-clause ] .

A function answers a value.

Used in routine-heading.

procedure-heading

procedure-headingprocedure-heading = "procedure" identifier formal-parameters [ fails-clause ] .procedureidentifieridentifierformal-parametersformal-parametersfails-clausefails-clause
procedure-heading = "procedure" identifier formal-parameters [ fails-clause ] .

A procedure answers nothing.

Used in routine-heading.

task-heading

task-headingtask-heading = "task" identifier formal-parameters [ fails-clause ] .taskidentifieridentifierformal-parametersformal-parametersfails-clausefails-clause
task-heading = "task" identifier formal-parameters [ fails-clause ] .

A task runs beside the routine that schedules it.

Used in routine-heading.

generator-heading

generator-headinggenerator-heading = "generator" identifier formal-parameters ":" "stream" "of" type-reference [ fails-clause ] .generatoridentifieridentifierformal-parametersformal-parameters:streamoftype-referencetype-referencefails-clausefails-clause
generator-heading = "generator" identifier formal-parameters ":" "stream" "of" type-reference [ fails-clause ] .

A generator answers a stream of the values it emits.

Used in routine-heading.

callback-heading

callback-headingcallback-heading = "callback" identifier formal-parameters [ ":" result-type ] [ fails-clause ] .callbackidentifieridentifierformal-parametersformal-parameters:result-typeresult-typefails-clausefails-clause
callback-heading = "callback" identifier formal-parameters [ ":" result-type ] [ fails-clause ] .

A callback is a routine a C library calls back into; it answers a value or nothing, as the C slot does.

Used in routine-heading.

formal-parameters

formal-parametersformal-parameters = "(" [ parameter-group { "," parameter-group } ] ")" .(parameter-groupparameter-group,parameter-groupparameter-group)
formal-parameters = "(" [ parameter-group { "," parameter-group } ] ")" .

The parameters in parentheses, which stand even when there are none.

Used in function-heading, procedure-heading, task-heading, generator-heading, callback-heading.

parameter-group

parameter-groupparameter-group = parameter-name { "," parameter-name } ":" parameter-type | parameter-name ":" conformant-schema | identifier ":" "pointer" conformant-schema .parameter-nameparameter-name,parameter-nameparameter-name:parameter-typeparameter-typeparameter-nameparameter-name:conformant-schemaconformant-schemaidentifieridentifier:pointerconformant-schemaconformant-schema
parameter-group = parameter-name { "," parameter-name } ":" parameter-type | parameter-name ":" conformant-schema
                | identifier ":" "pointer" conformant-schema .

Parameters of one type, or a parameter that takes any array of an element type with its bounds.

A conformant array parameter has a group of its own; its writable form behind pointer cannot be const.

Used in formal-parameters.

parameter-name

parameter-nameparameter-name = [ "const" ] identifier .constidentifieridentifier
parameter-name = [ "const" ] identifier .

A parameter, which const makes read-only.

Used in parameter-group.

parameter-type

parameter-typeparameter-type = pointer-tensor-type | value-type .pointer-tensor-typepointer-tensor-typevalue-typevalue-type
parameter-type = pointer-tensor-type | value-type .

A parameter’s type.

Used in parameter-group.

conformant-schema

conformant-schemaconformant-schema = "array" "[" identifier ".." identifier ":" type-name "]" "of" type-name .array[identifieridentifier..identifieridentifier:type-nametype-name]oftype-nametype-name
conformant-schema = "array" "[" identifier ".." identifier ":" type-name "]" "of" type-name .

An array of any bounds: the two names receive the caller’s bounds, of the type behind the colon.

Used in parameter-group.

result-type

result-typeresult-type = [ "tracked" (?= identifier | "array" ) ] ( shape-form | (?! "tensor" "of" ) type-reference ) .trackedfollowed by identifier | "array"shape-formshape-formnot followed by "tensor" "of"type-referencetype-reference
result-type = [ "tracked" (?= identifier | "array" ) ] ( shape-form | (?! "tensor" "of" ) type-reference ) .

The type a function answers.

tracked pairs the type with the gradient tape only before a type’s name or array; anywhere else it is the name of a type. A type named tensor takes no type arguments here: tensor of would begin a tensor without its rank, which is refused.

Used in function-heading, callback-heading.

fails-clause

fails-clausefails-clause = "fails" type-name .failstype-nametype-name
fails-clause = "fails" type-name .

The error domain a routine may fail with.

Used in function-heading, procedure-heading, task-heading, generator-heading, callback-heading, stream-type.