summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMattMorrow <>2008-07-01 01:22:23 (GMT)
committerLuite Stegeman <luite@luite.com>2008-07-01 01:22:23 (GMT)
commitb9f5e8172c820283ca103b3f5d5856e96c391a08 (patch)
tree56d010026246aa3f28d798ebb64284aced3a4a14
version 0.10.1
-rw-r--r--LICENSE26
-rw-r--r--Setupbin0 -> 4447689 bytes
-rw-r--r--Setup.lhs4
-rw-r--r--hstidy.cabal26
-rw-r--r--hstidy.hs118
-rw-r--r--x1
6 files changed, 175 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..73ad910
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,26 @@
+Copyright (c) Matt Morrow.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. The names of the author may not be used to endorse or promote
+ products derived from this software without specific prior written
+ permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
diff --git a/Setup b/Setup
new file mode 100644
index 0000000..4c6e3f3
--- /dev/null
+++ b/Setup
Binary files differ
diff --git a/Setup.lhs b/Setup.lhs
new file mode 100644
index 0000000..fa82b83
--- /dev/null
+++ b/Setup.lhs
@@ -0,0 +1,4 @@
+> import Distribution.Simple
+> main :: IO ()
+> main = defaultMain
+
diff --git a/hstidy.cabal b/hstidy.cabal
new file mode 100644
index 0000000..bd31e7e
--- /dev/null
+++ b/hstidy.cabal
@@ -0,0 +1,26 @@
+name: hstidy
+version: 0.1
+cabal-version: >= 1.2
+build-type: Simple
+license: BSD3
+license-file: LICENSE
+category: Text
+author: Matt Morrow
+copyright: Matt Morrow
+maintainer: Matt Morrow <mjm2002@gmail.com>
+homepage: http://code.haskell.org/~morrow/code/haskell/hstidy
+stability: unstable
+synopsis: Takes haskell source on stdin, parses it, then
+ prettyprints it to stdout.
+description: @hstidy@ reads haskell source from stdin, then
+ parses and prettyprints it to stdout. @hstidy@ accepts not
+ only modules as a parseable unit, but expressions
+ and patterns as well. Haddock docs are located at
+ <http://code.haskell.org/~morrow/code/haskell/hstidy/docs/>
+executable hstidy
+ executable: hstidy
+ main-is: hstidy.hs
+ build-depends: base, haskell-src-exts, filepath
+ ghc-options: -O2
+ hs-source-dirs: .
+
diff --git a/hstidy.hs b/hstidy.hs
new file mode 100644
index 0000000..6605496
--- /dev/null
+++ b/hstidy.hs
@@ -0,0 +1,118 @@
+{-# #-}
+
+{- |
+ Module : hstidy
+ Copyright : (c) Matt Morrow 2008
+ License : BSD3
+
+ Maintainer : Matt Morrow <mjm2002@gmail.com>
+ Stability : unstable
+ Portability : portable
+-}
+
+module Main where
+import System.IO
+import System.Exit
+import System.FilePath
+import System.Environment
+import System.Console.GetOpt
+import Language.Haskell.Exts.Syntax
+import Language.Haskell.Exts.Parser
+import Language.Haskell.Exts.Pretty
+
+main :: IO ()
+main = do
+ (opts,_,_) <- return . getOpt Permute options =<< getArgs
+ case opts of
+ ModO:_ -> go parseHsModule
+ ExpO:_ -> go parseHsExp
+ PatO:_ -> go parseHsPat
+ HelpO:_ -> goHelp
+ VersO:_ -> goVersion
+ _ -> go parseHsModule
+
+go :: (Pretty a) => (String -> Either String a) -> IO ()
+go p = either (hPutStrLn stderr)
+ (hPutStrLn stdout . prettyPrint)
+ . p =<< getContents
+
+goHelp :: IO ()
+goHelp = putStrLn (usageInfo "hstidy" options)
+
+goVersion :: IO ()
+goVersion = putStrLn "0.1"
+
+data Opt
+ = HelpO
+ | VersO
+ | ModO
+ | ExpO
+ | PatO
+ deriving (Eq,Ord,Show,Read)
+
+options :: [OptDescr Opt]
+options =
+ [ Option ['h'] ["help"] (NoArg HelpO) "display help and usage information"
+ , Option ['v','V'] ["version"] (NoArg VersO) "show version information"
+ , Option ['m'] ["module"] (NoArg ModO) "tidy a module from stdin"
+ , Option ['e'] ["expression"] (NoArg ExpO) "tidy an expression from stdin"
+ , Option ['p'] ["pattern"] (NoArg PatO) "tidy a pattern from stdin"
+ ]
+
+-- HsModule SrcLoc Module (Maybe [HsExportSpec]) [HsImportDecl] [HsDecl]
+
+parseHsModule :: String -> Either String HsModule
+parseHsModule s =
+ case parseModule s of
+ ParseOk m -> Right m
+ ParseFailed loc e ->
+ let line = srcLine loc - 1
+ in Left (unlines [show line,show loc,e])
+
+parseHsDecls :: String -> Either String [HsDecl]
+parseHsDecls s =
+ let s' = unlines [pprHsModule (emptyHsModule "Main"), s]
+ in case parseModule s' of
+ ParseOk m -> Right (moduleDecls m)
+ ParseFailed loc e ->
+ let line = srcLine loc - 1
+ in Left (unlines [show line,show loc,e])
+
+parseHsExp :: String -> Either String HsExp
+parseHsExp s =
+ case parseHsDecls ("main = ("++(filter (/='\n') s)++")") of
+ Left err -> Left err
+ Right xs ->
+ case [ e | HsPatBind _ _ (HsUnGuardedRhs e) _ <- xs] of
+ [] -> Left "invalid expression"
+ (e:_) -> Right e
+
+parseHsPat :: String -> Either String HsPat
+parseHsPat s =
+ case parseHsDecls ("("++(filter (/='\n') s)++")=()") of
+ Left err -> Left err
+ Right xs ->
+ case [ p | HsPatBind _ p _ _ <- xs] of
+ [] -> Left "invalid pattern"
+ (p:_) -> Right p
+
+pprHsModule :: HsModule -> String
+pprHsModule = prettyPrint
+
+moduleDecls :: HsModule -> [HsDecl]
+moduleDecls (HsModule _ _ _ _ x) = x
+
+mkModule :: String -> Module
+mkModule = Module
+
+emptySrcLoc :: SrcLoc
+emptySrcLoc = (SrcLoc [] 0 0)
+
+emptyHsModule :: String -> HsModule
+emptyHsModule n =
+ (HsModule
+ emptySrcLoc
+ (mkModule n)
+ Nothing
+ []
+ [])
diff --git a/x b/x
new file mode 100644
index 0000000..78f683d
--- /dev/null
+++ b/x
@@ -0,0 +1 @@
+\x-