Skip to content

Latest commit

 

History

History
212 lines (162 loc) · 4.68 KB

lexer.md

File metadata and controls

212 lines (162 loc) · 4.68 KB

Lexical Specification

This document defines the lexical grammar of Dijk Core: the rules by which raw source text is divided into tokens. It is written before the lexer implementation, and the implementation should be judged against it.

The lexer answers one question only:

What are the pieces of this source code?

It does not answer how those pieces are structured (the parser's job) or what they mean (semantic analysis's job). A token stream produced by the lexer for:

let x: i32 = 42;

contains IDENTIFIER("x") and IDENTIFIER("i32") as two syntactically identical tokens. The lexer does not know that x is a variable, or that i32 is a type in this position — both are simply identifiers. That distinction is introduced later, by semantic analysis.

Token categories

Identifiers

An identifier begins with an ASCII letter or underscore, followed by any number of ASCII letters, digits, or underscores.

foo
variable
my_function
User
_private

Unicode identifiers are explicitly out of scope for the initial lexer.

Integer literals

A contiguous run of ASCII digits.

0
42
12345

Hexadecimal, binary, octal forms, and numeric suffixes are out of scope for the initial lexer. The lexer does not convert the digits to a number or decide whether they are too large; the parser and the type checker do (docs/parser.md, docs/types.md).

String literals

Characters between a pair of unescaped double quotes, on a single line.

"Hello, world!"

Escape sequences are out of scope for the initial lexer. An unterminated string (no closing " before the end of the line or file) is a lexical error.

Keywords

The following identifiers are reserved and produce a dedicated token kind instead of IDENTIFIER:

fn
let
mut
if
else
while
for
return
struct
enum
true
false

This list is expected to grow only as constructs using these keywords are actually implemented. Primitive type names (i32, bool, ...) are deliberately not keywords at this stage — see the note above.

Operators

+   Plus
-   Minus
*   Star
/   Slash
%   Percent
==  EqualEqual
!=  BangEqual
<   Less
>   Greater
<=  LessEqual
>=  GreaterEqual
=   Equal
!   Bang
&   Amp
|   Pipe

Multi-character operators (==, !=, <=, >=) are matched greedily before falling back to their single-character prefix.

Punctuation

(   LeftParen
)   RightParen
{   LeftBrace
}   RightBrace
[   LeftBracket
]   RightBracket
,   Comma
:   Colon
;   Semicolon
.   Dot
->  Arrow

Comments

// a line comment, extending to the end of the line

Line comments are discarded and produce no token. Block comments are out of scope for the initial lexer.

Whitespace

Spaces, tabs, carriage returns, and newlines separate tokens and are otherwise discarded. Dijk Core is not whitespace-sensitive. Only these ASCII characters are whitespace: a non-breaking space, say, is not.

Non-ASCII text

Source files are UTF-8. A non-ASCII character is allowed inside a string literal or a comment, and nowhere else. Outside them it is an unexpected character error, reported once per character, with its Unicode code point, since it may be invisible or look like an ASCII one:

error: unexpected character 'é' (U+00E9)
 --> example.dk:2:12
  |
2 |     let café = 1;
  |            ^

Lexing continues after it with the next character.

End of file

The lexer always terminates the token stream with a single Eof token, so that a parser never needs to special-case running out of input.

Explicitly deferred

The initial lexer does not support:

- Unicode identifiers
- string escape sequences
- block comments
- hexadecimal / binary / octal integer literals
- floating-point literals
- macros or string interpolation
- indentation-sensitive syntax

These may be introduced later, once a reason to need them exists.

Token representation

Token
├── kind     — which category of token this is
├── lexeme   — the exact source text this token was built from
└── span     — the byte range [start, end) this token occupies

Byte offsets are sufficient for now. Line/column information for diagnostics is derived from the offset and the source text on demand, rather than stored on every token.

Errors

The lexer does not panic on invalid input. An unrecognized character or an unterminated string produces a lexical error carrying a span, which is rendered as a source-anchored diagnostic, e.g.:

error: unexpected character '@'
 --> example.dk:1:12
  |
1 | let x = 42 @ 10;
  |            ^

Lexing continues past an error where possible, so that a single pass can report more than one problem.