on master help sign in
Convert IdeMode.hs to use megaparsec
commita817addc31d205343129c0f85e4953a7f6f99652 authorJason Felice <jason.m.felice@gmail.com> dateThu 26 Oct 2017, 17:11 (9 years ago) parent0a7dcf0
2 files changed+12−10
idris.cabal+1 src/Idris/IdeMode.hs+11−10
idris.cabal+1
@@ -285,6 +285,7 @@ Library
285285 , fingertree >= 0.1 && < 0.2
286286 , haskeline >= 0.7 && < 0.8
287287 , ieee754 >= 0.7 && < 0.9
288+ , megaparsec >= 6.2 && <6.3
288289 , mtl >= 2.1 && < 2.3
289290 , network < 2.7
290291 , optparse-applicative >= 0.13 && < 0.15
src/Idris/IdeMode.hs+11−10
@@ -15,6 +15,7 @@ import Idris.Core.Binary ()
1515import Idris.Core.TT
1616
1717import Control.Applicative hiding (Const)
18+import Control.Arrow (left)
1819import qualified Data.Binary as Binary
1920import qualified Data.ByteString.Base64 as Base64
2021import qualified Data.ByteString.Lazy as Lazy
@@ -25,8 +26,8 @@ import qualified Data.Text as T
2526import Numeric
2627import System.IO
2728import Text.Printf
28−import qualified Text.Trifecta as P
29−import qualified Text.Trifecta.Delta as P
29+import qualified Text.Megaparsec as P
30+import qualified Text.Megaparsec.Char as P
3031
3132getNChar :: Handle -> Int -> String -> IO (String)
3233getNChar _ 0 s = return (reverse s)
@@ -205,28 +206,31 @@ escape = concatMap escapeChar
205206 escapeChar '"' = "\\\""
206207 escapeChar c = [c]
207208
209+type Parser a = P.Parsec () String a
210+
211+pSExp :: Parser SExp
208212pSExp = SexpList <$> P.between (P.char '(') (P.char ')') (pSExp `P.sepBy` (P.char ' '))
209213 <|> atom
210214
215+atom :: Parser SExp
211216atom = SexpList [] <$ P.string "nil"
212217 <|> P.char ':' *> atomC
213218 <|> StringAtom <$> P.between (P.char '"') (P.char '"') (P.many quotedChar)
214− <|> do ints <- some P.digit
219+ <|> do ints <- some P.digitChar
215220 case readDec ints of
216221 ((num, ""):_) -> return (IntegerAtom (toInteger num))
217222 _ -> return (StringAtom ints)
218223
224+atomC :: Parser SExp
219225atomC = BoolAtom True <$ P.string "True"
220226 <|> BoolAtom False <$ P.string "False"
221227 <|> SymbolAtom <$> many (P.noneOf " \n\t\r\"()")
222228
229+quotedChar :: Parser Char
223230quotedChar = P.try ('\\' <$ P.string "\\\\")
224231 <|> P.try ('"' <$ P.string "\\\"")
225232 <|> P.noneOf "\""
226233
227−parseSExp :: String -> P.Result SExp
228−parseSExp = P.parseString pSExp (P.Directed (UTF8.fromString "(unknown)") 0 0 0 0)
229−
230234data Opt = ShowImpl | ErrContext deriving Show
231235
232236data WhatDocs = Overview | Full
@@ -321,10 +325,7 @@ parseMessage x = case receiveString x of
321325 Left err -> Left err
322326
323327receiveString :: String -> Either Err SExp
324−receiveString x =
325− case parseSExp x of
326− P.Failure _ -> Left . Msg $ "parse failure"
327− P.Success r -> Right r
328+receiveString = left (const $ Msg "parse failure") . P.parse pSExp "(unknown)"
328329
329330convSExp :: SExpable a => String -> a -> Integer -> String
330331convSExp pre s id =