Created
October 6, 2014 19:25
-
-
Save iamvery/5b3e6321d194fbb89563 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ooo, this looks pretty nice.