on master help sign in
Convert IdeMode.hs to use megaparsec
a817addJason Felice
idris.cabal+1
@@ -285,6 +285,7 @@ Library
285 , fingertree >= 0.1 && < 0.2
285 , fingertree >= 0.1 && < 0.2
286 , haskeline >= 0.7 && < 0.8
286 , haskeline >= 0.7 && < 0.8
287 , ieee754 >= 0.7 && < 0.9
287 , ieee754 >= 0.7 && < 0.9
288+ , megaparsec >= 6.2 && <6.3
288 , mtl >= 2.1 && < 2.3
289 , mtl >= 2.1 && < 2.3
289 , network < 2.7
290 , network < 2.7
290 , optparse-applicative >= 0.13 && < 0.15
291 , optparse-applicative >= 0.13 && < 0.15
src/Idris/IdeMode.hs+11−10
@@ -15,6 +15,7 @@ import Idris.Core.Binary ()
15import Idris.Core.TT
15import Idris.Core.TT
16
16
17import Control.Applicative hiding (Const)
17import Control.Applicative hiding (Const)
18+import Control.Arrow (left)
18import qualified Data.Binary as Binary
19import qualified Data.Binary as Binary
19import qualified Data.ByteString.Base64 as Base64
20import qualified Data.ByteString.Base64 as Base64
20import qualified Data.ByteString.Lazy as Lazy
21import qualified Data.ByteString.Lazy as Lazy
@@ -25,8 +26,8 @@ import qualified Data.Text as T
25import Numeric
26import Numeric
26import System.IO
27import System.IO
27import Text.Printf
28import Text.Printf
28−import qualified Text.Trifecta as P
29+import qualified Text.Megaparsec as P
29−import qualified Text.Trifecta.Delta as P
30+import qualified Text.Megaparsec.Char as P
30
31
31getNChar :: Handle -> Int -> String -> IO (String)
32getNChar :: Handle -> Int -> String -> IO (String)
32getNChar _ 0 s = return (reverse s)
33getNChar _ 0 s = return (reverse s)
@@ -205,28 +206,31 @@ escape = concatMap escapeChar
205 escapeChar '"' = "\\\""
206 escapeChar '"' = "\\\""
206 escapeChar c = [c]
207 escapeChar c = [c]
207
208
209+type Parser a = P.Parsec () String a
210+
211+pSExp :: Parser SExp
208pSExp = SexpList <$> P.between (P.char '(') (P.char ')') (pSExp `P.sepBy` (P.char ' '))
212pSExp = SexpList <$> P.between (P.char '(') (P.char ')') (pSExp `P.sepBy` (P.char ' '))
209 <|> atom
213 <|> atom
210
214
215+atom :: Parser SExp
211atom = SexpList [] <$ P.string "nil"
216atom = SexpList [] <$ P.string "nil"
212 <|> P.char ':' *> atomC
217 <|> P.char ':' *> atomC
213 <|> StringAtom <$> P.between (P.char '"') (P.char '"') (P.many quotedChar)
218 <|> StringAtom <$> P.between (P.char '"') (P.char '"') (P.many quotedChar)
214− <|> do ints <- some P.digit
219+ <|> do ints <- some P.digitChar
215 case readDec ints of
220 case readDec ints of
216 ((num, ""):_) -> return (IntegerAtom (toInteger num))
221 ((num, ""):_) -> return (IntegerAtom (toInteger num))
217 _ -> return (StringAtom ints)
222 _ -> return (StringAtom ints)
218
223
224+atomC :: Parser SExp
219atomC = BoolAtom True <$ P.string "True"
225atomC = BoolAtom True <$ P.string "True"
220 <|> BoolAtom False <$ P.string "False"
226 <|> BoolAtom False <$ P.string "False"
221 <|> SymbolAtom <$> many (P.noneOf " \n\t\r\"()")
227 <|> SymbolAtom <$> many (P.noneOf " \n\t\r\"()")
222
228
229+quotedChar :: Parser Char
223quotedChar = P.try ('\\' <$ P.string "\\\\")
230quotedChar = P.try ('\\' <$ P.string "\\\\")
224 <|> P.try ('"' <$ P.string "\\\"")
231 <|> P.try ('"' <$ P.string "\\\"")
225 <|> P.noneOf "\""
232 <|> P.noneOf "\""
226
233
227−parseSExp :: String -> P.Result SExp
228−parseSExp = P.parseString pSExp (P.Directed (UTF8.fromString "(unknown)") 0 0 0 0)
229−
230data Opt = ShowImpl | ErrContext deriving Show
234data Opt = ShowImpl | ErrContext deriving Show
231
235
232data WhatDocs = Overview | Full
236data WhatDocs = Overview | Full
@@ -321,10 +325,7 @@ parseMessage x = case receiveString x of
321 Left err -> Left err
325 Left err -> Left err
322
326
323receiveString :: String -> Either Err SExp
327receiveString :: String -> Either Err SExp
324−receiveString x =
328+receiveString = left (const $ Msg "parse failure") . P.parse pSExp "(unknown)"
325− case parseSExp x of
326− P.Failure _ -> Left . Msg $ "parse failure"
327− P.Success r -> Right r
328
329
329convSExp :: SExpable a => String -> a -> Integer -> String
330convSExp :: SExpable a => String -> a -> Integer -> String
330convSExp pre s id =
331convSExp pre s id =