Created
March 7, 2011 04:44
-
-
Save andyl/858083 to your computer and use it in GitHub Desktop.
parslet line parsing
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'parslet' | |
class LineParser < Parslet::Parser | |
rule(:eol) { match['\n\r'] } | |
rule(:eol?) { eol.maybe } | |
rule(:line) { (any.repeat).as(:line) } | |
rule(:line_eol) { line >> eol? } | |
rule(:multi_line) { line_eol >> line.maybe } | |
root(:multi_line) | |
end | |
single = "asdf" | |
multi = "asdf\nqwer\nzxcv" | |
# this works! | |
p LineParser.new.line_eol.parse(single).inspect | |
# this doesn't work | |
# i want the parse results to contain three items | |
# instead it contains one | |
p LineParser.new.parse(multi).inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi andyl,
You would at least need to do a (eol.absnt? >> any).repeat in #line ...
Greetings,
kaspar