gram/packages/parser/grammar.ohm

200 lines
7.8 KiB
Text

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+
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* 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 = "="
unit = unitName
unitName = (alnum | sp | "°" | "%")+
preparation = "(" (~")" 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* 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* singleWordName alias? preparation? -- bare
// Cookware quantity is numbers only, no units (e.g. {}, {2}) -- unlike ingredientQuantity.
cookwareQuantity = "{" space* number? space* "}"
// Timer
Timer = "~" "_"? name? ingredientQuantity
// 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
| "&" 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
}