快速解析允许转义字符的字符串?

时间:2016-02-09 19:42:39

标签: parsing haskell parser-combinators attoparsec

我正在尝试解析一个包含转义字符的字符串,这是一个例子:

import qualified Data.Text as T

exampleParser :: Parser T.Text
exampleParser = T.pack <$> many (char '\\' *> escaped <|> anyChar)
  where escaped = satisfy (\c -> c `elem` ['\\', '"', '[', ']'])

上面的解析器会创建一个String,然后将其打包到Text。是否有任何方法使用attoparsec提供的高效字符串处理函数解析带有类似上述转义的字符串?与stringscanrunScannertakeWhile...

一样

解析"one \"two\" \[three\]"之类的内容会产生one "two" [three]

更新

感谢@epsilonhalbe,我能够提出一个完美满足我需求的通用解决方案;请注意,以下函数不会查找匹配的转义字符,例如[..]".."(..)等;而且,如果它找到一个无效的转义字符,它会将\视为文字字符。

takeEscapedWhile :: (Char -> Bool) -> (Char -> Bool) -> Parser Text
takeEscapedWhile isEscapable while = do
  x <- normal
  xs <- many escaped
  return $ T.concat (x:xs)
  where normal = Atto.takeWhile (\c -> c /= '\\' && while c)
        escaped = do
          x <- (char '\\' *> satisfy isEscapable) <|> char '\\'
          xs <- normal
          return $ T.cons x xs

1 个答案:

答案 0 :(得分:2)

可以编写一些转义代码,attoparsectext - 总之它非常简单 - 看到你已经使用了解析器

import Data.Attoparsec.Text as AT
import qualified Data.Text as T
import Data.Text (Text)

escaped, quoted, brackted :: Parser Text
normal =  AT.takeWhile (/= '\\')
escaped = do r <- normal
             rs <- many escaped'
             return $ T.concat $ r:rs
  where escaped' = do r1 <- normal
                      r2 <- quoted <|> brackted
                      return $ r1 <> r2

quoted = do string "\\\""
            res <- normal
            string "\\\""
            return $ "\""<>res <>"\""

brackted = do string "\\["
              res <- normal
              string "\\]"
              return $ "["<>res<>"]"

然后您可以使用它来解析以下测试用例

Prelude >: MyModule
Prelude MyModule> import Data.Attoparsec.Text as AT
Prelude MyModule AT> import Data.Text.IO as TIO
Prelude MyModule AT TIO>:set -XOverloadedStrings
Prelude MyModule AT TIO> TIO.putStrLn $ parseOnly escaped "test"
test
Prelude MyModule AT TIO> TIO.putStrLn $ parseOnly escaped "\\\"test\\\""
"test"
Prelude MyModule AT TIO> TIO.putStrLn $ parseOnly escaped "\\[test\\]"
[test]
Prelude MyModule AT TIO> TIO.putStrLn $ parseOnly escaped "test \\\"test\\\" \\[test\\]"
test "test" [test]

请注意你必须逃避逃脱 - 这就是为什么你看到\\\"而不是\"

此外,如果您只是解析它将打印转义的Text值,例如

Right "test \"text\" [test]"

为最后一个例子。

如果解析文件,则在文件中编写简单的转义文本。

test.txt

I \[like\] \"Haskell\"

然后你可以

Prelude MyModule AT TIO> file <- TIO.readFile "test.txt" 
Prelude MyModule AT TIO> TIO.putStrLn $ parseOnly escaped file
I [like] "Haskell"