Skip to content

Instantly share code, notes, and snippets.

@andyl
Created June 4, 2011 01:43
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/1007451 to your computer and use it in GitHub Desktop.
Save andyl/1007451 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'parslet'
class Address < Parslet::Parser
# Single character rules
rule(:comma) { str(',') >> space? }
rule(:space) { match('\s').repeat }
rule(:space?) { space.maybe }
rule(:newline) { str("\n").repeat }
rule(:digit) { match('[0-9]') }
# Things
rule(:line1) { (match('[A-z0-9 ]').repeat).as(:address1) }
rule(:line2) { newline >> (match('[A-z0-9 ]').repeat).as(:address2) }
rule(:word) { match('[A-z]').repeat }
rule(:capword) { space >> match['A-Z'] >> match['a-z'].repeat(1) }
# Address Parts
rule(:address) { line1 >> line2 }
rule(:state) { (str("CA") | str("CO")).as(:state) }
rule(:zip) { digit.repeat(5,5).as(:zip) }
rule(:city) { (word >> capword.maybe).as(:city) }
rule(:sz) { state >> space.maybe >> zip }
rule(:csz) { city >> space >> state >> space >> zip.maybe }
rule(:all) { (line1 >> newline >> csz) | (address >> newline >> csz) }
root :all
end
def run(rule, string)
begin
parser = Address.new
eval "p parser.#{rule}.parse('#{string}')"
rescue Parslet::ParseFailed => e
puts e, parser.root.error_tree
end
end
run( "comma", ",")
run( "comma", "x")
run( "digit", "5")
run( "comma", ",")
run( "digit", "5")
run( "zip", "94022")
run( "state", "CA")
run( "state", "CO")
run( "sz", "CA 94022")
run( "csz", " CA 94022")
run( "csz", "Mountain CA 94022")
run( "csz", "Mountain View CA 94022")
run( "csz", "SF CA 94022")
run( "csz", "SF CA")
run("address", "1523 Broker Way")
run("address", "1523 Broker Way\nApartment 22")
run( "all", "1523 Broker Way\nMountain View CA 94022")
run( "all", "1523 Broker Way\nApartment 22\nMountain View CA 94022")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment