Comments (8)
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.
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.
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.
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.
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.
@zevv can you run the above successfully?
from npeg.
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.
You were right, just needed to change to this: Block(content) <- &ib.Indent * content * (&ib.Dedent | !1)
. Thanks!
from npeg.
Related Issues (20)
- lazy matching HOT 1
- Large grammars break NPeg HOT 9
- Attach line information to generate functions HOT 3
- Print return stack on overflow HOT 4
- `-d:npegTrace` code allocates a number of closures equal to the number of states HOT 1
- Implement slicing operators for captures HOT 10
- PEG section in the Nim book HOT 2
- Just wanted to say npeg is great HOT 1
- More/larger examples of Error reporting HOT 5
- Memorization & Bryan Ford HOT 3
- Prevent code blocks captures from executing HOT 7
- A paper citing Npeg HOT 1
- Implement handling of left recursion HOT 5
- [Question] Parsing comments with two characters limits HOT 2
- Performance and Complexity HOT 1
- push() captures broken? HOT 2
- When defining grammar block parameterized rules block name needed? HOT 1
- FR: Deferred code block captures HOT 1
- Enhancing parser flexibility: improving type parsing with the push template HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from npeg.