vyrn

std/scan

15 exports, from std/scan.vyrn.

One comment- and string-aware cursor over foreign text (CSS, ICU messages, HTML templates, SDL) for generators.

Offsets are byte offsets. The scanner is a record advanced through modify functions, which keep line/col in sync. until, untilStr and balanced skip over quoted strings and line and block comments, so a delimiter inside one never ends the scan. Block comments do not nest: the first blockClose closes one, as in CSS and C.

Dependencies

The import graph shows the whole library.

Imports
Imported by
type Scanner = {
    src: String,
    pos: Int64,
    line: Int64,
    col: Int64,
    // The line-comment marker ("//", "#", ...); "" disables line comments.
    lineComment: String,
    // The block-comment open/close markers ("/*" and "*/" for CSS); "" for
    // `blockOpen` disables block comments. Non-nesting.
    blockOpen: String,
    blockClose: String,
    // Primary and secondary quote bytes (34 for ", 39 for '); -1 disables.
    quote1: Int64,
    quote2: Int64,
    // The escape byte inside a quoted string (92 for backslash); -1 disables.
    escape: Int64,
}

A cursor over src, with 1-based line/col and its lexical config.

fn newScanner(src: String) -> Scanner

A Vyrn-flavored scanner: // line comments, "/' strings, \ escape. Vyrn has no block comments, so blockOpen/blockClose are disabled.

fn scanner(
    src: String,
    lineComment: String,
    blockOpen: String,
    blockClose: String,
    quote1: Int64,
    quote2: Int64,
    escape: Int64,
) -> Scanner

A fully configured scanner. Pass "" for lineComment/blockOpen to disable that comment kind and -1 for any disabled byte.

fn quotedString(sc: modify Scanner) -> Option<String>

If the cursor is at a quote, consumes the quoted run and returns its inner text with escapes as written; the caller decodes its own dialect. None if not at a quote. An unterminated string returns the text to end of input.

fn until(sc: modify Scanner, stop: Int64) -> String

Consumes up to, not including, the first top-level byte stop, skipping quoted strings and comments, and returns the consumed text. Stops at end of input if stop never appears at the top level.

fn untilStr(sc: modify Scanner, stop: String) -> String

Like until, but for the multi-byte string stop. The cursor is left on stop.

fn balanced(sc: modify Scanner, open: Int64, close: Int64) -> String

With the cursor at byte open, consumes the balanced region through the matching close (nesting, string and comment aware) and returns the text between them. An unbalanced region returns everything to end of input. Not at open: returns "" and consumes nothing.

Tests

7 test blocks in std/scan.vyrn. CI runs all of them.

Test names
  • until is string- and line-comment aware
  • balanced is nesting- and string-aware
  • cssScanner: a block comment hides a delimiter from until
  • cssScanner: balanced reads through a brace inside a block comment
  • cssScanner: skipWs consumes block comments
  • cssScanner: an unterminated block comment consumes to end of input
  • a disabled block-comment marker leaves /* as ordinary bytes

Examples

    No example program imports this module.