summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-04-24 22:16:54 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-04-24 22:16:54 +0200
commit8f5499c4df960e82e44759a1368028ef5a30be33 (patch)
tree2bdbc1df7a0ede94c8842640e7c25754e46f88cb
parentf473b5ed7cdf46c764e8a77edd8333487c76f31d (diff)
switch to megaparsec-4.4; use RWS monad instead of Reader+Writer+State
-rw-r--r--lambdacube-compiler.cabal2
-rw-r--r--src/LambdaCube/Compiler/Lexer.hs25
2 files changed, 24 insertions, 3 deletions
diff --git a/lambdacube-compiler.cabal b/lambdacube-compiler.cabal
index d5b43e82..79dc8f42 100644
--- a/lambdacube-compiler.cabal
+++ b/lambdacube-compiler.cabal
@@ -78,7 +78,7 @@ library
78 exceptions >= 0.8 && <0.9, 78 exceptions >= 0.8 && <0.9,
79 filepath, 79 filepath,
80 mtl >=2.2 && <2.3, 80 mtl >=2.2 && <2.3,
81 megaparsec >= 4.3.0 && <4.5, 81 megaparsec >= 4.4.0 && <4.5,
82 wl-pprint >=1.2 && <1.3, 82 wl-pprint >=1.2 && <1.3,
83 pretty-show >= 1.6.9, 83 pretty-show >= 1.6.9,
84 text >= 1.2 && <1.3, 84 text >= 1.2 && <1.3,
diff --git a/src/LambdaCube/Compiler/Lexer.hs b/src/LambdaCube/Compiler/Lexer.hs
index e0aeece2..9f162c31 100644
--- a/src/LambdaCube/Compiler/Lexer.hs
+++ b/src/LambdaCube/Compiler/Lexer.hs
@@ -2,6 +2,9 @@
2{-# LANGUAGE ViewPatterns #-} 2{-# LANGUAGE ViewPatterns #-}
3{-# LANGUAGE PatternSynonyms #-} 3{-# LANGUAGE PatternSynonyms #-}
4{-# LANGUAGE FlexibleContexts #-} 4{-# LANGUAGE FlexibleContexts #-}
5{-# LANGUAGE FlexibleInstances #-}
6{-# LANGUAGE UndecidableInstances #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
5{-# LANGUAGE NoMonomorphismRestriction #-} 8{-# LANGUAGE NoMonomorphismRestriction #-}
6{-# LANGUAGE OverloadedStrings #-} 9{-# LANGUAGE OverloadedStrings #-}
7module LambdaCube.Compiler.Lexer 10module LambdaCube.Compiler.Lexer
@@ -28,6 +31,7 @@ import Control.DeepSeq
28 31
29import Text.Megaparsec hiding (State) 32import Text.Megaparsec hiding (State)
30import qualified Text.Megaparsec as P 33import qualified Text.Megaparsec as P
34import qualified Text.Megaparsec.Prim as P
31import Text.Megaparsec as ParseUtils hiding (try, Message, State) 35import Text.Megaparsec as ParseUtils hiding (try, Message, State)
32import Text.Megaparsec.Lexer hiding (lexeme, symbol, negate) 36import Text.Megaparsec.Lexer hiding (lexeme, symbol, negate)
33 37
@@ -46,6 +50,22 @@ manyNM k n p = (:) <$> p <*> manyNM (k-1) (n-1) p
46-- see http://blog.ezyang.com/2014/05/parsec-try-a-or-b-considered-harmful/comment-page-1/#comment-6602 50-- see http://blog.ezyang.com/2014/05/parsec-try-a-or-b-considered-harmful/comment-page-1/#comment-6602
47try_ s m = try m <?> s 51try_ s m = try m <?> s
48 52
53instance (Monoid w, P.MonadParsec st m t) => P.MonadParsec st (RWST r w s m) t where
54 failure = lift . failure
55 label n (RWST m) = RWST $ \r s -> label n $ m r s
56 try (RWST m) = RWST $ \r s -> try $ m r s
57 lookAhead (RWST m) = RWST $ \r s ->
58 (\(a, _, _) -> (a, s, mempty)) <$> lookAhead (m r s)
59 notFollowedBy (RWST m) = RWST $ \r s ->
60 notFollowedBy ((\(a, _, _) -> a) <$> m r s) >> return ((), s, mempty)
61 withRecovery rec (RWST m) = RWST $ \r s ->
62 withRecovery (\e -> runRWST (rec e) r s) (m r s)
63 eof = lift eof
64 token f e = lift $ token f e
65 tokens f e ts = lift $ tokens f e ts
66 getParserState = lift getParserState
67 updateParserState f = lift $ updateParserState f
68
49-------------------------------------------------------------------------------- literals 69-------------------------------------------------------------------------------- literals
50 70
51data Lit 71data Lit
@@ -239,10 +259,11 @@ type ParseState r = (ParseEnv r, P.State String)
239parseState :: FileInfo -> r -> ParseState r 259parseState :: FileInfo -> r -> ParseState r
240parseState fi di = (ParseEnv fi di ExpNS (SPos 0 0), either (error "impossible") id $ runParser getParserState (filePath fi) (fileContent fi)) 260parseState fi di = (ParseEnv fi di ExpNS (SPos 0 0), either (error "impossible") id $ runParser getParserState (filePath fi) (fileContent fi))
241 261
242type Parse r w = ReaderT (ParseEnv r) (WriterT [w] (StateT SPos (Parsec String))) 262--type Parse r w = ReaderT (ParseEnv r) (WriterT [w] (StateT SPos (Parsec String)))
263type Parse r w = RWST (ParseEnv r) [w] SPos (Parsec String)
243 264
244runParse :: Parse r w a -> ParseState r -> Either ParseError (a, [w]) 265runParse :: Parse r w a -> ParseState r -> Either ParseError (a, [w])
245runParse p (env, st) = snd . flip runParser' st . flip evalStateT (error "spos") . runWriterT . flip runReaderT env $ p 266runParse p (env, st) = snd . flip runParser' st $ evalRWST p env (error "spos")
246 267
247getParseState :: Parse r w (ParseState r) 268getParseState :: Parse r w (ParseState r)
248getParseState = (,) <$> ask <*> getParserState 269getParseState = (,) <$> ask <*> getParserState