2016-12-28 29 views
2

我試圖解析RFC5322電子郵件地址。我的解析器的工作原理是,在結果中,其中一個是正確的。但是,我該如何選擇「正確」的結果呢?選擇正確的ReadP解析結果

鑑於字符串Foo Bar <[email protected]>,我的解析器應該產生一個值Address (Just "Foo Bar") "[email protected]"

另外,給定字符串[email protected],我的分析器應該產生一個值Address Nothing "[email protected]"

包含名稱的值是首選。

我的解析器是這樣的:

import   Control.Applicative 
import   Data.Char 
import qualified Data.Text      as T 
import   Text.ParserCombinators.ReadP 

onlyEmail :: ReadP Address 
onlyEmail = do 
    skipSpaces 
    email <- many1 $ satisfy isAscii 
    skipSpaces 
    return $ Address Nothing (T.pack email) 

withName :: ReadP Address 
withName = do 
    skipSpaces 
    name <- many1 (satisfy isAscii) 
    skipSpaces 
    email <- between (char '<') (char '>') (many1 $ satisfy isAscii) 
    skipSpaces 
    return $ Address (Just $ T.pack name) (T.pack email) 

rfc5322 :: ReadP Address 
rfc5322 = withName <|> onlyEmail 

當我運行readP_to_S rfc5322 "Foo Bar <[email protected]>"分析器,它產生以下結果:

[ (Address {addressName = Nothing, addressEmail = "F"},"oo Bar <[email protected]>") 
, (Address {addressName = Nothing, addressEmail = "Fo"},"o Bar <[email protected]>") 
, (Address {addressName = Nothing, addressEmail = "Foo"},"Bar <[email protected]>") 
, (Address {addressName = Nothing, addressEmail = "Foo "},"Bar <[email protected]>") 
, (Address {addressName = Nothing, addressEmail = "Foo B"},"ar <[email protected]>") 
, (Address {addressName = Nothing, addressEmail = "Foo Ba"},"r <[email protected]>") 
, (Address {addressName = Nothing, addressEmail = "Foo Bar"},"<[email protected]>") 
, (Address {addressName = Nothing, addressEmail = "Foo Bar "},"<[email protected]>") 
, (Address {addressName = Nothing, addressEmail = "Foo Bar <"},"[email protected]>") 
, (Address {addressName = Nothing, addressEmail = "Foo Bar <f"},"[email protected]>") 
, (Address {addressName = Nothing, addressEmail = "Foo Bar <fo"},"[email protected]>") 
, (Address {addressName = Nothing, addressEmail = "Foo Bar <foo"},"@bar.com>") 
, (Address {addressName = Nothing, addressEmail = "Foo Bar <[email protected]"},"bar.com>") 
, (Address {addressName = Nothing, addressEmail = "Foo Bar <[email protected]"},"ar.com>") 
, (Address {addressName = Nothing, addressEmail = "Foo Bar <[email protected]"},"r.com>") 
, (Address {addressName = Nothing, addressEmail = "Foo Bar <[email protected]"},".com>") 
, (Address {addressName = Nothing, addressEmail = "Foo Bar <[email protected]"},"com>") 
, (Address {addressName = Nothing, addressEmail = "Foo Bar <[email protected]"},"om>") 
, (Address {addressName = Nothing, addressEmail = "Foo Bar <[email protected]"},"m>") 
, (Address {addressName = Nothing, addressEmail = "Foo Bar <[email protected]"},">") 
, (Address {addressName = Just "Foo Bar", addressEmail = "[email protected]"},"") 
, (Address {addressName = Just "Foo Bar ", addressEmail = "[email protected]"},"") 
, (Address {addressName = Nothing, addressEmail = "Foo Bar <[email protected]>"},"") 
] 

在這種情況下,結果其實我是想出現三階最後在列表中。我如何表達這種偏好?

回答

3

你不應該做偏好。你的問題是你的部分解析器正在接受比真正需要更大的字符串集。

例如,我的解決方案:

import   Control.Bool 
import   Control.Applicative 
import   Data.Char 
import qualified Data.Text      as T 
import   Data.Text (Text) 
import   Text.ParserCombinators.ReadP 

email :: ReadP Text 
email = do 
    l <- part 
    a <- char '@' 
    d <- part 
    return . T.pack $ l ++ a:d 
    where 
    part = munch1 (isAscii <&&> (/='@') <&&> (/='<') <&&> (/='>')) 

name :: ReadP Text 
name = T.pack <$> chainr1 part sep 
    where 
    part = munch1 (isAlpha <||> isDigit <||> (=='\'')) 
    sep = (\xs ys -> xs ++ ' ':ys) <$ munch1 (==' ') 

onlyEmail :: ReadP Address 
onlyEmail = Address Nothing <$> email 

withName :: ReadP Address 
withName = do 
    n <- name 
    skipSpaces 
    e <- between (char '<') (char '>') email 
    return $ Address (Just n) e 

address :: ReadP Address 
address = skipSpaces *> (withName <|> onlyEmail) 

main = print $ readP_to_S address "Foo Bar <[email protected]>" 

將被打印:

[(Address (Just "Foo Bar") "[email protected]","")]