Skip to content

Instantly share code, notes, and snippets.

@imajes
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save imajes/5236feada4aa9dbe5e03 to your computer and use it in GitHub Desktop.
Save imajes/5236feada4aa9dbe5e03 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) }
# composite literals
rule(:newline) { str("\r").maybe >> str("\n") }
# grammar structure
rule(:address_word) { match('[\w\-\. ]').repeat >> space.maybe }
# line repeat
rule(:line) { address_word.as(:line) >> newline.maybe }
rule(:new_string) { line.repeat }
root(:new_string)
end
str = "165 HOPKINS STREET\n\tHOPKINS STREET between THROOP AVENUE and TOMPKINS AVENUE\n159 HOPKINS STREET\n\tTOMPKINS AVENUE between FLUSHING AVENUE and PARK AVENUE\n630 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
run_parse(str)
@tobiasvl
Copy link

Change match('[\w\-\. ]').repeat to match('[\w\-\. ]').repeat(1) to avoid infinite loop.

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