git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Cleanup parser a bit.
4baa685joneshf
psci/Commands.hs+1−1
@@ -1,6 +1,6 @@
1-----------------------------------------------------------------------------
1-----------------------------------------------------------------------------
2--
2--
3−-- Module : Main
3+-- Module : Commands
4-- Copyright : (c) Phil Freeman 2014
4-- Copyright : (c) Phil Freeman 2014
5-- License : MIT
5-- License : MIT
6--
6--
psci/Main.hs+9−8
@@ -29,6 +29,8 @@ import Data.List (intercalate, isPrefixOf, nub, sortBy)
29import Data.Maybe (mapMaybe)
29import Data.Maybe (mapMaybe)
30import Data.Traversable (traverse)
30import Data.Traversable (traverse)
31
31
32+import Parser
33+
32import System.Console.Haskeline
34import System.Console.Haskeline
33import System.Directory (doesFileExist, findExecutable, getHomeDirectory)
35import System.Directory (doesFileExist, findExecutable, getHomeDirectory)
34import System.Exit
36import System.Exit
@@ -36,13 +38,12 @@ import System.Environment.XDG.BaseDir
36import System.FilePath ((</>), isPathSeparator)
38import System.FilePath ((</>), isPathSeparator)
37import System.Process
39import System.Process
38
40
39−import qualified Parser
41+import Text.Parsec (choice)
40
42
41import qualified Data.Map as M
43import qualified Data.Map as M
42import qualified Language.PureScript as P
44import qualified Language.PureScript as P
43import qualified Paths_purescript as Paths
45import qualified Paths_purescript as Paths
44import qualified System.IO.UTF8 as U (readFile)
46import qualified System.IO.UTF8 as U (readFile)
45−import qualified Text.Parsec as Parsec (try)
46
47
47-- |
48-- |
48-- The PSCI state.
49-- The PSCI state.
@@ -117,7 +118,7 @@ getPreludeFilename = Paths.getDataFileName "prelude/prelude.purs"
117-- Loads a file for use with imports.
118-- Loads a file for use with imports.
118--
119--
119loadModule :: FilePath -> IO (Either String [P.Module])
120loadModule :: FilePath -> IO (Either String [P.Module])
120−loadModule = fmap (either (Left . show) Right . Parser.parseModules) . U.readFile
121+loadModule = fmap (either (Left . show) Right . parseModules) . U.readFile
121
122
122-- |
123-- |
123-- Expands tilde in path.
124-- Expands tilde in path.
@@ -243,9 +244,9 @@ handleTypeOf value (PSCI imports loadedModules lets) = do
243handleCommand :: Command -> StateT PSCI (InputT IO) ()
244handleCommand :: Command -> StateT PSCI (InputT IO) ()
244handleCommand Empty = return ()
245handleCommand Empty = return ()
245handleCommand (Expression ls) =
246handleCommand (Expression ls) =
246− case Parser.psciParser (Left <$> Parsec.try Parser.parseLet <|> Right <$> Parser.parseExpression) (unlines ls) of
247+ case psciParser (choice [Left <$> psciLet, Right <$> psciExpression]) (unlines ls) of
247− Left err -> inputTToState $ outputStrLn (show err)
248+ Left err -> inputTToState $ outputStrLn (show err)
248− Right (Left l) -> modify (updateLets l)
249+ Right (Left l) -> modify (updateLets l)
249 Right (Right decl) -> get >>= inputTToState . handleDeclaration decl
250 Right (Right decl) -> get >>= inputTToState . handleDeclaration decl
250handleCommand Help = inputTToState $ outputStrLn helpMessage
251handleCommand Help = inputTToState $ outputStrLn helpMessage
251handleCommand (Import moduleName) = modify (updateImports moduleName)
252handleCommand (Import moduleName) = modify (updateImports moduleName)
@@ -260,8 +261,8 @@ handleCommand Reload = do
260 (Right prelude) <- ioToState $ loadModule =<< getPreludeFilename
261 (Right prelude) <- ioToState $ loadModule =<< getPreludeFilename
261 put (PSCI defaultImports prelude [])
262 put (PSCI defaultImports prelude [])
262handleCommand (TypeOf expr) =
263handleCommand (TypeOf expr) =
263− case Parser.psciParser Parser.parseExpression expr of
264+ case parseExpression expr of
264− Left err -> inputTToState $ outputStrLn (show err)
265+ Left err -> inputTToState $ outputStrLn (show err)
265 Right expr' -> get >>= inputTToState . handleTypeOf expr'
266 Right expr' -> get >>= inputTToState . handleTypeOf expr'
266handleCommand _ = outputTStrLn "Unknown command"
267handleCommand _ = outputTStrLn "Unknown command"
267
268
psci/Parser.hs+30−12
@@ -17,10 +17,31 @@ module Parser where
17
17
18import Control.Applicative
18import Control.Applicative
19
19
20−import Text.Parsec
20+import Text.Parsec hiding ((<|>))
21
21
22import qualified Language.PureScript as P
22import qualified Language.PureScript as P
23
23
24+-- |
25+-- PSCI version of modules
26+--
27+psciModules :: Parsec String P.ParseState [P.Module]
28+psciModules = P.parseModules
29+
30+-- |
31+-- PSCI version of 'let'.
32+-- This is essentially let from do-notation.
33+-- However, since we don't support the 'Eff' monad, we actually want the normal 'let'.
34+--
35+psciLet :: Parsec String P.ParseState (P.Value -> P.Value)
36+psciLet = P.Let <$> (P.reserved "let" *> P.indented *> P.parseBinder)
37+ <*> (P.indented *> P.reservedOp "=" *> P.parseValue)
38+
39+-- |
40+-- PSCI version of any other valid expression.
41+--
42+psciExpression :: Parsec String P.ParseState P.Value
43+psciExpression = P.whiteSpace *> P.parseValue <* eof
44+
24-- |
45-- |
25-- Parser for PSCI.
46-- Parser for PSCI.
26-- This is really just a wrapper around 'Language.PureScript.runIndentParser'
47-- This is really just a wrapper around 'Language.PureScript.runIndentParser'
@@ -29,22 +50,19 @@ psciParser :: Parsec String P.ParseState a -> String -> Either ParseError a
29psciParser = P.runIndentParser ""
50psciParser = P.runIndentParser ""
30
51
31-- |
52-- |
32−-- Parses modules
53+-- Parses PSCI modules.
33--
54--
34parseModules :: String -> Either ParseError [P.Module]
55parseModules :: String -> Either ParseError [P.Module]
35−parseModules = psciParser P.parseModules
56+parseModules = psciParser psciModules
36
57
37-- |
58-- |
38−-- Parser for our PSCI version of @let@.
59+-- Parses PSCI 'let' bindings.
39−-- This is essentially let from do-notation.
40−-- However, since we don't support the @Eff@ monad, we actually want the normal @let@.
41--
60--
42−parseLet :: Parsec String P.ParseState (P.Value -> P.Value)
61+parseLet :: String -> Either ParseError (P.Value -> P.Value)
43−parseLet = P.Let <$> (P.reserved "let" *> P.indented *> P.parseBinder)
62+parseLet = psciParser psciLet
44− <*> (P.indented *> P.reservedOp "=" *> P.parseValue)
45
63
46-- |
64-- |
47−-- Parser for any other valid expression.
65+-- Parses PSCI expressions.
48--
66--
49−parseExpression :: Parsec String P.ParseState P.Value
67+parseExpression :: String -> Either ParseError P.Value
50−parseExpression = P.whiteSpace *> P.parseValue <* eof
68+parseExpression = psciParser psciExpression