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§
- String
Part - A highlighted part of a string literal (#2619), for the semantic tokens.
- Token
Kind - 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). AnIntis a JS safe integer; the range is symmetric.
Functions§
- comment_
body - Extract the body of a
Commenttrivia 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::Commenttoken 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.
spanlocates 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 (unlikedoc_block_content, which additionally strips a common per-line indent — not offset-preserving). Callers that need to map a position in the body back tosource(e.g. document-link spans) use this instead of re-deriving it from the stringdoc_block_contentreturns. - 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
docis 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::DocLinetoken is spelled the legacy way,--|(#1888), rather than--- text. - doc_
line_ text - The text of a
TokenKind::DocLinetoken: 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, asdoc_line_textreturns 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
sourcestrictly between byte offsetsfrom(inclusive) andto(exclusive). Used by the parser to detect orphan doc blocks. - legacy_
comment_ error - #2203 (DECISION E): the
bynk.lex.legacy_commenterror for the legacy--comment atspan(aTokenKind::Commenttoken), with the fix to//. - legacy_
doc_ line_ error - #2203 (DECISION E): the
bynk.lex.legacy_doc_lineerror for the legacy--|doc line atspan(aTokenKind::DocLinetoken), 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
FileIddefaults toFileId::UNKNOWN. Seetokenize_infor 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 rebasecrate::parserapplies 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) withfile. - 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_inkeeps going past it). - tokenize_
legacy - #2203: tokenise
sourceas 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--noteand a--- Helpers ---divider are comments, as they were;--|first on its line is a doc line.//comments and---blocks lex as intokenize, so a half-migrated file reads too. Every legacy token keeps its legacy spelling, socomment_bodyanddoc_line_textread it and the formatter prints the new one. - tokenize_
recovering tokenize_recovering_inwith 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_echoesdrops the syntax errors that gap causes. - without_
lex_ echoes - #2807:
parse_errorswithout 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.tokensis the recovering lex’s token list. A heuristic: a genuine syntax error at that next token (thefnafter a stray character on its own line) is dropped with the echo, and surfaces once the lexical error is fixed.