Skip to content

Instantly share code, notes, and snippets.

@iamvery
Created October 6, 2014 19:25
Show Gist options
  • Save iamvery/5b3e6321d194fbb89563 to your computer and use it in GitHub Desktop.
Save iamvery/5b3e6321d194fbb89563 to your computer and use it in GitHub Desktop.
require 'parslet'
class FooParser < Parslet::Parser
rule(:space) { match('\s').repeat(1) }
rule(:space?) { space.maybe }
rule(:dot) { str('.') >> space? }
rule(:colon) { str(':') >> space? }
rule(:comma) { str(',') >> space? }
rule(:comma?) { comma.maybe }
rule(:word) { match('\w').repeat(1) }
rule(:key) { word >> space? }
rule(:value) { match('[^,.]').repeat }
rule(:pair) { key.as(:key) >> colon >> value.as(:value) >> comma? }
rule(:pairs) { pair.repeat(1) >> dot }
rule(:foos) { pairs.as(:pair).repeat }
root(:foos)
end
EXAMPLE = 'First: thing, Is :first. How:do, we : even.'
p FooParser.new.parse(EXAMPLE)
@wallace
Copy link

wallace commented Oct 6, 2014

Ooo, this looks pretty nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment