From 6620c244d2cccca225d1ca253abe64fc6e792b70 Mon Sep 17 00:00:00 2001 From: abiwab Date: Mon, 16 Nov 2026 23:46:13 +0100 Subject: [PATCH] fix(parser): end a preparation at the end of its line --- .changeset/preparation-single-line.md | 5 + .../048-preparation-single-line/analyzed.json | 595 ++++++++++++++++++ .../048-preparation-single-line/ast.json | 339 ++++++++++ .../048-preparation-single-line/compiled.json | 510 +++++++++++++++ .../048-preparation-single-line/input.gram | 15 + packages/parser/grammar.ohm | 5 +- packages/parser/src/grammar-content.ts | 2 +- .../tests/preparation-single-line.test.ts | 70 +++ 8 files changed, 1539 insertions(+), 2 deletions(-) create mode 100644 .changeset/preparation-single-line.md create mode 100644 conformance/cases/048-preparation-single-line/analyzed.json create mode 100644 conformance/cases/048-preparation-single-line/ast.json create mode 100644 conformance/cases/048-preparation-single-line/compiled.json create mode 100644 conformance/cases/048-preparation-single-line/input.gram create mode 100644 packages/parser/tests/preparation-single-line.test.ts diff --git a/.changeset/preparation-single-line.md b/.changeset/preparation-single-line.md new file mode 100644 index 00000000..085beb0b --- /dev/null +++ b/.changeset/preparation-single-line.md @@ -0,0 +1,5 @@ +--- +"@gram-lang/parser": patch +--- + +**Parser**: Fixed an unclosed parenthesis after an ingredient, a composite parent or cookware (`@flour{200g}(sifted and`) that silently swallowed the rest of the recipe up to the next `)`, steps and sections included. A preparation now ends on its line: an unclosed parenthesis stays in the text of the step, and what follows is read as usual. A preparation spread over two lines of the same step is now read as text too. diff --git a/conformance/cases/048-preparation-single-line/analyzed.json b/conformance/cases/048-preparation-single-line/analyzed.json new file mode 100644 index 00000000..73a60fe6 --- /dev/null +++ b/conformance/cases/048-preparation-single-line/analyzed.json @@ -0,0 +1,595 @@ +{ + "title": "Preparation Single Line", + "slug": "preparation-single-line", + "meta": { + "title": "Preparation Single Line" + }, + "registry": { + "ingredients": { + "flour": { + "id": "flour", + "name": "flour", + "default_unit": "g" + }, + "water": { + "id": "water", + "name": "water", + "default_unit": "ml" + }, + "butter": { + "id": "butter", + "name": "butter", + "default_unit": "g" + }, + "salt": { + "id": "salt", + "name": "salt" + }, + "zest": { + "id": "zest", + "name": "zest", + "is_composite": true, + "parent": "lemon" + }, + "lemon": { + "id": "lemon", + "name": "lemon", + "is_composite": true + } + }, + "cookware": { + "pan": { + "id": "pan", + "name": "pan" + } + } + }, + "shopping_list": [ + { + "id": "flour", + "name": "flour", + "_usageIds": [ + "1" + ], + "qty": 200, + "unit": "g", + "normalizedMass": 200, + "isEstimate": false, + "conversionMethod": "physical" + }, + { + "id": "water", + "name": "water", + "_usageIds": [ + "2" + ], + "qty": 120, + "unit": "ml" + }, + { + "id": "butter", + "name": "butter", + "_usageIds": [ + "4" + ], + "qty": 20, + "unit": "g", + "normalizedMass": 20, + "isEstimate": false, + "conversionMethod": "physical" + }, + { + "id": "salt", + "name": "salt", + "_usageIds": [ + "5" + ] + }, + { + "type": "composite", + "id": "lemon", + "name": "lemon", + "qty": 1, + "usage": [ + { + "id": "zest", + "qty": 1 + } + ] + } + ], + "cookware": [ + { + "id": "pan", + "_usageId": "3", + "qty": 1, + "fixed": false + } + ], + "sections": [ + { + "title": "Dough", + "ingredients": [ + { + "id": "flour", + "_usageId": "1", + "qty": 200, + "unit": "g", + "conversionMethod": "physical", + "isEstimate": false, + "normalizedMass": 200 + }, + { + "id": "water", + "_usageId": "2", + "qty": 120, + "unit": "ml" + }, + { + "id": "butter", + "_usageId": "4", + "qty": 20, + "unit": "g", + "preparation": "soft, cubed", + "conversionMethod": "physical", + "isEstimate": false, + "normalizedMass": 20 + } + ], + "cookware": [ + { + "id": "pan", + "_usageId": "3", + "qty": 1, + "fixed": false + } + ], + "steps": [ + { + "type": "step", + "content": [ + "Mix ", + { + "id": "flour", + "_usageId": "1", + "qty": 200, + "unit": "g", + "normalizedMass": 200, + "conversionMethod": "physical", + "isEstimate": false + }, + "(sifted and ", + { + "id": "water", + "_usageId": "2", + "qty": 120, + "unit": "ml" + }, + "." + ], + "timings": { + "start": 0, + "end": 2, + "activeDuration": 2 + } + }, + { + "type": "step", + "content": [ + "Heat a ", + { + "id": "pan", + "_usageId": "3", + "qty": 1, + "fixed": false + }, + "(large over ", + { + "type": "temperature", + "quantity": { + "type": "single", + "value": 180, + "text": "180" + }, + "unit": "°C" + }, + "." + ], + "timings": { + "start": 2, + "end": 4, + "activeDuration": 2 + } + }, + { + "type": "step", + "content": [ + "Knead ", + { + "id": "butter", + "_usageId": "4", + "qty": 20, + "unit": "g", + "preparation": "soft, cubed", + "normalizedMass": 20, + "conversionMethod": "physical", + "isEstimate": false + }, + "." + ], + "timings": { + "start": 4, + "end": 6, + "activeDuration": 2 + } + } + ], + "metrics": { + "totalMass": 220, + "massStatus": "incomplete", + "missingMassIngredients": [ + "water" + ] + } + }, + { + "title": "Serve", + "ingredients": [ + { + "id": "salt", + "_usageId": "5" + }, + { + "id": "zest", + "_usageId": "6", + "qty": 1, + "composite": { + "parent": "lemon", + "quantity": 1 + } + } + ], + "cookware": [], + "steps": [ + { + "type": "step", + "content": [ + "Serve with ", + { + "id": "salt", + "_usageId": "5" + }, + "(flaky and ", + { + "id": "zest", + "_usageId": "6", + "qty": 1, + "composite": { + "parent": "lemon", + "quantity": 1 + } + }, + "(organic." + ], + "timings": { + "start": 6, + "end": 8, + "activeDuration": 2 + } + } + ], + "metrics": { + "totalMass": 0, + "massStatus": "incomplete", + "missingMassIngredients": [ + "salt", + "zest" + ] + } + } + ], + "warnings": [ + { + "code": "MISSING_INGREDIENT", + "message": "Ingredient \"flour\" not found in database — nutritional metrics and density conversions unavailable." + }, + { + "code": "UNKNOWN_MASS", + "message": "Cannot calculate mass for \"water\" — omitted from nutritional totals." + }, + { + "code": "MISSING_INGREDIENT", + "message": "Ingredient \"butter\" not found in database — nutritional metrics and density conversions unavailable." + }, + { + "code": "UNKNOWN_MASS", + "message": "Cannot calculate mass for \"zest\" — omitted from nutritional totals." + } + ], + "metrics": { + "totalTime": 17, + "totalBreakdown": [ + { + "label": "section_active:Dough", + "duration": 6 + }, + { + "label": "section_active:Serve", + "duration": 2 + } + ], + "idleTime": 0, + "activeTime": 8, + "activeBreakdown": [ + { + "label": "section_active:Dough", + "duration": 6 + }, + { + "label": "section_active:Serve", + "duration": 2 + } + ], + "preparationTime": 9, + "prepBreakdown": [ + { + "label": "ingredients_overhead", + "duration": 6 + }, + { + "label": "cookware_overhead", + "duration": 1 + }, + { + "label": "prep_butter", + "duration": 2 + } + ], + "totalMass": 220, + "massStatus": "incomplete", + "missingMassIngredients": [ + "water", + "salt", + "zest" + ], + "nutrition": { + "total": { + "calories": 0, + "fat": 0, + "carbs": 0, + "protein": 0 + }, + "isEstimate": false, + "coverage": 0, + "warnings": [ + { + "code": "MISSING_INGREDIENT", + "message": "Ingredient \"flour\" not found in database — nutritional metrics and density conversions unavailable." + }, + { + "code": "UNKNOWN_MASS", + "message": "Cannot calculate mass for \"water\" — omitted from nutritional totals." + }, + { + "code": "MISSING_INGREDIENT", + "message": "Ingredient \"butter\" not found in database — nutritional metrics and density conversions unavailable." + }, + { + "code": "UNKNOWN_MASS", + "message": "Cannot calculate mass for \"zest\" — omitted from nutritional totals." + } + ] + } + }, + "miseEnPlace": [ + { + "section": 0, + "duration": 6, + "items": [ + { + "kind": "gather", + "target": "ingredient", + "count": 3, + "duration": 3 + }, + { + "kind": "gather", + "target": "cookware", + "count": 1, + "duration": 1 + }, + { + "kind": "prepare", + "ref": { + "type": "ingredient", + "id": "butter" + }, + "duration": 2 + } + ] + }, + { + "section": 1, + "duration": 3, + "items": [ + { + "kind": "gather", + "target": "ingredient", + "count": 3, + "duration": 3 + } + ] + } + ], + "tasks": { + "tasks": [ + { + "id": "s0.prep", + "kind": "prep", + "section": 0, + "duration": { + "nominal": 6 + }, + "items": [ + { + "kind": "gather", + "target": "ingredient", + "count": 3, + "duration": 3 + }, + { + "kind": "gather", + "target": "cookware", + "count": 1, + "duration": 1 + }, + { + "kind": "prepare", + "ref": { + "type": "ingredient", + "id": "butter" + }, + "duration": 2 + } + ], + "after": [] + }, + { + "id": "s0.0", + "kind": "step", + "section": 0, + "step": 0, + "duration": { + "nominal": 2 + }, + "after": [] + }, + { + "id": "s0.1", + "kind": "step", + "section": 0, + "step": 1, + "duration": { + "nominal": 2 + }, + "after": [ + "s0.0" + ] + }, + { + "id": "s0.2", + "kind": "step", + "section": 0, + "step": 2, + "duration": { + "nominal": 2 + }, + "after": [ + "s0.1" + ] + }, + { + "id": "s1.prep", + "kind": "prep", + "section": 1, + "duration": { + "nominal": 3 + }, + "items": [ + { + "kind": "gather", + "target": "ingredient", + "count": 3, + "duration": 3 + } + ], + "after": [] + }, + { + "id": "s1.0", + "kind": "step", + "section": 1, + "step": 0, + "duration": { + "nominal": 2 + }, + "after": [] + } + ], + "sections": [ + { + "day": 0 + }, + { + "day": 0 + } + ] + }, + "schedule": { + "miseEnPlace": "perSection", + "rests": "shortest", + "totalTime": 17, + "activeTime": 8, + "preparationTime": 9, + "idleTime": 0, + "blocks": [ + { + "kind": "prep", + "task": "s0.prep", + "section": 0, + "start": 0, + "end": 6 + }, + { + "kind": "step", + "task": "s0.0", + "section": 0, + "step": 0, + "start": 6, + "end": 8 + }, + { + "kind": "step", + "task": "s0.1", + "section": 0, + "step": 1, + "start": 8, + "end": 10 + }, + { + "kind": "step", + "task": "s0.2", + "section": 0, + "step": 2, + "start": 10, + "end": 12 + }, + { + "kind": "prep", + "task": "s1.prep", + "section": 1, + "start": 12, + "end": 15 + }, + { + "kind": "step", + "task": "s1.0", + "section": 1, + "step": 0, + "start": 15, + "end": 17 + } + ], + "days": [ + { + "day": 0, + "start": 0, + "end": 17, + "sections": [ + 0, + 1 + ] + } + ] + } +} diff --git a/conformance/cases/048-preparation-single-line/ast.json b/conformance/cases/048-preparation-single-line/ast.json new file mode 100644 index 00000000..b7e2f36f --- /dev/null +++ b/conformance/cases/048-preparation-single-line/ast.json @@ -0,0 +1,339 @@ +{ + "type": "Recipe", + "meta": { + "title": "Preparation Single Line" + }, + "imports": [], + "children": [ + { + "type": "Section", + "title": "Dough", + "retroPlanning": null, + "intermediateDecl": null, + "children": [ + { + "type": "Step", + "action": null, + "children": [ + { + "type": "Text", + "value": "Mix ", + "loc": { + "start": 50, + "end": 54 + } + }, + { + "type": "Ingredient", + "name": "flour", + "modifiers": [], + "quantity": { + "type": "Quantity", + "value": { + "type": "single", + "value": 200, + "text": "200" + }, + "unit": "g", + "fixed": false, + "loc": { + "start": 61, + "end": 65 + } + }, + "alias": null, + "preparation": null, + "composite": null, + "loc": { + "start": 54, + "end": 66 + } + }, + { + "type": "Text", + "value": "(sifted and ", + "loc": { + "start": 66, + "end": 78 + } + }, + { + "type": "Ingredient", + "name": "water", + "modifiers": [], + "quantity": { + "type": "Quantity", + "value": { + "type": "single", + "value": 120, + "text": "120" + }, + "unit": "ml", + "fixed": false, + "loc": { + "start": 85, + "end": 90 + } + }, + "alias": null, + "preparation": null, + "composite": null, + "loc": { + "start": 78, + "end": 91 + } + }, + { + "type": "Text", + "value": ".", + "loc": { + "start": 91, + "end": 92 + } + } + ], + "loc": { + "start": 50, + "end": 94 + } + }, + { + "type": "Step", + "action": null, + "children": [ + { + "type": "Text", + "value": "Heat a ", + "loc": { + "start": 94, + "end": 101 + } + }, + { + "type": "Cookware", + "name": "pan", + "modifiers": [], + "alias": null, + "quantity": { + "type": "Quantity", + "value": { + "type": "single", + "value": 1, + "text": "1" + }, + "unit": null, + "fixed": false, + "loc": { + "start": 105, + "end": 108 + } + }, + "preparation": null, + "loc": { + "start": 101, + "end": 108 + } + }, + { + "type": "Text", + "value": "(large over ", + "loc": { + "start": 108, + "end": 120 + } + }, + { + "type": "Temperature", + "name": null, + "loc": { + "start": 120, + "end": 128 + }, + "value": { + "type": "single", + "value": 180, + "text": "180" + }, + "unit": "°C" + }, + { + "type": "Text", + "value": ".", + "loc": { + "start": 128, + "end": 129 + } + } + ], + "loc": { + "start": 94, + "end": 131 + } + }, + { + "type": "Step", + "action": null, + "children": [ + { + "type": "Text", + "value": "Knead ", + "loc": { + "start": 131, + "end": 137 + } + }, + { + "type": "Ingredient", + "name": "butter", + "modifiers": [], + "quantity": { + "type": "Quantity", + "value": { + "type": "single", + "value": 20, + "text": "20" + }, + "unit": "g", + "fixed": false, + "loc": { + "start": 145, + "end": 148 + } + }, + "alias": null, + "preparation": "soft, cubed", + "composite": null, + "loc": { + "start": 137, + "end": 162 + } + }, + { + "type": "Text", + "value": ".", + "loc": { + "start": 162, + "end": 163 + } + } + ], + "loc": { + "start": 131, + "end": 165 + } + } + ], + "loc": { + "start": 40, + "end": 165 + } + }, + { + "type": "Section", + "title": "Serve", + "retroPlanning": null, + "intermediateDecl": null, + "children": [ + { + "type": "Step", + "action": null, + "children": [ + { + "type": "Text", + "value": "Serve with ", + "loc": { + "start": 175, + "end": 186 + } + }, + { + "type": "Ingredient", + "name": "salt", + "modifiers": [], + "quantity": null, + "alias": null, + "preparation": null, + "composite": null, + "loc": { + "start": 186, + "end": 191 + } + }, + { + "type": "Text", + "value": "(flaky and ", + "loc": { + "start": 191, + "end": 202 + } + }, + { + "type": "Ingredient", + "name": "zest", + "modifiers": [], + "quantity": { + "type": "Quantity", + "value": { + "type": "single", + "value": 1, + "text": "1" + }, + "unit": null, + "fixed": false, + "loc": { + "start": 208, + "end": 209 + } + }, + "alias": null, + "preparation": null, + "composite": { + "type": "Composite", + "parent": "lemon", + "quantity": { + "type": "Quantity", + "value": { + "type": "single", + "value": 1, + "text": "1" + }, + "unit": null, + "fixed": false, + "loc": { + "start": 218, + "end": 219 + } + }, + "preparation": null, + "loc": { + "start": 210, + "end": 220 + } + }, + "loc": { + "start": 202, + "end": 220 + } + }, + { + "type": "Text", + "value": "(organic.", + "loc": { + "start": 220, + "end": 229 + } + } + ], + "loc": { + "start": 175, + "end": 230 + } + } + ], + "loc": { + "start": 165, + "end": 230 + } + } + ] +} diff --git a/conformance/cases/048-preparation-single-line/compiled.json b/conformance/cases/048-preparation-single-line/compiled.json new file mode 100644 index 00000000..7272067b --- /dev/null +++ b/conformance/cases/048-preparation-single-line/compiled.json @@ -0,0 +1,510 @@ +{ + "title": "Preparation Single Line", + "slug": "preparation-single-line", + "meta": { + "title": "Preparation Single Line" + }, + "registry": { + "ingredients": { + "flour": { + "id": "flour", + "name": "flour", + "default_unit": "g" + }, + "water": { + "id": "water", + "name": "water", + "default_unit": "ml" + }, + "butter": { + "id": "butter", + "name": "butter", + "default_unit": "g" + }, + "salt": { + "id": "salt", + "name": "salt" + }, + "zest": { + "id": "zest", + "name": "zest", + "is_composite": true, + "parent": "lemon" + }, + "lemon": { + "id": "lemon", + "name": "lemon", + "is_composite": true + } + }, + "cookware": { + "pan": { + "id": "pan", + "name": "pan" + } + } + }, + "shopping_list": [ + { + "id": "flour", + "name": "flour", + "_usageIds": [ + "1" + ], + "qty": 200, + "unit": "g" + }, + { + "id": "water", + "name": "water", + "_usageIds": [ + "2" + ], + "qty": 120, + "unit": "ml" + }, + { + "id": "butter", + "name": "butter", + "_usageIds": [ + "4" + ], + "qty": 20, + "unit": "g" + }, + { + "id": "salt", + "name": "salt", + "_usageIds": [ + "5" + ] + }, + { + "type": "composite", + "id": "lemon", + "name": "lemon", + "qty": 1, + "usage": [ + { + "id": "zest", + "qty": 1 + } + ] + } + ], + "cookware": [ + { + "id": "pan", + "_usageId": "3", + "qty": 1, + "fixed": false + } + ], + "sections": [ + { + "title": "Dough", + "ingredients": [ + { + "id": "flour", + "_usageId": "1", + "qty": 200, + "unit": "g" + }, + { + "id": "water", + "_usageId": "2", + "qty": 120, + "unit": "ml" + }, + { + "id": "butter", + "_usageId": "4", + "qty": 20, + "unit": "g", + "preparation": "soft, cubed" + } + ], + "cookware": [ + { + "id": "pan", + "_usageId": "3", + "qty": 1, + "fixed": false + } + ], + "steps": [ + { + "type": "step", + "content": [ + "Mix ", + { + "id": "flour", + "_usageId": "1", + "qty": 200, + "unit": "g" + }, + "(sifted and ", + { + "id": "water", + "_usageId": "2", + "qty": 120, + "unit": "ml" + }, + "." + ], + "timings": { + "start": 0, + "end": 2, + "activeDuration": 2 + } + }, + { + "type": "step", + "content": [ + "Heat a ", + { + "id": "pan", + "_usageId": "3", + "qty": 1, + "fixed": false + }, + "(large over ", + { + "type": "temperature", + "quantity": { + "type": "single", + "value": 180, + "text": "180" + }, + "unit": "°C" + }, + "." + ], + "timings": { + "start": 2, + "end": 4, + "activeDuration": 2 + } + }, + { + "type": "step", + "content": [ + "Knead ", + { + "id": "butter", + "_usageId": "4", + "qty": 20, + "unit": "g", + "preparation": "soft, cubed" + }, + "." + ], + "timings": { + "start": 4, + "end": 6, + "activeDuration": 2 + } + } + ] + }, + { + "title": "Serve", + "ingredients": [ + { + "id": "salt", + "_usageId": "5" + }, + { + "id": "zest", + "_usageId": "6", + "qty": 1, + "composite": { + "parent": "lemon", + "quantity": 1 + } + } + ], + "cookware": [], + "steps": [ + { + "type": "step", + "content": [ + "Serve with ", + { + "id": "salt", + "_usageId": "5" + }, + "(flaky and ", + { + "id": "zest", + "_usageId": "6", + "qty": 1, + "composite": { + "parent": "lemon", + "quantity": 1 + } + }, + "(organic." + ], + "timings": { + "start": 6, + "end": 8, + "activeDuration": 2 + } + } + ] + } + ], + "warnings": [], + "metrics": { + "totalTime": 17, + "totalBreakdown": [ + { + "label": "section_active:Dough", + "duration": 6 + }, + { + "label": "section_active:Serve", + "duration": 2 + } + ], + "idleTime": 0, + "activeTime": 8, + "activeBreakdown": [ + { + "label": "section_active:Dough", + "duration": 6 + }, + { + "label": "section_active:Serve", + "duration": 2 + } + ], + "preparationTime": 9, + "prepBreakdown": [ + { + "label": "ingredients_overhead", + "duration": 6 + }, + { + "label": "cookware_overhead", + "duration": 1 + }, + { + "label": "prep_butter", + "duration": 2 + } + ] + }, + "miseEnPlace": [ + { + "section": 0, + "duration": 6, + "items": [ + { + "kind": "gather", + "target": "ingredient", + "count": 3, + "duration": 3 + }, + { + "kind": "gather", + "target": "cookware", + "count": 1, + "duration": 1 + }, + { + "kind": "prepare", + "ref": { + "type": "ingredient", + "id": "butter" + }, + "duration": 2 + } + ] + }, + { + "section": 1, + "duration": 3, + "items": [ + { + "kind": "gather", + "target": "ingredient", + "count": 3, + "duration": 3 + } + ] + } + ], + "tasks": { + "tasks": [ + { + "id": "s0.prep", + "kind": "prep", + "section": 0, + "duration": { + "nominal": 6 + }, + "items": [ + { + "kind": "gather", + "target": "ingredient", + "count": 3, + "duration": 3 + }, + { + "kind": "gather", + "target": "cookware", + "count": 1, + "duration": 1 + }, + { + "kind": "prepare", + "ref": { + "type": "ingredient", + "id": "butter" + }, + "duration": 2 + } + ], + "after": [] + }, + { + "id": "s0.0", + "kind": "step", + "section": 0, + "step": 0, + "duration": { + "nominal": 2 + }, + "after": [] + }, + { + "id": "s0.1", + "kind": "step", + "section": 0, + "step": 1, + "duration": { + "nominal": 2 + }, + "after": [ + "s0.0" + ] + }, + { + "id": "s0.2", + "kind": "step", + "section": 0, + "step": 2, + "duration": { + "nominal": 2 + }, + "after": [ + "s0.1" + ] + }, + { + "id": "s1.prep", + "kind": "prep", + "section": 1, + "duration": { + "nominal": 3 + }, + "items": [ + { + "kind": "gather", + "target": "ingredient", + "count": 3, + "duration": 3 + } + ], + "after": [] + }, + { + "id": "s1.0", + "kind": "step", + "section": 1, + "step": 0, + "duration": { + "nominal": 2 + }, + "after": [] + } + ], + "sections": [ + { + "day": 0 + }, + { + "day": 0 + } + ] + }, + "schedule": { + "miseEnPlace": "perSection", + "rests": "shortest", + "totalTime": 17, + "activeTime": 8, + "preparationTime": 9, + "idleTime": 0, + "blocks": [ + { + "kind": "prep", + "task": "s0.prep", + "section": 0, + "start": 0, + "end": 6 + }, + { + "kind": "step", + "task": "s0.0", + "section": 0, + "step": 0, + "start": 6, + "end": 8 + }, + { + "kind": "step", + "task": "s0.1", + "section": 0, + "step": 1, + "start": 8, + "end": 10 + }, + { + "kind": "step", + "task": "s0.2", + "section": 0, + "step": 2, + "start": 10, + "end": 12 + }, + { + "kind": "prep", + "task": "s1.prep", + "section": 1, + "start": 12, + "end": 15 + }, + { + "kind": "step", + "task": "s1.0", + "section": 1, + "step": 0, + "start": 15, + "end": 17 + } + ], + "days": [ + { + "day": 0, + "start": 0, + "end": 17, + "sections": [ + 0, + 1 + ] + } + ] + } +} diff --git a/conformance/cases/048-preparation-single-line/input.gram b/conformance/cases/048-preparation-single-line/input.gram new file mode 100644 index 00000000..b7ef7e91 --- /dev/null +++ b/conformance/cases/048-preparation-single-line/input.gram @@ -0,0 +1,15 @@ +--- +title: Preparation Single Line +--- + +## Dough + +Mix @flour{200g}(sifted and @water{120ml}. + +Heat a #pan{1}(large over ^{180°C}. + +Knead @butter{20g}(soft, cubed). + +## Serve + +Serve with @salt(flaky and @zest{1}<@lemon{1}(organic. diff --git a/packages/parser/grammar.ohm b/packages/parser/grammar.ohm index c688fdfb..0eea81f2 100644 --- a/packages/parser/grammar.ohm +++ b/packages/parser/grammar.ohm @@ -142,7 +142,10 @@ GRAM { // first word) and the braces leaked into prose -- merging distinct ingredients. // Each "-- malformed" branch below turns that into a readable error instead. malformedBraces = "{" (~("}" | nl) any)* ("}" | &nl | end) - preparation = "(" (~")" any)* ")" + // On one line: an unclosed `(` is prose, never the start of a preparation + // that would run to the next `)` further down the recipe, over blank lines + // and section headers. + preparation = "(" (~(")" | nl) any)* ")" // `modifier*` mirrors simpleIngredient: a composite parent is an ingredient // reference like any other, and authors (and AI importers) write `<@&parent` // to mark a parent already introduced earlier. Without it, `refName` rejected diff --git a/packages/parser/src/grammar-content.ts b/packages/parser/src/grammar-content.ts index 99786f5a..97e21887 100644 --- a/packages/parser/src/grammar-content.ts +++ b/packages/parser/src/grammar-content.ts @@ -1,2 +1,2 @@ // AUTO-GENERATED FILE. DO NOT EDIT. -export const grammarContent = "GRAM {\n Recipe = Frontmatter? UseBlock? Content?\n\n Content =\n | (nl* Block)* (nl* Section)+ -- explicit\n | (nl* Block)+ -- implicit\n\n Frontmatter = \"---\" nl KeyValue* \"---\" (nl | end)\n emptyLine = sp* nl\n KeyValue = emptyLine* key \":\" sp* value nl\n key = (alnum | \"_\" | \"-\")+\n value = (~nl any)*\n\n // Import directives, constrained between the frontmatter and the content --\n // this is what makes \"@use\" unambiguous with an ingredient literally named\n // \"use\" that happens to appear in a step (unchanged, still parses as such).\n // The block must open with a real `UseDirective`: a leading `UseComment` on\n // its own (no `@use` anywhere in the block) is deliberately NOT swallowed\n // here, so a document with no imports at all keeps treating its opening\n // comment as ordinary content, exactly as before this feature existed. Once\n // the block has started, a comment between two directives doesn't break it.\n UseBlock = nl* useDirective (nl* (useDirective | useComment))*\n useComment = applySyntactic nl\n useDirective = \"@use\" sp+ moduleSpecifier sp+ \"as\" sp+ bindings sp* retroPlanning? sp* (nl | end)\n moduleSpecifier = \"\\\"\" (~(\"\\\"\" | nl) any)+ \"\\\"\"\n bindings =\n | \"{\" sp* binding moreBindings* sp* \"}\" -- named\n | binding -- default\n moreBindings = sp* \",\" sp* binding\n binding = \"&\" bindingName (sp+ \"as\" sp+ \"&\" bindingName)?\n bindingName =\n | bindingFullName \"{}\" -- full\n | bindingBareName -- bare\n // Unlike `name`, this must stop at \",\" and \"&\" -- a `{}`-delimited multi-word\n // binding sits inside a comma-separated list (`{ &a, &b multi{} }`) and can be\n // followed by a rename (`&a as &b multi{}`), so it must not swallow past the\n // next binding's separator or sigil while scanning ahead for its own \"{}\".\n bindingFullName = (~bindingFullNameStop any)+\n bindingFullNameStop = syntaxChar | nl | \",\" | refSigil\n bindingBareName = (~bindingStop any)+\n bindingStop = space | nl | syntaxChar | sentencePunct | \"~{\"\n\n Section = header Block*\n header =\n | \"##\" sp+ title headerExtension? sp* (nl+ | end) -- ok\n | \"##\" sp+ title headerExtension sp* headerTrailing (nl+ | end) -- trailing\n // Text left after a header's `~{...}` / `->&name` (most often the second\n // word of a multi-word intermediate written without `{}`). Without this\n // branch the whole `##` line silently fell back to a step: the section\n // vanished, or its `~{-1d}` anchor surfaced as a misleading timer error.\n // Matched only to raise a readable error (see `header_trailing`).\n headerTrailing = (~nl any)+\n headerExtension = \n | retroPlanning sp* intermediateDecl? -- retro\n | intermediateDecl sp* retroPlanning? -- decl\n titleStop = nl | \"->&\" | \"~{\"\n title = (~titleStop any)*\n \n retroPlanning = \"~{\" (~\"}\" any)+ \"}\"\n \n Block = \n | Comment nl* -- comment\n | (~header step) -- step\n \n step = startAction? line (nl line)* (nl+ | end)\n startAction = \"[\" (~\"]\" any)+ \"]\" sp*\n line = stepContent+\n stepContent =\n | invalidUseDirective\n | invalidComposite\n | invalidAlternativeBar\n | text\n | ws\n | intermediateDecl\n | applySyntactic\n | fallback\n\n // A `@use \"...\"` past the import block (after the first step, misspelled,\n // or otherwise misplaced) would otherwise silently parse as an ingredient\n // literally named \"use\" followed by prose -- this turns it into a readable\n // error instead. Only triggers on the exact `@use \"` prefix, so an actual\n // ingredient named \"use\" followed by unrelated quoted text is unaffected.\n invalidUseDirective = \"@use\" sp+ \"\\\"\"\n\n invalidComposite = space+ \"<\" space* \"@\" name\n\n invalidAlternativeBar = \"|\"\n\n ws = space+\n\n fallback = ~nl any\n\n Element = \n | Ingredient\n | Cookware\n | Timer\n | Temperature\n | Reference\n | CommentBlock\n | Comment\n\n intermediateDecl =\n | \"->&\" name \"{}\" -- full\n | \"->&\" singleWordName -- bare\n\n // Ingredients\n Ingredient = \n | Alternative\n | simpleIngredient\n\n Alternative = simpleIngredient (\"|\" simpleIngredient)+\n \n // Rule: Quantity is mandatory unless it's a single word name\n // Uses refName (not name) so a bare @&chicken mention can't have its name\n // greedily swallow everything up to an unrelated later {...} that happens\n // to close a valid ingredientQuantity -- e.g. \"@&chicken for ~{3min}\" must\n // stop at \"~\" and fall back to the bare alternative, not consume \"chicken\n // for ~\" as a bogus multi-word name with the timer's quantity attached.\n simpleIngredient =\n | \"@\" modifier* refName alias? ingredientQuantity preparation? composite? -- full\n | \"@\" modifier* refName alias? malformedBraces -- malformed\n | \"@\" modifier* singleWordName alias? preparation? composite? -- bare\n\n modifier = \"?\" | \"-\" | \"*\" | \"&\" | \"=\"\n alias = \":\" refName\n ingredientQuantity = \"{\" space* (relativeQuantity | absoluteQuantity) space* \"}\"\n relativeQuantity = number space* \"%\" space* (\"@&\" | \"&\") space* name\n absoluteQuantity = (range | number)? space* (unit space*)?\n textQuantity = \"{\" space* (~\"}\" any)+ space* \"}\"\n fixed = \"=\"\n // A unit is one or more words. A word may contain \".\" or an apostrophe after\n // its first character (c.à.s, fl. oz., d'eau) but never starts with one, so a\n // stray \"{1. kg}\" or \"{.5 g}\" is still rejected rather than half-parsed.\n unit = unitName\n unitName = unitWord (sp+ unitWord)*\n unitWord = unitLead unitChar*\n unitLead = alnum | \"°\" | \"%\"\n unitChar = unitLead | \".\" | \"'\" | \"’\"\n\n // A \"{...}\" glued to an element that did not parse as a valid quantity. Without\n // this, the element silently fell back to its bare form (name truncated to its\n // first word) and the braces leaked into prose -- merging distinct ingredients.\n // Each \"-- malformed\" branch below turns that into a readable error instead.\n malformedBraces = \"{\" (~(\"}\" | nl) any)* (\"}\" | &nl | end)\n preparation = \"(\" (~\")\" any)* \")\"\n // `modifier*` mirrors simpleIngredient: a composite parent is an ingredient\n // reference like any other, and authors (and AI importers) write `<@&parent`\n // to mark a parent already introduced earlier. Without it, `refName` rejected\n // the leading \"&\" (it is a refSigil), the `bare` branch fell through to\n // `singleWordName` -- which does allow \"&\" -- and a multi-word parent silently\n // truncated at the first space, registering a phantom ingredient literally\n // named \"&citron\" alongside the real one. The modifiers are parsed and then\n // ignored (see composite_full/composite_bare): \"&\" is redundant on a parent\n // because the MAX rule already collapses repeat draws into one purchase.\n composite =\n | \"<@\" modifier* refName ingredientQuantity preparation? -- full\n | \"<@\" modifier* refName malformedBraces -- malformed\n | \"<@\" modifier* singleWordName preparation? -- bare\n\n // Cookware\n Cookware =\n | CookwareAlternative\n | simpleCookware\n\n CookwareAlternative = simpleCookware (\"|\" simpleCookware)+\n // Same refName fix as simpleIngredient above -- a bare #pan mention must not\n // swallow an unrelated later {...} (e.g. another ingredient's integer-only\n // quantity) into its own name.\n simpleCookware =\n | \"#\" modifier* refName alias? cookwareQuantity preparation? -- full\n | \"#\" modifier* refName alias? malformedBraces -- malformed\n | \"#\" modifier* singleWordName alias? preparation? -- bare\n\n // Cookware quantity is numbers only, no units (e.g. {}, {2}) -- unlike ingredientQuantity.\n cookwareQuantity = \"{\" space* number? space* \"}\"\n\n // Timer\n Timer =\n | \"~\" \"_\"? name? ingredientQuantity -- full\n | \"~\" \"_\"? name? malformedBraces -- malformed\n \n // Temperature\n Temperature = \"^\" name? (ingredientQuantity | textQuantity)\n\n\n // Comments\n Comment = \"//\" (~nl any)*\n CommentBlock = \"/*\" (~\"*/\" any)* \"*/\"\n\n // Primitives\n name = (~(syntaxChar | nl) any)+\n singleWordName = (~(syntaxChar | space | nl | sentencePunct) any)+\n sentencePunct = \",\" | \";\" | \"!\" | \"?\" | \".\"\n // Like name, but also stops at element sigils so a bare &dough is not consumed\n // into a Reference -- full when a sigil like # or @ appears later on the line.\n refName = (~(syntaxChar | refSigil | nl) any)+\n refSigil = \"@\" | \"#\" | \"~\" | \"^\" | \"&\"\n\n number = mixedFraction | unicodeFraction | plainNumber\n plainNumber = digit+ (\".\" digit+)? (\"/\" digit+)?\n mixedFraction = digit+ sp+ digit+ \"/\" digit+\n unicodeFraction =\n | digit+ unicodeFractionChar -- mixed\n | unicodeFractionChar -- bare\n unicodeFractionChar = \"¼\" | \"½\" | \"¾\" | \"⅓\" | \"⅔\" | \"⅕\" | \"⅖\" | \"⅗\" | \"⅘\" | \"⅙\" | \"⅚\" | \"⅛\" | \"⅜\" | \"⅝\" | \"⅞\"\n range = number space* \"-\" space* number\n\n text = space* (~(elementStart | nl | \"|\") any)+\n\n Reference =\n | \"&\" refName ingredientQuantity -- full\n | \"&\" refName malformedBraces -- malformed\n | \"&\" singleWordName -- bare\n\n // Delimiters that end a name (start of quantity/alias/preparation/composite/alternative).\n // @, #, ~, !, &, ?, * are deliberately absent here, so names may contain them freely --\n // only elementStart (below) treats those as starting a new element, and only in free text.\n syntaxChar = \"{\" | \"}\" | \"[\" | \"]\" | \"(\" | \")\" | \"<\" | \"|\" | \":\"\n\n // Stricter than syntaxChar: used to detect where an element starts within free text.\n elementStart = \"@\" | \"#\" | \"~\" | \"^\" | \"//\" | \"/*\" | \"->&\" | \"&\"\n \n nl = \"\\r\"? \"\\n\"\n sp = \" \" | \"\\t\"\n space := sp\n}\n"; +export const grammarContent = "GRAM {\n Recipe = Frontmatter? UseBlock? Content?\n\n Content =\n | (nl* Block)* (nl* Section)+ -- explicit\n | (nl* Block)+ -- implicit\n\n Frontmatter = \"---\" nl KeyValue* \"---\" (nl | end)\n emptyLine = sp* nl\n KeyValue = emptyLine* key \":\" sp* value nl\n key = (alnum | \"_\" | \"-\")+\n value = (~nl any)*\n\n // Import directives, constrained between the frontmatter and the content --\n // this is what makes \"@use\" unambiguous with an ingredient literally named\n // \"use\" that happens to appear in a step (unchanged, still parses as such).\n // The block must open with a real `UseDirective`: a leading `UseComment` on\n // its own (no `@use` anywhere in the block) is deliberately NOT swallowed\n // here, so a document with no imports at all keeps treating its opening\n // comment as ordinary content, exactly as before this feature existed. Once\n // the block has started, a comment between two directives doesn't break it.\n UseBlock = nl* useDirective (nl* (useDirective | useComment))*\n useComment = applySyntactic nl\n useDirective = \"@use\" sp+ moduleSpecifier sp+ \"as\" sp+ bindings sp* retroPlanning? sp* (nl | end)\n moduleSpecifier = \"\\\"\" (~(\"\\\"\" | nl) any)+ \"\\\"\"\n bindings =\n | \"{\" sp* binding moreBindings* sp* \"}\" -- named\n | binding -- default\n moreBindings = sp* \",\" sp* binding\n binding = \"&\" bindingName (sp+ \"as\" sp+ \"&\" bindingName)?\n bindingName =\n | bindingFullName \"{}\" -- full\n | bindingBareName -- bare\n // Unlike `name`, this must stop at \",\" and \"&\" -- a `{}`-delimited multi-word\n // binding sits inside a comma-separated list (`{ &a, &b multi{} }`) and can be\n // followed by a rename (`&a as &b multi{}`), so it must not swallow past the\n // next binding's separator or sigil while scanning ahead for its own \"{}\".\n bindingFullName = (~bindingFullNameStop any)+\n bindingFullNameStop = syntaxChar | nl | \",\" | refSigil\n bindingBareName = (~bindingStop any)+\n bindingStop = space | nl | syntaxChar | sentencePunct | \"~{\"\n\n Section = header Block*\n header =\n | \"##\" sp+ title headerExtension? sp* (nl+ | end) -- ok\n | \"##\" sp+ title headerExtension sp* headerTrailing (nl+ | end) -- trailing\n // Text left after a header's `~{...}` / `->&name` (most often the second\n // word of a multi-word intermediate written without `{}`). Without this\n // branch the whole `##` line silently fell back to a step: the section\n // vanished, or its `~{-1d}` anchor surfaced as a misleading timer error.\n // Matched only to raise a readable error (see `header_trailing`).\n headerTrailing = (~nl any)+\n headerExtension = \n | retroPlanning sp* intermediateDecl? -- retro\n | intermediateDecl sp* retroPlanning? -- decl\n titleStop = nl | \"->&\" | \"~{\"\n title = (~titleStop any)*\n \n retroPlanning = \"~{\" (~\"}\" any)+ \"}\"\n \n Block = \n | Comment nl* -- comment\n | (~header step) -- step\n \n step = startAction? line (nl line)* (nl+ | end)\n startAction = \"[\" (~\"]\" any)+ \"]\" sp*\n line = stepContent+\n stepContent =\n | invalidUseDirective\n | invalidComposite\n | invalidAlternativeBar\n | text\n | ws\n | intermediateDecl\n | applySyntactic\n | fallback\n\n // A `@use \"...\"` past the import block (after the first step, misspelled,\n // or otherwise misplaced) would otherwise silently parse as an ingredient\n // literally named \"use\" followed by prose -- this turns it into a readable\n // error instead. Only triggers on the exact `@use \"` prefix, so an actual\n // ingredient named \"use\" followed by unrelated quoted text is unaffected.\n invalidUseDirective = \"@use\" sp+ \"\\\"\"\n\n invalidComposite = space+ \"<\" space* \"@\" name\n\n invalidAlternativeBar = \"|\"\n\n ws = space+\n\n fallback = ~nl any\n\n Element = \n | Ingredient\n | Cookware\n | Timer\n | Temperature\n | Reference\n | CommentBlock\n | Comment\n\n intermediateDecl =\n | \"->&\" name \"{}\" -- full\n | \"->&\" singleWordName -- bare\n\n // Ingredients\n Ingredient = \n | Alternative\n | simpleIngredient\n\n Alternative = simpleIngredient (\"|\" simpleIngredient)+\n \n // Rule: Quantity is mandatory unless it's a single word name\n // Uses refName (not name) so a bare @&chicken mention can't have its name\n // greedily swallow everything up to an unrelated later {...} that happens\n // to close a valid ingredientQuantity -- e.g. \"@&chicken for ~{3min}\" must\n // stop at \"~\" and fall back to the bare alternative, not consume \"chicken\n // for ~\" as a bogus multi-word name with the timer's quantity attached.\n simpleIngredient =\n | \"@\" modifier* refName alias? ingredientQuantity preparation? composite? -- full\n | \"@\" modifier* refName alias? malformedBraces -- malformed\n | \"@\" modifier* singleWordName alias? preparation? composite? -- bare\n\n modifier = \"?\" | \"-\" | \"*\" | \"&\" | \"=\"\n alias = \":\" refName\n ingredientQuantity = \"{\" space* (relativeQuantity | absoluteQuantity) space* \"}\"\n relativeQuantity = number space* \"%\" space* (\"@&\" | \"&\") space* name\n absoluteQuantity = (range | number)? space* (unit space*)?\n textQuantity = \"{\" space* (~\"}\" any)+ space* \"}\"\n fixed = \"=\"\n // A unit is one or more words. A word may contain \".\" or an apostrophe after\n // its first character (c.à.s, fl. oz., d'eau) but never starts with one, so a\n // stray \"{1. kg}\" or \"{.5 g}\" is still rejected rather than half-parsed.\n unit = unitName\n unitName = unitWord (sp+ unitWord)*\n unitWord = unitLead unitChar*\n unitLead = alnum | \"°\" | \"%\"\n unitChar = unitLead | \".\" | \"'\" | \"’\"\n\n // A \"{...}\" glued to an element that did not parse as a valid quantity. Without\n // this, the element silently fell back to its bare form (name truncated to its\n // first word) and the braces leaked into prose -- merging distinct ingredients.\n // Each \"-- malformed\" branch below turns that into a readable error instead.\n malformedBraces = \"{\" (~(\"}\" | nl) any)* (\"}\" | &nl | end)\n // On one line: an unclosed `(` is prose, never the start of a preparation\n // that would run to the next `)` further down the recipe, over blank lines\n // and section headers.\n preparation = \"(\" (~(\")\" | nl) any)* \")\"\n // `modifier*` mirrors simpleIngredient: a composite parent is an ingredient\n // reference like any other, and authors (and AI importers) write `<@&parent`\n // to mark a parent already introduced earlier. Without it, `refName` rejected\n // the leading \"&\" (it is a refSigil), the `bare` branch fell through to\n // `singleWordName` -- which does allow \"&\" -- and a multi-word parent silently\n // truncated at the first space, registering a phantom ingredient literally\n // named \"&citron\" alongside the real one. The modifiers are parsed and then\n // ignored (see composite_full/composite_bare): \"&\" is redundant on a parent\n // because the MAX rule already collapses repeat draws into one purchase.\n composite =\n | \"<@\" modifier* refName ingredientQuantity preparation? -- full\n | \"<@\" modifier* refName malformedBraces -- malformed\n | \"<@\" modifier* singleWordName preparation? -- bare\n\n // Cookware\n Cookware =\n | CookwareAlternative\n | simpleCookware\n\n CookwareAlternative = simpleCookware (\"|\" simpleCookware)+\n // Same refName fix as simpleIngredient above -- a bare #pan mention must not\n // swallow an unrelated later {...} (e.g. another ingredient's integer-only\n // quantity) into its own name.\n simpleCookware =\n | \"#\" modifier* refName alias? cookwareQuantity preparation? -- full\n | \"#\" modifier* refName alias? malformedBraces -- malformed\n | \"#\" modifier* singleWordName alias? preparation? -- bare\n\n // Cookware quantity is numbers only, no units (e.g. {}, {2}) -- unlike ingredientQuantity.\n cookwareQuantity = \"{\" space* number? space* \"}\"\n\n // Timer\n Timer =\n | \"~\" \"_\"? name? ingredientQuantity -- full\n | \"~\" \"_\"? name? malformedBraces -- malformed\n \n // Temperature\n Temperature = \"^\" name? (ingredientQuantity | textQuantity)\n\n\n // Comments\n Comment = \"//\" (~nl any)*\n CommentBlock = \"/*\" (~\"*/\" any)* \"*/\"\n\n // Primitives\n name = (~(syntaxChar | nl) any)+\n singleWordName = (~(syntaxChar | space | nl | sentencePunct) any)+\n sentencePunct = \",\" | \";\" | \"!\" | \"?\" | \".\"\n // Like name, but also stops at element sigils so a bare &dough is not consumed\n // into a Reference -- full when a sigil like # or @ appears later on the line.\n refName = (~(syntaxChar | refSigil | nl) any)+\n refSigil = \"@\" | \"#\" | \"~\" | \"^\" | \"&\"\n\n number = mixedFraction | unicodeFraction | plainNumber\n plainNumber = digit+ (\".\" digit+)? (\"/\" digit+)?\n mixedFraction = digit+ sp+ digit+ \"/\" digit+\n unicodeFraction =\n | digit+ unicodeFractionChar -- mixed\n | unicodeFractionChar -- bare\n unicodeFractionChar = \"¼\" | \"½\" | \"¾\" | \"⅓\" | \"⅔\" | \"⅕\" | \"⅖\" | \"⅗\" | \"⅘\" | \"⅙\" | \"⅚\" | \"⅛\" | \"⅜\" | \"⅝\" | \"⅞\"\n range = number space* \"-\" space* number\n\n text = space* (~(elementStart | nl | \"|\") any)+\n\n Reference =\n | \"&\" refName ingredientQuantity -- full\n | \"&\" refName malformedBraces -- malformed\n | \"&\" singleWordName -- bare\n\n // Delimiters that end a name (start of quantity/alias/preparation/composite/alternative).\n // @, #, ~, !, &, ?, * are deliberately absent here, so names may contain them freely --\n // only elementStart (below) treats those as starting a new element, and only in free text.\n syntaxChar = \"{\" | \"}\" | \"[\" | \"]\" | \"(\" | \")\" | \"<\" | \"|\" | \":\"\n\n // Stricter than syntaxChar: used to detect where an element starts within free text.\n elementStart = \"@\" | \"#\" | \"~\" | \"^\" | \"//\" | \"/*\" | \"->&\" | \"&\"\n \n nl = \"\\r\"? \"\\n\"\n sp = \" \" | \"\\t\"\n space := sp\n}\n"; diff --git a/packages/parser/tests/preparation-single-line.test.ts b/packages/parser/tests/preparation-single-line.test.ts new file mode 100644 index 00000000..dcddc567 --- /dev/null +++ b/packages/parser/tests/preparation-single-line.test.ts @@ -0,0 +1,70 @@ +import { describe, expect, it } from "bun:test"; +import { ASTNodeType, getAST } from "../src/index"; + +// A preparation ends on its line: an unclosed `(` used to run to the next `)` +// of the recipe, over blank lines and section headers, and swallowed them. + +// biome-ignore lint/suspicious/noExplicitAny: walking a parsed AST in tests. +const sections = (src: string): any[] => getAST(src).children as any[]; + +// The elements of the only step of a one-step recipe. +// biome-ignore lint/suspicious/noExplicitAny: walking a parsed AST in tests. +const elements = (src: string): any[] => + sections(`## T\n\n${src}\n`)[0].children[0].children; + +// biome-ignore lint/suspicious/noExplicitAny: walking a parsed AST in tests. +const text = (nodes: any[]): string => + nodes + .filter((c) => c.type === ASTNodeType.Text) + .map((c) => c.value) + .join(""); + +describe("a preparation stays on its line", () => { + it("keeps the steps and sections after an unclosed parenthesis", () => { + const src = + "## A\n\nMix @flour{200g}(sifted and\n\nBake at ^{180°C}.\n\n## B\n\nServe (hot).\n"; + const [a, b] = sections(src); + expect(a.children).toHaveLength(2); + const flour = a.children[0].children.find( + // biome-ignore lint/suspicious/noExplicitAny: walking a parsed AST in tests. + (c: any) => c.type === ASTNodeType.Ingredient, + ); + expect(flour.name).toBe("flour"); + expect(flour.preparation).toBeNull(); + expect(text(a.children[0].children)).toContain("(sifted and"); + expect(b.title).toBe("B"); + expect(text(b.children[0].children)).toBe("Serve (hot)."); + }); + + it("leaves the unclosed parenthesis of a bare ingredient in the text", () => { + const nodes = elements("Add @salt(fine"); + const salt = nodes.find((c) => c.type === ASTNodeType.Ingredient); + expect(salt.name).toBe("salt"); + expect(salt.preparation).toBeNull(); + expect(text(nodes)).toContain("(fine"); + }); + + it("does the same for cookware and a composite parent", () => { + const pan = elements("Heat a #pan{1}(large").find( + (c) => c.type === ASTNodeType.Cookware, + ); + expect(pan.preparation).toBeNull(); + const zest = elements("Grate @zest{1}<@lemon{1}(organic").find( + (c) => c.type === ASTNodeType.Ingredient, + ); + expect(zest.composite.parent).toBe("lemon"); + expect(text(elements("Grate @zest{1}<@lemon{1}(organic"))).toContain( + "(organic", + ); + expect(zest.composite.preparation).toBeNull(); + }); + + it("reads a preparation closed on its line as before", () => { + const [flour] = elements("@flour{200g}(sifted, twice)"); + expect(flour.preparation).toBe("sifted, twice"); + const [pan] = elements("#pan{1}(20 cm)"); + expect(pan.preparation).toBe("20 cm"); + const [zest] = elements("@zest{1}<@lemon{1}(organic)"); + expect(zest.composite.preparation).toBe("organic"); + }); +}); -- 2.54.0