Quick parsing of a string that escapes escaped characters?

I am trying to parse a string that may contain escaped characters, here is an example:

import qualified Data.Text as T

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

The parser above creates Stringand then packs it in Text. Is there a way to parse a string with screens like the ones above using functions to efficiently handle strings that attoparsec provides? As String, scan, runScanner, takeWhile,...

An analysis of something like "one \"two\" \[three\]"will result in one "two" [three].

Update

Thanks to @epsilonhalbe, I was able to come up with a generic solution, perfect for my needs; Please note that the following function is not seeking appropriate escapes, such as [..], "..", (..)etc .; and also, if it finds an escape character that is invalid, it treats it \as a literal.

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
+4
source share
1 answer

It’s possible to write some escaping code, attoparsecand text- in general, it's pretty simple - if you've already worked with parsers

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<>"]"

then you can use it to analyze the following test cases

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]

Note that you need to avoid screens - that’s why you see \\\"instead\"

, , 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"
+2

Source: https://habr.com/ru/post/1628021/


All Articles