我會寫
integer :: Parser Integer
integer = read <$ many1 space <*> many1 digit
有一堆左結合(如應用程序)語法分析器建設運營<$>
,<*>
,<$
,<*
的。最左邊的東西應該是從組件值中彙集結果值的純函數。每個運算符右側的內容應該是一個解析器,將語法的組成部分從左到右集中起來。使用哪個運營商取決於兩個選擇,如下所示。
the thing to the right is signal/noise
_________________________
the thing to the left is \
+-------------------
pure/| <$> <$
a parser | <*> <*
所以,既然選擇read :: String -> Integer
作爲將要遞送解析器的語義的純函數,我們可以前導間隔作爲「噪聲」和數字的一串爲「信號」進行分類,因此
read <$ many1 space <*> many1 digit
(..) (.........) (.........)
pure noise parser |
(.................) |
parser signal parser
(.................................)
parser
您可以
p1 <|> ... <|> pn
和快速不可能結合的多種可能性與
empty
很少有必要在解析器中命名組件,並且生成的代碼看起來更像是帶有添加語義的語法。
爲什麼'const'? – MathematicalOrchid 2013-02-28 19:30:29
我們希望忽略'many1 space'的值(但不是效果),並將'read'應用於'many1 digit'的值。 (對不起,我剛剛進來,已經很晚了,我很疲倦:我用術語玩得很快而且鬆散。)如果你想象's'和'd'代表'many1 space'和' many1 digit',那麼'const讀取的值(忽略效果)<$> many1 space <*> many1 digit'是'const read sd' ='read d'。 – dave4420 2013-02-28 22:33:11