Coder Social home page Coder Social logo

Print return stack on overflow about npeg HOT 4 CLOSED

zevv avatar zevv commented on August 27, 2024
Print return stack on overflow

from npeg.

Comments (4)

zevv avatar zevv commented on August 27, 2024

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.

zevv avatar zevv commented on August 27, 2024

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.

zevv avatar zevv commented on August 27, 2024

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.

zevv avatar zevv commented on August 27, 2024

For now, enable printing the stack trace with -d:npegStacktrace

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.