1{-# LANGUAGE DeriveGeneric, CPP #-}
2module 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
9 module Text.Megaparsec.Text
14import ProjectM36.AtomType
15import ProjectM36.Relation
16import ProjectM36.DataFrame
18#if MIN_VERSION_megaparsec(6,0,0)
19import Text.Megaparsec.Char
20import qualified Text.Megaparsec.Char.Lexer as Lex
23import Control.Applicative hiding (many, some)
25import Text.Megaparsec.Text
26import qualified Text.Megaparsec.Lexer as Lex
29import Data.Text hiding (count)
31import qualified Data.Text as T
32import qualified Data.List as L
33import qualified Data.Vector as V
34import qualified Data.Text.IO as TIO
36import ProjectM36.Relation.Show.Term
39import qualified Data.UUID as U
40import Control.Monad.Random
41import Data.List.NonEmpty as NE
43import Data.Time.Format
44import Control.Monad (void)
46displayOpResult :: TutorialDOperatorResult -> IO ()
47displayOpResult QuitResult = return ()
48displayOpResult (DisplayResult out) = TIO.putStrLn out
49displayOpResult (DisplayIOResult ioout) = ioout
50displayOpResult (DisplayErrorResult err) = let outputf = if T.length err > 0 && T.last err /= '\n' then TIO.hPutStrLn else TIO.hPutStr in
51 outputf stderr ("ERR: " <> err)
52displayOpResult QuietSuccessResult = return ()
53displayOpResult (DisplayRelationResult rel) = do
55 let randomlySortedRel = evalRand (randomizeTupleOrder rel) gen
56 TIO.putStrLn (showRelation randomlySortedRel)
57displayOpResult (DisplayParseErrorResult mPromptLength err) = do
58 let errString = T.pack (parseErrorPretty err)
59 errorIndent = unPos (sourceColumn (NE.head (errorPos err)))
60 pointyString len = T.justifyRight (len + fromIntegral errorIndent) '_' "^"
61 maybe (pure ()) (TIO.putStrLn . pointyString) mPromptLength
62 TIO.putStr ("ERR:" <> errString)
63displayOpResult (DisplayDataFrameResult dFrame) = TIO.putStrLn (showDataFrame dFrame)
65#if MIN_VERSION_megaparsec(6,0,0)
66type Parser = Parsec Void Text
72spaceConsumer :: Parser ()
73spaceConsumer = Lex.space (void spaceChar) (Lex.skipLineComment "--") (Lex.skipBlockComment "{-" "-}")
76opChar = oneOf (":!#$%&*+./<=>?\\^|-~" :: String)-- remove "@" so it can be used as attribute marker without spaces
78reserved :: ParseStr -> Parser ()
79reserved word = try (string word *> notFollowedBy opChar *> spaceConsumer)
81reservedOp :: ParseStr -> Parser ()
82reservedOp op = try (spaceConsumer *> string op *> notFollowedBy opChar *> spaceConsumer)
84parens :: Parser a -> Parser a
85parens = between (symbol "(") (symbol ")")
87braces :: Parser a -> Parser a
88braces = between (symbol "{") (symbol "}")
90identifier :: Parser Text
92 istart <- letterChar <|> char '_'
93 irest <- many (alphaNumChar <|> char '_' <|> char '#')
95 pure (pack (istart:irest))
97symbol :: ParseStr -> Parser Text
98#if MIN_VERSION_megaparsec(6,0,0)
99symbol sym = Lex.symbol spaceConsumer sym
101symbol sym = pack <$> Lex.symbol spaceConsumer sym
113tripleQuote :: Parser Text
114tripleQuote = symbol "\"\"\""
122integer :: Parser Integer
123#if MIN_VERSION_megaparsec(6,0,0)
124integer = Lex.signed spaceConsumer Lex.decimal
126integer = Lex.signed spaceConsumer Lex.integer
129natural :: Parser Integer
130#if MIN_VERSION_megaparsec(6,0,0)
131natural = Lex.decimal <* spaceConsumer
133natural = Lex.integer <* spaceConsumer
136float :: Parser Double
139capitalizedIdentifier :: Parser Text
140capitalizedIdentifier = do
142 restOfIdentifier_ fletter
144restOfIdentifier_ :: Char -> Parser Text
145restOfIdentifier_ fletter = do
146 rest <- option "" identifier
148 pure (T.cons fletter rest)
150uncapitalizedIdentifier :: Parser Text
151uncapitalizedIdentifier = do
153 restOfIdentifier_ fletter
155showRelationAttributes :: Attributes -> Text
156showRelationAttributes attrs = "{" <> T.concat (L.intersperse ", " $ L.map showAttribute attrsL) <> "}"
158 showAttribute (Attribute name atomType) = name <> " " <> prettyAtomType atomType
159 attrsL = V.toList attrs
161type PromptLength = Int
163#if MIN_VERSION_megaparsec(6,0,0)
164type ParserError = ParseError Char Void
166type ParserError = ParseError Char Dec
169data TutorialDOperatorResult = QuitResult |
170 DisplayResult StringType |
171 DisplayIOResult (IO ()) |
172 DisplayRelationResult Relation |
173 DisplayDataFrameResult DataFrame |
174 DisplayErrorResult StringType |
175 DisplayParseErrorResult (Maybe PromptLength) ParserError | -- PromptLength refers to length of prompt text
179type TransactionGraphWasUpdated = Bool
181--allow for python-style triple quoting because guessing the correct amount of escapes in different contexts is annoying
182tripleQuotedString :: Parser Text
183tripleQuotedString = do
185 pack <$> manyTill anyChar (try (tripleQuote >> notFollowedBy quote))
187normalQuotedString :: Parser Text
188normalQuotedString = quote *> (T.pack <$> manyTill Lex.charLiteral quote)
190quotedString :: Parser Text
191quotedString = try tripleQuotedString <|> normalQuotedString
193uuidP :: Parser U.UUID
195 uuidStart <- count 8 hexDigitChar
196 _ <- char '-' -- min 28 with no dashes, maximum 4 dashes
197 uuidMid1 <- count 4 hexDigitChar
199 uuidMid2 <- count 4 hexDigitChar
201 uuidMid3 <- count 4 hexDigitChar
203 uuidEnd <- count 12 hexDigitChar
204 let uuidStr = L.intercalate "-" [uuidStart, uuidMid1, uuidMid2, uuidMid3, uuidEnd]
205 case U.fromString uuidStr of
206 Nothing -> fail "Invalid uuid string"
207 Just uuid -> return uuid
209utcTimeP :: Parser UTCTime
211 timeStr <- quotedString
212 case parseTimeM True defaultTimeLocale "%Y-%m-%d %H:%M:%S" (T.unpack timeStr) of
213 Nothing -> fail "invalid datetime input, use \"YYYY-MM-DD HH:MM:SS\""
214 Just stamp -> pure stamp
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list