Skip to content

Commit

Permalink
+ Outputs Treetop (or something almost like it)
Browse files Browse the repository at this point in the history
  • Loading branch information
kschiess committed Feb 2, 2011
1 parent 3ceab72 commit 5dc086e
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions lib/parslet/export.rb
Expand Up @@ -57,36 +57,38 @@ def accept(visitor)
end
end

require 'set'

class Parslet::Parser

class GrammarPrintVisitor
attr_reader :output
def initialize
@output = ''
@open = Hash.new
end

def str(str)
output << "'#{str.inspect[1..-2]}'"
end

def entity(name, context, block)
output << "rule #{name}\n"

contents = context.instance_eval(&block)
contents.accept(self)
@open[name] = [context, block]

output << "end\n"
output << name.to_s
end

def named(name, parslet)
parslet.accept(self)
end

def sequence(parslets)
output << '('
parslets.each do |parslet|
parslet.accept(self)
output << ' '
output << ' ' unless parslet == parslets.last
end
output << ')'
end

def repetition(min, max, parslet)
Expand All @@ -97,7 +99,7 @@ def repetition(min, max, parslet)
def alternative(alternatives)
alternatives.each do |parslet|
parslet.accept(self)
output << " / "
output << " / " unless parslet == alternatives.last
end
end

Expand All @@ -109,6 +111,28 @@ def lookahead(positive, bound_parslet)
def re(match)
output << match.inspect
end

def rules
@output = ''
seen = Set.new
loop do
remaining = @open.keys - seen.to_a
break if remaining.empty?

name = remaining.first
context, block = @open[name]

seen << name

output << " rule #{name}\n"
output << " "
context.instance_eval(&block).accept(self)
output << "\n"
output << " end\n"
end

output
end
end

# Exports this parser as a string in Treetop lingo. The resulting Treetop
Expand All @@ -117,6 +141,13 @@ def re(match)
def to_treetop
visitor = GrammarPrintVisitor.new
root.accept(visitor)

"grammar Test\n" <<
" rule root\n" <<
" " << visitor.output << "\n" <<
" end\n" <<
visitor.rules <<
"end\n"
end
end

0 comments on commit 5dc086e

Please sign in to comment.