[<<][meta][>>][..]
Fri Nov 4 21:34:24 EDT 2011

Parsing with Language.C

A first attempt to make it do something, starting from parseC and
following types on hackage[1].

> import Language.C
> import Data.ByteString.Char8
> import Language.C.Data.Position
> cCode = "int fun(void *state, void **ins, void **outs, int n) { }"
> c = parseC  (pack cCode)  $ initPos "/tmp/foo.c"

This gives:

Right (CTranslUnit [CFDefExt (CFunDef [CTypeSpec (CIntType (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),3) (Name {nameId = 1})))] (CDeclr (Just "fun") [CFunDeclr (Right ([CDecl [CTypeSpec (CVoidType (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),4) (Name {nameId = 3})))] [(Just (CDeclr (Just "state") [CPtrDeclr [] (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),5) (Name {nameId = 6}))] Nothing [] (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),5) (Name {nameId = 5}))),Nothing,Nothing)] (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),5) (Name {nameId = 7})),CDecl [CTypeSpec (CVoidType (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),4) (Name {nameId = 8})))] [(Just (CDeclr (Just "ins") [CPtrDeclr [] (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),3) (Name {nameId = 11})),CPtrDeclr [] (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),3) (Name {nameId = 12}))] Nothing [] (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),3) (Name {nameId = 10}))),Nothing,Nothing)] (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),3) (Name {nameId = 13})),CDecl [CTypeSpec (CVoidType (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),4) (Name {nameId = 14})))] [(Just (CDeclr (Just "outs") [CPtrDeclr [] (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),4) (Name {nameId = 17})),CPtrDeclr [] (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),4) (Name {nameId = 18}))] Nothing [] (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),4) (Name {nameId = 16}))),Nothing,Nothing)] (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),4) (Name {nameId = 19})),CDecl [CTypeSpec (CIntType (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),3) (Name {nameId = 21})))] [(Just (CDeclr (Just "n") [] Nothing [] (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),1) (Name {nameId = 22}))),Nothing,Nothing)] (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),1) (Name {nameId = 23}))],False)) [] (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),1) (Name {nameId = 24}))] Nothing [] (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),3) (Name {nameId = 2}))) [] (CCompound [] [] (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),1) (Name {nameId = 25}))) (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),1) (Name {nameId = 26})))] (NodeInfo ("/tmp/foo.c": line 1) (("/tmp/foo.c": line 1),1) (Name {nameId = 27})))

I tried to use the prettyprinter:

-- import Text.Show.Pretty           --            .. pretty-show

But this doesn't seem to work..  After removing the explicit Show
instances for NodeInfo and Position (botha are in directory
language-c-0.4.2/src/Language/C/Data/) it does prettyprint properly,
though the size of this tree makes it rather difficult to handle.  See
next post.

Let's just make a list of the constructors.

CTranslUnit
CFDefExt
CFunDef
CTypeSpec
CIntType
NodeInfo
Position
Name
CDeclr
CFunDeclr
CDecl
CVoidType
CDeclr
CPtrDeclr

Might not be such a big deal to make it work from the inside out.

Man this is too much.  C syntax isn't really so simple as it looks.




[Reply][About]
[<<][meta][>>][..]