-
-
Save imajes/5236feada4aa9dbe5e03 to your computer and use it in GitHub Desktop.
This file contains 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) } | |
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Change
match('[\w\-\. ]').repeat
tomatch('[\w\-\. ]').repeat(1)
to avoid infinite loop.