Literals

In the example below, long is a literal. Literals can appear in various contexts in an MF2 message:

  • As a key in a variant in a matcher.
  • Inside an expression:
    • An expression can appear in a pattern, in a declaration, or in a variant in a matcher.
  • As the value of an option in markup or an annotation (such as a function).

For example, the right-hand side (part appearing to the right of an '=' sign) of an option can be either a variable or a literal. You can tell that it's a literal in this case because it doesn't begin with $. Rarely, literals have to be quoted (enclosed in | / | characters).

Today is {$date :datetime weekday=long}.
{ "date": "2024-06-06" }

The text |image.png| is an example of a quoted literal. It has to be quoted because it includes a . character. In general, literals containing non-alphanumeric characters have to be quoted.

Some functions have option values that need to be quoted. For example:

The year is {$date :datetime year=|2-digit|}.
{ "date": "2024-06-06" }

Note that the option value 2-digit is quoted, because it includes a - character.

Unquoted literals Jump to heading

An unquoted literal can either be a number, or a string of alphanumeric characters beginning with an alphabetic character or underscore (_).

.local $x = {42}
.local $y = {number42}
.local $z = {_number}
{{{$x} {$y} {$z}}}

The curly braces are part of the expression that each of the variable names is bound to; not part of the literals themselves.

Note that MF2 is untyped. Like all other literals, number literals are treated as strings within the formatter. The distinction between number literals and other unquoted literals is purely syntactic. To handle number literals as numbers, functions must parse them from their string values.

(This is a simplification; for an exact specification of which characters are allowed in unquoted literals, see the name and number-literal productions in the formal grammar for MF2.)

Quoted literals Jump to heading

Quoted literals can include almost any character.

.local $x = {|@literal|}
.local $y = {|white space|}
.local $z = {|{{curly braces}}|}
{{{$x} {$y} {$z} {|and \\, a backslash|}}}

Note that the quoted literal |and \\, a backslash| also appears in curly braces, because it's inside a pattern and thus has to be an expression.

For an exact specification of which characters are allowed in quoted literals, see the quoted-char production in the formal grammar for MF2.

On this page