This page describes the lexical elements of the Amber language, except for keywords and symbols which are described elsewhere.
Amber is a case-sensitive language. The following identifiers are all different:
An Amber class name is an Upper identifier. The following are Amber class names in the preferred style:
It is also possible to use a class name that is not an Upper identifier (perhaps because the class is written in a language other than Amber) by preceding an Extended identifier with a double at-sign, for example:
An Amber feature name is a Lower identifier. The following are Amber feature names in the preferred style:
It is also possible to use a feature name that is not a Lower identifier (perhaps because the feature is written in a language other than Amber) by preceding an Extended identifier by an at-sign, for example:
An Upper identifier is used to form class names. It consists of an upper-case letter (A..Z) followed by zero or more of the following:
A Lower identifier is used to form feature names. It consists of a lower-case letter (a..z) followed by zero or more of the following:
An Extended identifier is used to form extended (non-Amber) class names and feature names. It consists of one or more of the following:
Spaces and tab characters are significant within:
One or more spaces or tab characters must be used to separate consecutive tokens in the following cases:
In other situations, spaces and tab characters are not significant and are ignored.
Line-ending characters ("carriage return" and "newline"/"linefeed") terminate a comment. They represent themselves within multi-line manifest string constants. They are not allowed within single-line manifest string constants.
Line-ending characters are used to disambiguate constructs that use parentheses. In other situations, line-ending characters are not significant and are ignored.
If a left-parenthesis is the first non-whitespace character on a line (or after a semicolon), it is an expression-grouping parenthesis, otherwise it is the parenthesis that opens the argument list of a call. Therefore, the following code fragment consists of one call chain:
a.b(c).d
On the other hand, the following code fragment consists of two separate calls:
a.b (c).d
A comment starts with two consecutive hyphens (--) and continues until the end of the line, for example:
-- A comment x := y + z -- Another comment
A manifest integer constant comprises one or more digits (0..9), for example:
0 00042 31472182
Negative integers are formed by preceding a manifest integer constant with a unary minus operator, for example:
-0 -42
As at September 2005, manifest integer constants are limited to 2147483647. In the future it is likely that Amber will accept arbitrarily-long manifest integer constants.
Manifest integer constants represent an object of type INTEGER, however there is an alternative form preceded by "@" that represents a native Parrot Integer PMC:
@42 -@5
Manifest character constants are formed by enclosing a character within single-quotes, for example:
'a' '9' '$' '''
The following escape sequences are recognised:
Amber accepts two varieties of manifest string constant: single-line and multi-line.
A single-line string is enclosed by doublequote characters, and may contain any character except doublequote, carriage return or newline. Within a single-line string, every character stands for itself except for the backslash which always introduces an escape sequence.
The following escape sequences are allowed within single-line strings:
These are valid single-line strings:
A multi-line string is enclosed within triple-doublequotes. It may include any character (including doublequote, carriage return and newline). However, no more than two consecutive doublequote characters may be appear except at the end. If more than three consecutive doublequote characters appear at the end of a multi-line string, the rightmost three terminate the string and the remaining doublequotes are part of the string.
Within a multi-line string, no escape codes are recognised. The following are valid multi-line strings:
"""A one line 'multi-line' string"""
"""""" -- An empty 'multi-line' string
"""He said "Hello!"""" -- Internal doublequotes are no problem
"""Line with a trailing newline Line without a trailing newline"""
"""Line with a trailing newline Line with a trailing newline """
""" A special-case (see the following explanation) """
In the last example above, the opening triple-doublequote appears at the start of a line and is followed immediately by a line-ending. In this case, the first line-ending is discarded. This special-case enables multi-line text to be cut-and-pasted directly into the body of a multi-line string without the number of line-endings being changed.
A Manifest Boolean constant is one of the following two literal values: