From 660906d42f60360a4e85bb30fe005796d965827a Mon Sep 17 00:00:00 2001 From: iphydf Date: Sun, 12 Aug 2018 17:13:56 +0000 Subject: [PATCH] Parse all toxav and toxcore sources in tokstyle. --- BUILD.bazel | 1 + src/Tokstyle/Cimple.hs | 25 ++++++++++--------------- src/Tokstyle/Cimple/Lexer.x | 4 +++- src/Tokstyle/Cimple/Parser.y | 1 + src/Tokstyle/Sources.hs | 23 ++++++++++++++--------- test/Tokstyle/CimpleSpec.hs | 4 ++++ tokstyle.cabal | 1 + 7 files changed, 34 insertions(+), 25 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index 471a028..f6dfde2 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -36,6 +36,7 @@ haskell_library( "//third_party/haskell:base", "//third_party/haskell:deepseq", "//third_party/haskell:filepath", + "@haskell_groom//:groom", "@haskell_language_c//:language-c", ], ) diff --git a/src/Tokstyle/Cimple.hs b/src/Tokstyle/Cimple.hs index bb8e7b9..2f7cb8a 100644 --- a/src/Tokstyle/Cimple.hs +++ b/src/Tokstyle/Cimple.hs @@ -1,21 +1,16 @@ module Tokstyle.Cimple (main) where ---import Text.Groom (groom) -import Tokstyle.Cimple.Lexer (alexScanTokens, runAlex) +import Text.Groom (groom) +import Tokstyle.Cimple.Lexer (runAlex) import Tokstyle.Cimple.Parser (parseCimple) +parseFile :: FilePath -> IO () +parseFile source = do + putStrLn $ "Processing " ++ source + contents <- readFile source + putStrLn . groom $ runAlex contents parseCimple + + main :: [String] -> IO () -main sources = do - putStrLn "[=] reading..." - contents <- mapM readFile sources - {- - putStrLn "[=] lexing..." - let tokens = map alexScanTokens contents - mapM_ (putStrLn . groom) tokens - -} - putStrLn "[=] parsing..." - let ast = map (`runAlex` parseCimple) contents - -- mapM_ (putStrLn . groom) ast - mapM_ print ast - return () +main = mapM_ parseFile diff --git a/src/Tokstyle/Cimple/Lexer.x b/src/Tokstyle/Cimple/Lexer.x index e9e3207..ef0b6c2 100644 --- a/src/Tokstyle/Cimple/Lexer.x +++ b/src/Tokstyle/Cimple/Lexer.x @@ -71,6 +71,7 @@ tokens :- -- Standard C (ish). defined { mkL PpDefined } + \"[^\"]*\" { mkL LitString } \n { mkL PpNewline `andBegin` 0 } \\\n ; $white ; @@ -129,6 +130,7 @@ tokens :- <0,ppSC> "bool" { mkL IdStdType } <0,ppSC> "char" { mkL IdStdType } <0,ppSC> "double" { mkL IdStdType } +<0,ppSC> "float" { mkL IdStdType } <0,ppSC> "int" { mkL IdStdType } <0,ppSC> "long int" { mkL IdStdType } <0,ppSC> "long signed int" { mkL IdStdType } @@ -154,7 +156,7 @@ tokens :- <0,ppSC> [a-z][a-z0-9_]*_cb { mkL IdFuncType } <0,ppSC> [a-z][A-Za-z0-9_]* { mkL IdVar } <0,ppSC> [0-9]+[LU]* { mkL LitInteger } -<0,ppSC> [0-9]+"."[0-9]+ { mkL LitInteger } +<0,ppSC> [0-9]+"."[0-9]+f? { mkL LitInteger } <0,ppSC> 0x[0-9a-fA-F]+[LU]* { mkL LitInteger } <0,ppSC> "=" { mkL PctEq } <0,ppSC> "==" { mkL PctEqEq } diff --git a/src/Tokstyle/Cimple/Parser.y b/src/Tokstyle/Cimple/Parser.y index 4cc49b3..e0d2a04 100644 --- a/src/Tokstyle/Cimple/Parser.y +++ b/src/Tokstyle/Cimple/Parser.y @@ -459,6 +459,7 @@ MemberDecls MemberDecl :: { () } MemberDecl : QualType DeclSpec(ConstExpr) ';' { () } +| QualType DeclSpec(ConstExpr) ':' LIT_INTEGER ';' { () } | PreprocIfdef(MemberDecls) { () } TypedefDecl :: { () } diff --git a/src/Tokstyle/Sources.hs b/src/Tokstyle/Sources.hs index ce96737..2c3f6bd 100644 --- a/src/Tokstyle/Sources.hs +++ b/src/Tokstyle/Sources.hs @@ -3,15 +3,19 @@ module Tokstyle.Sources (sources) where sources :: [String] sources = map ("../c-toxcore/" ++) [ "toxav/audio.c" - {-, "toxav/audio.h"-} - {-, "toxav/bwcontroller.c"-} - {-, "toxav/bwcontroller.h"-} - {-, "toxav/groupav.c"-} - {-, "toxav/msi.c"-} - {-, "toxav/rtp.c"-} - {-, "toxav/toxav.c"-} - {-, "toxav/toxav_old.c"-} - {-, "toxav/video.c"-} + , "toxav/audio.h" + , "toxav/bwcontroller.c" + , "toxav/bwcontroller.h" + , "toxav/groupav.c" + , "toxav/groupav.h" + , "toxav/msi.c" + , "toxav/msi.h" + , "toxav/rtp.c" + , "toxav/rtp.h" + , "toxav/toxav.c" + , "toxav/toxav_old.c" + , "toxav/video.c" + , "toxav/video.h" , "toxcore/DHT.c" , "toxcore/DHT.h" , "toxcore/LAN_discovery.c" @@ -39,6 +43,7 @@ sources = map ("../c-toxcore/" ++) , "toxcore/mono_time.c" , "toxcore/mono_time.h" , "toxcore/net_crypto.c" + , "toxcore/net_crypto.h" , "toxcore/network.c" , "toxcore/network.h" , "toxcore/onion.c" diff --git a/test/Tokstyle/CimpleSpec.hs b/test/Tokstyle/CimpleSpec.hs index 804bb33..7b7c930 100644 --- a/test/Tokstyle/CimpleSpec.hs +++ b/test/Tokstyle/CimpleSpec.hs @@ -16,3 +16,7 @@ spec = it "should parse a type declaration" $ do let ast = runAlex "typedef struct Foo { int x; } Foo;" parseCimple ast `shouldBe` Right [()] + + it "should parse a struct with bit fields" $ do + let ast = runAlex "typedef struct Foo { int x : 123; } Foo;" parseCimple + ast `shouldBe` Right [()] diff --git a/tokstyle.cabal b/tokstyle.cabal index b576475..89656b0 100644 --- a/tokstyle.cabal +++ b/tokstyle.cabal @@ -33,6 +33,7 @@ library , array , deepseq , filepath + , groom , language-c hs-source-dirs: src