diff --git a/packages/struktolab/src/common/pseudocode-parser.js b/packages/struktolab/src/common/pseudocode-parser.js index f7e2366..b134bde 100644 --- a/packages/struktolab/src/common/pseudocode-parser.js +++ b/packages/struktolab/src/common/pseudocode-parser.js @@ -114,7 +114,7 @@ function placeholder() { * "x > 0" → { text: "x > 0", columnWidths: null } */ function extractColumnWidths(str) { - const match = str.match(/^(.*?)\s*\[([0-9.,\s]+)\]\s*$/); + const match = str.match(/^(.*?)\s+\[([0-9.,\s]+)\]\s*$/); if (match) { const text = match[1].trim(); const widths = match[2].split(",").map(s => parseFloat(s.trim())).filter(n => !isNaN(n)); diff --git a/packages/struktolab/src/common/pseudocode-parser.test.js b/packages/struktolab/src/common/pseudocode-parser.test.js index 95fade8..f9a9088 100644 --- a/packages/struktolab/src/common/pseudocode-parser.test.js +++ b/packages/struktolab/src/common/pseudocode-parser.test.js @@ -15,3 +15,14 @@ test("keeps the body of a line it does not understand", () => { const out = roundTrip('while x > 0:\n x = x - 1\n output("x")\ny = 1'); assert.equal(out, 'while x > 0:\nx = x - 1\noutput("x")\ny = 1'); }); + +test("keeps an index at the end of a condition", () => { + const code = "switch notes[0]:\n case 1:\n y = 1"; + assert.equal(roundTrip(code), code); +}); + +test("reads column widths after the condition", () => { + const tree = parsePseudocode("if a[0] > 1 [0.7, 0.3]:\n y = 1\nelse:\n y = 2", KEYWORDS_EN); + assert.equal(tree.followElement.text, "a[0] > 1"); + assert.deepEqual(tree.followElement.columnWidths, [0.7, 0.3]); +});