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