Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392import { isTokenAtKeyword, isTokenCloseCurly, isTokenEOF, isTokenOpenCurly, isTokenSemicolon, type TokenCloseCurly,} from '@csstools/css-tokenizer';import { nonNullishOrThrow, } from '@monochromatic-dev/module-or-throw/ts';import { CssParseError, } from './errors.ts';import type { CssAtRule, CssBlock, CssNode, CssRule,} from './node.ts';import { classifyRun, } from './parse-classify.ts';import { type CSSToken, isClosingToken, isOpeningToken, isTriviaToken, tokenData,} from './token.ts';
//region Step results
/** One consumed node plus the index where consumption stopped. */type ConsumedNode<NodeKind extends CssNode,> = { readonly node: NodeKind; readonly nextIndex: number;};
/** Contents of a stylesheet or block plus where consumption stopped; the closing token is present exactly when consuming inside a block. */export type ConsumedContents = { readonly children: readonly CssNode[]; readonly nextIndex: number; readonly closeToken?: TokenCloseCurly;};
//endregion Step results
//region Block consumer
/** Consumes a braced block starting at its `{` token by delegating the interior to {@link consumeContents} in block mode. @param tokens - Full token array of the document. @param openIndex - Index of the block's `{` token. @returns Block node and the index after its `}`. @throws CssParseError when the block never closes. */function consumeBlock({ tokens, openIndex,}: { readonly tokens: readonly CSSToken[]; readonly openIndex: number;},): { readonly block: CssBlock; readonly nextIndex: number;} { /** Opening `{` token of the block. */ const openToken = tokens[openIndex]; if ((openToken === undefined) || (!isTokenOpenCurly(openToken,))) throw new CssParseError({ message: 'block consumer called off an opening curly token', offset: openToken === undefined ? 0 : openToken[2], },);
/** Structured interior of the block. */ const contents = consumeContents({ tokens, start: openIndex + 1, insideBlock: true, },);
return { block: { kind: 'block', openToken, children: contents.children, closeToken: nonNullishOrThrow(contents.closeToken,), }, nextIndex: contents.nextIndex, };}
//endregion Block consumer
//region At-rule consumer
/** Consumes an at-rule starting at its at-keyword token: prelude tokens up to a depth-zero `;` (statement form), `{` (block form via {@link consumeBlock}), or an enclosing block's `}` / end of input (statement form without a terminator). @param tokens - Full token array of the document. @param start - Index of the at-keyword token. @returns At-rule node and the index where consumption stopped. @throws CssParseError on unbalanced closing tokens inside the prelude. */function consumeAtRule({ tokens, start,}: { readonly tokens: readonly CSSToken[]; readonly start: number;},): ConsumedNode<CssAtRule> { /** At-keyword token opening the rule. */ const atToken = tokens[start]; if ((atToken === undefined) || (!isTokenAtKeyword(atToken,))) throw new CssParseError({ message: 'at-rule consumer called off an at-keyword token', offset: atToken === undefined ? 0 : atToken[2], },);
/** Parsed data of the at-keyword token, holding the unescaped name. */ const atData = tokenData(atToken,);
return (function scanAtRule(): ConsumedNode<CssAtRule> { /** Nesting depth relative to the prelude's own level. */ let depth = 0; /** Scan cursor over prelude tokens. */ let index = start + 1;
while (index < tokens.length) { /** Token at the scan cursor. */ const token = tokens[index]; if ((token === undefined) || isTokenEOF(token,) || ((depth === 0) && isTokenCloseCurly(token,))) break;
if (depth === 0) { if (isTokenSemicolon(token,)) { return { node: { kind: 'atRule', atToken, name: atData.value, preludeTokens: tokens.slice( start + 1, index, ), semicolonToken: token, }, nextIndex: index + 1, }; } if (isTokenOpenCurly(token,)) { /** Braced body of the at-rule. */ const consumed = consumeBlock({ tokens, openIndex: index, },); return { node: { kind: 'atRule', atToken, name: atData.value, preludeTokens: tokens.slice( start + 1, index, ), block: consumed.block, }, nextIndex: consumed.nextIndex, }; } }
// Depth bookkeeping mirrors classifyRun: function/paren/square tokens // nest; their closers un-nest. Curly handling above only fires at depth 0. if (isOpeningToken(token,)) depth += 1; else if (isClosingToken(token,)) { depth -= 1; if (depth < 0) throw new CssParseError({ message: 'unbalanced closing token in at-rule prelude', offset: token[2], },); } index += 1; } return { node: { kind: 'atRule', atToken, name: atData.value, preludeTokens: tokens.slice( start + 1, index, ), }, nextIndex: index, }; })();}
//endregion At-rule consumer
//region Contents consumer
/** Consumes stylesheet or block contents per the CSS Syntax section 5 unified model: trivia runs, at-rules, declarations, and qualified rules may all appear; {@link classifyRun} decides between the latter two. @param tokens - Full token array of the document. @param start - Index of the first content token. @param insideBlock - Whether a `}` legitimately ends these contents. @returns Children in source order, the stop index, and the closing token when inside a block. @throws CssParseError on a stray `}` at top level or an unclosed block. @example ```ts consumeContents({ tokens, start: 0, insideBlock: false }); // => { children: [...], nextIndex: 12 } ``` */export function consumeContents({ tokens, start, insideBlock,}: { readonly tokens: readonly CSSToken[]; readonly start: number; readonly insideBlock: boolean;},): ConsumedContents { return (function scanContents(): ConsumedContents { /** Accumulated child nodes in source order. */ const children: CssNode[] = []; /** Scan cursor over the token array. */ let index = start;
while (index < tokens.length) { /** Token at the scan cursor. */ const token = tokens[index];
if ((token === undefined) || isTokenEOF(token,)) break;
if (isTokenCloseCurly(token,)) { if (!insideBlock) throw new CssParseError({ message: 'stray closing brace at top level', offset: token[2], },); return { children, nextIndex: index + 1, closeToken: token, }; }
if (isTriviaToken(token,)) { /** Exclusive end of the trivia run. */ let runEnd = index + 1; /** Token at the trivia scan cursor, refreshed as the run advances. */ let runToken = tokens[runEnd]; while ((runToken !== undefined) && isTriviaToken(runToken,)) { runEnd += 1; runToken = tokens[runEnd]; } children.push({ kind: 'trivia', tokens: tokens.slice( index, runEnd, ), },); index = runEnd; continue; }
if (isTokenAtKeyword(token,)) { /** Consumed at-rule step. */ const consumed = consumeAtRule({ tokens, start: index, },); children.push(consumed.node,); index = consumed.nextIndex; continue; }
/** Declaration-versus-rule decision for this run. */ const classified = classifyRun({ tokens, start: index, },);
if (classified.outcome === 'declaration') { children.push({ kind: 'declaration', tokens: tokens.slice( index, classified.endExclusive, ), },); index = classified.endExclusive; continue; }
/** Consumed block of the qualified rule. */ const consumed = consumeBlock({ tokens, openIndex: classified.blockOpenIndex, },); /** Qualified rule assembled from prelude slice and block. */ const rule: CssRule = { kind: 'rule', preludeTokens: tokens.slice( index, classified.blockOpenIndex, ), block: consumed.block, }; children.push(rule,); index = consumed.nextIndex; } /** EOF token at the terminal cursor, when tokenizer supplied one. */ const terminalToken = tokens[index]; if (insideBlock) throw new CssParseError({ message: 'block reached end of input without its closing brace', offset: terminalToken === undefined ? 0 : terminalToken[2], },); return { children, nextIndex: index, }; })();}
//endregion Contents consumer