git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Start on parser module.
commit0863539ba79279979f354e22a2b944d39230a3d2 authorjoneshf <jones3.hardy@gmail.com> dateFri 21 Feb 2014, 09:40 (13 years ago) parent2e16a83
2 files changed+56−21
psci/Main.hs+6−21 psci/Parser.hs+50
psci/Main.hs+6−21
@@ -36,11 +36,13 @@ import System.Environment.XDG.BaseDir
3636import System.FilePath ((</>), isPathSeparator)
3737import System.Process
3838
39+import qualified Parser
40+
3941import qualified Data.Map as M
4042import qualified Language.PureScript as P
4143import qualified Paths_purescript as Paths
4244import qualified System.IO.UTF8 as U (readFile)
43−import qualified Text.Parsec as Parsec (Parsec, eof, try)
45+import qualified Text.Parsec as Parsec (try)
4446
4547-- |
4648-- The PSCI state.
@@ -115,7 +117,7 @@ getPreludeFilename = Paths.getDataFileName "prelude/prelude.purs"
115117-- Loads a file for use with imports.
116118--
117119loadModule :: FilePath -> IO (Either String [P.Module])
118−loadModule = fmap (either (Left . show) Right . P.runIndentParser "" P.parseModules) . U.readFile
120+loadModule = fmap (either (Left . show) Right . Parser.parseModules) . U.readFile
119121
120122-- |
121123-- Expands tilde in path.
@@ -233,23 +235,6 @@ handleTypeOf value (PSCI imports loadedModules lets) = do
233235 Just (ty, _) -> outputStrLn . P.prettyPrintType $ ty
234236 Nothing -> outputStrLn "Could not find type"
235237
236−-- Parser helpers
237−
238−-- |
239−-- Parser for our PSCI version of @let@.
240−-- This is essentially let from do-notation.
241−-- However, since we don't support the @Eff@ monad, we actually want the normal @let@.
242−--
243−parseLet :: Parsec.Parsec String P.ParseState (P.Value -> P.Value)
244−parseLet = P.Let <$> (P.reserved "let" *> P.indented *> P.parseBinder)
245− <*> (P.indented *> P.reservedOp "=" *> P.parseValue)
246−
247−-- |
248−-- Parser for any other valid expression.
249−--
250−parseExpression :: Parsec.Parsec String P.ParseState P.Value
251−parseExpression = P.whiteSpace *> P.parseValue <* Parsec.eof
252−
253238-- Commands
254239
255240-- |
@@ -258,7 +243,7 @@ parseExpression = P.whiteSpace *> P.parseValue <* Parsec.eof
258243handleCommand :: Command -> StateT PSCI (InputT IO) ()
259244handleCommand Empty = return ()
260245handleCommand (Expression ls) =
261− case P.runIndentParser "" (Left <$> Parsec.try parseLet <|> Right <$> parseExpression) (unlines ls) of
246+ case Parser.psciParser (Left <$> Parsec.try Parser.parseLet <|> Right <$> Parser.parseExpression) (unlines ls) of
262247 Left err -> inputTToState $ outputStrLn (show err)
263248 Right (Left l) -> modify (updateLets l)
264249 Right (Right decl) -> get >>= inputTToState . handleDeclaration decl
@@ -275,7 +260,7 @@ handleCommand Reload = do
275260 (Right prelude) <- ioToState $ loadModule =<< getPreludeFilename
276261 put (PSCI defaultImports prelude [])
277262handleCommand (TypeOf expr) =
278− case P.runIndentParser "" parseExpression expr of
263+ case Parser.psciParser Parser.parseExpression expr of
279264 Left err -> inputTToState $ outputStrLn (show err)
280265 Right expr' -> get >>= inputTToState . handleTypeOf expr'
281266handleCommand _ = outputTStrLn "Unknown command"
psci/Parser.hsnew file+50
@@ -0,0 +1,50 @@
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+-----------------------------------------------------------------------------
15+
16+module Parser where
17+
18+import Control.Applicative
19+
20+import Text.Parsec
21+
22+import qualified Language.PureScript as P
23+
24+-- |
25+-- Parser for PSCI.
26+-- This is really just a wrapper around 'Language.PureScript.runIndentParser'
27+--
28+psciParser :: Parsec String P.ParseState a -> String -> Either ParseError a
29+psciParser = P.runIndentParser ""
30+
31+-- |
32+-- Parses modules
33+--
34+parseModules :: String -> Either ParseError [P.Module]
35+parseModules = psciParser P.parseModules
36+
37+-- |
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@.
41+--
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)
45+
46+-- |
47+-- Parser for any other valid expression.
48+--
49+parseExpression :: Parsec String P.ParseState P.Value
50+parseExpression = P.whiteSpace *> P.parseValue <* eof