fix(parser): parse a document that ends with blank lines only #212

Merged
abiwab merged 2 commits from fix/parser-blank-source into main 2026-11-21 21:23:55 +01:00
12 changed files with 140 additions and 9 deletions

View file

@ -0,0 +1,5 @@
---
"@gram-lang/parser": patch
---
**Parser**: A file holding only blank lines, or only a frontmatter or `@use` lines followed by a blank line, now parses as an empty recipe instead of failing with a syntax error. Since formatting an empty file gives it a final newline, a new `.gram` file no longer shows an error in the editor after its first save.

View file

@ -0,0 +1,49 @@
{
"title": "Blank",
"slug": "blank",
"meta": {
"title": "Blank"
},
"registry": {
"ingredients": {},
"cookware": {}
},
"shopping_list": [],
"cookware": [],
"sections": [],
"warnings": [],
"metrics": {
"totalTime": 0,
"idleTime": 0,
"activeTime": 0,
"preparationTime": 0,
"totalMass": 0,
"massStatus": "incomplete",
"missingMassIngredients": [],
"nutrition": {
"total": {
"calories": 0,
"fat": 0,
"carbs": 0,
"protein": 0
},
"isEstimate": false,
"coverage": 0
}
},
"miseEnPlace": [],
"tasks": {
"tasks": [],
"sections": []
},
"schedule": {
"miseEnPlace": "perSection",
"rests": "shortest",
"totalTime": 0,
"activeTime": 0,
"preparationTime": 0,
"idleTime": 0,
"blocks": [],
"days": []
}
}

View file

@ -0,0 +1,8 @@
{
"type": "Recipe",
"meta": {
"title": "Blank"
},
"imports": [],
"children": []
}

View file

@ -0,0 +1,36 @@
{
"title": "Blank",
"slug": "blank",
"meta": {
"title": "Blank"
},
"registry": {
"ingredients": {},
"cookware": {}
},
"shopping_list": [],
"cookware": [],
"sections": [],
"warnings": [],
"metrics": {
"totalTime": 0,
"idleTime": 0,
"activeTime": 0,
"preparationTime": 0
},
"miseEnPlace": [],
"tasks": {
"tasks": [],
"sections": []
},
"schedule": {
"miseEnPlace": "perSection",
"rests": "shortest",
"totalTime": 0,
"activeTime": 0,
"preparationTime": 0,
"idleTime": 0,
"blocks": [],
"days": []
}
}

View file

@ -0,0 +1,5 @@
---
title: Blank
---

View file

@ -59,12 +59,6 @@
"issue": "https://git.gram-lang.org/gram-lang/gram/issues/200",
"example": "@\r"
},
{
"signature": "format-preserves-meaning | formatted source no longer parses | parser:getAST",
"invariant": "format-preserves-meaning",
"issue": "https://git.gram-lang.org/gram-lang/gram/issues/199",
"example": ""
},
{
"signature": "format-preserves-meaning | compilation differs after formatting at $.meta",
"invariant": "format-preserves-meaning",

View file

@ -0,0 +1,4 @@
---
title: x
---

View file

View file

@ -1,5 +1,8 @@
GRAM {
Recipe = Frontmatter? UseBlock? Content?
// Blank lines may end the document whatever comes before them, nothing
// included: "\n" (an empty file once formatted), the blank line after a
// frontmatter or after the last `@use`.
Recipe = Frontmatter? UseBlock? Content? nl*
Content =
| (nl* Block)* (nl* Section)+ -- explicit

File diff suppressed because one or more lines are too long

View file

@ -260,7 +260,7 @@ const semantics = grammar.createSemantics();
semantics.addOperation("toAST", {
// --- Structure ---
Recipe(frontmatter, useBlock, content): RecipeAST {
Recipe(frontmatter, useBlock, content, _trailingBlankLines): RecipeAST {
const rawMeta = getOpt(frontmatter) || {};
const metaResult = MetaSchema.safeParse(rawMeta);
let meta: Meta;

View file

@ -858,3 +858,30 @@ describe("sums of quantities (since 1.5.0)", () => {
expect(() => getAST(`## T\n\n${src}\n`)).toThrow(fragment);
});
});
// #199: `formatGram("")` returns "\n", so an empty file must parse once
// formatted; and nothing but blank lines may follow a frontmatter or the
// last `@use`.
describe("blank lines with nothing after them", () => {
for (const source of ["\n", "\n\n", " \n", "\r\n"]) {
it(`parses ${JSON.stringify(source)} as an empty recipe`, () => {
expect(getAST(source)).toMatchObject({ children: [], imports: [] });
});
}
it("parses a frontmatter followed by a blank line", () => {
const ast = getAST("---\ntitle: Bread\n---\n\n");
expect(ast.meta).toEqual({ title: "Bread" });
expect(ast.children).toEqual([]);
});
it("parses @use followed by a blank line", () => {
const ast = getAST('@use "./dough.gram" as &dough\n\n');
expect(ast.imports).toHaveLength(1);
expect(ast.children).toEqual([]);
});
it("keeps the steps before trailing blank lines", () => {
expect(getAST("Mix.\n\nBake.\n\n\n").children).toHaveLength(2);
});
});