使用 Megaparsec,如果我想將包含表單注釋的字串決議~{content}為Comment記錄,我將如何去做呢?例如:
data Comment = { id :: Integer, content :: String }
parse :: Parser [Comment]
parse = _
parse
"hello world ~{1-sometext} bla bla ~{2-another comment}"
== [Comment { id = 1, content = "sometext" }, Comment { id = 2, content = "another comment"}]
我堅持的事情是允許所有不能~{}被忽略的東西,包括 lone char~和 lone brackets {}。
uj5u.com熱心網友回復:
您可以通過將字符放到下一個波浪號,然后決議波浪號(可選地后跟有效注釋)并回圈來執行此操作。
特別是,如果我們定義nonTildes丟棄非波浪號:
nonTildes :: Parser String
nonTildes = takeWhileP (Just "non-tilde") (/= '~')
然后optionalComment在大括號中決議波浪號和可選的以下注釋:
optionalComment :: Parser (Maybe Comment)
optionalComment = char '~' *>
optional (braces (Comment <$> ident_ <* char '-' <*> content_))
where
braces = between (char '{') (char '}')
ident_ = read <$> takeWhile1P (Just "digit") isDigit
content_ = takeWhileP Nothing (/= '}')
然后可以使用以下方式決議注釋:
comments :: Parser [Comment]
comments = catMaybes <$> (nonTildes *> many (optionalComment <* nonTildes))
這假設~{沒有匹配}的 a 是決議錯誤,而不是有效的非注釋文本,這似乎是明智的。但是,content_決議器的定義可能過于寬松。它將所有內容都吞噬到下一個},這意味著:
"~{1-{{{\n}"
是帶有內容的有效評論"{{{\n"。在評論中禁止{(也許~是),或者要求大括號正確嵌套在評論中似乎是個好主意。
無論如何,這是一個完整的代碼示例供您擺弄:
{-# OPTIONS_GHC -Wall #-}
import Data.Char
import Data.Maybe
import Data.Void
import Text.Megaparsec
import Text.Megaparsec.Char
type Parser = Parsec Void String
data Comment = Comment { ident :: Integer, content :: String } deriving (Show)
nonTildes :: Parser String
nonTildes = takeWhileP (Just "non-tilde") (/= '~')
optionalComment :: Parser (Maybe Comment)
optionalComment = char '~' *>
optional (braces (Comment <$> ident_ <* char '-' <*> content_))
where
braces = between (char '{') (char '}')
ident_ = read <$> takeWhile1P (Just "digit") isDigit
content_ = takeWhileP Nothing (/= '}')
comments :: Parser [Comment]
comments = catMaybes <$> (nonTildes *> many (optionalComment <* nonTildes))
main :: IO ()
main = do
parseTest comments "hello world ~{1-sometext} bla bla ~{2-another comment}"
parseTest comments "~~~ ~~~{1-sometext} {junk}"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/526319.html
上一篇:從bs4腳本中獲取資料
