Source Files
A Rux program is written in source files with the .rux extension. Each file is UTF-8 text that the compiler reads as a sequence of tokens — identifiers, keywords, literals, operators and punctuation — separated by whitespace and comments.
Files and packages
The source files of a package live under its Src/ directory, beside the Rux.toml manifest. An executable package starts at Src/Main.rux, which defines Main; a library package conventionally starts at Src/Lib.rux. Files are named in PascalCase: Main.rux, HttpClient.rux, JsonParser.rux.
Hello/
├── Rux.toml
└── Src/
└── Main.rux
Package layout describes the directory structure, and Modules how declarations in several files and module blocks are named and imported.
Encoding
A source file must be valid UTF-8. Non-ASCII characters may appear inside comments and inside character and string literals; everywhere else the source is ASCII.
| Problem | Error |
|---|---|
| A byte that is not valid UTF-8 | error: source contains invalid UTF-8 byte 0xFF |
| A byte-order mark at the start | reported as an unexpected character, U+FEFF |
| A non-ASCII character outside literals | error: unexpected character 'é' (U+00E9) |
| A control byte outside literals | error: unexpected control byte 0x0C |
Save files as UTF-8 without a byte-order mark.
Whitespace and line endings
Whitespace is the space, the horizontal tab, the carriage return and the line feed. It separates tokens and is otherwise ignored: Rux has no significant indentation, and statements end with ;, not with a line break. Both LF and CRLF line endings are accepted.
let total = 1 + 2;
let same =
1
+ 2;
A string or character literal cannot contain a raw line break or tab; write \n and \t instead.
Tokens
The compiler reads the source from left to right and always takes the longest token that can start at the current position. That rule settles every spelling that could be read two ways:
| Source | Read as | Not as |
|---|---|---|
x<-1 | x <- 1 | x < -1 |
a**p | a * * p | a power |
a??b | a ?? b | a ? ?b |
0..=9 | 0 ..= 9 | 0 .. =9 |
x>>>=2 | x >>>= 2 | x >> >=2 |
So x<-1 is a move assignment into x, and on an immutable x it fails with error: cannot modify immutable variable 'x'. Write x < -1 to compare. a**p multiplies a by the value p points to.
Whitespace matters in exactly one place: before ?. A ? with whitespace before it is the conditional operator, c ? a : b; a ? that touches the expression before it is postfix propagation, value?. So c? 1 : 2 is reported as error: expected ';' after the binding declaration before '1'.
The Token Reference lists every token the lexer produces.
See also
- Comments — the other thing the lexer skips
- Package layout — where
Src/sits in a package - Hello — a first source file, explained