Sha256: 36d1fe8f1af6694c43d33c24ebb0d35671e0eecced0eec9d28109cc8c54a1dcf
Contents?: true
Size: 1.29 KB
Versions: 2
Compression:
Stored size: 1.29 KB
Contents
#!/usr/bin/env ruby # Example contributed by Hal Brodigan (postmodern). Thanks! $:.unshift '../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 + "@" } rule(:email => sequence(:email)) { email.join } end parser = EmailParser.new sanitizer = EmailSanitizer.new unless ARGV[0] STDERR.puts "usage: #{$0} \"EMAIL_ADDR\"" exit -1 end begin p sanitizer.apply(parser.parse(ARGV[0])) rescue Parslet::ParseFailed => error puts error puts parser.root.error_tree end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
parslet-1.1.1 | example/email_parser.rb |
parslet-1.1.0 | example/email_parser.rb |