Reputation: 868
I am working on a new programming language rip, and I'm having trouble getting to the bottom of some infinite loops. Is there a way to print out each rule as it gets called, such that I can see the rules that are recursing? I've tried walking through the code in my head, and I just don't see it. Any help would be much appreciated.
Upvotes: 1
Views: 326
Reputation: 30398
To flesh out Raving Genius’s answer:
The method to patch is actually Parslet::Atoms::Context#lookup
. View it on GitHub (permalink to current version). In your own code, you can patch that method to print obj
like this:
class Parslet::Atoms::Context
def lookup(obj, pos)
p obj
@cache[pos][obj.object_id]
end
end
Run that code any time before you call parse
on your parser, and it will take effect. Sample output:
>> parser = ConsistentNewlineTextParser.new
=> LINES
>> parser.parse("abc")
LINES
(line_content:LINE_CONTENT NEWLINE){0, } line_content:LINE_CONTENT
(line_content:LINE_CONTENT NEWLINE){0, }
line_content:LINE_CONTENT NEWLINE
LINE_CONTENT
WORD
\\w{0, }
\\w
\\w
\\w
\\w
NEWLINE
dynamic { ... }
FIRST_NEWLINE
'? '
'
'?
'
'
'
LINE_CONTENT
=> {:line_content=>"abc"@0}
Upvotes: 3
Reputation: 21548
My branch of Parslet automatically detects endless loops, and exits out reporting expression that is repeating without consuming anything.
https://github.com/nigelthorne/parslet
see Parse markdown indented code block for an example.
Upvotes: 0
Reputation: 868
I figured it out: editing Parslet::Atom::Context#lookup
to output the obj
parameter will show each rule as it is being called.
Upvotes: 1