Permalink
Browse files

! Fixing all examples

Added BlankSlate to the context, this makes it much cleaner
  • Loading branch information...
1 parent fe0a33f commit 23a4a5c7d7f7fcc3f7d42b573ab9b656e3e3b899 @kschiess kschiess committed Nov 13, 2010
Showing with 22 additions and 26 deletions.
  1. +2 −0 Gemfile
  2. +2 −0 Gemfile.lock
  3. +1 −1 Rakefile
  4. +7 −11 example/simple_xml.rb
  5. +7 −13 example/string_parser.rb
  6. +3 −1 lib/parslet/pattern/context.rb
View
@@ -1,6 +1,8 @@
# A sample Gemfile
source "http://rubygems.org"
+gem 'blankslate', '>= 2.1.2.3'
+
group :development do
gem 'rspec'
gem 'flexmock'
View
@@ -1,6 +1,7 @@
GEM
remote: http://rubygems.org/
specs:
+ blankslate (2.1.2.3)
diff-lcs (1.1.2)
flexmock (0.8.11)
rspec (2.0.1)
@@ -18,5 +19,6 @@ PLATFORMS
ruby
DEPENDENCIES
+ blankslate (>= 2.1.2.3)
flexmock
rspec
View
@@ -34,7 +34,7 @@ spec = Gem::Specification.new do |s|
# If you want to depend on other gems, add them here, along with any
# relevant versions
- # s.add_dependency("map", "~> 1.5.1")
+ s.add_dependency("blankslate", "~> 2.1.2.3")
# If your tests use any gems, include them here
s.add_development_dependency("rspec")
View
@@ -38,20 +38,16 @@ def check(xml)
include XML
r=parse(xml)
- # We'll validate the tree by reducing valid pairs of tags into simply the string
- # ok. If the transformation ends on a string, then the document was 'valid'.
+ # We'll validate the tree by reducing valid pairs of tags into simply the
+ # string "verified". If the transformation ends on a string, then the
+ # document was 'valid'.
+ #
t = Parslet::Transform.new
t.rule(
- o: {name: simple(:o)},
- c: {name: simple(:c)},
+ o: {name: simple(:tag)},
+ c: {name: simple(:tag)},
i: simple(:t)
- ) { |d|
- if d[:o] == d[:c]
- "ok."
- else
- fail "Open tag doesn't match close tag ... #{d.inspect}"
- end
- }
+ ) { 'verified' }
t.apply(r)
end
@@ -9,9 +9,7 @@
include Parslet
-class LiteralsParser
- include Parslet
-
+class LiteralsParser < Parslet::Parser
rule :space do
(match '[ ]').repeat(1)
end
@@ -48,10 +46,8 @@ class LiteralsParser
rule :crlf do
match('[\r\n]').repeat(1)
end
-
- def parse(str)
- literals.parse(str)
- end
+
+ root :literals
end
parsetree = LiteralsParser.new.parse(
@@ -70,12 +66,10 @@ def to_s
end
end
-transform = Parslet::Transform.new
-
-transform.rule(:literal => {:integer => simple(:x)}) { |d|
- IntLit.new(*d.values) }
-transform.rule(:literal => {:string => simple(:x)}) { |d|
- StringLit.new(*d.values) }
+transform = Parslet::Transform.new do
+ rule(:literal => {:integer => simple(:x)}) { IntLit.new(x) }
+ rule(:literal => {:string => simple(:s)}) { StringLit.new(s) }
+end
ast = transform.apply(parsetree)
pp ast
@@ -1,3 +1,5 @@
+require 'blankslate'
+
# Provides a context for tree transformations to run in. The context allows
# accessing each of the bindings in the bindings hash as local method.
#
@@ -8,7 +10,7 @@
# a # => :b
# end
#
-class Parslet::Pattern::Context
+class Parslet::Pattern::Context < BlankSlate
def initialize(bindings)
@bindings = bindings
end

0 comments on commit 23a4a5c

Please sign in to comment.