Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

+ regular expressions and alternatives

  • Loading branch information...
commit 56bec3877ef7fc18ad067537b8e2742dc689e374 1 parent 2cc4ac7
@kschiess authored
View
8 lib/parslet/atoms/transform.rb
@@ -15,4 +15,12 @@ def visit_str(str)
def visit_sequence(parslets)
parslets[1..-1].inject(parslets[0]) { |a,p| a >> p }
end
+
+ def visit_re(match)
+ Parslet.match(match)
+ end
+
+ def visit_alternative(parslets)
+ parslets[1..-1].inject(parslets[0]) { |a,p| a | p }
+ end
end
View
45 spec/parslet/atoms/transform_spec.rb
@@ -9,30 +9,57 @@
it "should transform all inputs onto itself"
end
- class ModifyAll < Parslet::Atoms::Transform
- def visit_str(s)
- super(s.reverse)
- end
- def visit_sequence(seq)
- super(seq.reverse)
- end
- end
+ class ModifyAll < Parslet::Atoms::Transform; end
describe ModifyAll do
subject { ModifyAll.new }
def apply(grammar)
subject.apply(grammar)
end
-
+
context "str" do
+ class ModifyAll
+ def visit_str(s)
+ super(s.reverse)
+ end
+ end
it "should reverse the string" do
apply(str('foo')).should parse('oof')
end
end
context "sequence" do
+ class ModifyAll
+ def visit_sequence(seq)
+ super(seq.reverse)
+ end
+ end
it "should reverse sequences" do
apply(str('a') >> str('b')).should parse('ba')
end
end
+ context "re" do
+ class ModifyAll
+ def visit_re(match)
+ super(match.delete('a'))
+ end
+ end
+ it "should not match a's" do
+ apply(match['ab']).should_not parse('a')
+ apply(match['ab']).should parse('b')
+ end
+ end
+ context "alternative" do
+ class ModifyAll
+ def visit_alternative(parslets)
+ super(parslets.reverse)
+ end
+ end
+ it "should reverse priorities" do
+ apply(
+ str('foobar').as(:whole) |
+ str('foo').as(:foo) >> str('bar').as(:bar)
+ ).should parse('foobar').as(:foo => 'foo', :bar => 'bar')
+ end
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.