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.
Asked
Active
Viewed 301 times
3 Answers
3
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}

Community
- 1
- 1

Rory O'Kane
- 29,210
- 11
- 96
- 131
1
I figured it out: editing Parslet::Atom::Context#lookup
to output the obj
parameter will show each rule as it is being called.

ravinggenius
- 816
- 1
- 6
- 14
0
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.

Community
- 1
- 1

Nigel Thorne
- 21,158
- 3
- 35
- 51