Apply RFC-compliant ast-parser and exporter

- ast-parser: railsbackCurve moved to variation level, FM shape child
  parsing, buildLabelSpec direct A/S/R handling, bar IDs as opaque strings
- exporter: RFC-compact shape format, correct instrument NAME: key,
  patchScore regex fixed, RAILSBACK_CURVE emitted from variationLines
- .gitignore: exclude test-parser.mjs pending author confirmation
This commit is contained in:
c0dev0id 2026-06-22 10:52:32 +02:00
parent 822e2c9f42
commit c8b8d03e3f
3 changed files with 132 additions and 109 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
test-parser.mjs

View File

@ -178,7 +178,6 @@ function buildInstrument(node) {
isDirty: false, isDirty: false,
variations: [], variations: [],
basicProperties: null, basicProperties: null,
railsbackCurve: null,
volumes: null, volumes: null,
timbre: null, timbre: null,
fmModulations: [], fmModulations: [],
@ -193,9 +192,6 @@ function buildInstrument(node) {
case 'character.basic_properties': case 'character.basic_properties':
instr.basicProperties = buildBasicProperties(child); instr.basicProperties = buildBasicProperties(child);
break; break;
case 'RAILSBACK_CURVE.shape':
instr.railsbackCurve = buildShape(child);
break;
case 'VOLUMES.shape': case 'VOLUMES.shape':
instr.volumes = buildShape(child); instr.volumes = buildShape(child);
break; break;
@ -221,6 +217,7 @@ function buildVariation(node) {
labelSpecs: [], labelSpecs: [],
subvariations: [], subvariations: [],
spread: null, spread: null,
railsbackCurve: null,
rawChildren: [], rawChildren: [],
}; };
@ -239,6 +236,9 @@ function buildVariation(node) {
case 'variation.SPREAD': case 'variation.SPREAD':
v.spread = child.positionals; v.spread = child.positionals;
break; break;
case 'RAILSBACK_CURVE.shape':
v.railsbackCurve = buildShape(child);
break;
default: default:
v.rawChildren.push(buildGeneric(child)); v.rawChildren.push(buildGeneric(child));
} }
@ -267,7 +267,10 @@ function buildBasicProperties(node) {
} else if (child.parentSlot === 'variation' && child.slot === 'O') { } else if (child.parentSlot === 'variation' && child.slot === 'O') {
bp.oscillator = child.props.ref ?? child.positionals[0]; bp.oscillator = child.props.ref ?? child.positionals[0];
} else if (child.parentSlot === 'FM' && child.slot === 'modulation') { } else if (child.parentSlot === 'FM' && child.slot === 'modulation') {
bp.fmModulations.push({ ...child.props }); const fm = { ...child.props };
const envChild = child.children.find(c => c.slot === 'shape');
if (envChild) fm.shape = buildShape(envChild);
bp.fmModulations.push(fm);
} else { } else {
bp.rawChildren.push(buildGeneric(child)); bp.rawChildren.push(buildGeneric(child));
} }
@ -284,13 +287,23 @@ function buildLabelSpec(node) {
rawChildren: [], rawChildren: [],
}; };
const directBpChildren = [];
for (const child of node.children) { for (const child of node.children) {
if (child.parentSlot === 'variation' && child.slot === 'basic_properties') { if (child.parentSlot === 'variation' && child.slot === 'basic_properties') {
ls.basicProperties = buildBasicProperties(child); ls.basicProperties = buildBasicProperties(child);
} else if (
(child.slot === 'shape' && (child.parentSlot === 'A' || child.parentSlot === 'S' || child.parentSlot === 'R')) ||
(child.parentSlot === 'variation' && child.slot === 'O') ||
(child.parentSlot === 'FM' && child.slot === 'modulation')
) {
directBpChildren.push(child);
} else { } else {
ls.rawChildren.push(buildGeneric(child)); ls.rawChildren.push(buildGeneric(child));
} }
} }
if (!ls.basicProperties && directBpChildren.length > 0) {
ls.basicProperties = buildBasicProperties({ children: directBpChildren });
}
return ls; return ls;
} }
@ -300,7 +313,6 @@ function buildShape(node) {
type: 'shape', type: 'shape',
length: node.props.length, length: node.props.length,
start: node.props.start, start: node.props.start,
z: node.props.z ?? 1,
coords: node.children coords: node.children
.filter(c => c.slot === 'coords') .filter(c => c.slot === 'coords')
.map(c => ({ .map(c => ({
@ -313,15 +325,9 @@ function buildShape(node) {
} }
function buildBar(node) { function buildBar(node) {
const id = node.positionals[0] ?? '';
const idMatch = id.match(/^(\w?)(\d+)P(\d+)L(\d+)M(\d+)$/);
const bar = { const bar = {
type: 'bar', type: 'bar',
id, id: node.positionals[0] ?? '',
movement: idMatch ? idMatch[1] : '',
part: idMatch ? parseInt(idMatch[2]) : 0,
line: idMatch ? parseInt(idMatch[3]) : 0,
measure: idMatch ? parseInt(idMatch[4]) : 0,
stressor: null, stressor: null,
tempoShape: null, tempoShape: null,
tempoLevels: null, tempoLevels: null,

View File

@ -1,138 +1,154 @@
// Template-based YAML serializer — instrument blocks only (v1). // RFC-compliant YAML serializer for Sompyler instrument blocks.
// Each object selects a template by finding the first entry in its // Operates on the model produced by ast-parser.js buildModel().
// selectExportTemplate() list where all required slots have values.
// Placeholders #0, #1, ... are filled with slot values.
function fillTemplate(template, slots) {
return template.replace(/#(\d+)/g, (_, i) => slots[parseInt(i, 10)] ?? '');
}
function indent(text, level) {
const pad = ' '.repeat(level);
return text.split('\n').map((line, i) => {
if (i === 0) return line;
if (line.startsWith('- ')) return pad.slice(2) + line;
return pad + line;
}).join('\n');
}
// ── Shape ────────────────────────────────────────────────────────────────── // ── Shape ──────────────────────────────────────────────────────────────────
// RFC §1.3.4.5: SHAPE = [PREFIX (":" / ";")] Node 1*(";" Node)
// Node = x "," y ["*" z] ["!"]
// PREFIX+colon is the duration/resolution; optional START+semicolon follows.
function exportCoord(coord) { function serializeShape(shape) {
let s = `x=${coord.x} y=${coord.y}`; if (!shape) return null;
if (coord.z !== undefined && coord.z !== 1) s += ` z=${coord.z}`; const nodes = shape.coords.map(c => {
if (coord.isSharp) s += ` is_sharp=True`; let s = `${c.x},${c.y}`;
if (c.z !== undefined && c.z !== 1) s += `*${c.z}`;
if (c.isSharp) s += '!';
return s;
}).join(';');
let prefix = '';
if (shape.length != null) prefix = `${shape.length}:`;
if (shape.start != null) prefix += `${shape.start};`;
return prefix + nodes;
}
// ── FM / AM modulation ─────────────────────────────────────────────────────
// RFC §3.2.1.1.6-7: FM = FREQUENCY ["f"/"F"] ["@" OSC] ["[" SHAPE "]"] ";" MOD ":" BASE
function serializeFm(fm) {
let s = String(fm.frequency ?? '');
if (fm.factor) s += fm.factor;
if (fm.osc) s += `@${fm.osc}`;
if (fm.shape) s += `[${serializeShape(fm.shape)}]`;
s += `;${fm.mod ?? ''}:${fm.base ?? ''}`;
return s; return s;
} }
function exportShape(shape, slotName, level) { // ── Basic properties ───────────────────────────────────────────────────────
if (!shape) return ''; // RFC §3.2.1.1: O, A, S, R, FM go directly in the variation MAPPING.
const coordLines = shape.coords.map(c => ` - coords: ${exportCoord(c)}`).join('\n'); // Returns array of YAML lines at 0 indent.
let header = `${slotName}: length=${shape.length}`;
if (shape.start !== undefined) header += ` start=${shape.start}`;
if (shape.z !== undefined && shape.z !== 1) header += ` z=${shape.z}`;
const block = coordLines ? `${header}\n${coordLines}` : header;
return indent(block, level);
}
// ── BasicProperties ──────────────────────────────────────────────────────── function basicPropLines(bp) {
if (!bp) return [];
function exportBasicProperties(bp, level) {
if (!bp) return '';
const lines = []; const lines = [];
if (bp.oscillator) lines.push(` O: ref=${bp.oscillator}`); if (bp.oscillator) lines.push(`O: ${bp.oscillator}`);
if (bp.A) lines.push(` ${exportShape(bp.A, 'A', 1)}`); const a = serializeShape(bp.A);
if (bp.S) lines.push(` ${exportShape(bp.S, 'S', 1)}`); if (a) lines.push(`A: "${a}"`);
if (bp.R) lines.push(` ${exportShape(bp.R, 'R', 1)}`); const s = serializeShape(bp.S);
for (const fm of (bp.fmModulations ?? [])) { if (s) lines.push(`S: "${s}"`);
const parts = Object.entries(fm).map(([k, v]) => `${k}=${v}`).join(' '); const r = serializeShape(bp.R);
lines.push(` FM:\n modulation: ${parts}`); if (r) lines.push(`R: "${r}"`);
} for (const fm of (bp.fmModulations ?? [])) lines.push(`FM: "${serializeFm(fm)}"`);
if (!lines.length) return ''; return lines;
return indent('basic_properties:\n' + lines.join('\n'), level);
} }
// ── LabelSpec ───────────────────────────────────────────────────────────── // ── Labelled property groups ───────────────────────────────────────────────
// RFC §3.2.1.2: label name (3+ lowercase chars) is the MAPPING KEY directly.
// Returns array of YAML lines at 0 indent.
function exportLabelSpec(ls, level) { function labelSpecLines(ls) {
const label = ls.label ? ` '${ls.label}'` : ''; const inner = basicPropLines(ls.basicProperties);
const bp = exportBasicProperties(ls.basicProperties, 1); if (!inner.length) return [`${ls.label}:`];
const body = bp ? `label_spec:${label}\n ${bp}` : `label_spec:${label}`; return [`${ls.label}:`, ...inner.map(l => ` ${l}`)];
return indent(body, level);
} }
// ── Variation ───────────────────────────────────────────────────────────── // ── Variation ─────────────────────────────────────────────────────────────
// Returns YAML lines for one variation MAPPING (no leading "- ").
function exportVariation(v, level) { function variationLines(v) {
const dep = v.dependsOn ? ` depends_on=${v.dependsOn}` : ''; const lines = [];
const lines = [`variation:${dep}`]; if (v.dependsOn) lines.push(`ATTR: ${v.dependsOn}`);
if (v.basicProperties) lines.push(` ${exportBasicProperties(v.basicProperties, 1)}`); lines.push(...basicPropLines(v.basicProperties));
for (const ls of (v.labelSpecs ?? [])) lines.push(` ${exportLabelSpec(ls, 1)}`); for (const ls of (v.labelSpecs ?? [])) lines.push(...labelSpecLines(ls));
for (const sv of (v.subvariations ?? [])) lines.push(` ${exportVariation(sv, 1)}`); if (v.spread?.length) lines.push(`SPREAD: [${v.spread.join(', ')}]`);
if (v.spread?.length) lines.push(` SPREAD: ${v.spread.join(' ')}`); if (v.railsbackCurve) { const rc = serializeShape(v.railsbackCurve); if (rc) lines.push(`RAILSBACK_CURVE: "${rc}"`); }
return indent(lines.join('\n'), level); for (const sv of (v.subvariations ?? [])) lines.push(...variationLines(sv));
return lines;
}
// ── Instrument character block ─────────────────────────────────────────────
// VOLUMES / TIMBRE / FM appear at depth 01 (direct instrument children per RFC §3.2.1.3).
// RAILSBACK_CURVE is depth 02 (inside variation) and is emitted by variationLines().
function instrCharacterLines(instr) {
const extraLines = [];
const vol = serializeShape(instr.volumes);
if (vol) extraLines.push(`VOLUMES: "${vol}"`);
const timbre = serializeShape(instr.timbre);
if (timbre) extraLines.push(`TIMBRE: "${timbre}"`);
for (const fm of (instr.fmModulations ?? [])) extraLines.push(`FM: "${serializeFm(fm)}"`);
const variations = instr.variations ?? [];
const syntheticRoot = instr.basicProperties
? { basicProperties: instr.basicProperties, labelSpecs: [], subvariations: [], spread: null, dependsOn: null }
: null;
const allVariations = [
...(syntheticRoot ? [syntheticRoot] : []),
...variations,
];
if (allVariations.length <= 1) {
// Single variation — emit as MAPPING directly under character:
const vLines = allVariations.length
? [...variationLines(allVariations[0]), ...extraLines]
: extraLines;
return vLines.map(l => ` ${l}`);
}
// Multiple variations — RFC MAYBE_LIST<VARIATION> as YAML sequence.
const result = [];
for (let i = 0; i < allVariations.length; i++) {
const vLines = variationLines(allVariations[i]);
const allLines = i === 0 ? [...vLines, ...extraLines] : vLines;
if (!allLines.length) continue;
result.push(` - ${allLines[0]}`);
for (const l of allLines.slice(1)) result.push(` ${l}`);
}
return result;
} }
// ── Instrument ──────────────────────────────────────────────────────────── // ── Instrument ────────────────────────────────────────────────────────────
// RFC §4.4: embedded instrument key is "instrument NAME:" not "instrument: 'NAME'"
export function exportInstrument(instr) { export function exportInstrument(instr) {
const lines = []; const lines = [`instrument ${instr.name}:`];
const name = instr.name; if (instr.notChangedSince) lines.push(` NOT_CHANGED_SINCE: ${instr.notChangedSince}`);
lines.push(`instrument: '${name}'`); lines.push(` character:`);
lines.push(...instrCharacterLines(instr));
for (const v of (instr.variations ?? [])) {
lines.push(` character:\n ${exportVariation(v, 2)}`);
}
if (instr.basicProperties) {
lines.push(` character:\n ${exportBasicProperties(instr.basicProperties, 2)}`);
}
if (instr.railsbackCurve) {
lines.push(` ${exportShape(instr.railsbackCurve, 'RAILSBACK_CURVE', 1)}`);
}
if (instr.volumes) {
lines.push(` ${exportShape(instr.volumes, 'VOLUMES', 1)}`);
}
if (instr.timbre) {
lines.push(` ${exportShape(instr.timbre, 'TIMBRE', 1)}`);
}
for (const fm of (instr.fmModulations ?? [])) {
const parts = Object.entries(fm).map(([k, v]) => `${k}=${v}`).join(' ');
lines.push(` FM:\n modulation: ${parts}`);
}
return lines.join('\n'); return lines.join('\n');
} }
// ── Score patch ──────────────────────────────────────────────────────────── // ── Score patch ────────────────────────────────────────────────────────────
// Replace dirty instrument blocks in rawScoreText with RFC-serialized output.
// Non-dirty instruments are left verbatim.
// Replace instrument blocks in rawScoreText with serialized model instruments.
// Non-dirty linked instruments (NOT_CHANGED_SINCE set, not edited) are left as-is
// from rawScoreText. Embedded and dirty instruments are emitted from the model.
export function patchScore(rawScoreText, instruments) { export function patchScore(rawScoreText, instruments) {
// Split raw text into instrument blocks and other sections.
// Strategy: locate each `^instrument:` line and replace that block
// (up to next same-indent section or EOF) with the serialized model.
const lines = rawScoreText.split('\n'); const lines = rawScoreText.split('\n');
const result = []; const result = [];
const instrMap = {}; const instrMap = {};
for (const instr of instruments) { for (const instr of instruments) {
const basename = instr.name.includes('/') ? instr.name.split('/').pop() : instr.name;
instrMap[basename] = instr;
instrMap[instr.name] = instr; instrMap[instr.name] = instr;
if (instr.name.includes('/')) instrMap[instr.name.split('/').pop()] = instr;
} }
let i = 0; let i = 0;
while (i < lines.length) { while (i < lines.length) {
const line = lines[i]; const line = lines[i];
const m = line.match(/^instrument:\s+'?([^']+)'?/); // RFC §4.4: "instrument NAME:" — strip optional quotes around name
const m = line.match(/^instrument\s+(.+?)\s*:/);
if (m) { if (m) {
const rawName = m[1]; const rawName = m[1].replace(/^'|'$/g, '');
const instr = instrMap[rawName]; const instr = instrMap[rawName];
if (instr && instr.isDirty) { if (instr && instr.isDirty) {
// consume the raw block
i++; i++;
while (i < lines.length && (lines[i].startsWith(' ') || lines[i] === '')) i++; while (i < lines.length && (lines[i].startsWith(' ') || lines[i] === '')) i++;
result.push(exportInstrument(instr)); result.push(exportInstrument(instr));