forked from CakeML/cakeml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.txt
195 lines (143 loc) · 5.24 KB
/
grammar.txt
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
184
185
186
187
188
189
190
191
192
193
194
195
Infixes are assigned to 9 different levels. From tightest to loosest, they are
9 : (‘multiplication’) "div" "mod" and symbolic tokens beginning with "*",
"/", "%" or "&". Left associative
8 : (‘addition’) symbolic tokens beginning with "+", "-", or "^", or
tokens of at least two characters beginning
with "|". On its own, "|" is used to separate
patterns. Left associative
7 : (‘list-ops’) symbolic tokens other than ":" and ":=" that begin
with ":" or "@". *Right* associative
6 : (‘relations’) symbolic tokens beginning with "<", ">", "=", or "~"
(except that exactly "~" can't be an infix; it is used
for unary negation). Left associative
5 : (‘composition’) "o" and ":=" only. Left associative
4 : (‘before’) "before" only. Left associative
3 : (‘typing’) the ":" of type annotation. Not associative.
2 : (‘andalso’) "andalso". Left associative
1 : (‘orelse’) "orelse". Left associative
Other symbol tokens are possible as prefixes. These are "~" (exactly), and those that begin with "?" or "!".
The 5 token-infix classes are defined in gramScript under the names
valid{Add,Mult,Prefix,List,Rel}Sym
They can be adjusted safely as long as the mutual incompatibility
result is proved, and ":=" isn't claimed by any of them.
----------------------------------------------------------------------
V ::= "op" ID | ID
InfixOp ::= "*" | "+" | "-" | "/" | "::" | ":=" | "<" | "<="
| "<>" | "=" | ">" | ">=" | "@" | "before" | "div"
| "mod" | "o"
// some of these are boolean (Opb), some numeric (Opn)
Error ::= "Bind" // Bind_error
| "Div" // Div_error
LogicalOp ::= "andalso" // And
| "orelse" // Or
Literal ::= IntegerLiteral // IntLit i
| "True" // Bool T
| "False" // Bool F
| "()" // Unit
| StringLiteral // String s
(* See http://www.mlton.org/OperatorPrecedence *)
Ebase ::=
"(" E ")"
| V
| Literal
| "let" LetDecs "in" Eseq "end"
Eseq ::= E | E ";" Eseq
Eapp ::=
Eapp Ebase | Ebase | ConstructorName Etuple
Etuple ::= "(" Elist2 ")"
Elist2 ::= Elist1 "," E
Elist1 ::= E | Elist1 "," E
(* level 7 *)
Emult ::=
Emult MultOp Ebase | Ebase
(* level 6 *)
Eadd ::=
Eadd AddOp Emult | Emult
(* level 5 - note, right associative *)
Econs ::=
Eadd ListOp Econs | Eadd
(* level 4 *)
Erel ::=
Erel RelOp Econs | Econs
(* level 3 *)
Ecomp ::=
Ecomp CompOp Erel | Erel
(* level 0 *)
Ebefore ::=
Ebefore BeforeOp Ecomp | Ecomp
Etyped ::=
Ebefore
| Ebefore ":" Type
(* logical connectives *)
ElogicAND ::=
ElogicAND "andalso" Etyped
| Etyped
ElogicOR ::=
| ElogicOR "orelse" ElogicAND
| ElogicAND
E ::=
"if" E "then" E "else" E
| "case" E "of" PEs
| "fn" V "=>" E
| "raise" E
| ElogicOR
Fdecl ::= V V "=" E // (V,V,E)
// second V is parameter
AndFDecls ::= FDecl // ([FDecl])
| FDecl "and" AndFDecls // FDecl :: AndFDecls
LetDec ::= "val" V "=" E
| "fun" AndFDecls
LetDecs ::= LetDec LetDecs
| ";" LetDecs
|
PEs ::= PE // ([PE])
| PE "|" PEs // PE::PEs
PE ::= Pattern "=>" E // (Pattern, E)
Pattern ::= E
// operators must be constructors and variables used linearly.
// constructors are Pvar, Plit and Pcon
Type ::=
DType // DType
| DType "->" Type // Ast_Tfn DType Type
DType ::=
TyVar // Ast_Tvar
| DType TyOp // Ast_Tapp [DType] TyOp
| "(" TypeList ")" Tyop // Ast_Tapp TypeList TyOp
| "(" Type ")" // Type
TypeList ::=
Type // [Type]
| Type "," TypeList // Type::TypeList
TypeDec ::=
"datatype" DtypeDecls // DtypeDeclList
DtypeDecls ::=
DtypeDecl // [DtypeDecl]
| DtypeDecl "and" DtypeDecls // DtypeDecl :: DtypeDecls
DtypeDecl ::= TypeName "=" DtypeCons
TypeName ::= TyOp
| TyVar TyOp
| "(" TyVarList ")" TyOp
TyVarList ::= TyVar | TyVarList "," TyVar
Dconstructor ::= ConstructorName "of" StarTypesP
| ConstructorName
DtypeCons ::= Dconstructor
| Dconstructor "|" DtypeCons
StarTypesP ::= "(" StarTypes ")" | StarTypes
StarTypes ::=
| DType
| StarTypes "*" DType
Decl ::= "val" Pattern "=" E
| "fun" AndFDecls
| TypeDec
| "type" TypeName "=" Type
SpecLine ::= "val" V ":" Type
| "type" TypeName OptTypEqn
| "exception" Dconstructor
| TypeDec
OptTypEqn ::= "=" Type |
SpecLineList ::= SpecLine SpecLineList
| ";" SpecLineList
|
SignatureValue ::= "sig" SpecLineList "end"
OptionalSignatureAscription ::= ":>" SignatureValue
|
Structure ::= "structure" StructName OptionalSignatureAscription "=" "struct" Decls "end"