git.iko.soy/rowtype-yoga/purescript-native
on e82e607 help sign in
92 lines 2.5 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Parser
4-- Copyright : (c) Phil Freeman 2014
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12-- Parser for PSCI.
13--
14-----------------------------------------------------------------------------
16module Parser (
17 parseCommand
18 ) where
20import Commands
22import Data.Char (isSpace)
24import Control.Applicative hiding (many)
26import Text.Parsec hiding ((<|>))
28import qualified Language.PureScript as P
29import qualified Language.PureScript.Parser.Common as C (mark, same)
31-- |
32-- PSCI version of @let@.
33-- This is essentially let from do-notation.
34-- However, since we don't support the @Eff@ monad,
35-- we actually want the normal @let@.
36--
37psciLet :: Parsec String P.ParseState Command
38psciLet = Let <$> (P.Let <$> (P.reserved "let" *> P.indented *> C.mark (many1 (C.same *> P.parseDeclaration))))
40-- |
41-- Parses PSCI metacommands or expressions input from the user.
42--
43parseCommand :: String -> Either ParseError Command
44parseCommand = P.runIndentParser "" $ choice
45 [ P.whiteSpace *> char ':' *> (psciHelp <|> psciImport <|> psciLoadFile <|> psciQuit <|> psciReload <|> psciTypeOf)
46 , try psciLet
47 , psciExpression
48 ] <* eof
50-- |
51-- Parses expressions entered at the PSCI repl.
52--
53psciExpression :: Parsec String P.ParseState Command
54psciExpression = Expression <$> P.parseValue
56-- |
57-- Parses 'Commands.Help' command.
58--
59psciHelp :: Parsec String P.ParseState Command
60psciHelp = Help <$ char '?'
62-- |
63-- Parses 'Commands.Import' command.
64--
65psciImport :: Parsec String P.ParseState Command
66psciImport = Import <$> (char 'i' *> P.whiteSpace *> P.moduleName)
68-- |
69-- Parses 'Commands.LoadFile' command.
70--
71psciLoadFile :: Parsec String P.ParseState Command
72psciLoadFile = LoadFile . trimEnd <$> (char 'm' *> P.whiteSpace *> manyTill anyChar eof)
73 where
74 trimEnd = reverse . dropWhile isSpace . reverse
76-- |
77-- Parses 'Commands.Quit' command.
78--
79psciQuit :: Parsec String P.ParseState Command
80psciQuit = Quit <$ char 'q'
82-- |
83-- Parses 'Commands.Reload' command.
84--
85psciReload :: Parsec String P.ParseState Command
86psciReload = Reset <$ char 'r'
88-- |
89-- Parses 'Commands.TypeOf' command.
90--
91psciTypeOf :: Parsec String P.ParseState Command
92psciTypeOf = TypeOf <$> (char 't' *> P.whiteSpace *> P.parseValue)