Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

57 lines (43 sloc) 1.467 kb
#!/usr/bin/env ruby
# Example contributed by Hal Brodigan (postmodern). Thanks!
$:.unshift File.dirname(__FILE__) + "/../lib"
require 'parslet'
class EmailParser < Parslet::Parser
rule(:space) { match('\s').repeat(1) }
rule(:space?) { space.maybe }
rule(:dash?) { match['_-'].maybe }
rule(:at) {
str('@') |
(dash? >> (str('at') | str('AT')) >> dash?)
}
rule(:dot) {
str('.') |
(dash? >> (str('dot') | str('DOT')) >> dash?)
}
rule(:word) { match('[a-z0-9]').repeat(1).as(:word) >> space? }
rule(:separator) { dot.as(:dot) >> space? | space }
rule(:words) { word >> (separator >> word).repeat }
rule(:email) {
(words.as(:username) >> space? >> at >> space? >> words).as(:email)
}
root(:email)
end
class EmailSanitizer < Parslet::Transform
rule(:dot => simple(:dot), :word => simple(:word)) { ".#{word}" }
rule(:word => simple(:word)) { word }
rule(:username => sequence(:username)) { username.join + "@" }
rule(:username => simple(:username)) { username.to_s + "@" }
rule(:email => sequence(:email)) { email.join }
end
parser = EmailParser.new
sanitizer = EmailSanitizer.new
unless ARGV[0]
STDERR.puts "usage: #{$0} \"EMAIL_ADDR\""
STDOUT.puts "since you haven't specified any EMAIL_ADDR, for testing purposes we're using a.b.c.d@gmail.com"
end
begin
p sanitizer.apply(parser.parse(ARGV[0] || 'a.b.c.d@gmail.com'))
rescue Parslet::ParseFailed => error
puts error
puts parser.error_tree
end
Jump to Line
Something went wrong with that request. Please try again.