Skip to documentation
SLOP

tiny.syn

Reference tiny.syn

Overview · API · Code relationships · Verification · Audit

Overview

The package marks up source code for display one line at a time: for each line it records which byte ranges are keywords, strings, comments or other roles. Colors, themes and terminal formatting stay with the caller. The package reads the caller's text in place and keeps no copy of it.

A program that shows code to a person, in a terminal or an editor pane, wants each token marked by its role, so the reader can tell a string from a comment at a glance. Such a program meets many languages, forty here, and often knows a language only by a file name or a short name such as py or bash. Text reaches the program a line at a time, and a comment or string opened on one line can run on into the lines after it.

A line cannot be marked correctly without knowing whether an earlier line left a comment or string open, so that fact has to travel from one line to the next. That fact belongs to one language: a comment left open in C-like code means nothing to a following line of Python, and carrying it across would mark the Python wrongly. A program with a fixed memory budget wants the room for one line's marks set aside in advance, but lines have no fixed length. Dropping the marks of a line that does not fit cannot drop that line's effect on what stays open, because the line can itself open or close a comment.

The highlighting of bat and syntect, and the language modes of CodeMirror, are the sources of three ideas the package keeps: a syntax state carried from line to line, processing text one line at a time, and marks that name a token's role and leave its look to the renderer.

One call scans one line with the scanner for its family of languages and records each marked range (span) as a start byte, an end byte and a role. The scanners are hand-written byte scanners, one per family of languages, and they build no syntax tree. The caller keeps a small value between calls (continuation state), State. State records the open comment or string (its mode, State.Mode) together with the language that opened it, and a line in any other language starts fresh. The caller creates one buffer for spans (span storage), SpanStorage, sized by the longest line whose spans it wants kept, and no scan allocates after that. A longer line (discarded line) is still scanned in full, so what it leaves open stays exact, but its spans are dropped and the call reports the line as discarded. The two properties about carried state and discarded lines are proved for a model of the scanner in verification/syn, and property tests check the Zig scanners against that model.

Definitions

Actions

Public operations.

Types and contracts

Public types and contracts.

Namespaces

Public namespaces.

Code relationships

Direct static dependencies extracted from parsed source by semantic graph analysis.

Uses: tiny.hypothesis, tiny.machine
Used by: None

Verification

No verification records are cataloged for this module in this build.

Audit

EvidenceValue
Sourcelib/syn/src/root.zig
Definitions20 of 20 documented
Members0 of 0 documented
Public names20 API, 109 indexed
Version26.7.0
Revisiondaab053ee433