Parsing single qoute char in a single-quoted string in parsec

非 Y 不嫁゛ 提交于 2019-12-25 03:27:09

问题


I've got a silly situation in my parsec parsers that I would like your help on.

I need to parse a sequence of strongs / chars that are separated by | characters. So, we could have a|b|'c'|'abcd'

which should be turned into

[a,b,c,abcd]

Space is not allowed, unless inside of a ' ' string. Now, in my naïve attempt, I got the situation now where I can parse strings like a'a|'bb' to [a'a,bb] but not aa|'b'b' to [aa,b'b].

singleQuotedChar :: Parser Char
singleQuotedChar = noneOf "'" <|> try (string "''" >> return '\'')

simpleLabel = do
    whiteSpace haskelldef
    lab <- many1 (noneOf "|")
    return $ lab

quotedLabel = do
    whiteSpace haskelldef
    char '\''
    lab <- many singleQuotedChar
    char '\''
    return $ lab

Now, how do I tell the parser to consider ' a stoping ' iff it is followed by a | or white space? (Or, get some ' char counting into this). The input is user generated, so I cannot rely on them \'-ing chars.


回答1:


Note that allowing a quote in the middle of a string delimited by quotes is very confusing to read, but I believe this should allow you to parse it.

quotedLabel = do -- reads the first quote.
    whiteSpace
    char '\''
    quotedLabel2

quotedLabel2 = do -- reads the string and the finishing quote.
    lab <- many singleQuotedChar
    try  (do more <- quotedLabel3
             return $ lttrace "quotedLabel2" (lab ++ more))
     <|> (do char '\''
             return $ lttrace "quotedLabel2" lab)


quotedLabel3 = do -- handle middle quotes
    char '\''
    lookAhead $ noneOf ['|']
    ret <- quotedLabel2
    return $ lttrace "quotedLabel3" $ "'" ++ ret


来源:https://stackoverflow.com/questions/24940541/parsing-single-qoute-char-in-a-single-quoted-string-in-parsec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!