git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Cleanup parser a bit.
commit4baa68599c0e9e6a5b504e1bc39d880e287e460a authorjoneshf <jones3.hardy@gmail.com> dateFri 21 Feb 2014, 11:01 (13 years ago) parent0863539
3 files changed+40−21
psci/Commands.hs+1−1 psci/Main.hs+9−8 psci/Parser.hs+30−12
psci/Commands.hs+1−1
@@ -1,6 +1,6 @@
11-----------------------------------------------------------------------------
22--
3−-- Module : Main
3+-- Module : Commands
44-- Copyright : (c) Phil Freeman 2014
55-- License : MIT
66--
psci/Main.hs+9−8
@@ -29,6 +29,8 @@ import Data.List (intercalate, isPrefixOf, nub, sortBy)
2929import Data.Maybe (mapMaybe)
3030import Data.Traversable (traverse)
3131
32+import Parser
33+
3234import System.Console.Haskeline
3335import System.Directory (doesFileExist, findExecutable, getHomeDirectory)
3436import System.Exit
@@ -36,13 +38,12 @@ import System.Environment.XDG.BaseDir
3638import System.FilePath ((</>), isPathSeparator)
3739import System.Process
3840
39−import qualified Parser
41+import Text.Parsec (choice)
4042
4143import qualified Data.Map as M
4244import qualified Language.PureScript as P
4345import qualified Paths_purescript as Paths
4446import qualified System.IO.UTF8 as U (readFile)
45−import qualified Text.Parsec as Parsec (try)
4647
4748-- |
4849-- The PSCI state.
@@ -117,7 +118,7 @@ getPreludeFilename = Paths.getDataFileName "prelude/prelude.purs"
117118-- Loads a file for use with imports.
118119--
119120loadModule :: 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
121122
122123-- |
123124-- Expands tilde in path.
@@ -243,9 +244,9 @@ handleTypeOf value (PSCI imports loadedModules lets) = do
243244handleCommand :: Command -> StateT PSCI (InputT IO) ()
244245handleCommand Empty = return ()
245246handleCommand (Expression ls) =
246− case Parser.psciParser (Left <$> Parsec.try Parser.parseLet <|> Right <$> Parser.parseExpression) (unlines ls) of
247− Left err -> inputTToState $ outputStrLn (show err)
248− Right (Left l) -> modify (updateLets l)
247+ case psciParser (choice [Left <$> psciLet, Right <$> psciExpression]) (unlines ls) of
248+ Left err -> inputTToState $ outputStrLn (show err)
249+ Right (Left l) -> modify (updateLets l)
249250 Right (Right decl) -> get >>= inputTToState . handleDeclaration decl
250251handleCommand Help = inputTToState $ outputStrLn helpMessage
251252handleCommand (Import moduleName) = modify (updateImports moduleName)
@@ -260,8 +261,8 @@ handleCommand Reload = do
260261 (Right prelude) <- ioToState $ loadModule =<< getPreludeFilename
261262 put (PSCI defaultImports prelude [])
262263handleCommand (TypeOf expr) =
263− case Parser.psciParser Parser.parseExpression expr of
264− Left err -> inputTToState $ outputStrLn (show err)
264+ case parseExpression expr of
265+ Left err -> inputTToState $ outputStrLn (show err)
265266 Right expr' -> get >>= inputTToState . handleTypeOf expr'
266267handleCommand _ = outputTStrLn "Unknown command"
267268
psci/Parser.hs+30−12
@@ -17,10 +17,31 @@ module Parser where
1717
1818import Control.Applicative
1919
20−import Text.Parsec
20+import Text.Parsec hiding ((<|>))
2121
2222import qualified Language.PureScript as P
2323
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+
2445-- |
2546-- Parser for PSCI.
2647-- This is really just a wrapper around 'Language.PureScript.runIndentParser'
@@ -29,22 +50,19 @@ psciParser :: Parsec String P.ParseState a -> String -> Either ParseError a
2950psciParser = P.runIndentParser ""
3051
3152-- |
32−-- Parses modules
53+-- Parses PSCI modules.
3354--
3455parseModules :: String -> Either ParseError [P.Module]
35−parseModules = psciParser P.parseModules
56+parseModules = psciParser psciModules
3657
3758-- |
38−-- Parser for our PSCI version of @let@.
39−-- This is essentially let from do-notation.
40−-- However, since we don't support the @Eff@ monad, we actually want the normal @let@.
59+-- Parses PSCI 'let' bindings.
4160--
42−parseLet :: Parsec String P.ParseState (P.Value -> P.Value)
43−parseLet = P.Let <$> (P.reserved "let" *> P.indented *> P.parseBinder)
44− <*> (P.indented *> P.reservedOp "=" *> P.parseValue)
61+parseLet :: String -> Either ParseError (P.Value -> P.Value)
62+parseLet = psciParser psciLet
4563
4664-- |
47−-- Parser for any other valid expression.
65+-- Parses PSCI expressions.
4866--
49−parseExpression :: Parsec String P.ParseState P.Value
50−parseExpression = P.whiteSpace *> P.parseValue <* eof
67+parseExpression :: String -> Either ParseError P.Value
68+parseExpression = psciParser psciExpression