gram/packages/parser/grammar.ohm

132 lines
3.6 KiB
Text

GRAM {
Recipe = Frontmatter? Content?
Content =
| (nl* Section)+ -- explicit
| (nl* Block)+ -- implicit
Frontmatter = "---" nl KeyValue* "---" (nl | end)
KeyValue = key ":" sp* value nl
key = (alnum | "_" | "-")+
value = (~nl any)*
Section = header Block*
header = "##" sp+ title headerExtension? sp* nl+
headerExtension =
| retroPlanning sp* intermediateDecl? -- retro
| intermediateDecl -- decl
titleStop = nl | "->&" | "{T-"
title = (~titleStop any)*
retroPlanning = "{T-" (~"}" any)+ "}"
Block =
| Comment nl* -- comment
| (~header step) -- step
step = startAction? line (nl line)* (nl+ | end)
startAction = "[" (~"]" any)+ "]" sp*
line = stepContent+
stepContent =
| text
| ws
| intermediateDecl
| applySyntactic<Element>
| fallback
ws = space+
fallback = ~nl any
Element =
| Ingredient
| Cookware
| Timer
| Temperature
| Reference
| CommentBlock
| Comment
intermediateDecl = "->&" name "{}"
// Ingredients
Ingredient =
| Alternative
| simpleIngredient
Alternative = simpleIngredient ("|" simpleIngredient)+
// Rule: Quantity is mandatory (it's the delimiter)
simpleIngredient = "@" modifier* name alias? ingredientQuantity preparation? composite?
modifier = "?" | "-" | "*" | "&"
alias = "[" space* name space* "]"
ingredientQuantity = "{" space* (relativeQuantity | absoluteQuantity) space* "}"
relativeQuantity = number space* "%" space* ("@" | "&") space* name
absoluteQuantity = (fixed space*)? (range | number)? space* (unit space*)?
textQuantity = "{" space* (~"}" any)+ space* "}"
fixed = "="
unit = unitName
unitName = (alnum | sp | "°" | "%")+
preparation = "(" (~")" any)* ")"
composite = "<" space* "@" space* name space* ingredientQuantity?
// Cookware
// Specs say use braces too.
Cookware =
| CookwareAlternative
| simpleCookware
CookwareAlternative = simpleCookware ("|" simpleCookware)+
simpleCookware = "#" modifier* name alias? cookwareQuantity preparation?
// Restrict cookware quantity to only numbers/fixed, NO UNITS.
// Allowed: {}, {2}, {=1}
cookwareQuantity = "{" space* (fixed space*)? number? space* "}"
// Timer
Timer = "~" name? ingredientQuantity
// Temperature
Temperature = "!" name? (ingredientQuantity | textQuantity)
// Comments
Comment = "//" (~nl any)*
CommentBlock = "/*" (~"*/" any)* "*/"
// Primitives
name = (~(syntaxChar | nl) any)+
number = digit+ ("." digit+)? ("/" digit+)?
range = number space* "-" space* number
text = space* (~(elementStart | nl) any)+
Reference = "&" name ingredientQuantity
// SyntaxChar used for name content exclusion
// Restricted to true delimiters that start a new component
syntaxChar = "{" | "}" | "[" | "]" | "(" | ")" | "<" | "|"
// Name can contain '&' or '@' or other symbols, as long as it doesn't look like a start of a new element if specific context requires it?
// Actually, standard grammar strategy: consume until a known delimiter.
// The delimiters are:
// - `{` (start of quantity)
// - `[` (start of alias)
// - `(` (start of preparation)
// - `<` (start of composite)
// - `|` (start of alternative)
// - newline
// We removed @, #, ~, !, &, ?, * from syntaxChar list effectively allowing them in names.
// ElementStart used for text content exclusion
// This remains strict to identify WHERE an element starts in free text.
elementStart = "@" | "#" | "~" | "!" | "//" | "/*" | "->&" | "&"
nl = "\r"? "\n"
sp = " " | "\t"
space := sp
}