on de45d29 help sign in
Idris-dev/src/Idris/REPLParser.hs
215 lines 11 kB history raw
2module Idris.REPLParser(parseCmd) where
4import System.FilePath ((</>))
5import System.Console.ANSI (Color(..))
7import Idris.Colours
8import Idris.AbsSyntax
9import Idris.Core.TT
10import qualified Idris.Parser as P
12import Control.Applicative
13import Control.Monad.State.Strict
15import Text.Parser.Combinators
16import Text.Parser.Char(anyChar)
17import Text.Trifecta(Result, parseString)
18import Text.Trifecta.Delta
20import Debug.Trace
21import Data.List
22import Data.List.Split(splitOn)
23import Data.Char(toLower)
24import qualified Data.ByteString.UTF8 as UTF8
26parseCmd :: IState -> String -> String -> Result Command
27parseCmd i inputname = P.runparser pCmd i inputname
29cmd :: [String] -> P.IdrisParser ()
30cmd xs = try (do P.lchar ':'; docmd (sortBy (\x y -> compare (length y) (length x)) xs))
31 where docmd [] = fail "No such command"
32 docmd (x:xs) = try (discard (P.reserved x)) <|> docmd xs
34pCmd :: P.IdrisParser Command
36pCmd = do P.whiteSpace; do cmd ["q", "quit"]; eof; return Quit
37 <|> do cmd ["h", "?", "help"]; eof; return Help
38 <|> do cmd ["w", "warranty"]; eof; return Warranty
39 <|> do cmd ["r", "reload"]; eof; return Reload
40 <|> do cmd ["module"]; f <- P.identifier; eof;
41 return (ModImport (toPath f))
42 <|> do cmd ["e", "edit"]; eof; return Edit
43 <|> do cmd ["exec", "execute"]; eof; return Execute
44 <|> try (do cmd ["c", "compile"]
45 i <- get
46 f <- P.identifier
47 eof
48 return (Compile (Via "c") f))
49 <|> do cmd ["c", "compile"]
50 i <- get
51 c <- codegenOption
52 f <- P.identifier
53 eof
54 return (Compile c f)
55 <|> do cmd ["proofs"]; eof; return Proofs
56 <|> do cmd ["rmproof"]; n <- P.name; eof; return (RmProof n)
57 <|> do cmd ["showproof"]; n <- P.name; eof; return (ShowProof n)
58 <|> do cmd ["log"]; i <- P.natural; eof; return (LogLvl (fromIntegral i))
59 <|> do cmd ["let"]
60 defn <- concat <$> many (P.decl defaultSyntax)
61 return (NewDefn defn)
62 <|> do cmd ["unlet","undefine"]
63 Undefine `fmap` many P.name
64 <|> do cmd ["lto", "loadto"];
65 toline <- P.natural
66 f <- many anyChar;
67 return (Load f (Just (fromInteger toline)))
68 <|> do cmd ["l", "load"]; f <- many anyChar;
69 return (Load f Nothing)
70 <|> do cmd ["cd"]; f <- many anyChar; return (ChangeDirectory f)
71 <|> do cmd ["spec"]; P.whiteSpace; t <- P.fullExpr defaultSyntax; return (Spec t)
72 <|> do cmd ["hnf"]; P.whiteSpace; t <- P.fullExpr defaultSyntax; return (HNF t)
73 <|> do cmd ["inline"]; P.whiteSpace; t <- P.fullExpr defaultSyntax; return (TestInline t)
74 <|> do c <- try (cmd ["doc"] *> P.constant); eof; return (DocStr (Right c))
75 <|> do cmd ["doc"]; n <- (P.fnName <|> (P.string "_|_" >> return falseTy)); eof; return (DocStr (Left n))
76 <|> do cmd ["d", "def"]; P.whiteSpace; n <- P.fnName; eof; return (Defn n)
77 <|> do cmd ["total"]; do n <- P.fnName; eof; return (TotCheck n)
78 <|> do cmd ["t", "type"]; do P.whiteSpace; t <- P.fullExpr defaultSyntax; return (Check t)
79 <|> do cmd ["u", "universes"]; eof; return Universes
80 <|> do cmd ["di", "dbginfo"]; n <- P.fnName; eof; return (DebugInfo n)
81 <|> do cmd ["miss", "missing"]; n <- P.fnName; eof; return (Missing n)
82 <|> try (do cmd ["dynamic"]; eof; return ListDynamic)
83 <|> do cmd ["dynamic"]; l <- many anyChar; return (DynamicLink l)
84 <|> do cmd ["color", "colour"]; pSetColourCmd
85 <|> do cmd ["set"]; o <- pOption; return (SetOpt o)
86 <|> do cmd ["unset"]; o <- pOption; return (UnsetOpt o)
87 <|> do cmd ["s", "search"]; P.whiteSpace;
88 t <- P.typeExpr (defaultSyntax { implicitAllowed = True }); return (Search t)
89 <|> do cmd ["cs", "casesplit"]; P.whiteSpace;
90 upd <- option False (do P.lchar '!'; return True)
91 l <- P.natural; n <- P.name;
92 return (CaseSplitAt upd (fromInteger l) n)
93 <|> do cmd ["apc", "addproofclause"]; P.whiteSpace;
94 upd <- option False (do P.lchar '!'; return True)
95 l <- P.natural; n <- P.name;
96 return (AddProofClauseFrom upd (fromInteger l) n)
97 <|> do cmd ["ac", "addclause"]; P.whiteSpace;
98 upd <- option False (do P.lchar '!'; return True)
99 l <- P.natural; n <- P.name;
100 return (AddClauseFrom upd (fromInteger l) n)
101 <|> do cmd ["am", "addmissing"]; P.whiteSpace;
102 upd <- option False (do P.lchar '!'; return True)
103 l <- P.natural; n <- P.name;
104 return (AddMissing upd (fromInteger l) n)
105 <|> do cmd ["mw", "makewith"]; P.whiteSpace;
106 upd <- option False (do P.lchar '!'; return True)
107 l <- P.natural; n <- P.name;
108 return (MakeWith upd (fromInteger l) n)
109 <|> do cmd ["ml", "makelemma"]; P.whiteSpace;
110 upd <- option False (do P.lchar '!'; return True)
111 l <- P.natural; n <- P.name;
112 return (MakeLemma upd (fromInteger l) n)
113 <|> do cmd ["ps", "proofsearch"]; P.whiteSpace;
114 upd <- option False (do P.lchar '!'; return True)
115 l <- P.natural; n <- P.name;
116 hints <- many P.fnName
117 return (DoProofSearch upd True (fromInteger l) n hints)
118 <|> do cmd ["ref", "refine"]; P.whiteSpace;
119 upd <- option False (do P.lchar '!'; return True)
120 l <- P.natural; n <- P.name;
121 hint <- P.fnName
122 return (DoProofSearch upd False (fromInteger l) n [hint])
123 <|> do cmd ["p", "prove"]; n <- P.name; eof; return (Prove n)
124 <|> do cmd ["m", "metavars"]; eof; return Metavars
125 <|> do cmd ["a", "addproof"]; do n <- option Nothing (do x <- P.name;
126 return (Just x))
127 eof; return (AddProof n)
128 <|> do cmd ["x"]; P.whiteSpace; t <- P.fullExpr defaultSyntax; return (ExecVal t)
129 <|> do cmd ["patt"]; P.whiteSpace; t <- P.fullExpr defaultSyntax; return (Pattelab t)
130 <|> do cmd ["errorhandlers"]; eof ; return ListErrorHandlers
131 <|> do cmd ["consolewidth"]; w <- pConsoleWidth ; return (SetConsoleWidth w)
132 <|> do cmd ["apropos"]; str <- many anyChar ; return (Apropos str)
133 <|> do cmd ["wc", "whocalls"]; P.whiteSpace; n <- P.fnName ; return (WhoCalls n)
134 <|> do cmd ["cw", "callswho"]; P.whiteSpace; n <- P.fnName ; return (CallsWho n)
135 <|> do cmd ["mkdoc"]; str <- many anyChar; return (MakeDoc str)
136 <|> do cmd ["printdef"]; P.whiteSpace; n <- P.fnName; return (PrintDef n)
137 <|> do P.whiteSpace; do eof; return NOP
138 <|> do t <- P.fullExpr defaultSyntax; return (Eval t)
140 where toPath n = foldl1' (</>) $ splitOn "." n
142pOption :: P.IdrisParser Opt
143pOption = do discard (P.symbol "errorcontext"); return ErrContext
144 <|> do discard (P.symbol "showimplicits"); return ShowImpl
145 <|> do discard (P.symbol "originalerrors"); return ShowOrigErr
146 <|> do discard (P.symbol "autosolve"); return AutoSolve
147 <|> do discard (P.symbol "nobanner") ; return NoBanner
148 <|> do discard (P.symbol "warnreach"); return WarnReach
150codegenOption :: P.IdrisParser Codegen
151codegenOption = do discard (P.symbol "bytecode"); return Bytecode
152 <|> do x <- P.identifier
153 return (Via (map toLower x))
155pConsoleWidth :: P.IdrisParser ConsoleWidth
156pConsoleWidth = do discard (P.symbol "auto"); return AutomaticWidth
157 <|> do discard (P.symbol "infinite"); return InfinitelyWide
158 <|> do n <- fmap fromInteger P.natural; return (ColsWide n)
160colours :: [(String, Maybe Color)]
161colours = [ ("black", Just Black)
162 , ("red", Just Red)
163 , ("green", Just Green)
164 , ("yellow", Just Yellow)
165 , ("blue", Just Blue)
166 , ("magenta", Just Magenta)
167 , ("cyan", Just Cyan)
168 , ("white", Just White)
169 , ("default", Nothing)
170 ]
172pColour :: P.IdrisParser (Maybe Color)
173pColour = doColour colours
174 where doColour [] = fail "Unknown colour"
175 doColour ((s, c):cs) = (try (P.symbol s) >> return c) <|> doColour cs
177pColourMod :: P.IdrisParser (IdrisColour -> IdrisColour)
178pColourMod = try (P.symbol "vivid" >> return doVivid)
179 <|> try (P.symbol "dull" >> return doDull)
180 <|> try (P.symbol "underline" >> return doUnderline)
181 <|> try (P.symbol "nounderline" >> return doNoUnderline)
182 <|> try (P.symbol "bold" >> return doBold)
183 <|> try (P.symbol "nobold" >> return doNoBold)
184 <|> try (P.symbol "italic" >> return doItalic)
185 <|> try (P.symbol "noitalic" >> return doNoItalic)
186 <|> try (pColour >>= return . doSetColour)
187 where doVivid i = i { vivid = True }
188 doDull i = i { vivid = False }
189 doUnderline i = i { underline = True }
190 doNoUnderline i = i { underline = False }
191 doBold i = i { bold = True }
192 doNoBold i = i { bold = False }
193 doItalic i = i { italic = True }
194 doNoItalic i = i { italic = False }
195 doSetColour c i = i { colour = c }
197-- | Generate the colour type names using the default Show instance.
198colourTypes :: [(String, ColourType)]
199colourTypes = map (\x -> ((map toLower . reverse . drop 6 . reverse . show) x, x)) $
200 enumFromTo minBound maxBound
202pColourType :: P.IdrisParser ColourType
203pColourType = doColourType colourTypes
204 where doColourType [] = fail $ "Unknown colour category. Options: " ++
205 (concat . intersperse ", " . map fst) colourTypes
206 doColourType ((s,ct):cts) = (try (P.symbol s) >> return ct) <|> doColourType cts
208pSetColourCmd :: P.IdrisParser Command
209pSetColourCmd = (do c <- pColourType
210 let defaultColour = IdrisColour Nothing True False False False
211 opts <- sepBy pColourMod (P.whiteSpace)
212 let colour = foldr ($) defaultColour $ reverse opts
213 return $ SetColour c colour)
214 <|> try (P.symbol "on" >> return ColourOn)
215 <|> try (P.symbol "off" >> return ColourOff)