Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help - how to handle captures and optionals #71

Closed
mantielero opened this issue Jan 6, 2024 · 1 comment
Closed

Help - how to handle captures and optionals #71

mantielero opened this issue Jan 6, 2024 · 1 comment

Comments

@mantielero
Copy link

I am having two difficutties and probably I am not using probably npeg here.

First issue: capturing optional elements

title <- >( whatever)
attributes <- >(whatever2)
delimitedBlocks <- ?title * >?attributes * >body:
   mycode

Given above's situation, $1 could be tittle, attributes or body. How can I check what it is? If in the code I do echo capture, the field name is empty.

Second issue: creating objects

I want to capture above's data in a ref object, where should I create it?

var blk:Block
new(blk)

I'd like to create the object in one place in order to do something like:

let parserBlocks* = peg("delimitedBlocks", myBlk):
  title <- >( whatever):
    blk.title = $1
  attributes <- >(whatever2):
    blk.attributes = $2
  delimitedBlocks <- ?title * >?attributes * >body:
    blk.body 
    myBlk.blocks &= blk
@mantielero
Copy link
Author

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant