0

私の parsec パーサーで、あなたの助けが必要なばかげた状況が発生しました。

| で区切られたストロング/文字のシーケンスを解析する必要があります。文字。したがって、a|b|'c'|'abcd' となる可能性があります。

に変換する必要があります

[a,b,c,abcd]

' ' 文字列内を除き、スペースは使用できません。さて、私の素朴な試みで、a'a|'bb' のような文字列を [a'a,bb] に解析できるが、aa|'b'b' を [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

では、パーサーに | が続く場合に 'a stoping' を考慮するように指示するにはどうすればよいでしょうか。または空白?(または、これにいくつかの ' char カウントを入れます)。入力はユーザーが生成したものなので、\'-ing 文字に頼ることはできません。

4

1 に答える 1

1

引用符で区切られた文字列の途中で引用符を許可すると、非常に読みにくくなりますが、これにより解析できるようになるはずです。

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
于 2014-07-27T14:32:46.863 に答える