Esta página aún no está traducida al español. Leer la versión en inglés

Lexical structure

How Antril source text is read - characters, whitespace, comments and doc comments, identifiers, keywords and contextual words, literals of every kind, operators and punctuation.

Última actualización: 8 min de lectura Editar en GitHub

En esta página

An Antril source file (.ant) is text in UTF-8. The compiler first splits it into tokens: identifiers, keywords, literals, operators and punctuation. This page describes each kind.

Whitespace

Spaces, tabs, line breaks and any other Unicode white space separate tokens and are otherwise ignored. Line breaks have no meaning of their own: a statement ends at its ;, not at the end of the line.

Indentation does not change what a program means. The convention for access sections is one tab for the label and two for its members.

Comments

Form What it is
// … A comment to the end of the line.
/* … */ A comment that can span lines. It does not nest: the first */ ends it. An unterminated one is error AN0002.
/// … A documentation comment: a line comment placed right before a declaration, which the editor shows on hover and larva show prints.
AntrilRun
import System;

/// The area of a rectangle.
/// Lines of a documentation comment are joined.
Area(f64 width, f64 height) : f64 => width * height;

Main()
{
	/* A comment
	   over two lines. */
	Console.WriteLine(Area(2, 3)); // 6
}

Identifiers

An identifier starts with a letter or _ and goes on with letters, digits and _. Letters can be any Unicode letters: año, größe and 名前 are identifiers. Case matters: count and Count are different names.

Antril
i32 año = 2026;
i32 _hidden = 1;
str Name2 = "x";

Antril has no escaped identifiers: a keyword cannot be used as a name.

Keywords

These words are reserved everywhere and cannot be identifiers:

Files and namespaces import into namespace
Access and modifiers public friendly internal inherited fixed readonly static virtual override
Types class interface enum bitflag void bool new
Statements if else loop do in switch try catch finally throw return break continue
Values and operators true false null typeof sizeof is as declbits allbits
Number types i8 i16 i32 i64 i128 i256 u8 u16 u32 u64 u128 u256 iptr uptr f16 f32 f64 f128 f256

Contextual words

Many words have a meaning only in one place and are ordinary identifiers everywhere else. A variable can be called value, record or from:

Word Where it means something
private, file An access word or an access section label. See Access.
base base class; base.M() and : base(…); base get / base set in interfaces.
value, record value, record class, record value declarations; value inside a set accessor.
final, const, unsafe, async, extern Modifiers before a declaration.
attribute, exception Declarations of attribute and exception classes.
reactive, computed, reacts, effect Reactive code.
ext Extension blocks.
op, left, right, precedence, associativity Operator declarations.
func Function types: func<i32> : i32, func*<…>.
get, set Property accessors.
this The current object.
from Only inside an import.
await, yield Inside async functions and iterators.
delete, region, checked, unchecked Statements.
when, and, or, not, var, _ Patterns.
with, default, nameof, mut Expressions: x with { … }, default(T), nameof(x), &mut x.
str, any The types System.String<u8> and System.Any.
number, snumber, int, uint, float, numeric, integer, plain, copy, managed, unmanaged, fixedsize Constraint words.

Literals

Integer literals

Antril
42            // decimal
1_000_000     // `_` separates digits, anywhere after the first one
0xFF          // hexadecimal (0x or 0X)
0b1010        // binary (0b or 0B)
255u8         // with a type suffix
1_000_i64     // `_` may separate the suffix

Without a suffix, an integer literal takes the type it is assigned to, when it fits. When nothing is expected, it is the first of i32, i64, u64, i128, u128, i256, u256 that holds it. A literal that does not fit the type it goes to is an error: i8 x = 300i8; is AN2028.

A minus sign written before a literal makes it a negative literal, so -2147483648 is a valid i32.

Floating-point literals

Antril
3.14          // f64
2.5e3         // 2500: an exponent with e or E, and an optional sign
1e-3f32       // with a suffix
3f16          // an integer with a float suffix is a float

A floating-point literal is written in decimal, with digits on both sides of the point: 0.5, not .5 or 5.. Without a suffix it is an f64, or the floating-point type it is assigned to.

Type suffixes

A suffix names the literal's type: i8 i16 i32 i64 i128 i256 u8 u16 u32 u64 u128 u256 f16 f32 f64 f128 f256. There is no suffix for iptr and uptr; cast instead ((uptr)8).

Boolean and null

true and false are the two bool values. null is the empty value of a nullable type.

Character literals

A character literal is one character between single quotes: 'A', 'é', '😀'. Antril has no character type: a character literal is the number of its Unicode code point. Its type is the integer type expected where it goes, or u16 when nothing is expected (u32 for a character outside the Basic Multilingual Plane, such as '😀').

AntrilRun
import System;

Main()
{
	u8 letter = 'A';
	u32 smile = '😀';
	Console.WriteLine(letter);
	Console.WriteLine(smile);
	Console.WriteLine(Char.IsDigit('7'));
}
Terminal
larva run
65
128512
true

An empty literal, or one with more than one character, is error AN0005.

String literals

A string literal is text between double quotes, on one line. Its type is str.

Escape Character
\n line feed
\t tab
\r carriage return
\0 the character 0
\\ backslash
\" double quote
\' single quote

Any other character after \ stands for itself: \q is q. There are no numeric escapes; write the character itself ("año"), or build the text from code points. An unterminated string is error AN0004.

Warning

Not yet available: a \\ followed by more text in the same literal is lost when the program runs: "a\\b" gives ab (two characters). A \\ at the end of a literal, as in "x\\", works.

Interpolated strings

$"…" is a string with holes. Each {…} holds an expression, an optional alignment after , and an optional format after :. {{ and }} stand for the braces themselves.

AntrilRun
import System;

Main()
{
	f64 price = 3.5;
	i32 count = 4;
	Console.WriteLine($"{count} x {price} = {price * count}");
	Console.WriteLine($"[{count,4}] [{count,-4}] [{price:F2}] {{literal}}");
}
Terminal
larva run
4 x 3.5 = 14
[   4] [4   ] [3.50] {literal}

The details (what a format does, how alignment pads) are in Expressions.

Warning

Not yet available: a hole cannot hold a string literal ($"{ok ? "yes" : "no"}" is error AN0007). Put the value in a variable first.

Resource literals

$resource("name") gives the bytes of a file built into the program with a RESOURCES chunk in its .queen file. See Expressions.

Operators and punctuation

Tokens Use
( ) [ ] { } Grouping, calls, indexing, arrays, blocks.
; , : . Statement end, lists, types and bases, member access.
.. ..= Ranges.
... A pack of type parameters (T...).
=> Expression bodies, lambdas, switch arms.
-> A member through a pointer (p->x).
?. ?? ? Null-conditional access, null coalescing, nullable types and the conditional operator.
+ - * / % Arithmetic. * also makes pointer types and reads through pointers.
& | ^ ~ << >> Bitwise operators. & also takes addresses and borrows.
! && || Logical operators.
== != < <= > >= Comparisons.
= += -= *= /= %= &= |= ^= <<= >>= Assignment.
++ -- Increment and decrement.
$ Starts an interpolated string or $resource.
@ # Only in operator symbols a program declares.

>> is read as two > tokens, so nested generic types close without a space: List<List<i32>>.

A program can declare new operator symbols made of the characters + - * / % & | ^ ~ ! < > = ? @ # $ written together, such as <+> or ^^. When several readings are possible, the longest declared symbol wins. See Operators.

A character that is not part of any token, such as `, is error AN0001.

¿Te resultó útil esta página?

No se guarda ninguna dirección; tu nombre de usuario sí, si iniciaste sesión.

Esc