Hi there!

Is there a better way ? Would simple_or_sequence(:x) or whatever(:x) as
> captures make sense ?


I kinda like your approach, there are separate transforms to deal with the
tricky corner cases.

If I'm understanding what you want correctly, it is possible to do your
transformation in a single rule, but only if you re-jig the grammar
slightly:



class Parser < Parslet::Parser
 rule(:indent) { str(' ').repeat(1) }
 rule(:content) { match('[^\n]').repeat(1) }
 rule(:line) { str('\n') | indent.maybe.as(:indentation) >>
content.maybe.as(:stuff)
>> str("\n") }
 rule(:lines) { line.repeat }
 root(:lines)
end

class Transformer < Parslet::Transform
 # vanilla rule
 rule(:indentation => simple(:ind), :stuff => simple(:stu)) {
   [ ind.to_s.length, stu.to_s ]
 }
end


Although I don't think that's as easy to follow, personally.


cheers
ant

Reply via email to