diff options
author | Csaba Hruska <csaba.hruska@gmail.com> | 2016-02-13 12:54:26 +0100 |
---|---|---|
committer | Csaba Hruska <csaba.hruska@gmail.com> | 2016-02-13 12:58:19 +0100 |
commit | 7ea1b33b2ec58fe08070dc7762ffcfa6b71c07d0 (patch) | |
tree | f134e7cbed9dfd4d654eb586fc814d26e2e0fb8c /tool | |
parent | d2458a26b7caf03063b47f4ea5b954ec8f954b63 (diff) |
pipeline pretty printing from json
Diffstat (limited to 'tool')
-rw-r--r-- | tool/Compiler.hs | 45 |
1 files changed, 39 insertions, 6 deletions
diff --git a/tool/Compiler.hs b/tool/Compiler.hs index 215cf106..a1a9671b 100644 --- a/tool/Compiler.hs +++ b/tool/Compiler.hs | |||
@@ -3,6 +3,7 @@ import Options.Applicative | |||
3 | import Data.Aeson | 3 | import Data.Aeson |
4 | import qualified Data.ByteString.Lazy as B | 4 | import qualified Data.ByteString.Lazy as B |
5 | import System.FilePath | 5 | import System.FilePath |
6 | import qualified Text.Show.Pretty as PP | ||
6 | 7 | ||
7 | import LambdaCube.Compiler | 8 | import LambdaCube.Compiler |
8 | 9 | ||
@@ -11,6 +12,7 @@ data Config | |||
11 | { srcName :: String | 12 | { srcName :: String |
12 | , backend :: Backend | 13 | , backend :: Backend |
13 | , includePaths :: [FilePath] | 14 | , includePaths :: [FilePath] |
15 | , pretty :: Bool | ||
14 | } | 16 | } |
15 | 17 | ||
16 | sample :: Parser Config | 18 | sample :: Parser Config |
@@ -18,6 +20,7 @@ sample = Config | |||
18 | <$> argument str (metavar "SOURCE_FILE") | 20 | <$> argument str (metavar "SOURCE_FILE") |
19 | <*> flag OpenGL33 WebGL1 (long "webgl" <> help "generate WebGL 1.0 pipeline" ) | 21 | <*> flag OpenGL33 WebGL1 (long "webgl" <> help "generate WebGL 1.0 pipeline" ) |
20 | <*> pure ["."] | 22 | <*> pure ["."] |
23 | <*> switch (long "pretty" <> help "pretty prints pipeline") | ||
21 | 24 | ||
22 | main :: IO () | 25 | main :: IO () |
23 | main = compile =<< execParser opts | 26 | main = compile =<< execParser opts |
@@ -29,10 +32,40 @@ main = compile =<< execParser opts | |||
29 | 32 | ||
30 | compile :: Config -> IO () | 33 | compile :: Config -> IO () |
31 | compile Config{..} = do | 34 | compile Config{..} = do |
32 | let dropExt n | takeExtension n == ".lc" = dropExtension n | 35 | let ext = takeExtension srcName |
33 | dropExt n = n | 36 | baseName | ext == ".lc" = dropExtension srcName |
34 | baseName = dropExt srcName | 37 | | otherwise = srcName |
35 | pplRes <- compileMain includePaths backend baseName | 38 | case ext of |
36 | case pplRes of | 39 | ".json" | pretty -> prettyPrint srcName |
40 | _ -> do | ||
41 | pplRes <- compileMain includePaths backend baseName | ||
42 | case pplRes of | ||
43 | Left err -> putStrLn err | ||
44 | Right ppl -> case pretty of | ||
45 | False -> B.writeFile (baseName <> ".json") $ encode ppl | ||
46 | True -> writeFile (baseName <> ".ppl") $ ppUnlines $ PP.ppShow ppl | ||
47 | |||
48 | prettyPrint :: String -> IO () | ||
49 | prettyPrint srcName = do | ||
50 | let baseName = dropExtension srcName | ||
51 | json <- B.readFile srcName | ||
52 | case eitherDecode json :: Either String Pipeline of | ||
37 | Left err -> putStrLn err | 53 | Left err -> putStrLn err |
38 | Right ppl -> B.writeFile (baseName <> ".json") $ encode ppl | 54 | Right ppl -> writeFile (baseName <> ".ppl") $ ppUnlines $ PP.ppShow ppl |
55 | |||
56 | ppUnlines :: String -> String | ||
57 | ppUnlines = goPP 0 | ||
58 | where goPP _ [] = [] | ||
59 | goPP n ('"':xs) | isMultilineString xs = "\"\"\"\n" ++ indent ++ go xs where | ||
60 | indent = replicate n ' ' | ||
61 | go ('\\':'n':xs) = "\n" ++ indent ++ go xs | ||
62 | go ('\\':c:xs) = '\\':c:go xs | ||
63 | go ('"':xs) = "\n" ++ indent ++ "\"\"\"" ++ goPP n xs | ||
64 | go (x:xs) = x : go xs | ||
65 | goPP n (x:xs) = x : goPP (if x == '\n' then 0 else n+1) xs | ||
66 | |||
67 | isMultilineString ('\\':'n':xs) = True | ||
68 | isMultilineString ('\\':c:xs) = isMultilineString xs | ||
69 | isMultilineString ('"':xs) = False | ||
70 | isMultilineString (x:xs) = isMultilineString xs | ||
71 | isMultilineString [] = False | ||