Skip to main content

Module lexer

Module lexer 

Source
Expand description

Lexer for Bynk v0.

Token kinds correspond to the terminals defined in the grammar (spec §3 and §4). Whitespace is skipped; // line comments are emitted as Comment tokens so the formatter can preserve them through round-trips (v1.1 LSP spec §3.5). Doc blocks (---) are emitted as DocBlock tokens, and one-line docs (--- text, #2203) as DocLine tokens, both lexed outside of logos (see tokenize).

#2203: the legacy spellings, a -- comment and a --| doc line, still lex as Comment and DocLine tokens, so every tool keeps working on an unmigrated file and the formatter can rewrite it. The parser reports each as an error with a machine-applicable fix (legacy_comment_error, legacy_doc_line_error); comment_is_legacy and doc_line_is_legacy tell the spellings apart.

Structs§

Token
A token plus its source span.

Enums§

StringPart
A highlighted part of a string literal (#2619), for the semantic tokens.
TokenKind
Token kinds. Discriminants without payload data; the lexeme is recovered from the source string via the token’s Span.

Constants§

MAX_SAFE_INT
#1657: the largest Int, Number.MAX_SAFE_INTEGER (2^53 − 1). An Int is a JS safe integer; the range is symmetric.

Functions§

comment_body
Extract the body of a Comment trivia token: everything after the leading // marker, preserving its inline whitespace verbatim. Used by the parser when attaching comments to declarations.
comment_is_legacy
#2203: whether a TokenKind::Comment token is spelled the legacy way, --, rather than //.
decode_string_literal
The value of a hole-free string lexeme (any form), for callers that hold the lexeme rather than the surrounding source. span locates the lexeme in the file so a (normally unreachable — the lexer already validated it) error points at the right place.
doc_block_body_range
The doc-block body as a byte range into source — leading/trailing --- marker lines stripped, no further processing (unlike doc_block_content, which additionally strips a common per-line indent — not offset-preserving). Callers that need to map a position in the body back to source (e.g. document-link spans) use this instead of re-deriving it from the string doc_block_content returns.
doc_block_content
Extract the body content of a doc-block token from its source span. Strips the leading and trailing --- marker lines and returns the body verbatim. If every non-empty content line begins with the same horizontal whitespace prefix (e.g., because the doc block sits inside a brace-form commons body), that common prefix is removed so the body reads naturally when emitted as JSDoc.
doc_fence_len
#1885: the dash count of the fence a doc block holding doc is printed with: one more than the longest marker-shaped line in it (a line of three or more dashes alone, surrounding whitespace aside), and never fewer than three. A shorter fence would close at that line.
doc_line_is_legacy
#2203: whether a TokenKind::DocLine token is spelled the legacy way, --| (#1888), rather than --- text.
doc_line_text
The text of a TokenKind::DocLine token: everything after the marker, less one separating space or tab, less a closer (#2203: a final run of exactly the opener’s dashes, preceded by whitespace, with that whitespace), and less the line ending. Further indentation is kept, so a doc line can indent a Markdown code block. A legacy --| line (#1888) has no closer.
doc_line_text_range
#1888: the byte range of a doc line’s text within source, as doc_line_text returns it, for callers that map positions in a doc back to the source (document links).
has_blank_line_between
Returns true if there is a blank line (a line containing only whitespace) in source strictly between byte offsets from (inclusive) and to (exclusive). Used by the parser to detect orphan doc blocks.
legacy_comment_error
#2203 (DECISION E): the bynk.lex.legacy_comment error for the legacy -- comment at span (a TokenKind::Comment token), with the fix to //.
legacy_doc_line_error
#2203 (DECISION E): the bynk.lex.legacy_doc_line error for the legacy --| doc line at span (a TokenKind::DocLine token), with the fix to --- text. A --| with no text has no one-line spelling (a bare --- opens a block), so it gets no fix.
one_line_doc
#2203: the source spelling of a one-line doc whose text is text: --- and the text, or a ---- marker when the text ends in a whitespace-preceded run of three dashes (which a --- marker would read as its closer). Empty text is spelled with a closer, --- ---, since a bare --- opens a block.
string_parts
The escape sequences and raw delimiters of every string literal in source (including those inside interpolation holes), for the editor’s semantic tokens. Empty when the file does not lex.
tokenize
Tokenise a source string with no real file identity — every span’s FileId defaults to FileId::UNKNOWN. See tokenize_in for the real-identity entry point production callers use.
tokenize_expanding_holes
Like tokenize, but with every interpolated-string token replaced by the tokens of its holes — each hole’s bytes re-lexed and its token spans rebased to absolute source positions (the same rebase crate::parser applies when parsing a hole), recursing through nested interpolation. Chunk (literal) text between holes yields no tokens.
tokenize_expanding_holes_in
Like tokenize_expanding_holes, but stamping every token’s span (including rebased hole tokens) with file.
tokenize_in
Tokenise a source string, stamping every token’s span with file. Returns the full token vector or the first lexical error (tokenize_recovering_in keeps going past it).
tokenize_legacy
#2203: tokenise source as the lexer read it before // comments, for the formatter’s migration of a file that still uses the legacy spellings. A -- opens a comment wherever it starts the input or follows whitespace (ADR 0188 D1), so --note and a --- Helpers --- divider are comments, as they were; --| first on its line is a doc line. // comments and --- blocks lex as in tokenize, so a half-migrated file reads too. Every legacy token keeps its legacy spelling, so comment_body and doc_line_text read it and the formatter prints the new one.
tokenize_recovering
tokenize_recovering_in with no real file identity.
tokenize_recovering_in
#2807: tokenise source, recovering from lexical errors instead of stopping at the first. Every error is returned, in source order, beside the tokens of everything the lexer could read: a bad character, a malformed or out-of-range literal, an unterminated string, an unclosed doc block and a /// doc habit each yield an error and are then skipped (a string to the end of its line), so the text after them still lexes. The skipped text produces no token, which the parser sees as a gap; without_lex_echoes drops the syntax errors that gap causes.
without_lex_echoes
#2807: parse_errors without the ones a lexical error caused. Recovery skips the text a lex error covers, so the parser meets a gap where the literal or character stood and complains about it at the same spot. An error that starts in a lex error’s span, or at the token right after it, is that echo and is dropped; every other syntax error stays. tokens is the recovering lex’s token list. A heuristic: a genuine syntax error at that next token (the fn after a stray character on its own line) is dropped with the echo, and surfaces once the lexical error is fixed.