Closed mantielero closed 8 months ago
Right now solved with closures:
proc parserBlocksGen():auto =
var db:Block
new(db)
return peg("delimitedBlocks", blk: Block):
# Title
title <- '.' * >adoc.txt * adoc.crlf:
db.title = $1
attributes <- '[' * >*(1 - '[' - ']' - '\r' - '\n') * ']' * adoc.crlf:
db.attributes = $1
delimitedBlocks <- ?title * ?attributes * >R("blockDelimiter", adoc.blockDelimiters * adoc.crlf ) * >*(!R("blockDelimiter") * *(1 - '\r' - '\n') * adoc.crlf) * R("blockDelimiter"):
db.content = $2
db.delimiter = $1
blk.blocks &= db
let parserBlocks* = parserBlocksGen()
I will close this issue. If there is a better pattern, let me know:
I am having two difficutties and probably I am not using probably npeg here.
First issue: capturing optional elements
Given above's situation,
$1
could betittle
,attributes
orbody
. How can I check what it is? If in the code I doecho capture
, the fieldname
is empty.Second issue: creating objects
I want to capture above's data in a
ref object
, where should I create it?I'd like to create the object in one place in order to do something like: