Skip to content

Instantly share code, notes, and snippets.

@andyl
Created March 7, 2011 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyl/858083 to your computer and use it in GitHub Desktop.
Save andyl/858083 to your computer and use it in GitHub Desktop.
parslet line parsing
#!/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
@kschiess
Copy link

kschiess commented Mar 7, 2011

Hi andyl,

You would at least need to do a (eol.absnt? >> any).repeat in #line ...

Greetings,
kaspar

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