-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParCinnabar.y
184 lines (172 loc) · 5.62 KB
/
ParCinnabar.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
179
180
181
182
183
-- This Happy file was machine-generated by the BNF converter
{
{-# OPTIONS_GHC -fno-warn-incomplete-patterns -fno-warn-overlapping-patterns #-}
module ParCinnabar where
import AbsCinnabar
import LexCinnabar
import ErrM
}
%name pProgram Program
-- no lexer declaration
%monad { Err } { thenM } { returnM }
%tokentype {Token}
%token
'!' { PT _ (TS _ 1) }
'!=' { PT _ (TS _ 2) }
'#{' { PT _ (TS _ 3) }
'%' { PT _ (TS _ 4) }
'&&' { PT _ (TS _ 5) }
'(' { PT _ (TS _ 6) }
')' { PT _ (TS _ 7) }
'*' { PT _ (TS _ 8) }
'+' { PT _ (TS _ 9) }
',' { PT _ (TS _ 10) }
'-' { PT _ (TS _ 11) }
'.' { PT _ (TS _ 12) }
'/' { PT _ (TS _ 13) }
':' { PT _ (TS _ 14) }
';' { PT _ (TS _ 15) }
'<' { PT _ (TS _ 16) }
'<=' { PT _ (TS _ 17) }
'=' { PT _ (TS _ 18) }
'==' { PT _ (TS _ 19) }
'>' { PT _ (TS _ 20) }
'>=' { PT _ (TS _ 21) }
'[' { PT _ (TS _ 22) }
']' { PT _ (TS _ 23) }
'^' { PT _ (TS _ 24) }
'assert' { PT _ (TS _ 25) }
'else' { PT _ (TS _ 26) }
'extend' { PT _ (TS _ 27) }
'false' { PT _ (TS _ 28) }
'for' { PT _ (TS _ 29) }
'fun' { PT _ (TS _ 30) }
'if' { PT _ (TS _ 31) }
'in' { PT _ (TS _ 32) }
'lambda' { PT _ (TS _ 33) }
'new' { PT _ (TS _ 34) }
'print' { PT _ (TS _ 35) }
'return' { PT _ (TS _ 36) }
'true' { PT _ (TS _ 37) }
'while' { PT _ (TS _ 38) }
'with' { PT _ (TS _ 39) }
'{' { PT _ (TS _ 40) }
'||' { PT _ (TS _ 41) }
'}' { PT _ (TS _ 42) }
L_ident { PT _ (TV $$) }
L_charac { PT _ (TC $$) }
L_quoted { PT _ (TL $$) }
L_integ { PT _ (TI $$) }
%%
Ident :: { Ident } : L_ident { Ident $1 }
Char :: { Char } : L_charac { (read ( $1)) :: Char }
String :: { String } : L_quoted { $1 }
Integer :: { Integer } : L_integ { (read ( $1)) :: Integer }
Program :: { Program }
Program : ListStmt { AbsCinnabar.Prog (reverse $1) }
Block :: { Block }
Block : '{' ListStmt '}' { AbsCinnabar.SBlock (reverse $2) }
ListStmt :: { [Stmt] }
ListStmt : {- empty -} { [] } | ListStmt Stmt { flip (:) $1 $2 }
Stmt :: { Stmt }
Stmt : 'while' '(' Expr ')' Block { AbsCinnabar.SWhile $3 $5 }
| 'if' '(' Expr ')' Block { AbsCinnabar.SCond $3 $5 }
| 'if' '(' Expr ')' Block 'else' Block { AbsCinnabar.SCondElse $3 $5 $7 }
| LVal '=' Expr ';' { AbsCinnabar.SAssign $1 $3 }
| 'return' Expr ';' { AbsCinnabar.SReturn $2 }
| 'print' Expr ';' { AbsCinnabar.SPrint $2 }
| 'assert' Expr ';' { AbsCinnabar.SAssert $2 }
| Expr ';' { AbsCinnabar.SExpr $1 }
LVal :: { LVal }
LVal : LVal1 { $1 } | '{' ListLVal1 '}' { AbsCinnabar.ATuple $2 }
ListLVal1 :: { [LVal] }
ListLVal1 : LVal1 { (:[]) $1 } | LVal1 ',' ListLVal1 { (:) $1 $3 }
LVal1 :: { LVal }
LVal1 : Expr8 '[' Expr ']' { AbsCinnabar.AAt $1 $3 }
| Expr8 '.' Ident { AbsCinnabar.AMember $1 $3 }
| Ident { AbsCinnabar.AVar $1 }
Expr :: { Expr }
Expr : 'lambda' ListIdent ':' Expr { AbsCinnabar.ELambda $2 $4 }
| 'fun' '(' ListIdent ')' Block { AbsCinnabar.EFun $3 $5 }
| Expr1 'if' Expr 'else' Expr { AbsCinnabar.EIf $1 $3 $5 }
| Expr1 { $1 }
ListIdent :: { [Ident] }
ListIdent : {- empty -} { [] }
| Ident { (:[]) $1 }
| Ident ',' ListIdent { (:) $1 $3 }
Expr1 :: { Expr }
Expr1 : Expr2 '||' Expr1 { AbsCinnabar.EOr $1 $3 } | Expr2 { $1 }
Expr2 :: { Expr }
Expr2 : Expr3 '&&' Expr2 { AbsCinnabar.EAnd $1 $3 } | Expr3 { $1 }
Expr3 :: { Expr }
Expr3 : Expr4 RelOp Expr4 { AbsCinnabar.ERel $1 $2 $3 }
| Expr4 { $1 }
Expr4 :: { Expr }
Expr4 : Expr4 AddOp Expr5 { AbsCinnabar.EAdd $1 $2 $3 }
| Expr5 { $1 }
Expr5 :: { Expr }
Expr5 : Expr5 MulOp Expr6 { AbsCinnabar.EMul $1 $2 $3 }
| Expr6 { $1 }
Expr6 :: { Expr }
Expr6 : Expr6 '^' Expr7 { AbsCinnabar.EPow $1 $3 } | Expr7 { $1 }
Expr7 :: { Expr }
Expr7 : '!' Expr7 { AbsCinnabar.ENot $2 }
| '-' Expr7 { AbsCinnabar.ENeg $2 }
| Expr8 { $1 }
Expr8 :: { Expr }
Expr8 : Expr8 '(' ListExpr ')' { AbsCinnabar.ECall $1 $3 }
| Expr8 '.' Ident { AbsCinnabar.EMember $1 $3 }
| Expr8 '[' Expr ']' { AbsCinnabar.EAt $1 $3 }
| Expr9 { $1 }
Expr9 :: { Expr }
Expr9 : 'extend' Expr 'with' Expr9 { AbsCinnabar.EExtend $2 $4 }
| 'new' Expr9 { AbsCinnabar.ENew $2 }
| Expr10 { $1 }
Expr10 :: { Expr }
Expr10 : Char { AbsCinnabar.EChar $1 }
| String { AbsCinnabar.EString $1 }
| Integer { AbsCinnabar.ELitInt $1 }
| 'true' { AbsCinnabar.ELitTrue }
| 'false' { AbsCinnabar.ELitFalse }
| Ident { AbsCinnabar.EVar $1 }
| '[' ListExpr ']' { AbsCinnabar.EList $2 }
| '[' Expr 'for' LVal 'in' Expr ']' { AbsCinnabar.EListComp $2 $4 $6 }
| '#{' ListDictMap '}' { AbsCinnabar.EDict $2 }
| '(' Expr ')' { $2 }
ListExpr :: { [Expr] }
ListExpr : {- empty -} { [] }
| Expr { (:[]) $1 }
| Expr ',' ListExpr { (:) $1 $3 }
DictMap :: { DictMap }
DictMap : Expr ':' Expr { AbsCinnabar.EDictMap $1 $3 }
ListDictMap :: { [DictMap] }
ListDictMap : {- empty -} { [] }
| DictMap { (:[]) $1 }
| DictMap ',' ListDictMap { (:) $1 $3 }
RelOp :: { RelOp }
RelOp : '<' { AbsCinnabar.Lt }
| '<=' { AbsCinnabar.Le }
| '>' { AbsCinnabar.Gt }
| '>=' { AbsCinnabar.Ge }
| '==' { AbsCinnabar.Eq }
| '!=' { AbsCinnabar.Ne }
AddOp :: { AddOp }
AddOp : '+' { AbsCinnabar.Add } | '-' { AbsCinnabar.Sub }
MulOp :: { MulOp }
MulOp : '*' { AbsCinnabar.Mul }
| '/' { AbsCinnabar.Div }
| '%' { AbsCinnabar.Mod }
{
returnM :: a -> Err a
returnM = return
thenM :: Err a -> (a -> Err b) -> Err b
thenM = (>>=)
happyError :: [Token] -> Err a
happyError ts =
Bad $ "syntax error at " ++ tokenPos ts ++
case ts of
[] -> []
[Err _] -> " due to lexer error"
_ -> " before " ++ unwords (map (id . prToken) (take 4 ts))
myLexer = tokens
}