@@ -1,12 +1,30 @@
11{-# LANGUAGE DeriveGeneric, CPP #-}
2−module TutorialD.Interpreter.Base where
2+module TutorialD.Interpreter.Base (
3+ module TutorialD.Interpreter.Base,
4+ module Text.Megaparsec,
5+#if MIN_VERSION_megaparsec(6,0,0)
6+ module Text.Megaparsec.Char,
7+ module Control.Applicative
8+#else
9+ module Text.Megaparsec.Text
10+#endif
11+ )
12+ where
313import ProjectM36.Base
414import ProjectM36.AtomType
515import ProjectM36.Relation
616
17+#if MIN_VERSION_megaparsec(6,0,0)
18+import Text.Megaparsec.Char
19+import qualified Text.Megaparsec.Char.Lexer as Lex
720import Text.Megaparsec
21+import Data.Void
22+import Control.Applicative
23+#else
824import Text.Megaparsec.Text
925import qualified Text.Megaparsec.Lexer as Lex
26+#endif
27+
1028import Data.Text hiding (count)
1129import System.Random
1230import qualified Data.Text as T
@@ -42,16 +60,23 @@ displayOpResult (DisplayParseErrorResult mPromptLength err) = do
4260 maybe (pure ()) (TIO.putStrLn . pointyString) mPromptLength
4361 TIO.putStr ("ERR:" <> errString)
4462
63+#if MIN_VERSION_megaparsec(6,0,0)
64+type Parser = Parsec Void Text
65+type ParseStr = Text
66+#else
67+type ParseStr = String
68+#endif
69+
4570spaceConsumer :: Parser ()
4671spaceConsumer = Lex.space (void spaceChar) (Lex.skipLineComment "--") (Lex.skipBlockComment "{-" "-}")
4772
4873opChar :: Parser Char
4974opChar = oneOf (":!#$%&*+./<=>?\\^|-~" :: String)-- remove "@" so it can be used as attribute marker without spaces
5075
51−reserved :: String -> Parser ()
76+reserved :: ParseStr -> Parser ()
5277reserved word = try (string word *> notFollowedBy opChar *> spaceConsumer)
5378
54−reservedOp :: String -> Parser ()
79+reservedOp :: ParseStr -> Parser ()
5580reservedOp op = try (spaceConsumer *> string op *> notFollowedBy opChar *> spaceConsumer)
5681
5782parens :: Parser a -> Parser a
@@ -67,8 +92,12 @@ identifier = do
6792 spaceConsumer
6893 pure (pack (istart:irest))
6994
70−symbol :: String -> Parser Text
95+symbol :: ParseStr -> Parser Text
96+#if MIN_VERSION_megaparsec(6,0,0)
97+symbol sym = Lex.symbol spaceConsumer sym
98+#else
7199symbol sym = pack <$> Lex.symbol spaceConsumer sym
100+#endif
72101
73102comma :: Parser Text
74103comma = symbol ","
@@ -94,7 +123,11 @@ whiteSpace = Token.whiteSpace lexer
94123-}
95124
96125integer :: Parser Integer
126+#if MIN_VERSION_megaparsec(6,0,0)
127+integer = Lex.decimal
128+#else
97129integer = Lex.integer
130+#endif
98131
99132float :: Parser Double
100133float = Lex.float
@@ -123,12 +156,18 @@ showRelationAttributes attrs = "{" <> T.concat (L.intersperse ", " $ L.map showA
123156
124157type PromptLength = Int
125158
159+#if MIN_VERSION_megaparsec(6,0,0)
160+type ParserError = ParseError Char Void
161+#else
162+type ParserError = ParseError Char Dec
163+#endif
164+
126165data TutorialDOperatorResult = QuitResult |
127166 DisplayResult StringType |
128167 DisplayIOResult (IO ()) |
129168 DisplayRelationResult Relation |
130169 DisplayErrorResult StringType |
131− DisplayParseErrorResult (Maybe PromptLength) (ParseError Char Dec) | -- Int refers to length of prompt text
170+ DisplayParseErrorResult (Maybe PromptLength) ParserError | -- PromptLength refers to length of prompt text
132171 QuietSuccessResult
133172 deriving (Generic)
134173