Coder Social home page Coder Social logo

push() captures broken? about npeg HOT 2 CLOSED

zevv avatar zevv commented on July 24, 2024 1
push() captures broken?

from npeg.

Comments (2)

zevv avatar zevv commented on July 24, 2024

I just took the time to look into this, and I believe that push() is just fine. Your above grammar needs some reworking though; for example the infix rule has two captures, but the push() there only consumes the first using $1.

Let me know if you still need help building your parser, if not I'll close this ticket in a few days.

from npeg.

EyeCon avatar EyeCon commented on July 24, 2024

I don't understand, there's an "or" (|) in the line you are talking about, so I thought that is just one capture performed in one of the two alternative ways?

In any case, it does work exactly like that, just without push, like:

import std/[strformat, strutils]
import npeg

let tmp = "Z1Z + A1A / B1B + -C1C"
var cap: seq[string]

let parser = peg "exp":
  code <- >(?'-' * Alpha * Alnum[2..4]):
    cap.add("Cod " & $1)
  parenExp <- ( "(" * exp * ")" ) ^ 0
  prefix <- code | parenExp
  infix <- (>{'/'} * exp ^ 1) | (>{'+'} * exp ^ 2):
    cap.add("Opr " & $1)
  exp <- prefix * *infix
discard parser.match(tmp.replace(" ", ""))
echo cap

# As expected:
# @["Cod Z1Z", "Cod A1A", "Opr +", "Cod B1B", "Cod -C1C", "Opr +", "Opr /"]

from npeg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.