-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.y
178 lines (161 loc) · 2.97 KB
/
Parser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{
module Parser (parseStr) where
import Lexer
import ExprU
}
%name parse prog
%tokentype { LToken }
%error { parseError }
%monad { Alex }
%lexer { lexer } { TkEOF }
%token
'(' {TkLParen}
')' {TkRParen}
int {TkInt $$}
'()' {TkUnit}
fun {TkFun}
'->' {TkArrow}
'-U>' {TkArrowU}
'-R>' {TkArrowR}
'-A>' {TkArrowA}
'-L>' {TkArrowL}
let {TkLet}
'=' {TkEq}
in {TkIn}
',' {TkComma}
letp {TkLetp}
Left {TkInl}
Right {TkInr}
case {TkMatch}
of {TkWith}
';' {TkSemi}
';;' {TkDoubleSemi}
'[' {TkLBrack}
']' {TkRBrack}
fst {TkFst}
snd {TkSnd}
var {TkVar $$}
gvar {TkGVar $$}
'+' {TkPlus}
'-' {TkMinus}
'<' {TkLt}
'>' {TkGt}
'==' {TkEqq}
or {TkOr}
and {TkAnd}
fix {TkFix}
wnew {TkWNew}
snew {TkSNew}
release {TkRelease}
srelease {TkSRelease}
swap {TkSwap}
sswap {TkSSwap}
%%
primop1 :: { PrimOp1 }
primop1 :
Left
{ PrInl }
| Right
{ PrInr }
| fst
{ PrFst }
| snd
{ PrSnd }
| fix
{ PrFix }
| wnew
{ PrWNew }
| snew
{ PrSNew }
| release
{ PrRelease }
| srelease
{ PrSRelease }
| swap
{ PrSwap }
| sswap
{ PrSSwap }
infixop2 :: { PrimOp2 }
infixop2 :
'+'
{ PrPlus }
| '-'
{ PrMinus }
| '>'
{ PrGt }
| '<'
{ PrLt }
| '=='
{ PrEqq }
| and
{ PrAnd }
| or
{ PrOr }
expr :: { Expr }
expr :
infexp
{ $1 }
| fun var '-U>' expr
{ ExAbs AQU (IdS $2) $4 }
| fun var '-R>' expr
{ ExAbs AQR (IdS $2) $4 }
| fun var '-A>' expr
{ ExAbs AQA (IdS $2) $4 }
| fun var '-L>' expr
{ ExAbs AQL (IdS $2) $4 }
| let var '=' expr in expr
{ ExLet (IdS $2) $4 $6 }
| letp '(' var ',' var ')' '=' expr in expr
{ ExLetp (IdS $3) (IdS $5) $8 $10 }
| case expr of Left var '->' expr ';' Right var '->' expr
{ ExMatch $2 (IdS $5) $7 (IdS $10) $12 }
| primop1 expr
{ ExPrim1 $1 $2 }
infexp :: {Expr}
infexp :
appexp
{ $1 }
| infexp infixop2 appexp
{ ExPrim2 $2 $1 $3 }
appexp :: {Expr}
appexp :
atexp
{ $1 }
| appexp atexp
{ ExApp $1 $2 }
atexp :: {Expr}
atexp :
int
{ ExLit (LiNum $1) }
| '()'
{ ExLit LiUnit }
| '(' expr ',' expr ')'
{ ExPrim2 PrPair $2 $4 }
| '[' expr ',' expr ']'
{ ExWith $2 $4 }
| var
{ ExVar (IdS $1) }
| gvar
{ ExGVar (IdS $1) }
| '(' expr ')'
{ $2 }
decl :: {Decl}
decl :
expr
{ DcLet (IdS "_") ($1)}
| let var '=' expr
{ DcLet (IdS $2) ($4) }
prog :: {[Decl]}
prog :
decl
{ [$1] }
| prog ';;' decl
{ $1 ++ [$3] }
{
parseError :: LToken -> Alex a
parseError t = do
p <- getPos
alexError (showPosn p ++ ":" ++ (show t))
parseStr :: String -> Either String [Decl]
parseStr s = runAlex s parse
}