Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 44 lines (32 sloc) 1.361 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
$:.unshift File.dirname(__FILE__) + "/../lib"
require 'parslet'

class ErbParser < Parslet::Parser
  rule(:ruby) { (str('%>').absnt? >> any).repeat.as(:ruby) }
  
  rule(:expression) { (str('=') >> ruby).as(:expression) }
  rule(:comment) { (str('#') >> ruby).as(:comment) }
  rule(:code) { ruby.as(:code) }
  rule(:erb) { expression | comment | code }
  
  rule(:erb_with_tags) { str('<%') >> erb >> str('%>') }
  rule(:text) { (str('<%').absnt? >> any).repeat(1) }
  
  rule(:text_with_ruby) { (text.as(:text) | erb_with_tags).repeat.as(:text) }
  root(:text_with_ruby)
end

parser = ErbParser.new
p parser.parse "The value of x is <%= x %>."
p parser.parse "<% 1 + 2 %>"
p parser.parse "<%# commented %>"
 

evaluator = Parslet::Transform.new do
  
  erb_binding = binding
  
  rule(:code => { :ruby => simple(:ruby) }) { eval(ruby, erb_binding); '' }
  rule(:expression => { :ruby => simple(:ruby) }) { eval(ruby, erb_binding) }
  rule(:comment => { :ruby => simple(:ruby) }) { '' }
  
  rule(:text => simple(:text)) { text }
  rule(:text => sequence(:texts)) { texts.join }
  
end

puts evaluator.apply(parser.parse(<<-ERB
The <% a = 2 %>not printed result of "a = 2".
The <%# a = 1 %>not printed non-evaluated comment "a = 1", see the value of a below.
The <%= 'nicely' %> printed result.
The <% b = 3 %>value of a is <%= a %>, and b is <%= b %>.
ERB
))
Something went wrong with that request. Please try again.