fix(parser): report leftover text in section headers instead of dropping the section #135
7 changed files with 117 additions and 3 deletions
8
.changeset/section-header-leftover-text.md
Normal file
8
.changeset/section-header-leftover-text.md
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
---
|
||||||
|
"@gram-lang/parser": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
**Parser**: A section header with text left after its anchor or intermediate now raises a clear error, instead of losing the section without a word or blaming the anchor:
|
||||||
|
- `## Garnish ~{-10h} ->&glazed rhubarb` now asks for the braces a multi-word intermediate needs (`->&glazed rhubarb{}`), instead of reporting "Invalid quantity '{-10h}' for timer".
|
||||||
|
- Without an anchor, the same header used to turn silently into a step, so the section vanished from the recipe.
|
||||||
|
- A section header written on the very last line of a file, with no final line break, is now read as a header too.
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"message": "Gram Syntax Error: Intermediate name 'rhubarbe laquée' in a section header has several words and must end with braces, e.g. ->&rhubarbe laquée{}.",
|
||||||
|
"offset": 30,
|
||||||
|
"expected": "a multi-word intermediate name ending with braces, e.g. ->&rhubarbe laquée{}",
|
||||||
|
"code": null,
|
||||||
|
"file": null
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
## Garniture rhubarbe ~{-10h} ->&rhubarbe laquée
|
||||||
|
|
||||||
|
Couper la @rhubarbe{500g} en tronçons.
|
||||||
|
|
||||||
|
## Montage
|
||||||
|
|
||||||
|
Disposer la &rhubarbe laquée{} sur la pâte.
|
||||||
|
|
@ -41,7 +41,15 @@ GRAM {
|
||||||
bindingStop = space | nl | syntaxChar | sentencePunct | "~{"
|
bindingStop = space | nl | syntaxChar | sentencePunct | "~{"
|
||||||
|
|
||||||
Section = header Block*
|
Section = header Block*
|
||||||
header = "##" sp+ title headerExtension? sp* nl+
|
header =
|
||||||
|
| "##" sp+ title headerExtension? sp* (nl+ | end) -- ok
|
||||||
|
| "##" sp+ title headerExtension sp* headerTrailing (nl+ | end) -- trailing
|
||||||
|
// Text left after a header's `~{...}` / `->&name` (most often the second
|
||||||
|
// word of a multi-word intermediate written without `{}`). Without this
|
||||||
|
// branch the whole `##` line silently fell back to a step: the section
|
||||||
|
// vanished, or its `~{-1d}` anchor surfaced as a misleading timer error.
|
||||||
|
// Matched only to raise a readable error (see `header_trailing`).
|
||||||
|
headerTrailing = (~nl any)+
|
||||||
headerExtension =
|
headerExtension =
|
||||||
| retroPlanning sp* intermediateDecl? -- retro
|
| retroPlanning sp* intermediateDecl? -- retro
|
||||||
| intermediateDecl sp* retroPlanning? -- decl
|
| intermediateDecl sp* retroPlanning? -- decl
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -219,7 +219,28 @@ semantics.addOperation("toAST", {
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
header(_1, _2, title, extension, _3, _4): HeaderResult {
|
header_trailing(_1, _2, _title, extension, _sp, trailing, _end): never {
|
||||||
|
const text = trailing.sourceString.trimEnd();
|
||||||
|
// `->&rhubarbe laquée`: the bare form keeps one word, so the next word
|
||||||
|
// lands here. Point at the braces rather than at the leftover text.
|
||||||
|
const bareDecl = extension.sourceString.trimEnd().match(/->&([^\s{}]+)$/);
|
||||||
|
const nextWords = (text.split("~{")[0] ?? "").trim();
|
||||||
|
if (bareDecl && /^[\p{L}\p{N}]/u.test(nextWords)) {
|
||||||
|
const example = `->&${bareDecl[1]} ${nextWords}{}`;
|
||||||
|
throw new GramParseError(
|
||||||
|
`Gram Syntax Error: Intermediate name '${bareDecl[1]} ${nextWords}' in a section header has several words and must end with braces, e.g. ${example}.`,
|
||||||
|
extension.source.startIdx + (bareDecl.index ?? 0),
|
||||||
|
`a multi-word intermediate name ending with braces, e.g. ${example}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw new GramParseError(
|
||||||
|
`Gram Syntax Error: Unexpected text '${text}' in a section header. A header ends with its anchor '~{...}' and its intermediate '->&name', with nothing after them.`,
|
||||||
|
trailing.source.startIdx,
|
||||||
|
"the end of the section header",
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
header_ok(_1, _2, title, extension, _3, _4): HeaderResult {
|
||||||
let retroPlanning = null;
|
let retroPlanning = null;
|
||||||
let intermediateDecl = null;
|
let intermediateDecl = null;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -353,6 +353,69 @@ describe("section retro-planning", () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("text left after a section header's anchor / intermediate", () => {
|
||||||
|
// Before 1.4.1 any text after `~{...}` / `->&name` made the header rule fail:
|
||||||
|
// the `##` line fell back to a step, so the section vanished silently, or its
|
||||||
|
// `~{-10h}` anchor surfaced as a misleading "Invalid quantity for timer".
|
||||||
|
function parseError(source: string): GramParseError {
|
||||||
|
try {
|
||||||
|
getAST(source);
|
||||||
|
} catch (e) {
|
||||||
|
expect(e).toBeInstanceOf(GramParseError);
|
||||||
|
return e as GramParseError;
|
||||||
|
}
|
||||||
|
throw new Error("expected a GramParseError");
|
||||||
|
}
|
||||||
|
|
||||||
|
const MULTI_WORD =
|
||||||
|
/Intermediate name 'rhubarbe laquée' .* ->&rhubarbe laquée\{\}/;
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
["anchor first", "## Garniture ~{-10h} ->&rhubarbe laquée\n\nStep.\n", 21],
|
||||||
|
["anchor last", "## Garniture ->&rhubarbe laquée ~{-10h}\n\nStep.\n", 13],
|
||||||
|
["no anchor", "## Garniture ->&rhubarbe laquée\n\nStep.\n", 13],
|
||||||
|
["end of file", "## Garniture ->&rhubarbe laquée", 13],
|
||||||
|
])("asks for braces on a multi-word intermediate (%s)", (_label, source, offset) => {
|
||||||
|
const err = parseError(source);
|
||||||
|
expect(err.message).toMatch(MULTI_WORD);
|
||||||
|
expect(err.offset).toBe(offset);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
[
|
||||||
|
"text after the anchor",
|
||||||
|
"## Garniture ~{-10h} au frais\n\nStep.\n",
|
||||||
|
"au frais",
|
||||||
|
],
|
||||||
|
["two intermediates", "## Garniture ->&a ->&b\n\nStep.\n", "->&b"],
|
||||||
|
])("rejects any other leftover text (%s)", (_label, source, leftover) => {
|
||||||
|
expect(parseError(source).message).toContain(
|
||||||
|
`Unexpected text '${leftover}' in a section header`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("still accepts braces, trailing spaces and either order", () => {
|
||||||
|
for (const header of [
|
||||||
|
"## Garniture ->&rhubarbe laquée{} ~{-10h}",
|
||||||
|
"## Garniture ~{-10h} ->&rhubarbe laquée{} ",
|
||||||
|
]) {
|
||||||
|
const section = getAST(`${header}\n\nStep.\n`).children[0] as any;
|
||||||
|
expect(section.type).toBe(ASTNodeType.Section);
|
||||||
|
expect(section.retroPlanning.raw).toBe("-10h");
|
||||||
|
expect(section.intermediateDecl.name).toBe("rhubarbe laquée");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps a header written on the last line, without a final newline", () => {
|
||||||
|
const section = getAST("Step.\n\n## Mousse ->&mousse ~{-1d}").children.at(
|
||||||
|
-1,
|
||||||
|
) as any;
|
||||||
|
expect(section.type).toBe(ASTNodeType.Section);
|
||||||
|
expect(section.retroPlanning.raw).toBe("-1d");
|
||||||
|
expect(section.intermediateDecl.name).toBe("mousse");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("known hard error", () => {
|
describe("known hard error", () => {
|
||||||
it("throws a descriptive syntax error for a space before the composite `<` sigil", () => {
|
it("throws a descriptive syntax error for a space before the composite `<` sigil", () => {
|
||||||
expect(() => getAST("## Section\n@ <@parent\n")).toThrow(/composite/i);
|
expect(() => getAST("## Section\n@ <@parent\n")).toThrow(/composite/i);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue