Skip to content

Commit

Permalink
+ regular expressions and alternatives
Browse files Browse the repository at this point in the history
  • Loading branch information
kschiess committed Feb 21, 2011
1 parent 2cc4ac7 commit 56bec38
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
8 changes: 8 additions & 0 deletions lib/parslet/atoms/transform.rb
Expand Up @@ -15,4 +15,12 @@ def visit_str(str)
def visit_sequence(parslets) def visit_sequence(parslets)
parslets[1..-1].inject(parslets[0]) { |a,p| a >> p } parslets[1..-1].inject(parslets[0]) { |a,p| a >> p }
end 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 end
45 changes: 36 additions & 9 deletions spec/parslet/atoms/transform_spec.rb
Expand Up @@ -9,30 +9,57 @@
it "should transform all inputs onto itself" it "should transform all inputs onto itself"
end end


class ModifyAll < Parslet::Atoms::Transform class ModifyAll < Parslet::Atoms::Transform; end
def visit_str(s)
super(s.reverse)
end
def visit_sequence(seq)
super(seq.reverse)
end
end
describe ModifyAll do describe ModifyAll do
subject { ModifyAll.new } subject { ModifyAll.new }
def apply(grammar) def apply(grammar)
subject.apply(grammar) subject.apply(grammar)
end end

context "str" do context "str" do
class ModifyAll
def visit_str(s)
super(s.reverse)
end
end
it "should reverse the string" do it "should reverse the string" do
apply(str('foo')).should parse('oof') apply(str('foo')).should parse('oof')
end end
end end
context "sequence" do context "sequence" do
class ModifyAll
def visit_sequence(seq)
super(seq.reverse)
end
end
it "should reverse sequences" do it "should reverse sequences" do
apply(str('a') >> str('b')).should parse('ba') apply(str('a') >> str('b')).should parse('ba')
end end
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


end end

0 comments on commit 56bec38

Please sign in to comment.