Lexical Structure
This document describes the low-level lexical structure of Mux: tokens, keywords, identifiers, literals, and comments.
Source Files
Mux source files use UTF-8 encoding. File extension is .mux.
Case Sensitivity
Mux is case-sensitive. Keywords, identifiers, and type names must match the exact case as defined.
Whitespace
The only significant whitespace in Mux is the Newline character (\n), which separates statements. Other whitespace characters (space, tab, carriage return) are ignored except for separating tokens.
Line continuation
A newline inside an open ( or [ continues the expression instead of ending
the statement, so a long call, a chain of operators, or a collection literal can
be wrapped across lines.
A trailing comma before the closing bracket is allowed, in a call as well as in a collection literal, so adding an entry stays a one-line change.
Braces are not continuation brackets. { opens both a block and a map or
set literal, and those are not distinguishable at this level, so a newline
inside { still separates statements - which is what makes an ordinary function
body work:
A map or set literal therefore spans lines only when it is itself inside parentheses or brackets.
Comments
Single-Line Comments
Single-line comments start with // and continue to the end of the line:
Multi-Line Comments
Multi-line comments start with /* and end with */. They can span multiple lines:
Multi-line comments do not nest. The first */ terminates the comment.
Identifiers
Identifiers name variables, functions, types, and other entities.
Rules
- Must start with a letter or an underscore
- Can contain letters, digits, and underscores
- An identifier that starts with an underscore needs at least one more
character, because a lone
_is the placeholder - Cannot be a reserved keyword
As a pattern:
[a-zA-Z][a-zA-Z0-9_]* | _[a-zA-Z0-9_]+Valid Identifiers
A leading underscore carries no meaning to the compiler. It is a convention, shared with Rust, Python, Go, C and JavaScript, for marking something deliberately unused or private to its module.
Note: Most Mux code uses snake_case, with some exceptions of course, but this is a convention, not a requirement.
Invalid Identifiers
Underscore Placeholder
A lone _ is not an identifier at all. It is a placeholder, and it is the one
spelling a name cannot take:
The underscore has special semantics:
- Cannot be read (assigning to
_discards the value) - Multiple uses of
_in the same scope do not conflict, including several in one signature:func f(int _, string _)is fine
None of this applies to a name that merely starts with an underscore. _x is
an ordinary identifier: it binds, it can be read, and a second _x in the same
scope is a duplicate like any other.
Keywords
The following words are reserved keywords and cannot be used as identifiers:
Declaration Keywords
| Keyword | Purpose |
|---|---|
func | Function declaration |
returns | Return type specification for functions |
const | Constant declaration |
auto | Type inference declaration |
class | Class declaration |
interface | Interface declaration |
enum | Enum declaration |
common | Static/class method |
where | Runtime constraint clause on declarations |
Control Flow Keywords
| Keyword | Purpose |
|---|---|
match | Pattern matching expression |
if | Conditional expression |
else | Else branch of conditional |
for | Iteration loop |
while | While loop |
break | Exit loop |
continue | Skip to next iteration |
return | Return from function |
Module Keywords
| Keyword | Purpose |
|---|---|
import | Module import |
as | Import alias |
Operator Keywords
| Keyword | Purpose |
|---|---|
is | Type constraint / interface implementation |
in | Membership test |
true | Boolean true literal |
false | Boolean false literal |
none | optional none literal |
some | optional some literal |
ok | result ok literal |
err | result error literal |
Special Keywords
| Keyword | Purpose |
|---|---|
self | Instance reference (inside class methods only) |
Literals
Integer Literals
Decimal integers:
Float Literals
Character Literals
Single Unicode code point in single quotes:
String Literals
UTF-8 sequences in double quotes:
Escape sequences:
\\- Backslash\"- Double quote\'- Single quote\n- Newline\t- Tab\r- Carriage return
Boolean Literals
none Literal
Operators and Punctuation
Arithmetic Operators
| Symbol | Meaning |
|---|---|
+ | Addition |
- | Subtraction / Negation |
* | Multiplication |
/ | Division |
% | Modulo |
** | Exponentiation |
Comparison Operators
| Symbol | Meaning |
|---|---|
== | Equality |
!= | Inequality |
< | Less than |
<= | Less than or equal |
> | Greater than |
>= | Greater than or equal |
Logical Operators
| Symbol | Meaning |
|---|---|
&& | Logical AND (short-circuit) |
| ` | |
! | Logical NOT |
Other Operators
| Symbol | Meaning |
|---|---|
= | Assignment |
+= | Compound assignment |
-= | Compound assignment |
*= | Compound assignment |
/= | Compound assignment |
%= | Compound assignment |
++ | Postfix increment |
-- | Postfix decrement |
. | Member access |
& | Reference creation |
* | Dereference |
Punctuation
| Symbol | Usage |
|---|---|
( ) | tuple literals, Grouping, function calls, parameters |
[ ] | List literals, indexing |
{ } | Block statements, map literals, set literals |
< > | Type parameters (generics) |
, | Separator |
Line Continuation
Mux does not support implicit line continuation. Each statement must be on its own line. The only way to continue an expression across lines is with explicit grouping:
See Also
- Language Guide - Practical examples