From b80ad4561196f4acb16b0d039c5d45fb6aa6c3cf Mon Sep 17 00:00:00 2001 From: abiwab Date: Tue, 17 Nov 2026 00:03:57 +0100 Subject: [PATCH] refactor(parser): use one rule for every glued parenthesis --- packages/parser/grammar.ohm | 17 +++++++---------- packages/parser/src/grammar-content.ts | 2 +- packages/parser/src/index.ts | 3 --- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/packages/parser/grammar.ohm b/packages/parser/grammar.ohm index 08eb3b49..33ec3d68 100644 --- a/packages/parser/grammar.ohm +++ b/packages/parser/grammar.ohm @@ -157,9 +157,12 @@ 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) - // 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. + // The parenthesis glued to an element: an ingredient's or a tool's + // preparation, a composite parent's, a temperature's note (and the sensory + // checkpoints' to come). Every rule using it is lexical, so it touches what + // comes before it. 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` @@ -212,13 +215,7 @@ GRAM { // start with a space, as it could when the rule was syntactic: `^ {180°C}` // and `^oven {180°C}` read as before (`clean` drops the space). The note // since 1.5.0. - temperature = "^" name? (ingredientQuantity | textQuantity) gluedNote? - - // A note glued to an element's closing brace, on one line: an unclosed - // `(` is prose, never the start of a note that would run to the next `)` - // further down the recipe. Meant to be shared by the elements to come - // (the sensory checkpoints). - gluedNote = "(" (~(")" | nl) any)* ")" + temperature = "^" name? (ingredientQuantity | textQuantity) preparation? // Comments diff --git a/packages/parser/src/grammar-content.ts b/packages/parser/src/grammar-content.ts index f36cdf48..a4fd86fc 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 ingredientAmount -- 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? ingredientAmount 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 // The amount of an ingredient, a composite parent or a reference: an\n // ingredientQuantity, or a sum of quantities (`{1 cup + 1 tbsp}`,\n // `{1 cup - 2 tbsp}`). Since 1.5.0. Timers and temperatures keep\n // ingredientQuantity. PEG order matters: a choice that matches a prefix is\n // final, so a plain quantity is only taken when the braces close right\n // after it (`&`), otherwise it would take `1 cup` from `{1 cup + 1 tbsp}`\n // and fail on the `+`. Tried before the sum, it also keeps its range\n // (`{1 - 2 cup}` is one range, not 1 minus 2 cups) and its AST.\n ingredientAmount = \"{\" space* (relativeQuantity | closedQuantity | compoundQuantity) space* \"}\"\n closedQuantity = absoluteQuantity &(space* \"}\")\n compoundQuantity = quantityTerm (space* compoundOp space* quantityTerm)+\n // The unit is optional so `{1 + 1 cup}` reaches the compiler, which says\n // what is wrong with it, instead of failing as a syntax error.\n quantityTerm = (range | number) (space* unit)?\n compoundOp = \"+\" | \"-\"\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 ingredientAmount 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 // `ingredientQuantity` comes first (PEG order) so a plain timer keeps its\n // AST: `~{10 min}`, and also `~{1h30}`, read as 1 + unit \"h30\". Anything\n // else closed on the same line (`1:30 h`, `5'30\"`, `1h + 30min`, `1h30-2h`)\n // is kept as text. Either way the AST carries `raw`, the text between the\n // braces, and the compiler reads the duration from it (`parseDuration` in\n // @gram-lang/i18n), never from the quantity. Only an unclosed brace is\n // still a syntax error.\n Timer =\n | \"~\" \"_\"? name? ingredientQuantity -- full\n | \"~\" \"_\"? name? timerText -- text\n | \"~\" \"_\"? name? malformedBraces -- malformed\n timerText = \"{\" (~(\"}\" | nl) any)* \"}\"\n \n // Temperature\n // Lexical, so only a note glued to `}` is one: `^{180°C}(fan)` has a note,\n // `^{180°C} (preheated)` keeps its parenthesis in the prose. `name` may\n // start with a space, as it could when the rule was syntactic: `^ {180°C}`\n // and `^oven {180°C}` read as before (`clean` drops the space). The note\n // since 1.5.0.\n temperature = \"^\" name? (ingredientQuantity | textQuantity) gluedNote?\n\n // A note glued to an element's closing brace, on one line: an unclosed\n // `(` is prose, never the start of a note that would run to the next `)`\n // further down the recipe. Meant to be shared by the elements to come\n // (the sensory checkpoints).\n gluedNote = \"(\" (~(\")\" | nl) any)* \")\"\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 // \".\" or \",\" between two digits is a decimal separator (1.5, 1,5): Gram has\n // no thousands separator. A comma not between two digits ({1, 5 kg}) still\n // fails, so the commas of the prose around an element are never touched.\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 ingredientAmount -- 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 ingredientAmount -- 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? ingredientAmount 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 // The amount of an ingredient, a composite parent or a reference: an\n // ingredientQuantity, or a sum of quantities (`{1 cup + 1 tbsp}`,\n // `{1 cup - 2 tbsp}`). Since 1.5.0. Timers and temperatures keep\n // ingredientQuantity. PEG order matters: a choice that matches a prefix is\n // final, so a plain quantity is only taken when the braces close right\n // after it (`&`), otherwise it would take `1 cup` from `{1 cup + 1 tbsp}`\n // and fail on the `+`. Tried before the sum, it also keeps its range\n // (`{1 - 2 cup}` is one range, not 1 minus 2 cups) and its AST.\n ingredientAmount = \"{\" space* (relativeQuantity | closedQuantity | compoundQuantity) space* \"}\"\n closedQuantity = absoluteQuantity &(space* \"}\")\n compoundQuantity = quantityTerm (space* compoundOp space* quantityTerm)+\n // The unit is optional so `{1 + 1 cup}` reaches the compiler, which says\n // what is wrong with it, instead of failing as a syntax error.\n quantityTerm = (range | number) (space* unit)?\n compoundOp = \"+\" | \"-\"\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 // The parenthesis glued to an element: an ingredient's or a tool's\n // preparation, a composite parent's, a temperature's note (and the sensory\n // checkpoints' to come). Every rule using it is lexical, so it touches what\n // comes before it. On one line: an unclosed `(` is prose, never the start\n // of a preparation that would run to the next `)` further down the recipe,\n // over blank lines 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 ingredientAmount 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 // `ingredientQuantity` comes first (PEG order) so a plain timer keeps its\n // AST: `~{10 min}`, and also `~{1h30}`, read as 1 + unit \"h30\". Anything\n // else closed on the same line (`1:30 h`, `5'30\"`, `1h + 30min`, `1h30-2h`)\n // is kept as text. Either way the AST carries `raw`, the text between the\n // braces, and the compiler reads the duration from it (`parseDuration` in\n // @gram-lang/i18n), never from the quantity. Only an unclosed brace is\n // still a syntax error.\n Timer =\n | \"~\" \"_\"? name? ingredientQuantity -- full\n | \"~\" \"_\"? name? timerText -- text\n | \"~\" \"_\"? name? malformedBraces -- malformed\n timerText = \"{\" (~(\"}\" | nl) any)* \"}\"\n \n // Temperature\n // Lexical, so only a note glued to `}` is one: `^{180°C}(fan)` has a note,\n // `^{180°C} (preheated)` keeps its parenthesis in the prose. `name` may\n // start with a space, as it could when the rule was syntactic: `^ {180°C}`\n // and `^oven {180°C}` read as before (`clean` drops the space). The note\n // since 1.5.0.\n temperature = \"^\" name? (ingredientQuantity | textQuantity) preparation?\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 // \".\" or \",\" between two digits is a decimal separator (1.5, 1,5): Gram has\n // no thousands separator. A comma not between two digits ({1, 5 kg}) still\n // fails, so the commas of the prose around an element are never touched.\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 ingredientAmount -- 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/src/index.ts b/packages/parser/src/index.ts index 1b53c847..aadb3cfa 100644 --- a/packages/parser/src/index.ts +++ b/packages/parser/src/index.ts @@ -595,9 +595,6 @@ semantics.addOperation("toAST", { preparation(_lp, text, _rp): string { return clean(text.sourceString); }, - gluedNote(_lp, text, _rp): string { - return clean(text.sourceString); - }, unit(name): string { return clean(name.sourceString); }, -- 2.54.0