tiny.syn.state
Defined in tiny.syn.
What one line of a document leaves open for the next: nothing, or an unfinished comment or string together with the language that opened it.
API (2)
Types and contracts
Public types and contracts.
Mode: One kind of comment or string that is still open at the end of a line.State: The continuation state: what the last scanned line left open.
Source
Source: lib/syn/src/root.zig:41
zig
pub const state = @import("state.zig");Source: lib/syn/src/state.zig
zig
//! What one line of a document leaves open for the next: nothing, or an unfinished comment or//! string together with the language that opened it. A scan that walks a document line by line has//! to know whether the previous line ended inside a comment or string. That fact means something//! only in the language that produced it: a C-style comment left open means nothing to a following//! line of Python. Each kind of unfinished comment or string closes with its own delimiter, so the//! record has to say which kind is open.//!//! The record (*continuation state*) pairs the open kind with the language that opened it, and a//! line in any other language starts fresh. Eight kinds (*continuation mode*) cover every scanner,//! and each language may leave open only the kinds its scanner produces, which the code checks with//! assertions. The record holds a language and nothing about which document it came from, so a//! caller that moves to another document clears it. A Lean model in `verification/syn` proves that//! a record carried from another language gives the same result as a fresh scan, and property tests//! check the Zig code against it.//!//! - *owning language*: the language that left the comment or string open.const std = @import("std");const syn = @import("root.zig");const Language = syn.Language;/// One kind of comment or string that is still open at the end of a line. Each mode has its own/// closing delimiter, which the next line looks for. A caller reads it from `State.mode` to learn/// which comment or string a line left open.pub const Mode = enum { /// An open `/*` comment, closed by `*/`. C, C++, C#, Java, JavaScript, TypeScript, Go, Rust, /// PHP, Swift, Kotlin, Scala, Dart, Zig and CSS can leave it open. block_comment, /// An open Haskell `{-` comment, closed by `-}`. haskell_comment, /// An open Lua `--[[` comment, closed by `]]`. lua_comment, /// An open `<!--` comment in HTML, XML, Vue or Svelte, closed by `-->`. markup_comment, /// An open Python string that began with `'''`, closed by `'''`. The opening quotes may follow /// a prefix such as `f` or `rb`. python_triple_single, /// An open Python string that began with `"""`, closed by `"""`. python_triple_double, /// An open TOML literal string that began with `'''`, closed by `'''`. A backslash has no /// special meaning inside it. toml_triple_single, /// An open TOML basic string that began with `"""`, closed by `"""`. A backslash escapes the /// next character, so `\"""` leaves the string open. toml_triple_double, /// Returns true when the scanner for `language` can leave this mode open. `State` checks it /// before it stores a mode, and the tests use it to pin down which language can leave which /// mode open. A test compares the answer for every language and mode against a fixed list of /// pairs. pub fn admittedBy(self: Mode, language: Language) bool { return switch (self) { .block_comment => switch (language) { .zig, .c, .cpp, .csharp, .java, .javascript, .typescript, .go, .rust, .php, .swift, .kotlin, .scala, .dart, .css, => true, else => false, }, .haskell_comment => language == .haskell, .lua_comment => language == .lua, .markup_comment => switch (language) { .html, .xml, .vue, .svelte => true, else => false, }, .python_triple_single, .python_triple_double, => language == .python, .toml_triple_single, .toml_triple_double, => language == .toml, }; }};const Continuation = struct { language: Language, mode: Mode,};/// The continuation state: what the last scanned line left open. A caller keeps one for each/// document and passes it to every `highlightLine` call for that document. The value `.{}` is the/// fresh state for the start of a document. The struct holds no pointers and needs no cleanup. The/// state records no document, so a caller that moves to another document calls `reset`.pub const State = struct { /// The open mode with its owning language, or null when nothing is open. The package changes it /// only through `reset`, `rebind` and `retain`, which keep the mode admitted by its language. continuation: ?Continuation = null, /// Clears the state, so nothing is open, for a caller starting a new document. pub fn reset(self: *State) void { self.* = .{}; } /// Keeps the open mode when `language` owns it, and clears the state otherwise, for /// `highlightLine` at the start of each line so an open comment or string from another language /// never reaches this one. The call asserts that a stored mode is admitted by its owning /// language. pub fn rebind(self: *State, language: Language) void { const continuation = self.continuation orelse return; std.debug.assert(continuation.mode.admittedBy( continuation.language, )); if (continuation.language != language) self.reset(); } /// Stores `next_mode` with `language` as its owner, or clears the state when `next_mode` is /// null, for `highlightLine` after scanning a line to store what the line left open. The call /// asserts that `language` admits `next_mode`. pub fn retain( self: *State, language: Language, next_mode: ?Mode, ) void { if (next_mode) |actual| { std.debug.assert(actual.admittedBy(language)); } self.continuation = if (next_mode) |actual| .{ .language = language, .mode = actual } else null; } /// Returns the owning language, or null when nothing is open, for a caller or a test reading /// which language left something open. pub fn owner(self: *const State) ?Language { const continuation = self.continuation orelse return null; return continuation.language; } /// Returns the open mode, or null when nothing is open, for a caller or a test reading which /// comment or string is open after a line. pub fn mode(self: *const State) ?Mode { const continuation = self.continuation orelse return null; return continuation.mode; }};Audit
| Definitions | 1 |
|---|---|
| Public names | 1 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |