LEVIATHAN v962456e · 962456eee1

Lexical

Punctuation, operators and template literals

The punctuation and operator tokens, and the backtick-delimited template literal passed to macros and rules.

since 0.1.0-alpha.1linuxwindowswasm

Description

Besides names, keywords and literals, the lexer reads these tokens:

::  :  ;  ,  .  ..  (  )  {  }  [  ]
=>  =  ==  !=  !  <  >  <=  >=  +  -  *  /  %
+=  -=  *=  /=  %=  <<  >>  &&  ||  &  |  ^  ~  ?  ??  ?.  @

The longest match wins, so <= is one token and >> is one token. .. is the range operator, ?. is optional chaining, ?? supplies a default for None, => introduces a lambda or method body, :: qualifies a name and @ starts an attribute.

A text between backticks is a template literal. It is read as raw text and handed to a macro or a rule, which turns it into code.

A template literal delivers raw text to a macro

macro keepRaw(string payload) comptime {
    return meta::parseExpr("${payload.length()}");
}
int n = keepRaw!(`<Text title="demo"/>`);
console.writeln(n);
20

Rules

  • Tokens are matched greedily: the longest valid token is read.
  • .. is a single token. In 0xA..0xF it separates two hexadecimal integers; a dot followed by a digit after a hex literal (0x1.5) is an error.
  • A backtick-delimited literal is raw text. In templates used by rules, $name marks a hole to fill in.
  • attribute, comptime, rule, macro and as are contextual keywords; see lang.keywords.

Examples

Several of the tokens in one program:

Operators and range tokens

int a = 7;
a += 3;
console.writeln(a % 4);
console.writeln(a >> 1);
console.writeln(a != 10 || a == 10 && 1 < 2);
console.writeln(~0);
Range r = 1..3;
console.writeln(r.end);
2
5
true
-1
3

Notes

Operator precedence and the meaning of each operator belong to the expression rules, not to the lexer.