2module Idris.REPLParser(parseCmd) where
4import System.FilePath ((</>))
5import System.Console.ANSI (Color(..))
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
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 = 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.symbol x)) <|> docmd xs
34pCmd :: P.IdrisParser Command
35pCmd = do P.whiteSpace; try (do cmd ["q", "quit"]; eof; return Quit)
36 <|> try (do cmd ["h", "?", "help"]; eof; return Help)
37 <|> try (do cmd ["w", "warranty"]; eof; return Warranty)
38 <|> try (do cmd ["r", "reload"]; eof; return Reload)
39 <|> try (do cmd ["module"]; f <- P.identifier; eof;
40 return (ModImport (toPath f)))
41 <|> try (do cmd ["e", "edit"]; eof; return Edit)
42 <|> try (do cmd ["exec", "execute"]; eof; return Execute)
43 <|> try (do cmd ["c", "compile"]
45 c <- option (opt_codegen $ idris_options i) codegenOption
49 <|> try (do cmd ["proofs"]; eof; return Proofs)
50 <|> try (do cmd ["rmproof"]; n <- P.name; eof; return (RmProof n))
51 <|> try (do cmd ["showproof"]; n <- P.name; eof; return (ShowProof n))
52 <|> try (do cmd ["log"]; i <- P.natural; eof; return (LogLvl (fromIntegral i)))
53 <|> try (do cmd ["let"]
54 defn <- concat <$> many (P.decl defaultSyntax)
55 return (NewDefn defn))
56 <|> try (do cmd ["unlet","undefine"]
57 Undefine `fmap` many P.name)
58 <|> try (do cmd ["lto", "loadto"];
61 return (Load f (Just (fromInteger toline))))
62 <|> try (do cmd ["l", "load"]; f <- many anyChar;
63 return (Load f Nothing))
64 <|> try (do cmd ["cd"]; f <- many anyChar; return (ChangeDirectory f))
65 <|> try (do cmd ["spec"]; P.whiteSpace; t <- P.fullExpr defaultSyntax; return (Spec t))
66 <|> try (do cmd ["hnf"]; P.whiteSpace; t <- P.fullExpr defaultSyntax; return (HNF t))
67 <|> try (do cmd ["inline"]; P.whiteSpace; t <- P.fullExpr defaultSyntax; return (TestInline t))
68 <|> try (do cmd ["doc"]; c <- P.constant; eof; return (DocStr (Right c)))
69 <|> try (do cmd ["doc"]; n <- (P.fnName <|> (P.string "_|_" >> return falseTy)); eof; return (DocStr (Left n)))
70 <|> try (do cmd ["d", "def"]; P.whiteSpace; n <- P.fnName; eof; return (Defn n))
71 <|> try (do cmd ["total"]; do n <- P.fnName; eof; return (TotCheck n))
72 <|> try (do cmd ["t", "type"]; do P.whiteSpace; t <- P.fullExpr defaultSyntax; return (Check t))
73 <|> try (do cmd ["u", "universes"]; eof; return Universes)
74 <|> try (do cmd ["di", "dbginfo"]; n <- P.fnName; eof; return (DebugInfo n))
75 <|> try (do cmd ["miss", "missing"]; n <- P.fnName; eof; return (Missing n))
76 <|> try (do cmd ["dynamic"]; eof; return ListDynamic)
77 <|> try (do cmd ["dynamic"]; l <- many anyChar; return (DynamicLink l))
78 <|> try (do cmd ["color", "colour"]; pSetColourCmd)
79 <|> try (do cmd ["set"]; o <- pOption; return (SetOpt o))
80 <|> try (do cmd ["unset"]; o <- pOption; return (UnsetOpt o))
81 <|> try (do cmd ["s", "search"]; P.whiteSpace;
82 t <- P.typeExpr (defaultSyntax { implicitAllowed = True }); return (Search t))
83 <|> try (do cmd ["cs", "casesplit"]; P.whiteSpace;
84 upd <- option False (do P.lchar '!'; return True)
85 l <- P.natural; n <- P.name;
86 return (CaseSplitAt upd (fromInteger l) n))
87 <|> try (do cmd ["apc", "addproofclause"]; P.whiteSpace;
88 upd <- option False (do P.lchar '!'; return True)
89 l <- P.natural; n <- P.name;
90 return (AddProofClauseFrom upd (fromInteger l) n))
91 <|> try (do cmd ["ac", "addclause"]; P.whiteSpace;
92 upd <- option False (do P.lchar '!'; return True)
93 l <- P.natural; n <- P.name;
94 return (AddClauseFrom upd (fromInteger l) n))
95 <|> try (do cmd ["am", "addmissing"]; P.whiteSpace;
96 upd <- option False (do P.lchar '!'; return True)
97 l <- P.natural; n <- P.name;
98 return (AddMissing upd (fromInteger l) n))
99 <|> try (do cmd ["mw", "makewith"]; P.whiteSpace;
100 upd <- option False (do P.lchar '!'; return True)
101 l <- P.natural; n <- P.name;
102 return (MakeWith upd (fromInteger l) n))
103 <|> try (do cmd ["ml", "makelemma"]; P.whiteSpace;
104 upd <- option False (do P.lchar '!'; return True)
105 l <- P.natural; n <- P.name;
106 return (MakeLemma upd (fromInteger l) n))
107 <|> try (do cmd ["ps", "proofsearch"]; P.whiteSpace;
108 upd <- option False (do P.lchar '!'; return True)
109 l <- P.natural; n <- P.name;
110 hints <- many P.fnName
111 return (DoProofSearch upd True (fromInteger l) n hints))
112 <|> try (do cmd ["ref", "refine"]; P.whiteSpace;
113 upd <- option False (do P.lchar '!'; return True)
114 l <- P.natural; n <- P.name;
116 return (DoProofSearch upd False (fromInteger l) n [hint]))
117 <|> try (do cmd ["p", "prove"]; n <- P.name; eof; return (Prove n))
118 <|> try (do cmd ["m", "metavars"]; eof; return Metavars)
119 <|> try (do cmd ["a", "addproof"]; do n <- option Nothing (do x <- P.name;
121 eof; return (AddProof n))
122 <|> try (do cmd ["x"]; P.whiteSpace; t <- P.fullExpr defaultSyntax; return (ExecVal t))
123 <|> try (do cmd ["patt"]; P.whiteSpace; t <- P.fullExpr defaultSyntax; return (Pattelab t))
124 <|> try (do cmd ["errorhandlers"]; eof ; return ListErrorHandlers)
125 <|> try (do cmd ["consolewidth"]; w <- pConsoleWidth ; return (SetConsoleWidth w))
126 <|> try (do cmd ["apropos"]; str <- many anyChar ; return (Apropos str))
127 <|> try (do cmd ["wc", "whocalls"]; P.whiteSpace; n <- P.fnName ; return (WhoCalls n))
128 <|> try (do cmd ["cw", "callswho"]; P.whiteSpace; n <- P.fnName ; return (CallsWho n))
129 <|> try (do cmd ["mkdoc"]; str <- many anyChar; return (MakeDoc str))
130 <|> do P.whiteSpace; do eof; return NOP
131 <|> do t <- P.fullExpr defaultSyntax; return (Eval t)
133 where toPath n = foldl1' (</>) $ splitOn "." n
135pOption :: P.IdrisParser Opt
136pOption = do discard (P.symbol "errorcontext"); return ErrContext
137 <|> do discard (P.symbol "showimplicits"); return ShowImpl
138 <|> do discard (P.symbol "originalerrors"); return ShowOrigErr
139 <|> do discard (P.symbol "autosolve"); return AutoSolve
140 <|> do discard (P.symbol "nobanner") ; return NoBanner
141 <|> do discard (P.symbol "warnreach"); return WarnReach
143codegenOption :: P.IdrisParser Codegen
144codegenOption = do discard (P.symbol "javascript"); return ViaJavaScript
145 <|> do discard (P.symbol "node"); return ViaNode
146 <|> do discard (P.symbol "Java"); return ViaJava
147 <|> do discard (P.symbol "llvm"); return ViaLLVM
148 <|> do discard (P.symbol "bytecode"); return Bytecode
149 <|> do discard (P.symbol "C"); return ViaC
151pConsoleWidth :: P.IdrisParser ConsoleWidth
152pConsoleWidth = do discard (P.symbol "auto"); return AutomaticWidth
153 <|> do discard (P.symbol "infinite"); return InfinitelyWide
154 <|> do n <- fmap fromInteger P.natural; return (ColsWide n)
156colours :: [(String, Maybe Color)]
157colours = [ ("black", Just Black)
159 , ("green", Just Green)
160 , ("yellow", Just Yellow)
161 , ("blue", Just Blue)
162 , ("magenta", Just Magenta)
163 , ("cyan", Just Cyan)
164 , ("white", Just White)
165 , ("default", Nothing)
168pColour :: P.IdrisParser (Maybe Color)
169pColour = doColour colours
170 where doColour [] = fail "Unknown colour"
171 doColour ((s, c):cs) = (try (P.symbol s) >> return c) <|> doColour cs
173pColourMod :: P.IdrisParser (IdrisColour -> IdrisColour)
174pColourMod = try (P.symbol "vivid" >> return doVivid)
175 <|> try (P.symbol "dull" >> return doDull)
176 <|> try (P.symbol "underline" >> return doUnderline)
177 <|> try (P.symbol "nounderline" >> return doNoUnderline)
178 <|> try (P.symbol "bold" >> return doBold)
179 <|> try (P.symbol "nobold" >> return doNoBold)
180 <|> try (P.symbol "italic" >> return doItalic)
181 <|> try (P.symbol "noitalic" >> return doNoItalic)
182 <|> try (pColour >>= return . doSetColour)
183 where doVivid i = i { vivid = True }
184 doDull i = i { vivid = False }
185 doUnderline i = i { underline = True }
186 doNoUnderline i = i { underline = False }
187 doBold i = i { bold = True }
188 doNoBold i = i { bold = False }
189 doItalic i = i { italic = True }
190 doNoItalic i = i { italic = False }
191 doSetColour c i = i { colour = c }
193-- | Generate the colour type names using the default Show instance.
194colourTypes :: [(String, ColourType)]
195colourTypes = map (\x -> ((map toLower . reverse . drop 6 . reverse . show) x, x)) $
196 enumFromTo minBound maxBound
198pColourType :: P.IdrisParser ColourType
199pColourType = doColourType colourTypes
200 where doColourType [] = fail $ "Unknown colour category. Options: " ++
201 (concat . intersperse ", " . map fst) colourTypes
202 doColourType ((s,ct):cts) = (try (P.symbol s) >> return ct) <|> doColourType cts
204pSetColourCmd :: P.IdrisParser Command
205pSetColourCmd = (do c <- pColourType
206 let defaultColour = IdrisColour Nothing True False False False
207 opts <- sepBy pColourMod (P.whiteSpace)
208 let colour = foldr ($) defaultColour $ reverse opts
209 return $ SetColour c colour)
210 <|> try (P.symbol "on" >> return ColourOn)
211 <|> try (P.symbol "off" >> return ColourOff)
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