Comments (4)
I poked around to see if I can make a proper implementation of this, but it's not trivial: the symbol table does not from survive compilation to run time, so there is no info available to generate a friendly stack trace at the time of the overflow.
I can make this work, but that will require keeping information around for longer then I'd like. Not sure of that is worth the effort at this time.
Any ideas on this?
from npeg.
Hi @Varriount, I got a reasonable implementation for collecting and dumping a backtraces happening at NPeg parse time; its a available in the backtrace
branch. It doesn't only handle retstack overflows, but any exception that is raised from NPeg, so both NPegException
and any other exceptions as well.
The current implementation runs the parse fucntion in a try
block; if an exception happens it will print the NPeg part of the stack and re-raise the original exception.
Ideally I would like to augment the original Nim stacktrace with the NPeg stackframes instead of just printing the trace, but I'm not sure if the current Nim infrastructure allows for this; NPeg would have to prepend its own backtrace to the Exceptions trace: seq[StackTraceEntry]
fields, but these are not exported and I can't seem to find any other way to modify these. Maybe I should whip up a Nim PR for this.
Example:
import npeg
import std/strutils
import npeg
const parser = peg("nodes", data: seq[string]):
nodes <- *(node * (' ' | !1))
node <- extraWord | word
extraWord <- word * extra * dash
ident <- +Alpha
word <- ident:
data.add($0)
dash <- '-':
data.add($0)
extra <- number | dot
number <- +Digit:
raise newException(IOError, "IO failed")
data.add($0 & "(int)")
dot <- '.':
data.add($0 & "(dot)")
var data: seq[string]
assert parser.match("a b1- c.", data).ok
echo data
Output:
/home/ico/sandbox/prjs/npeg/main.nim(30) main
/home/ico/sandbox/prjs/npeg/main.nim(27) flap
/home/ico/sandbox/prjs/npeg/src/npeg.nim(135) match
/home/ico/sandbox/prjs/npeg/src/npeg/patt.nim(90) fn`gensym46
/home/ico/sandbox/prjs/npeg/src/npeg/patt.nim(90) fn
/home/ico/sandbox/prjs/npeg/main.nim(11) nodes <- *(node * (' ' | !1))
/home/ico/sandbox/prjs/npeg/main.nim(12) node <- extraWord | word
/home/ico/sandbox/prjs/npeg/main.nim(13) extraWord <- word * extra * dash
/home/ico/sandbox/prjs/npeg/main.nim(19) extra <- number | dot
/home/ico/sandbox/prjs/npeg/main.nim(20) number <- +Digit
The last 5 lines show the NPeg part of the stacktrace including the regular lineInfo, with the NPeg rule appended for more clarity.
from npeg.
let's see of nim-lang/Nim#20772 gets through, that will allow NPeg stack frames to go on the native Nim exception stack trace.
from npeg.
For now, enable printing the stack trace with -d:npegStacktrace
from npeg.
Related Issues (20)
- `-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
- [Question] Why is my npeg parser done, then starts backtracking and fails. HOT 8
- FR: Deferred code block captures HOT 1
- Enhancing parser flexibility: improving type parsing with the push template HOT 1
- Captures not working inside of proc with generic arguments HOT 21
- javascript support HOT 6
- Help - how to handle captures and optionals HOT 1
- Best way of handling languages that have unicode identifiers? HOT 10
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.