Skip to content

Commit

Permalink
+ Supports sequence of atoms
Browse files Browse the repository at this point in the history
  • Loading branch information
kschiess committed Nov 21, 2010
1 parent bc03948 commit 669ec36
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
13 changes: 9 additions & 4 deletions lib/parslet/expression/treetop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ class Parser < Parslet::Parser

rule(:expression) {
(atom >> str('?')).as(:maybe) |
atom
atom.repeat
}

rule(:atom) {
str('(') >> expression >> str(')') |
str('(') >> expression.as(:unwrap) >> str(')') |
string
}

Expand All @@ -18,12 +18,17 @@ class Parser < Parslet::Parser
(str('\\') >> any) |
(str("'").absnt? >> any)
).repeat.as(:string) >>
str('\'')
str('\'') >> space?
}

rule(:space) { match("\s").repeat(1) }
rule(:space?) { space.maybe }
end

class Transform < Parser::Transform
rule(:maybe => simple(:m)) { |d| d[:m].maybe }
rule(:unwrap => simple(:u)) { u }
rule(sequence(:s)) { |d| Parslet::Atoms::Sequence.new(*d[:s]) }
rule(:maybe => simple(:m)) { |d| d[:m].maybe }
rule(:string => simple(:s)) { |d| str(d[:s]) }
end

Expand Down
6 changes: 5 additions & 1 deletion spec/unit/parslet/expression_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@

"'abc'?", 'abc',
"'abc'?", '',

"('abc')", 'abc',

"'a' 'b'", 'ab',
].each_slice(2) do |pattern, input|
context "exp(#{pattern})" do
context "exp(#{pattern.inspect})" do
subject { exp(pattern) }
it { should accept(input) }
end
Expand Down

0 comments on commit 669ec36

Please sign in to comment.