我有这个Parser(48小时写一个Lisp的练习):
import Text.ParserCombinators.Parsec
import Control.Monad
data LispVal = String String deriving Show
parseString :: Parser LispVal
parseString = do char '"'
x <- many innerChar
char '"'
return $ String x
where innerChar = noneOf ['\\','\"'] <|> escapeChar
escapeChar =
do char '\\'
c <- oneOf ['n', '"', 'r', 't']
return $ case c of
'"' -> '\"'
'n' -> '\n'
'r' -> '\r'
't' -> '\t'
所以,这个代码似乎适用于所有转义字符的字符串,只有当char是\&#34;它似乎结束了String。
答案 0 :(得分:1)
我没有看到任何问题:
λ *Main > parseTest parseString $ show "some\"string"
String "some\"string"
λ *Main > show "some\"string"
"\"some\\\"string\""
λ *Main > parseTest parseString "\"some\\\"string\""
String "some\"string"
-- This happened? Escaping is tricky.
λ *Main > parseTest parseString "\"some\"string\""
String "some"