Permalink
Browse files

Spec for Atoms::Scope

  • Loading branch information...
1 parent 44f145f commit 25ecbf6e22ca36fe1b40de0ca0ef451a563a26de @kschiess committed Dec 27, 2012
Showing with 43 additions and 11 deletions.
  1. +4 −0 lib/parslet/atoms/capture.rb
  2. +9 −11 lib/parslet/atoms/dynamic.rb
  3. +4 −0 lib/parslet/atoms/scope.rb
  4. +26 −0 spec/parslet/atoms/scope_spec.rb
@@ -30,5 +30,9 @@ def apply(source, context, consume_all)
return result
end
+
+ def to_s_inner(prec)
+ "(#{name.inspect} = #{parslet.to_s(prec)})"
+ end
end
@@ -1,6 +1,6 @@
-# Evaluates a block at parse time. The result from the block can be either
-# a parser or a result from calling a parser. In the first case, the parser
-# will then be applied to the input, creating the result.
+# Evaluates a block at parse time. The result from the block must be a parser
+# (something which implements #apply). In the first case, the parser will then
+# be applied to the input, creating the result.
#
# Dynamic parses are never cached.
#
@@ -21,14 +21,12 @@ def cached?
def try(source, context, consume_all)
result = block.call(source, context)
- # Result is either a parslet atom, in which case we apply it to the input,
- # or it is a result from a parslet atom, in which case we return it
- # directly.
- if result.respond_to?(:apply)
- return result.apply(source, context, consume_all)
- else
- return result
- end
+ # Result is a parslet atom.
+ return result.apply(source, context, consume_all)
+ end
+
+ def to_s_inner(prec)
+ "dynamic { ... }"
end
end
@@ -19,4 +19,8 @@ def apply(source, context, consume_all)
return parslet.apply(source, context, consume_all)
end
end
+
+ def to_s_inner(prec)
+ "scope { #{block.call.to_s(prec)} }"
+ end
end
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+describe Parslet::Atoms::Scope do
+ include Parslet
+ include Parslet::Atoms::DSL
+
+
+ let(:context) { Parslet::Atoms::Context.new(nil) }
+ let(:captures) { context.captures }
+
+ def inject string, parser
+ source = Parslet::Source.new(string)
+ parser.apply(source, context, true)
+ end
+
+ let(:aabb) {
+ scope {
+ match['ab'].capture(:f) >> dynamic { |s,c| str(c.captures[:f]) }
+ }
+ }
+ it "keeps values of captures outside" do
+ captures[:f] = 'old_value'
+ inject 'aa', aabb
+ captures[:f].should == 'old_value'
+ end
+end

0 comments on commit 25ecbf6

Please sign in to comment.