-
-
Save tobiasvl/99b6ebab875373eb6698 to your computer and use it in GitHub Desktop.
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
require 'parslet' | |
require 'parslet/convenience' | |
require 'awesome_print' | |
require 'byebug' | |
class LocationParser < Parslet::Parser | |
# literals | |
rule(:space) { str(' ') } | |
rule(:spaces) { space.repeat(0) } | |
rule(:comma) { str(',') } | |
# composite literals | |
rule(:newline) { str("\r").maybe >> str("\n") } | |
# rule(:blank_line) { spaces >> newline >> spaces } | |
# grammar structure | |
rule(:line) { ( address_part.as(:address_part) >> comma.maybe >> space.maybe ) >> newline.maybe } | |
# NORTH HENRY STREET | |
rule(:address_part) { match('[[:print:]&&[^,]]').repeat(1) } | |
rule(:new_string) { line.repeat } | |
root(:new_string) | |
end | |
str = "165 HOPKINS STREET, HOPKINS STREET between THROOP AVENUE and TOMPKINS AVENUE, 159 HOPKINS STREET, TOMPKINS AVENUE between FLUSHING AVENUE and PARK AVENUE, 630 FLUSHING AVENUE" | |
def run_parse(str) | |
begin | |
x = LocationParser.new.parse(str) | |
puts "---\n#{x}\n---" | |
rescue Parslet::ParseFailed => failure | |
puts failure.cause.ascii_tree | |
print "\n" | |
ap str | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment