Code Monkey home page Code Monkey logo

Comments (8)

zevv avatar zevv commented on May 22, 2024

Hard to tell without seeing your grammar, any chance you could post a minimal self contained snippet of nim code + grammar that shows this behavior?

from npeg.

thatrandomperson5 avatar thatrandomperson5 commented on May 22, 2024

I have no idea where the issue lies so it is hard to make a minimal self contained snippet. I will try tho

from npeg.

thatrandomperson5 avatar thatrandomperson5 commented on May 22, 2024

Is this good enough?

  let testParser = peg("tinput", o: seq[OC]):
    name <- >(+Alpha) * ":":
      o.add ("name", $1, indent())
    value <- >(+Alnum) * !":":
      o.add ("value", $1, indent())
    tcontent <-  (ib.Line(name) * ib.Block(tblock)) | ib.Line(value)
    tblock <- +tcontent
    tinput <- tblock * !1

It’s minimal but not self contained

from npeg.

zevv avatar zevv commented on May 22, 2024

I'd like to help you debug this, but [lease try to come up with something I can run to inspect what is happening; I can see from the trace that NPeg is backtracking so your input does not match the grammar, but I can not tell what is wrong with your grammar without having your grammar and the subject string you are trying to parse.

from npeg.

thatrandomperson5 avatar thatrandomperson5 commented on May 22, 2024

This is self contained but not minimal:

  var indentStack = newSeq[int]()

  template top[T](s: seq[T]): T = 
    if s.high == -1:
      0
    else:
      s[s.high]
  
  grammar "ib":
    EOL <- "\r\n" | "\n" | "\r" | !1
    whitespace <- *Blank

    Indent <- >whitespace: 
        validate len($0) > indentStack.top
        indentStack.add len($0)
    Dedent <- >whitespace:
        let delStack = len($0)
        validate delStack < indentStack.top
        while delStack < indentStack.top:
            discard indentStack.pop
    Static <- >whitespace:
        validate len($0) == indentStack.top
    Line(content) <- ib.Static * content * ib.EOL
    Block(content) <- &ib.Indent * content * &ib.Dedent
    
  
  type OC = tuple[kind: string, s: string, indent: int]
  let testParser = peg("tinput", o: seq[OC]):
    name <- >(+Alpha) * ":":
      o.add ("name", $1, indentStack.len)
    value <- >(+Alnum) * !":":
      o.add ("value", $1, indentStack.len)
    tcontent <-  (ib.Line(name) * ib.Block(tblock)) | ib.Line(value)
    tblock <- +tcontent
    tinput <- tblock * !1

  var o = newSeq[OC]()
  const testStr = """Foo:
    FooBar:
        Bar
        Bar2
    Bar3
Fooo:
    Barr
    Foooo:
        Bar4
Bar5"""
  echo testStr
  let r = testParser.match(testStr, o)
  echo o
  doAssert r.ok

from npeg.

thatrandomperson5 avatar thatrandomperson5 commented on May 22, 2024

@zevv can you run the above successfully?

from npeg.

zevv avatar zevv commented on May 22, 2024

The parser parses ok, but your Dedent rule fails because of validate delStack < indentStack.top, causing the whole shebang to backtrack to the start.

from npeg.

thatrandomperson5 avatar thatrandomperson5 commented on May 22, 2024

You were right, just needed to change to this: Block(content) <- &ib.Indent * content * (&ib.Dedent | !1). Thanks!

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.