Created
January 13, 2012 04:41
-
-
Save VoQn/1604725 to your computer and use it in GitHub Desktop.
Applicative Style Parsec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Applicative hiding ( (<|>) ) | |
import Text.Parsec | |
-- parse like ruby's String.strip | |
escSpace parser = spaces *> parser <* spaces | |
-- parse like "between" in Text.Parsec | |
close opener parser closer = char opener *> escSpace parser <* char closer | |
-- Example | |
-- "while loop data" expression as Functor | |
data WhileExp = While Cond [Proc] deriving ( Eq, Show ) | |
type Cond = (a -> Bool) | |
type Proc = (a -> ()) | |
-- "while loop parser" | |
whileExp = WhileExp <$> ( string "while" *> close '(' cond ')' ) <*> close '{' body '}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
今だったら Text.Parsec.Token 使ってこう書けるね
Parser.hs (do block Style)
do 文でやるとこう
Parser.hs (Applicative Style)
Applicative Style を取った方が「要はこういうことの結果をまとめてしまいたい」が表現しやすい
A a , B b , C c な型から a, b, c 型を取り出して、T a b c な型コンストラクタに渡したい時によく使う
話の流れで要るやつ
Syntax.hs
Lexer.hs