Lexical analysis cuts character streams into located tokens and prepares document source kinds for shared later stages.

From characters to located tokens

A parser that stares at an undifferentiated character stream spends its life guessing. Where does the identifier end? Is = part of == or a lonely assignment? Did a string already open?

Tokenization (lexical analysis) cuts the stream into tokens: small classified chunks with spelling and source locations. Later stages stop arguing about character boundaries and start arguing about meaning.

Token boundaries and spelling

A token itself is two fields: a kind and a source location span pointing back at the file.

The text doesn't ride along inside the token. The kind carries a small handle instead, and the real payload lives in a side table the file owns. A quoted string resolves to its decoded value, while a numeric literal keeps the authored text so diagnostics can quote what you typed. Interning text once and passing handles keeps tokens small and comparisons cheap.

Take the page dependency clause:

@greeting greet

A lexer might emit a sequence in this spirit:

Spelling

Kind family

@greeting

Path, a handle into the file's path table

greet

Symbol, a handle into the string table

Notice what the lexer does not record: that @greeting names a dependency, or that greet is a function. It emits a path handle and a symbol handle. Header processing decides what those mean later.

Identifiers, delimiters, operators and literals each become discrete pieces. The parser will arrange them. The type checker will judge them.

Marking boundaries is the headline job, though not the only one. The lexer also classifies each piece, tracks locations, tracks string and template delimiter context, scans numeric text and raises its own spacing and directive diagnostics. What it leaves alone is meaning.

Source locations

Every useful diagnostic eventually points at text the author wrote. Locations make that possible without forcing every later stage to keep the entire file string beside every node forever.

A span says "these characters in this source identity." When interface binding later fails to find greet, the diagnostic can underline the selected name rather than shrug at "somewhere in the module."

Tokenization attaches locations. It does not decide that greet is a function. That decision waits for binding and AST.

Code and document source kinds

Not every file starts in the same lexical mode.

Those three are the whole list. .bst is not a compiler source kind, and a Moth-native build-script system stays deferred.

Those paths converge. Moth template preparation synthesises an ordinary private content #String declaration whose initializer is a Markdown template over the body tokens. Markdown preparation reaches the same shape by a blunter route: it renders the raw Markdown to HTML first, then hands that HTML over as a synthetic string literal. Later stages see ordinary declarations, not a forever-special document dialect in HIR.

The greeting project uses .moth files. Document kinds still matter because the same compiler owners serve docs sites and content dependencies without a second semantic pipeline.

Each source kind gets its own way in. After preparation they all look like ordinary declarations, so no later stage needs a document dialect.

Why avoid duplicate pipelines for documents and code? Because a second pipeline is a second place for meaning to drift.

Lexical failures

Some mistakes die at the first cut:

moth-lexical::numeric owns shared numeric grammar, normalisation, separators and exponents. Its identifier and source-word modules also own the shared shape and reservation law. The compiler tokenizer maps neutral word identities to its own token tags and diagnostics. Lexical numeric failure is not yet a semantic Dec versus Byte debate. Semantic numeric types arrive later. Keep those layers separate when you read diagnostics.

Boundary of ownership

Tokenization recognises directive spelling through the frontend directive registry, locations and delimiter context. It preserves enough metadata for later placement and availability diagnostics; it does not implement directive semantics or silently skip unsupported forms.

It does not:

If a stage after the lexer rediscovers token boundaries from raw text, something has slipped.

What can fail here

Defer semantic errors to their owning stages. A good lexer stays humble.

Why Moth does it this way

Alternative: give each source kind a separate parser and backend pipeline.

Choice: adapt source kinds early, then share declaration and semantic stages.

Benefit: one semantic owner for ordinary declarations and runtime lowering. Document content becomes data the rest of the compiler already understands.

Cost: source-kind adapters must preserve locations and keep boundaries crisp.

Roadmap and current status

Exit state

You leave with located tokens and source-kind prepared payloads. Next: which declarations and dependency relationships those tokens describe.

Compiler design / Previous / Next