Skip to content

Instantly share code, notes, and snippets.

@tobiasvl
Forked from imajes/location_parser_gist.rb
Last active August 29, 2015 14:08
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 tobiasvl/99b6ebab875373eb6698 to your computer and use it in GitHub Desktop.
Save tobiasvl/99b6ebab875373eb6698 to your computer and use it in GitHub Desktop.
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