Syntax
Tokens and whitespace
Whitespace (spaces, tabs, newlines) is not significant anywhere in Jennifer. The lexer skips it between tokens. Statements end with ;, blocks with a matching }, never with indentation or a line break. A program written on one line or across many parses the same:
# canonical form
use io;
def x as int init 21;
io.printf("%d\n", $x + $x);# all three statements on one line - same program
use io; def x as int init 21; io.printf("%d\n", $x + $x);# split across many lines - also the same program
use io
;
def
x
as
int
init
21 ;
io.printf (
"%d\n"
,
$x
+
$x
)
;The same holds for qualified (namespaced) calls. All three print the host OS:
use io;
use os;
io.printf(os.PLATFORM); # canonical - tight everywhere
io.printf( os.PLATFORM ); # ugly - spaces inside the call parens
io . printf ( os . PLATFORM ); # uglier - spaces around `.` tooEach . is an ordinary token boundary. jennifer fmt rewrites all three into the canonical form; the style guide's "no space around ." is a style rule, not a syntax rule.
Consequences:
jennifer fmtenforces style. The style guide describes the canonical shape (one space around binary operators, no space inside(/[/{, 1TBS braces, tight.in qualified calls, ...) andfmtre-emits any well-formed source in that shape.- The first line may be a
#!shebang because#starts a line comment; everything from#to the next\nis whitespace to the parser. - Inside a string literal, whitespace is content. A space, tab or newline between the quotes is part of the string value.
Comments
# line comment - runs to end of line
/* block comment -
can span multiple lines */Block comments nest, so /* ... */ can wrap code that already contains one. Because # starts a line comment, the first line of a script may be a Unix shebang:
#!/usr/bin/env -S jennifer run
use io;
io.printf("hi\n");(env -S splits the rest of the line into arguments, which is how jennifer run reaches the interpreter on Linux.)
/usr/bin/env is not universal: Android, some minimal containers and userlands under another prefix have no /usr/bin/env, and an old env lacks -S. There, name the interpreter by its absolute path; the kernel passes one argument after it, so run fits:
#!/opt/jennifer/bin/jennifer runjvc app install writes such a launcher by itself when /usr/bin/env is missing.
Source files normally must end in .j, but a file with a #! shebang first line is exempt: chmod +x it and install it under a bare command name (/usr/local/bin/mytool, no extension) and it runs as a normal command. Only the entry point is exempt - files it imports or includes still need .j.
Number literals
Decimal:
42
1_000_000 # `_` is a visual digit separator
3.14
1_000.000_5 # the mantissa side of a float accepts `_` tooNon-decimal integer prefixes:
0xff # hex
0xDEAD_BEEF
0o755 # octal
0b1010_0110 # binaryAll four bases produce ordinary int values. The _ separator is allowed between digits but never adjacent to the prefix, adjacent to another _, or at the start / end of the digit run (0x_ff, 1__000, 100_ are all lex errors).
Identifiers
- Variable, function, parameter and library names start with a letter, then letters and digits:
[A-Za-z][A-Za-z0-9]*, up to 64 characters. No underscores.sha256,x2andtoUtf8are legal. - Constants are uppercase chunks joined by single
_separators:[A-Z][A-Z0-9]*(_[A-Z][A-Z0-9]*)*, up to 64 characters. Each chunk starts with an uppercase letter and may contain digits.MAX,MAX_RETRIES,HTTP2,SHA256andSCRAM_SHA256are legal;_MAX,MAX_,MAX__INTandAES_256are not. - Variable references use a leading
$: definename, refer to it as$name. - Constant references are bare (no
$). - Function calls are bare and followed by
(...).