gram/packages/parser/grammar.ohm
abiwab 6620c244d2
All checks were successful
CI / Build & Test (pull_request) Successful in 3m37s
fix(parser): end a preparation at the end of its line
2026-11-16 23:46:13 +01:00

229 lines
9.5 KiB
Text
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

GRAM {
Recipe = Frontmatter? UseBlock? Content?
Content =
| (nl* Block)* (nl* Section)+ -- explicit
| (nl* Block)+ -- implicit
Frontmatter = "---" nl KeyValue* "---" (nl | end)
emptyLine = sp* nl
KeyValue = emptyLine* key ":" sp* value nl
key = (alnum | "_" | "-")+
value = (~nl any)*
// Import directives, constrained between the frontmatter and the content --
// this is what makes "@use" unambiguous with an ingredient literally named
// "use" that happens to appear in a step (unchanged, still parses as such).
// The block must open with a real `UseDirective`: a leading `UseComment` on
// its own (no `@use` anywhere in the block) is deliberately NOT swallowed
// here, so a document with no imports at all keeps treating its opening
// comment as ordinary content, exactly as before this feature existed. Once
// the block has started, a comment between two directives doesn't break it.
UseBlock = nl* useDirective (nl* (useDirective | useComment))*
useComment = applySyntactic<Comment> nl
useDirective = "@use" sp+ moduleSpecifier sp+ "as" sp+ bindings sp* retroPlanning? sp* (nl | end)
moduleSpecifier = "\"" (~("\"" | nl) any)+ "\""
bindings =
| "{" sp* binding moreBindings* sp* "}" -- named
| binding -- default
moreBindings = sp* "," sp* binding
binding = "&" bindingName (sp+ "as" sp+ "&" bindingName)?
bindingName =
| bindingFullName "{}" -- full
| bindingBareName -- bare
// Unlike `name`, this must stop at "," and "&" -- a `{}`-delimited multi-word
// binding sits inside a comma-separated list (`{ &a, &b multi{} }`) and can be
// followed by a rename (`&a as &b multi{}`), so it must not swallow past the
// next binding's separator or sigil while scanning ahead for its own "{}".
bindingFullName = (~bindingFullNameStop any)+
bindingFullNameStop = syntaxChar | nl | "," | refSigil
bindingBareName = (~bindingStop any)+
bindingStop = space | nl | syntaxChar | sentencePunct | "~{"
Section = header Block*
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 =
| retroPlanning sp* intermediateDecl? -- retro
| intermediateDecl sp* retroPlanning? -- decl
titleStop = nl | "->&" | "~{"
title = (~titleStop any)*
retroPlanning = "~{" (~"}" any)+ "}"
Block =
| Comment nl* -- comment
| (~header step) -- step
step = startAction? line (nl line)* (nl+ | end)
startAction = "[" (~"]" any)+ "]" sp*
line = stepContent+
stepContent =
| invalidUseDirective
| invalidComposite
| invalidAlternativeBar
| text
| ws
| intermediateDecl
| applySyntactic<Element>
| fallback
// A `@use "..."` past the import block (after the first step, misspelled,
// or otherwise misplaced) would otherwise silently parse as an ingredient
// literally named "use" followed by prose -- this turns it into a readable
// error instead. Only triggers on the exact `@use "` prefix, so an actual
// ingredient named "use" followed by unrelated quoted text is unaffected.
invalidUseDirective = "@use" sp+ "\""
invalidComposite = space+ "<" space* "@" name
invalidAlternativeBar = "|"
ws = space+
fallback = ~nl any
Element =
| Ingredient
| Cookware
| Timer
| Temperature
| Reference
| CommentBlock
| Comment
intermediateDecl =
| "->&" name "{}" -- full
| "->&" singleWordName -- bare
// Ingredients
Ingredient =
| Alternative
| simpleIngredient
Alternative = simpleIngredient ("|" simpleIngredient)+
// Rule: Quantity is mandatory unless it's a single word name
// Uses refName (not name) so a bare @&chicken mention can't have its name
// greedily swallow everything up to an unrelated later {...} that happens
// to close a valid ingredientQuantity -- e.g. "@&chicken for ~{3min}" must
// stop at "~" and fall back to the bare alternative, not consume "chicken
// for ~" as a bogus multi-word name with the timer's quantity attached.
simpleIngredient =
| "@" modifier* refName alias? ingredientQuantity preparation? composite? -- full
| "@" modifier* refName alias? malformedBraces -- malformed
| "@" modifier* singleWordName alias? preparation? composite? -- bare
modifier = "?" | "-" | "*" | "&" | "="
alias = ":" refName
ingredientQuantity = "{" space* (relativeQuantity | absoluteQuantity) space* "}"
relativeQuantity = number space* "%" space* ("@&" | "&") space* name
absoluteQuantity = (range | number)? space* (unit space*)?
textQuantity = "{" space* (~"}" any)+ space* "}"
fixed = "="
// A unit is one or more words. A word may contain "." or an apostrophe after
// its first character (c.à.s, fl. oz., d'eau) but never starts with one, so a
// stray "{1. kg}" or "{.5 g}" is still rejected rather than half-parsed.
unit = unitName
unitName = unitWord (sp+ unitWord)*
unitWord = unitLead unitChar*
unitLead = alnum | "°" | "%"
unitChar = unitLead | "." | "'" | "’"
// A "{...}" glued to an element that did not parse as a valid quantity. Without
// this, the element silently fell back to its bare form (name truncated to its
// 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.
preparation = "(" (~(")" | nl) any)* ")"
// `modifier*` mirrors simpleIngredient: a composite parent is an ingredient
// reference like any other, and authors (and AI importers) write `<@&parent`
// to mark a parent already introduced earlier. Without it, `refName` rejected
// the leading "&" (it is a refSigil), the `bare` branch fell through to
// `singleWordName` -- which does allow "&" -- and a multi-word parent silently
// truncated at the first space, registering a phantom ingredient literally
// named "&citron" alongside the real one. The modifiers are parsed and then
// ignored (see composite_full/composite_bare): "&" is redundant on a parent
// because the MAX rule already collapses repeat draws into one purchase.
composite =
| "<@" modifier* refName ingredientQuantity preparation? -- full
| "<@" modifier* refName malformedBraces -- malformed
| "<@" modifier* singleWordName preparation? -- bare
// Cookware
Cookware =
| CookwareAlternative
| simpleCookware
CookwareAlternative = simpleCookware ("|" simpleCookware)+
// Same refName fix as simpleIngredient above -- a bare #pan mention must not
// swallow an unrelated later {...} (e.g. another ingredient's integer-only
// quantity) into its own name.
simpleCookware =
| "#" modifier* refName alias? cookwareQuantity preparation? -- full
| "#" modifier* refName alias? malformedBraces -- malformed
| "#" modifier* singleWordName alias? preparation? -- bare
// Cookware quantity is numbers only, no units (e.g. {}, {2}) -- unlike ingredientQuantity.
cookwareQuantity = "{" space* number? space* "}"
// Timer
Timer =
| "~" "_"? name? ingredientQuantity -- full
| "~" "_"? name? malformedBraces -- malformed
// Temperature
Temperature = "^" name? (ingredientQuantity | textQuantity)
// Comments
Comment = "//" (~nl any)*
CommentBlock = "/*" (~"*/" any)* "*/"
// Primitives
name = (~(syntaxChar | nl) any)+
singleWordName = (~(syntaxChar | space | nl | sentencePunct) any)+
sentencePunct = "," | ";" | "!" | "?" | "."
// Like name, but also stops at element sigils so a bare &dough is not consumed
// into a Reference -- full when a sigil like # or @ appears later on the line.
refName = (~(syntaxChar | refSigil | nl) any)+
refSigil = "@" | "#" | "~" | "^" | "&"
number = mixedFraction | unicodeFraction | plainNumber
plainNumber = digit+ ("." digit+)? ("/" digit+)?
mixedFraction = digit+ sp+ digit+ "/" digit+
unicodeFraction =
| digit+ unicodeFractionChar -- mixed
| unicodeFractionChar -- bare
unicodeFractionChar = "¼" | "½" | "¾" | "⅓" | "⅔" | "⅕" | "⅖" | "⅗" | "⅘" | "⅙" | "⅚" | "⅛" | "⅜" | "⅝" | "⅞"
range = number space* "-" space* number
text = space* (~(elementStart | nl | "|") any)+
Reference =
| "&" refName ingredientQuantity -- full
| "&" refName malformedBraces -- malformed
| "&" singleWordName -- bare
// Delimiters that end a name (start of quantity/alias/preparation/composite/alternative).
// @, #, ~, !, &, ?, * are deliberately absent here, so names may contain them freely --
// only elementStart (below) treats those as starting a new element, and only in free text.
syntaxChar = "{" | "}" | "[" | "]" | "(" | ")" | "<" | "|" | ":"
// Stricter than syntaxChar: used to detect where an element starts within free text.
elementStart = "@" | "#" | "~" | "^" | "//" | "/*" | "->&" | "&"
nl = "\r"? "\n"
sp = " " | "\t"
space := sp
}