Commit 99bdcc1b authored by Kenton Varda's avatar Kenton Varda

Merge branch 'check-language' of https://github.com/mosesn/capnproto

Supports choosing output languages on the command line.
parents 7a00f5c2 718b5c05
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
module Main ( main ) where module Main ( main ) where
import System.Environment import System.Environment
import System.Console.GetOpt
import Compiler import Compiler
import Util(delimit) import Util(delimit)
import Text.Parsec.Pos import Text.Parsec.Pos
...@@ -31,27 +32,54 @@ import Text.Parsec.Error ...@@ -31,27 +32,54 @@ import Text.Parsec.Error
import Text.Printf(printf) import Text.Printf(printf)
import qualified Data.List as List import qualified Data.List as List
import qualified Data.ByteString.Lazy.Char8 as LZ import qualified Data.ByteString.Lazy.Char8 as LZ
import Data.List
import Data.Maybe
import Semantics
import CxxGenerator import CxxGenerator
main::IO() main::IO()
main = do main = do
files <- getArgs let options = [Option ['o'] ["output"] (ReqArg id "FILE") "Where to send the files"]
mapM_ handleFile files args <- getArgs
let tup = getOpt RequireOrder options args
let (optionResults, files, _) = tup
let langDirs = catMaybes (map splitAtEquals optionResults)
handleFilesLangs (generatorFnsFor langDirs) files
handleFile filename = do splitAtEquals :: String -> Maybe (String, String)
splitAtEquals str = do
holder <- (elemIndex '=' str)
Just((splitAt holder str))
handleFilesLangs eithers files = mapM_ (\x -> handleFiles x files) eithers
handleFiles (Right fn) files = mapM_ (handleFile fn) files
handleFiles (Left str) _ = putStrLn str
handleFile generateCode filename = do
text <- readFile filename text <- readFile filename
case parseAndCompileFile filename text of case parseAndCompileFile filename text of
Active desc [] -> do Active desc [] -> do
print desc print desc
header <- generateCxxHeader desc generateCode desc filename
LZ.writeFile (filename ++ ".h") header
source <- generateCxxSource desc
LZ.writeFile (filename ++ ".c++") source
Active _ e -> mapM_ printError (List.sortBy compareErrors e) Active _ e -> mapM_ printError (List.sortBy compareErrors e)
Failed e -> mapM_ printError (List.sortBy compareErrors e) Failed e -> mapM_ printError (List.sortBy compareErrors e)
generatorFnsFor :: [(String, String)] -> [Either String (FileDesc -> FilePath -> (IO ()))]
generatorFnsFor langDirs = do
map (\langDir -> generatorFnFor (fst langDir) (tail (snd langDir)))langDirs
generatorFnFor :: String -> String -> Either String (FileDesc -> FilePath -> (IO ()))
generatorFnFor lang dir = case lang of
"c++" -> Right (\desc filename -> do
header <- generateCxxHeader desc
LZ.writeFile (dir ++ "/" ++ filename ++ ".h") header
source <- generateCxxSource desc
LZ.writeFile (dir ++ "/" ++ filename ++ ".c++") source)
_ -> Left "Only c++ is supported for now"
compareErrors a b = compare (errorPos a) (errorPos b) compareErrors a b = compare (errorPos a) (errorPos b)
-- TODO: This is a fairly hacky way to make showErrorMessages' output not suck. We could do better -- TODO: This is a fairly hacky way to make showErrorMessages' output not suck. We could do better
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment