Sha256: eca866a83a4bf99febe4772ee32a3c4ebc5601e0397d0099936b2787672ccc77

Contents?: true

Size: 1.41 KB

Versions: 11

Compression:

Stored size: 1.41 KB

Contents

#!/usr/bin/env ruby

# Example contributed by Hal Brodigan (postmodern). Thanks!

$:.unshift File.dirname(__FILE__) + "/../lib"
require 'parslet'
require 'parslet/convenience'

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

input = ARGV[0] || begin
  default = "a.b.c.d@gmail.com"
  STDERR.puts "usage: #{$0} \"EMAIL_ADDR\""
  STDOUT.puts "since you haven't specified any EMAIL_ADDR, for testing purposes we're using #{default}"
  default
end

p sanitizer.apply(parser.parse_with_debug(input))

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
parslet-2.0.0 example/email_parser.rb
parslet-1.8.2 example/email_parser.rb
parslet-1.8.1 example/email_parser.rb
parslet-1.8.0 example/email_parser.rb
swift-pyrite-0.1.1 vendor/bundle/ruby/2.0.0/gems/parslet-1.7.1/example/email_parser.rb
swift-pyrite-0.1.0 vendor/bundle/ruby/2.0.0/gems/parslet-1.7.1/example/email_parser.rb
parslet-1.7.1 example/email_parser.rb
parslet-1.7.0 example/email_parser.rb
parslet-1.6.2 example/email_parser.rb
parslet-1.6.1 example/email_parser.rb
parslet-1.6.0 example/email_parser.rb