Started working on utility for parsing C headers.

This commit is contained in:
Erik Svedäng 2018-02-27 09:50:20 +01:00
parent f572161985
commit 01d77688ea
3 changed files with 50 additions and 1 deletions

View File

@ -64,6 +64,18 @@ executable carp
, process
default-language: Haskell2010
executable carp-header-parse
hs-source-dirs: headerparse
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends: base
, CarpHask
, containers
, directory
, cmdargs
, parsec
default-language: Haskell2010
test-suite CarpHask-test
type: exitcode-stdio-1.0
hs-source-dirs: test
@ -77,4 +89,4 @@ test-suite CarpHask-test
source-repository head
type: git
location: https://github.com/eriksvedang/Carp
location: https://github.com/carp-lang/Carp

4
examples/parse_me.h Normal file
View File

@ -0,0 +1,4 @@
void boo();
char hoo();
int foo(int x);
float goo(double x, double y);

33
headerparse/Main.hs Normal file
View File

@ -0,0 +1,33 @@
{-# LANGUAGE DeriveDataTypeable #-}
{-# OPTIONS_GHC -fno-cse #-}
module Main where
import System.Console.CmdArgs
import System.IO (readFile)
import Text.Parsec ((<|>))
import qualified Text.Parsec as Parsec
import Util
import Types
import Obj
data Args = Args { sourcePath :: String
} deriving (Show, Data, Typeable)
main = do parsedArgs <- cmdArgs (Args { sourcePath = def &= argPos 0 }
&= summary "Carp Header Parse 0.0.1")
let path = sourcePath parsedArgs
if path /= ""
then do source <- readFile path
putStrLn (joinWith "\n" (map pretty (parseHeaderFile path source)))
else print parsedArgs
parseHeaderFile :: FilePath -> String -> [XObj]
parseHeaderFile path src =
case Parsec.runParser cSyntax () path src of
Left err -> error (show err)
Right ok -> ok
where
cSyntax :: Parsec.Parsec String () [XObj]
cSyntax = do _ <- Parsec.many Parsec.anyChar
return [(XObj (Sym (SymPath [] "foobar") Symbol) Nothing Nothing)]