fix(parser): parse a document that ends with blank lines only #212
12 changed files with 140 additions and 9 deletions
5
.changeset/parser-trailing-blank-lines.md
Normal file
5
.changeset/parser-trailing-blank-lines.md
Normal 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.
|
||||||
49
conformance/cases/055-blank-lines-only/analyzed.json
Normal file
49
conformance/cases/055-blank-lines-only/analyzed.json
Normal 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": []
|
||||||
|
}
|
||||||
|
}
|
||||||
8
conformance/cases/055-blank-lines-only/ast.json
Normal file
8
conformance/cases/055-blank-lines-only/ast.json
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"type": "Recipe",
|
||||||
|
"meta": {
|
||||||
|
"title": "Blank"
|
||||||
|
},
|
||||||
|
"imports": [],
|
||||||
|
"children": []
|
||||||
|
}
|
||||||
36
conformance/cases/055-blank-lines-only/compiled.json
Normal file
36
conformance/cases/055-blank-lines-only/compiled.json
Normal 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": []
|
||||||
|
}
|
||||||
|
}
|
||||||
5
conformance/cases/055-blank-lines-only/input.gram
Normal file
5
conformance/cases/055-blank-lines-only/input.gram
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
title: Blank
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -59,12 +59,6 @@
|
||||||
"issue": "https://git.gram-lang.org/gram-lang/gram/issues/200",
|
"issue": "https://git.gram-lang.org/gram-lang/gram/issues/200",
|
||||||
"example": "@\r"
|
"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",
|
"signature": "format-preserves-meaning | compilation differs after formatting at $.meta",
|
||||||
"invariant": "format-preserves-meaning",
|
"invariant": "format-preserves-meaning",
|
||||||
|
|
|
||||||
4
fuzz/regressions/199-blank-after-frontmatter.gram
Normal file
4
fuzz/regressions/199-blank-after-frontmatter.gram
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
---
|
||||||
|
title: x
|
||||||
|
---
|
||||||
|
|
||||||
0
fuzz/regressions/199-empty-file.gram
Normal file
0
fuzz/regressions/199-empty-file.gram
Normal file
|
|
@ -1,5 +1,8 @@
|
||||||
GRAM {
|
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 =
|
Content =
|
||||||
| (nl* Block)* (nl* Section)+ -- explicit
|
| (nl* Block)* (nl* Section)+ -- explicit
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -260,7 +260,7 @@ const semantics = grammar.createSemantics();
|
||||||
semantics.addOperation("toAST", {
|
semantics.addOperation("toAST", {
|
||||||
// --- Structure ---
|
// --- Structure ---
|
||||||
|
|
||||||
Recipe(frontmatter, useBlock, content): RecipeAST {
|
Recipe(frontmatter, useBlock, content, _trailingBlankLines): RecipeAST {
|
||||||
const rawMeta = getOpt(frontmatter) || {};
|
const rawMeta = getOpt(frontmatter) || {};
|
||||||
const metaResult = MetaSchema.safeParse(rawMeta);
|
const metaResult = MetaSchema.safeParse(rawMeta);
|
||||||
let meta: Meta;
|
let meta: Meta;
|
||||||
|
|
|
||||||
|
|
@ -858,3 +858,30 @@ describe("sums of quantities (since 1.5.0)", () => {
|
||||||
expect(() => getAST(`## T\n\n${src}\n`)).toThrow(fragment);
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue