11-- A very simple-minded parser for C declarations of the following syntax:
22-- "const"? type-specifier ("*" "const"?)* identifier ("[" number "]")?
3- module DeclarationParser ( parse ) where
3+ module DeclarationParser
4+ ( parse
5+ ) where
46
57import Control.Monad
68import Data.Char
@@ -26,51 +28,56 @@ optionalConst :: ReadP ()
2628optionalConst = option' () (void (token " const" ))
2729
2830parseTypeSpecifier :: ReadP String
29- parseTypeSpecifier = choice' [
30- token " void" >> return " ()" ,
31- token " float" >> return " CFloat" ,
32- token " double" >> return " CDouble" ,
33- token " int32_t" >> return " Int32" ,
34- token " int64_t" >> return " Int64" ,
35- do c <- option' " CChar" (token " signed" >> return " CSChar" )
36- choice' [
37- token " char" >> return c,
38- token " short" >> return " CShort" ,
39- token " int" >> return " CInt" ,
40- token " long" >> choice' [token " long" >> return " CLLong" ,
41- return " CLong" ]],
42- do _ <- token " unsigned"
43- choice' [
44- token " char" >> return " CUChar" ,
45- token " short" >> return " CUShort" ,
46- token " int" >> return " CUInt" ,
47- token " long" >> choice' [token " long" >> return " CULLong" ,
48- return " CULong" ]],
49- token " struct" >> parseIdentifier >> return " ()" , -- Hmmm...
50- token " GLvoid" >> return " ()" , -- glGetPerfQueryDataINTEL still mentions this
51- parseIdentifier ]
31+ parseTypeSpecifier =
32+ choice'
33+ [ token " void" >> return " ()"
34+ , token " float" >> return " CFloat"
35+ , token " double" >> return " CDouble"
36+ , token " int32_t" >> return " Int32"
37+ , token " int64_t" >> return " Int64"
38+ , do c <- option' " CChar" (token " signed" >> return " CSChar" )
39+ choice'
40+ [ token " char" >> return c
41+ , token " short" >> return " CShort"
42+ , token " int" >> return " CInt"
43+ , token " long" >>
44+ choice' [token " long" >> return " CLLong" , return " CLong" ]
45+ ]
46+ , do _ <- token " unsigned"
47+ choice'
48+ [ token " char" >> return " CUChar"
49+ , token " short" >> return " CUShort"
50+ , token " int" >> return " CUInt"
51+ , token " long" >>
52+ choice' [token " long" >> return " CULLong" , return " CULong" ]
53+ ]
54+ , token " struct" >> parseIdentifier >> return " ()" -- Hmmm...
55+ , token " GLvoid" >> return " ()" -- glGetPerfQueryDataINTEL still mentions this
56+ , parseIdentifier
57+ ]
5258
5359parseIdentifier :: ReadP String
5460parseIdentifier = do
5561 skipSpaces
5662 x <- satisfy (\ c -> isAlpha c || c == ' _' )
57- xs <- munch (\ c -> isAlphaNum c || c == ' _' )
58- return (x: xs)
63+ xs <- munch (\ c -> isAlphaNum c || c == ' _' )
64+ return (x : xs)
5965
6066parseArray :: ReadP Int
61- parseArray = choice' [
62- do _ <- token " ["
63- skipSpaces
64- _ <- munch1 isDigit
65- _ <- token " ]"
66- return 1 ,
67- return 0 ]
67+ parseArray =
68+ choice'
69+ [ do _ <- token " ["
70+ skipSpaces
71+ _ <- munch1 isDigit
72+ _ <- token " ]"
73+ return 1
74+ , return 0
75+ ]
6876
6977token :: String -> ReadP String
7078token s = skipSpaces >> string s
7179
7280-- deterministic versions
73-
7481choice' :: [ReadP a ] -> ReadP a
7582choice' = foldr (<++) pfail
7683
0 commit comments