Skip to content
This repository
Kaspar Schiess March 02, 2011
file 31 lines (22 sloc) 0.919 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
# The example from the readme. With this, I am making sure that the readme
# 'works'. Is this too messy?

$:.unshift File.dirname(__FILE__) + "/../lib"

# cut here -------------------------------------------------------------------
require 'parslet'
include Parslet

# Constructs a parser using a Parser Expression Grammar like DSL:
parser = str('"') >>
          (
            str('\\') >> any |
            str('"').absent? >> any
          ).repeat.as(:string) >>
          str('"')
  
# Parse the string and capture parts of the interpretation (:string above)
tree = parser.parse('"This is a \\"String\\" in which you can escape stuff"')

tree # => {:string=>"This is a \\\"String\\\" in which you can escape stuff"}

# Here's how you can grab results from that tree:

transform = Parslet::Transform.new do
  rule(:string => simple(:x)) {
    puts "String contents: #{x}" }
end
transform.apply(tree)

Something went wrong with that request. Please try again.