# Lexical structure

### Comments

`// to the end of the line` and `/* block */`.

The compiler copies code comments to JavaScript near the statements they describe. Comments
inside expressions or attached to erased declarations may move to a nearby generated line.
File headers also appear in worker modules, along with comments on the declarations they copy.

### Semicolons

They are written only to separate several statements on one line. Otherwise the lexer inserts them
by Go's rule: at the end of a line if its last token is

- an identifier or a literal (a number, a string, `true`, `false`, `null`);
- `this`, `return`, `break` or `continue`;
- `++`, `--`, `)`, `]`, `}`.

Hence, as in Go:

- the opening `{` is on the same line as `if`, `for`, `func`, `class`;
- when a long expression is split, the binary operator stays at the end of the line;
- no semicolon is needed before `}`: `if ok { return x }`.

**Unlike Go**, no semicolon is inserted if the next line starts

- with `.` or `?.`, so the familiar chains of calls work;
- with `?` or `:`, so a conditional expression can be split before its operators;
- with a closing `)`, `]` or `}`, so a comma after the last item of a multi-line list is optional.

A line may end with `>` too: after the type arguments of a declaration, `votes Map<string, number>`,
the line break ends it, while `a >` with a value on the next line goes on, as other operators do.
A line that ends with `x!` (see "null and null safety") ends there too.

```go
const names = users
    .filter(u => u.active)
    .map(u => u.name)

const point = {
    x: 1,
    y: 2
}

const label = count == 0
    ? "empty"
    : `${count} items`
```

### Literals

- Numbers as in JS: `42`, `3.14`, `1e3`, `0xff`, `1_000_000`; a bigint ends with `n`: `10n`,
  `0xffn`.
- Strings: `"..."`, `'...'` and template strings `` `Hello, ${name}` ``.
- `true`, `false`, `null`.
- Regular expressions as in JS: `/ab+c/gi`, a `RegExp`. Where a value cannot end, `/` starts one,
  and after a value it divides: `a / b / c`. The compiler checks the pattern and the flags, as JS
  does when the module loads, so `/(/` is a compile error.
- Arrays `[1, 2, ...rest]` and objects `{ name: "Ann", age, "content-type": t, ...defaults }`
  (the shorthand `age` and spread are as in JS). A method is written in an object as in a class,
  `{ fetch(req Request) Response { ... } }`, with the types of a `func` literal, and is a method in
  JS. Its `this` would be the object, not the class around it, so it cannot be used there.
- A tagged template, `` sql`select * from users where id = ${id}` ``, calls the function before it
  with the strings, a `TemplateStringsArray`, and the values, as in JS: the values are checked as
  the arguments after the strings, `func sql(parts TemplateStringsArray, ...values any[])`. The tag
  is on the same line as the template.

### Keywords

```
func  comp  return  let  const  interface  class  extends  new  this  super
if  else  for  in  break  continue  switch  case  default  defer
try  catch  finally  throw  import  export  async  await  go  chan
null  true  false  typeof  instanceof
```

Inside a class declaration `static`, `private`, `protected`, `public`, `readonly`, `abstract` and
`implements` are keywords too, and so are `get` and `set` before the name of a method; at the
start of a statement before a name `type` declares a type, `type Id string`, and elsewhere it is a
name, `const type = blob.type`; before
`class` so are `data` and `abstract`, in `import` and `export` so is `from`, at the start of a statement in a component's body
`state`, `derived`, `ref`, `style`, `mount` and `effect`, at the start of a top-level declaration `dec`, after its
parameters `needs` and `includes`, in a decorator's body `public`, before `{` at the start of a statement
`select`, and `queue` in a type before the type of its values and before the `<` that makes a
queue. Elsewhere they are ordinary names. Decorators are written before `comp`: `@html-tag` (see
"Web components") and those declared with `dec` (see "Decorators").

After `.` and in object keys, keywords can be used as ordinary names: `xs.map(f)`, `event.type`,
`{ default: 1 }`.

Predeclared identifiers (not keywords, they can be redeclared): the types `number`, `string`,
`bool`, `any`, `error`, `void`, `never` and the functions `error()`, `close()` (of channels) and the others of
goroutines and channels: `transfer()`, `after()`, `tick()`, `cancel()` and `stream()`.

Names that start with `$$` are reserved for generated code (`$$defer`, `$$0`). Names that are valid
in Rook but not in JS (`delete`, `static`, `function`…) get a `$` suffix in the output:
`delete` → `delete$`.

