require 'journey' def asts paths parser = Journey::Parser.new Journey::Nodes::Or.new paths.map { |x| parser.parse x } end class Ragel < Journey::Visitors::Visitor def terminal(n) n.left.inspect end def visit_OR(n) "(#{n.children.map { |x| visit x }.join ' | '})" end def visit_GROUP(n) "(#{visit n})" end def visit_CAT(n) "#{visit n.left} . #{visit n.right}" end end ## These come from your routes.rb ast = asts %w{ /articles /articles/new } ragel = Ragel.new.accept ast data = <<-eoc #include %%{ machine router; action match { ret = Qtrue; } main := #{ragel} %match; }%% %% write data; static VALUE match(VALUE self, VALUE string) { int cs = 0; char *p = RSTRING_PTR(string); char *pe = p + RSTRING_LEN(string); char *eof = pe; VALUE ret = Qfalse; %% write init; %% write exec; return ret; } void Init_router() { VALUE mRouter = rb_define_class("Router", rb_cObject); rb_define_method(mRouter, "match", match, 1); } eoc puts data