1
-----------------------------------------------------------------------------
4
Abstract syntax for grammar files.
6
(c) 1993-2001 Andy Gill, Simon Marlow
7
-----------------------------------------------------------------------------
9
Here is the abstract syntax of the language we parse.
12
> AbsSyn(..), Directive(..),
13
> getTokenType, getTokenSpec, getParserNames, getLexer, getImportedIdentity, getMonad,
14
> getPrios, getPrioNames, getExpect
19
> (Maybe String) -- header
20
> [Directive String] -- directives
21
> [(String,[([String],String,Int,Maybe String)],Maybe String)] -- productions
22
> (Maybe String) -- footer
30
%-----------------------------------------------------------------------------
31
Parser Generator Directives.
33
ToDo: find a consistent way to analyse all the directives together and
34
generate some error messages.
37
> = TokenType String -- %tokentype
38
> | TokenSpec [(a,String)] -- %token
39
> | TokenName String (Maybe String) Bool -- %name/%partial (True <=> %partial)
40
> | TokenLexer String String -- %lexer
41
> | TokenImportedIdentity -- %importedidentity
42
> | TokenMonad String String String String -- %monad
43
> | TokenNonassoc [String] -- %nonassoc
44
> | TokenRight [String] -- %right
45
> | TokenLeft [String] -- %left
46
> | TokenExpect Int -- %expect
55
> = case [ t | (TokenType t) <- ds ] of
57
> [] -> error "no token type given"
58
> _ -> error "multiple token types"
60
> getParserNames ds = [ t | t@(TokenName _ _ _) <- ds ]
63
> = case [ (a,b) | (TokenLexer a b) <- ds ] of
66
> _ -> error "multiple lexer directives"
68
> getImportedIdentity ds
69
> = case [ (()) | TokenImportedIdentity <- ds ] of
72
> _ -> error "multiple importedidentity directives"
75
> = case [ (True,a,b,c,d) | (TokenMonad a b c d) <- ds ] of
77
> [] -> (False,"()","HappyIdentity",">>=","return")
78
> _ -> error "multiple monad directives"
80
> getTokenSpec ds = concat [ t | (TokenSpec t) <- ds ]
82
> getPrios ds = [ d | d <- ds,
84
> TokenNonassoc _ -> True
86
> TokenRight _ -> True
90
> getPrioNames (TokenNonassoc s) = s
91
> getPrioNames (TokenLeft s) = s
92
> getPrioNames (TokenRight s) = s
95
> = case [ n | (TokenExpect n) <- ds ] of
98
> _ -> error "multiple expect directives"