1
1
-- A very simple-minded parser for C declarations of the following syntax:
2
2
-- "const"? type-specifier ("*" "const"?)* identifier ("[" number "]")?
3
- module DeclarationParser ( parse ) where
3
+ module DeclarationParser
4
+ ( parse
5
+ ) where
4
6
5
7
import Control.Monad
6
8
import Data.Char
@@ -26,51 +28,56 @@ optionalConst :: ReadP ()
26
28
optionalConst = option' () (void (token " const" ))
27
29
28
30
parseTypeSpecifier :: 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
+ ]
52
58
53
59
parseIdentifier :: ReadP String
54
60
parseIdentifier = do
55
61
skipSpaces
56
62
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)
59
65
60
66
parseArray :: 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
+ ]
68
76
69
77
token :: String -> ReadP String
70
78
token s = skipSpaces >> string s
71
79
72
80
-- deterministic versions
73
-
74
81
choice' :: [ReadP a ] -> ReadP a
75
82
choice' = foldr (<++) pfail
76
83
0 commit comments