fix(parser): read a colon followed by a blank as punctuation, not an alias #303

Merged
abiwab merged 1 commit from fix/parser-alias-no-leading-blank into dev/1.6 2026-11-27 22:00:53 +01:00
9 changed files with 74 additions and 8 deletions

View file

@ -0,0 +1,5 @@
---
"@gram-lang/parser": minor
---
**Parser**: A component alias now starts right after the colon, with no blank (`@dry white wine:wine{10ml}`, `#cast iron skillet:skillet{}`); a colon followed by a space or a tab is ordinary punctuation. `Heat the #pan: then add oil.` read `then add oil.` as the alias of `pan`, and that text vanished from the step without a word; it now stays in the step. A recipe that wrote an alias after a blank (`@salt: fleur de sel{1g}`) should glue it to the colon (`@salt:fleur de sel{1g}`).

View file

@ -0,0 +1,3 @@
Heat the #pan: then add oil.
Add @flour: sifted.

View file

@ -4,6 +4,7 @@ description: "How to declare cookware with the # symbol, including quantity coun
---
import { Badge, Tabs, TabItem, LinkCard, CardGrid } from '@astrojs/starlight/components';
import Since from '../../../../../components/Since.astro';
You can define the tools and equipment needed for a recipe using the `#` symbol.
@ -84,6 +85,8 @@ You can rename an equipment item for display while keeping a clean canonical slu
```gram
[Use] The #cast iron skillet:skillet{}.
```
The display name is glued to the colon: in `Heat the #pan: then add oil.`, the colon followed by a space is punctuation and the rest of the line stays text <Since v="1.6.0" />.
</TabItem>
<TabItem label="Alternatives">
You can declare acceptable equipment substitutions using the pipe `|` operator:

View file

@ -145,6 +145,8 @@ You can rename an `@ingredient` for display purposes using a colon `:` immediate
**Format**: `@Real Name:Display Name{Quantity}`
The display name starts right after the colon, with no space. A colon followed by a space is ordinary punctuation: `Heat the @butter: then add the onions.` keeps `: then add the onions.` as text <Since v="1.6.0" />.
<CodeTabs>
```gram title="Code"
[Deglaze] With @dry white wine:wine{100ml}.

View file

@ -4,6 +4,7 @@ description: "Comment déclarer le matériel de cuisine avec le symbole #, entre
---
import { Badge, Tabs, TabItem, LinkCard, CardGrid } from '@astrojs/starlight/components';
import Since from '../../../../../../components/Since.astro';
Le matériel, les ustensiles et l'équipement requis par une recette se déclarent à l'aide du symbole `#`.
@ -84,6 +85,8 @@ Vous pouvez renommer un équipement pour l'affichage tout en conservant un ident
```gram
[Utiliser] La #poêle en fonte:poêle{}.
```
Le nom d'affichage se colle aux deux-points : dans `Chauffez la #poêle: puis versez l'huile.`, les deux-points suivis d'une espace sont une ponctuation et la fin de la ligne reste du texte <Since v="1.6.0" />.
</TabItem>
<TabItem label="Alternatives">
Vous pouvez définir des alternatives d'ustensiles interchangeables avec l'opérateur `|` :

View file

@ -145,6 +145,8 @@ Vous pouvez surcharger le nom d'affichage d'un `@ingrédient` en utilisant les d
**Format** : `@Vrai Nom:Nom d'Affichage{Quantité}`
Le nom d'affichage se colle aux deux-points, sans espace. Suivis d'une espace, les deux-points redeviennent une simple ponctuation : dans `Faites fondre le @beurre: puis ajoutez les oignons.`, la suite `: puis ajoutez les oignons.` reste du texte <Since v="1.6.0" />.
<CodeTabs>
```gram title="Code"
[Déglacer] Avec le @vin blanc sec:vin{100 ml}.

View file

@ -159,11 +159,12 @@ GRAM {
| "@" modifier* singleWordName alias? preparation? composite? -- bare
modifier = "?" | "-" | "*" | "&" | "="
// Blanks alone up to the end of the line are no alias: `@salt: ` took them
// as an empty one and the `:` vanished from the step, while `@salt:` kept
// it as text, so stripping a trailing blank (the formatter) changed the
// recipe. The `:` is text in both now.
alias = ":" ~(sp* (nl | end)) refName
// The alias starts right after the `:`, with no blank (`@dry white
// wine:wine{10ml}`). A `:` followed by a blank is punctuation: `Heat the
// #pan: then add oil.` read the rest of the line as the alias of `pan` and
// it vanished from the step (#273). It also covers `@salt: ` at the end of
// a line, which took the blanks as an empty alias (#281).
alias = ":" ~sp refName
ingredientQuantity = "{" space* (relativeQuantity | absoluteQuantity) space* "}"
// The amount of an ingredient, a composite parent or a reference: an
// ingredientQuantity, or a sum of quantities (`{1 cup + 1 tbsp}`,

File diff suppressed because one or more lines are too long

View file

@ -976,14 +976,61 @@ describe("a colon followed by blanks alone is no alias", () => {
]);
});
it("still reads a real alias, with or without a blank after the colon", () => {
it("still reads a real alias, glued to the colon", () => {
expect(step("With @dry white wine:wine{100ml}.")[1]).toEqual({
name: "dry white wine",
alias: "wine",
});
expect(step("Add @salt: fleur de sel.")[1]).toEqual({
expect(step("Use the #cast iron skillet:skillet{}.")[1]).toEqual({
name: "cast iron skillet",
alias: "skillet",
});
expect(step("Add @salt:fleur de sel.")[1]).toEqual({
name: "salt",
alias: "fleur de sel.",
});
});
});
// #273: `alias = ":" refName` also took a name starting with a blank, so a `:`
// written as punctuation after a bare mention read the rest of the line as
// its alias, and that text vanished from the step. The alias now starts right
// after the colon, or the colon is text.
describe("a colon followed by a blank is punctuation, not an alias", () => {
const step = (source: string) =>
(getAST(source).children[0] as any).children.map((c: any) =>
c.type === ASTNodeType.Ingredient || c.type === ASTNodeType.Cookware
? { name: c.name, alias: c.alias }
: c.value,
);
it("keeps the rest of the line after a bare #cookware", () => {
expect(step("Heat the #pan: then add oil.")).toEqual([
"Heat the ",
{ name: "pan", alias: null },
": then add oil.",
]);
});
it("keeps the rest of the line after a bare @ingredient", () => {
expect(step("Add @flour: sifted.")).toEqual([
"Add ",
{ name: "flour", alias: null },
": sifted.",
]);
});
it("keeps it after a tab too", () => {
expect(step("Heat the #pan:\tthen add oil.")).toEqual([
"Heat the ",
{ name: "pan", alias: null },
":\tthen add oil.",
]);
});
it("leaves a colon after braces as text, as before", () => {
expect(step("Mix @flour{200g}: then rest.").slice(2)).toEqual([
": then rest.",
]);
});
});