refactor: unify declare block scanning between CLI and client
Introduce `scanDeclareBlocks` in `declare-parser.ts` to provide a single source of truth for parsing `role=declare` blocks. This replaces redundant scanning logic in both the CLI and the journal completions. Also remove unused `dice.ts` and `spark-scanner.ts` files.
This commit is contained in:
@@ -16,6 +16,56 @@
|
||||
|
||||
import { parse } from "csv-parse/browser/esm/sync";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Shared block scanning — used by both CLI (block-processor) and client
|
||||
// (completions.ts) so there's a single source of truth for declare parsing.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Matches fenced code blocks with an info string. */
|
||||
const FENCED_BLOCK_RE = /^```(\w*)\s+(\S.*?)\s*\n([\s\S]*?)```\s*$/gm;
|
||||
|
||||
/** Parse key="value" and key=value pairs from an attribute string. */
|
||||
function parseBlockAttrs(info: string): Record<string, string> {
|
||||
const attrs: Record<string, string> = {};
|
||||
const re = /(\w+)\s*=\s*("[^"]*"|\S+)/g;
|
||||
let m: RegExpExecArray | null;
|
||||
while ((m = re.exec(info)) !== null) {
|
||||
attrs[m[1]] = m[2].replace(/^"|"$/g, "");
|
||||
}
|
||||
return attrs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan markdown content for ```csv role=declare blocks and return
|
||||
* parsed declarations and tag modifiers. Shared between CLI and client.
|
||||
*/
|
||||
export function scanDeclareBlocks(content: string, filePath: string): DeclareResult {
|
||||
const variables: VarDeclaration[] = [];
|
||||
const tagModifiers: TagModifier[] = [];
|
||||
|
||||
FENCED_BLOCK_RE.lastIndex = 0;
|
||||
let m: RegExpExecArray | null;
|
||||
while ((m = FENCED_BLOCK_RE.exec(content)) !== null) {
|
||||
const [, , infoString, body] = m;
|
||||
const attrs = parseBlockAttrs(infoString);
|
||||
if (attrs.role !== "declare") continue;
|
||||
|
||||
try {
|
||||
const result = parseDeclareCsv(body, filePath);
|
||||
variables.push(...result.variables);
|
||||
tagModifiers.push(...result.tagModifiers);
|
||||
} catch (e) {
|
||||
console.warn(`[declare-parser] ${filePath}: ${e}`);
|
||||
}
|
||||
}
|
||||
|
||||
return { variables, tagModifiers };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export interface VarDeclaration {
|
||||
key: string; // "$hp" (always starts with $)
|
||||
expression: string; // "$con*5+$mod_hp"
|
||||
@@ -33,7 +83,7 @@ export interface DeclareResult {
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a ```csv role=declare block.
|
||||
* Parse a single ```csv role=declare block body.
|
||||
*/
|
||||
export function parseDeclareCsv(csv: string, source: string): DeclareResult {
|
||||
const trimmed = csv.trim();
|
||||
@@ -92,4 +142,4 @@ export function parseDeclareCsv(csv: string, source: string): DeclareResult {
|
||||
}
|
||||
|
||||
return { variables, tagModifiers };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user