a6a095b9abdc8c44807e5d8a343a5ce3c19c21eb
[ghc-hetmet.git] / utils / hpc / HpcParser.y
1
2 module HpcParser where
3
4 import HpcLexer
5 }
6
7 %name parser
8 %tokentype { Token }
9 %error { \ e -> error $ show (take 10 e) }
10
11 %token
12         MODULE          { ID "module" }
13         TICK            { ID "tick" }
14         EXPRESSION      { ID "expression" }
15         ON              { ID "on" }
16         LINE            { ID "line" }
17         POSITION        { ID "position" }
18         FUNCTION        { ID "function" }
19         INSIDE          { ID "inside" }
20         AT              { ID "at" }
21         ':'             { SYM ':' }
22         '-'             { SYM '-' }
23         ';'             { SYM ';' }
24         '{'             { SYM '{' }
25         '}'             { SYM '}' }
26         int             { INT $$ }
27         string          { STR $$ }
28         cat             { STR $$ }
29 %%
30
31 Spec    :: { Spec }
32 Spec    : Ticks Modules         { Spec ($1 []) ($2 []) }
33
34 Modules :: { L (ModuleName,[Tick]) }
35 Modules : Modules Module        { $1 . ((:) $2) }
36         |                       { id }
37         
38 Module :: { (ModuleName,[Tick]) }
39 Module  : MODULE string '{' TopTicks '}'
40                                 { ($2,$4 []) }
41
42 TopTicks :: { L Tick }
43 TopTicks : TopTicks TopTick     { $1 . ((:) $2) }
44          |                      { id }
45         
46 TopTick :: { Tick }
47 TopTick : Tick                  { ExprTick $1 }
48         | TICK FUNCTION string optQual optCat ';'
49                                 { TickFunction $3 $4 $5 }
50         | INSIDE string '{' TopTicks '}'
51                                 { InsideFunction $2 ($4 []) }
52                                  
53 Ticks   :: { L ExprTick }
54 Ticks   : Ticks  Tick           { $1 . ((:) $2) }
55         |                       { id } 
56         
57 Tick   :: { ExprTick }
58 Tick    : TICK optString optQual optCat ';'
59                                 { TickExpression False $2 $3 $4 }
60
61 optString :: { Maybe String }
62 optString : string              { Just $1 }
63           |                     { Nothing }
64         
65 optQual :: { Maybe Qualifier }
66 optQual : ON LINE int           { Just (OnLine $3) }
67         | AT POSITION int ':' int '-' int ':' int
68                                 { Just (AtPosition $3 $5 $7 $9) }
69         |                       { Nothing }
70 optCat  :: { Maybe String }
71 optCat  : cat                   { Just $1 }
72         |                       { Nothing }
73
74 {
75 type L a = [a] -> [a]
76         
77 type ModuleName = String
78
79 data Spec 
80   = Spec [ExprTick] [(ModuleName,[Tick])]
81    deriving (Show)
82
83 data ExprTick
84   = TickExpression Bool (Maybe String) (Maybe Qualifier) (Maybe String)
85    deriving (Show)
86
87 data Tick
88   = ExprTick ExprTick
89   | TickFunction   String (Maybe Qualifier) (Maybe String)
90   | InsideFunction String [Tick]
91    deriving (Show)
92
93 data Qualifier = OnLine Int
94                | AtPosition Int Int Int Int
95    deriving (Show)             
96
97
98
99 hpcParser :: String -> IO Spec
100 hpcParser filename = do
101   txt <- readFile filename
102   let tokens = initLexer txt
103   return $ parser tokens        
104
105
106 }