import type { BehaviorInputFieldSpec } from '../../../sdk/behavior-plugin/index.js'; import { normalizeBehaviorInputFields } from '../behavior-input-fields.js'; export type LiteralBehaviorInputFieldsResult = | Readonly<{ status: 'missing' }> | Readonly<{ status: 'valid'; inputs: readonly Readonly[] }> | Readonly<{ status: 'invalid'; diagnostic: string }>; interface ParsedValue { readonly value: unknown; readonly next: number; } class LiteralParseError extends Error {} /** Shared, non-evaluating parser for a complete module manifest literal. */ export function parseLiteralModuleManifest(source: string): Record | null { try { const parsed = parseValue(source, skipTrivia(source, 0)); if (skipTrivia(source, parsed.next) !== source.length || !parsed.value || typeof parsed.value !== 'object' || Array.isArray(parsed.value)) return null; return parsed.value as Record; } catch { return null; } } function skipTrivia(source: string, start: number): number { let cursor = start; while (cursor < source.length) { if (/\s/.test(source[cursor])) { cursor += 1; continue; } if (source.startsWith('//', cursor)) { const end = source.indexOf('\n', cursor + 2); cursor = end < 0 ? source.length : end + 1; continue; } if (source.startsWith('/*', cursor)) { const end = source.indexOf('*/', cursor + 2); if (end < 0) throw new LiteralParseError('behavior_inputs_literal_invalid'); cursor = end + 2; continue; } break; } return cursor; } function parseString(source: string, start: number): ParsedValue { const quote = source[start]; if (quote !== "'" && quote !== '"' && quote !== '`') { throw new LiteralParseError('behavior_inputs_literal_invalid'); } let value = ''; for (let cursor = start + 1; cursor < source.length; cursor += 1) { const char = source[cursor]; if (char === quote) return { value, next: cursor + 1 }; if (quote === '`' && char === '$' && source[cursor + 1] === '{') { throw new LiteralParseError('behavior_inputs_not_literal'); } if (char !== '\\') { value += char; continue; } cursor += 1; if (cursor >= source.length) throw new LiteralParseError('behavior_inputs_literal_invalid'); const escaped = source[cursor]; if (escaped === 'n') value += '\n'; else if (escaped === 'r') value += '\r'; else if (escaped === 't') value += '\t'; else if (escaped === 'b') value += '\b'; else if (escaped === 'f') value += '\f'; else if (escaped === 'v') value += '\v'; else if (escaped === '0') value += '\0'; else if (escaped === '\n') continue; else if (escaped === '\r') { if (source[cursor + 1] === '\n') cursor += 1; } else if (escaped === 'x') { const hex = source.slice(cursor + 1, cursor + 3); if (!/^[0-9a-f]{2}$/i.test(hex)) throw new LiteralParseError('behavior_inputs_literal_invalid'); value += String.fromCharCode(Number.parseInt(hex, 16)); cursor += 2; } else if (escaped === 'u') { const hex = source.slice(cursor + 1, cursor + 5); if (!/^[0-9a-f]{4}$/i.test(hex)) throw new LiteralParseError('behavior_inputs_literal_invalid'); value += String.fromCharCode(Number.parseInt(hex, 16)); cursor += 4; } else value += escaped; } throw new LiteralParseError('behavior_inputs_literal_invalid'); } function consumeAsConst(source: string, start: number): number { const cursor = skipTrivia(source, start); const match = /^as\s+const\b/.exec(source.slice(cursor)); return match ? cursor + match[0].length : start; } function parseArray(source: string, start: number): ParsedValue { const values: unknown[] = []; let cursor = skipTrivia(source, start + 1); if (source[cursor] === ']') { const next = cursor + 1; return { value: values, next: consumeAsConst(source, next) }; } while (cursor < source.length) { const parsed = parseValue(source, cursor); values.push(parsed.value); cursor = skipTrivia(source, parsed.next); if (source[cursor] === ']') { const next = cursor + 1; return { value: values, next: consumeAsConst(source, next) }; } if (source[cursor] !== ',') throw new LiteralParseError('behavior_inputs_literal_invalid'); cursor = skipTrivia(source, cursor + 1); if (source[cursor] === ']') { const next = cursor + 1; return { value: values, next: consumeAsConst(source, next) }; } } throw new LiteralParseError('behavior_inputs_literal_invalid'); } function parseObject(source: string, start: number): ParsedValue { const value: Record = {}; let cursor = skipTrivia(source, start + 1); if (source[cursor] === '}') return { value, next: cursor + 1 }; while (cursor < source.length) { let key: string; if (source[cursor] === "'" || source[cursor] === '"' || source[cursor] === '`') { const parsedKey = parseString(source, cursor); key = String(parsedKey.value); cursor = skipTrivia(source, parsedKey.next); } else { const identifier = /^[A-Za-z_$][\w$]*/.exec(source.slice(cursor)); if (!identifier) throw new LiteralParseError('behavior_inputs_literal_invalid'); key = identifier[0]; cursor = skipTrivia(source, cursor + key.length); } if (Object.prototype.hasOwnProperty.call(value, key)) { throw new LiteralParseError(`behavior_inputs_literal_duplicate_property:${key}`); } if (source[cursor] !== ':') throw new LiteralParseError('behavior_inputs_literal_invalid'); const parsed = parseValue(source, skipTrivia(source, cursor + 1)); Object.defineProperty(value, key, { configurable: true, enumerable: true, writable: true, value: parsed.value, }); cursor = skipTrivia(source, parsed.next); if (source[cursor] === '}') return { value, next: cursor + 1 }; if (source[cursor] !== ',') throw new LiteralParseError('behavior_inputs_literal_invalid'); cursor = skipTrivia(source, cursor + 1); if (source[cursor] === '}') return { value, next: cursor + 1 }; } throw new LiteralParseError('behavior_inputs_literal_invalid'); } function parseValue(source: string, start: number): ParsedValue { const cursor = skipTrivia(source, start); const char = source[cursor]; if (char === "'" || char === '"' || char === '`') return parseString(source, cursor); if (char === '[') return parseArray(source, cursor); if (char === '{') return parseObject(source, cursor); const boolean = /^(true|false)\b/.exec(source.slice(cursor)); if (boolean) return { value: boolean[1] === 'true', next: cursor + boolean[0].length }; const nil = /^null\b/.exec(source.slice(cursor)); if (nil) return { value: null, next: cursor + nil[0].length }; const number = /^-?(?:(?:\d(?:_?\d)*)(?:\.(?:\d(?:_?\d)*)?)?|\.\d(?:_?\d)*)(?:e[+-]?\d(?:_?\d)*)?/i .exec(source.slice(cursor)); if (number) { const parsed = Number(number[0].replaceAll('_', '')); if (!Number.isFinite(parsed)) throw new LiteralParseError('behavior_inputs_literal_invalid'); return { value: parsed, next: cursor + number[0].length }; } throw new LiteralParseError('behavior_inputs_not_literal'); } function topLevelPropertyValue(source: string, property: string): number | undefined { let depth = 0; for (let cursor = 0; cursor < source.length;) { const next = skipTrivia(source, cursor); if (next !== cursor) { cursor = next; continue; } const char = source[cursor]; if (char === "'" || char === '"' || char === '`') { const parsed = parseString(source, cursor); if (depth === 1 && parsed.value === property) { const colon = skipTrivia(source, parsed.next); if (source[colon] === ':') return skipTrivia(source, colon + 1); } cursor = parsed.next; continue; } if (char === '{') { depth += 1; cursor += 1; continue; } if (char === '}') { depth -= 1; cursor += 1; continue; } if (depth === 1 && /[A-Za-z_$]/.test(char ?? '')) { const identifier = /^[A-Za-z_$][\w$]*/.exec(source.slice(cursor))![0]; if (identifier === property) { const colon = skipTrivia(source, cursor + identifier.length); if (source[colon] === ':') return skipTrivia(source, colon + 1); } cursor += identifier.length; continue; } cursor += 1; } return undefined; } /** Detects a real top-level manifest property without matching comments or string values. */ export function hasTopLevelManifestProperty( manifestSource: string, property: string, ): boolean { try { return topLevelPropertyValue(manifestSource, property) !== undefined; } catch { return false; } } /** Parses only a top-level manifest.inputs literal; it never evaluates module source. */ export function parseLiteralBehaviorInputFields( manifestSource: string, ): LiteralBehaviorInputFieldsResult { try { const start = topLevelPropertyValue(manifestSource, 'inputs'); if (start === undefined) return Object.freeze({ status: 'missing' }); if (manifestSource[start] !== '[') { return Object.freeze({ status: 'invalid', diagnostic: 'behavior_inputs_not_literal' }); } const parsed = parseArray(manifestSource, start); const tail = skipTrivia(manifestSource, parsed.next); if (manifestSource[tail] !== ',' && manifestSource[tail] !== '}') { return Object.freeze({ status: 'invalid', diagnostic: 'behavior_inputs_literal_invalid' }); } const inputs = normalizeBehaviorInputFields(parsed.value); return Object.freeze({ status: 'valid', inputs }); } catch (error) { return Object.freeze({ status: 'invalid', diagnostic: error instanceof Error ? error.message : 'behavior_inputs_literal_invalid', }); } }