-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntaxes.ebnf
More file actions
189 lines (157 loc) · 8.08 KB
/
Copy pathsyntaxes.ebnf
File metadata and controls
189 lines (157 loc) · 8.08 KB
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
(* =========================================
File & Top-level
========================================= *)
SourceFile = TopItem*
TopItem = Annotation* Visibility? (
| UseDecl
| FunDecl | ConstFunDecl | ConstDecl | GlobalDecl
| TypeDecl | AbstractDecl | EffectDecl
| ExternalDecl
)
Visibility = "pub" | "pub" "(" "external" ")"
Annotation = "#" Ident ( "(" (Expr ("," Expr)*)? ")" )? NewLine
(* =========================================
Import / Use
========================================= *)
UseDecl = "use" "@"? Ident ("." Ident)* ("only" Ident ("," Ident)*)? "*"? NewLine
(* =========================================
Declarations
========================================= *)
ConstDecl = "const" Ident (":" Type)? "=" Expr NewLine
GlobalDecl = "global" Ident (":" Type)? "=" Expr NewLine
FunDecl = "fun" Ident GenericParams? "(" Param ("," Param)* ")" ("->" Type)? EffectRow? Where? ";"
ConstFunDecl= "const" FunDecl
FunDef = "fun" Ident GenericParams? "(" Param ("," Param)* ")" ("->" Type)? EffectRow? Where?
Indent Expr* Dedent
Param = Ident (":" Type)?
GenericParams = "[" GenericVar ("," GenericVar)* "]"
GenericVar = Ident
(* --- Type declarations --- *)
TypeDecl = "type" Ident GenericParams? ImplList? ";" (* opaque *)
TypeAlias = "type" Ident GenericParams? ImplList? "=" Type Where? NewLine (* alias *)
TypeStruct = "type" Ident GenericParams? ImplList? Where?
Indent Field* Dedent (* struct *)
AdtDecl = "type" Ident GenericParams? ImplList? Where?
Indent ("|" Ctor)* Dedent (* ADT *)
ImplList = "impl" Ident ("+" Ident)*
Field = Ident ":" Type NewLine
Ctor = Ident GenericParams? ("of" Type ("->" Type)?)? NewLine
(* --- Abstract type (trait-like) --- *)
AbstractDecl = "abst" Ident GenericParams? ImplList? NewLine Where?
Indent MethodDecl* Dedent
MethodDecl = "fun" Ident "(" Param ("," Param)* ")" ("->" Type)? EffectRow? ";" NewLine
(* --- Effect --- *)
EffectDecl = "effect" Ident NewLine
Indent ("|" EffectControl NewLine)* Dedent
EffectControl= Ident ("(" Param ("," Param)* ")")? ("->" Type)?
(* --- External --- *)
ExternalDecl = "external" "fun" Ident "(" Param ("," Param)* ("...")? ")" ("->" Type)? ("=" String)? ";" NewLine
CTypeDecl = "external" "ctype" Ident ";"
(* =========================================
Types
========================================= *)
Type = RefType | MutRefType | ShareType | ImplType
| TypeofType | WildcardType | TupleType | FunType | NamedType
RefType = "ref" Type
MutRefType = "ref" "mut" Type
ShareType = "share" Type
ImplType = "impl" Type
TypeofType = "typeof" "(" Expr ")"
WildcardType = "_"
TupleType = "(" TupleElement ("," TupleElement)* ")"
TupleElement = Type ("*" Int)?
FunType = "fun" "(" Type ("," Type)* ")" "->" Type EffectRow?
NamedType = Path ("[" Type ("," Type)* "]")?
(* Effect-row annotation:
fun f() pure (no raise): closed empty row
fun f() raise inferred open row
fun f() raise Foo+Bar definite {Foo, Bar}, closed
fun f() raise Foo | Bar definite {Foo}, possible {Bar}, closed
fun f() raise Foo | ^r definite {Foo}, open named tail r
fun f() raise Foo | ... definite {Foo}, anonymous open tail
fun f() raise ... fully polymorphic open row
Legacy: `raise Foo+...` == `raise Foo | ...`. *)
EffectRow = "raise" (EffectBody)?
EffectBody = "..." | EffectSum ("|" EffectTail)?
EffectSum = EffectAtom ("+" EffectAtom)*
EffectAtom = Path
EffectTail = "^" Ident | "..." | EffectSum
Where = "where" WhereConstraint ("," WhereConstraint)*
WhereConstraint = Ident ":" Type ("+" Type)*
(* =========================================
Expressions (Pratt parser, BP-based)
========================================= *)
(* Dispatcher first tries keyword forms, then falls to parse_expr_bp(0) *)
Expr = IfExpr | DoExpr | LetExpr | ReturnExpr | MatchExpr
| RaiseExpr | WithExpr | ResumeExpr | ConstEvalExpr
| UnsafeExternalCallExpr
| parse_expr_bp(0) (* Pratt core *)
(* Binding powers (higher = tighter):
60 : ^
50 : * / %
40 : + -
30 : == != < > <= >= (and `is`)
20 : && (and `as`)
10 : ||
1 : |> (Pipeline, left-assoc) <| (Reverse Pipe, right-assoc)
70 : prefix - ! , prefix custom UserOp
*)
(* Prefix (NUD) *)
PrefixExpr = ("-" | "!") PrefixExpr (* UnaryExpr, rbp=70 *)
| UserOp PrefixExpr (* custom prefix *)
| ("move" | "copy" | "share") Expr (* rbp=60 *)
| "ref" ["mut"] Expr (* rbp=60 *)
| Path (* StaticPathExpr *)
| ParenExpr | AtomExpr
AtomExpr = Int | Float | String | "..."
ParenExpr = "(" ")" (* empty tuple, AtomExpr *)
| "(" Expr ("," Expr)* ")" (* tuple, AtomExpr *)
| "(" Expr ")" (* grouping *)
(* Infix (LED) + postfix ops *)
ExprBP = ExprBP BinaryOp ExprBP (* BinaryExpr *)
| ExprBP "|>" ExprBP (* PipeLineExpr, left-assoc *)
| ExprBP "<|" ExprBP (* PipeBackExpr, right-assoc *)
| ExprBP UserOp{postfix} (* UnaryExpr, no RHS *)
| ExprBP "(" CallArg ("," CallArg)* ")" (* CallExpr *)
| ExprBP "is" Pattern (* IsExpr, bp=30 *)
| ExprBP "as" Type (* TypeCastExpr, bp=20 *)
| ExprBP "." Ident (* MemberAccessExpr *)
| ExprBP "." Int (* TupleIndexExpr *)
| ExprBP "{" StructFieldInit ("," StructFieldInit)* "}" (* MakeStructExpr *)
CallArg = (Ident "=")? Expr
StructFieldInit = Ident "=" Expr
(* Keyword expressions *)
DoExpr = "do" NewLine Indent Expr* Dedent
LetExpr = "let" ["mut"] Ident (":" Type)? "=" Expr
ReturnExpr= "return" Expr?
IfExpr = "if" Expr ("then" Expr | NewLine BlockExpr)
("elif" Expr NewLine BlockExpr)*
("else" (NewLine BlockExpr | Expr))?
MatchExpr = "when" Expr NewLine Indent MatchArm* Dedent
MatchArm = Pattern ("if" Expr)? "=>" Expr NewLine
RaiseExpr = "raise" Path (" Expr ("," Expr)* ")"
WithExpr = "with" Expr ("catch" Path "(" CatchParam ("," CatchParam)* ")" NewLine BlockExpr)*
CatchParam= ".." | "binding" Ident
ResumeExpr = "resume" Expr
ConstEvalExpr = "const" "(" Expr ")"
UnsafeExternalCallExpr = "unsafe_call_external" Path "(" Expr ("," Expr)* ")"
(* =========================================
Patterns
========================================= *)
Pattern = PatternUnit ("|" PatternUnit)* (* OrPat *)
PatternUnit = SinglePattern ("binding" Ident)? (* AliasPat *)
SinglePattern = AtomicPattern | (SinglePattern "binding" Ident)
AtomicPattern =
| "_" (* WildcardPat *)
| "binding" Ident (* BindingPat *)
| ".." (* RestPat *)
| Int | Float | String (* LiteralPat *)
| "(" PatternItem ("," PatternItem)* ")" (* TuplePat *)
| Ident "(" PatternItem ("," PatternItem)* ")" (* ConstructorPat *)
| Ident "{" StructPatField ("," StructPatField)* ".."? "}" (* StructPat *)
| Ident (* plain BindingPat *)
PatternItem = Pattern | ".."
StructPatField = Ident (* sugar: x = binding x *)
(* Shared helpers *)
Path = Ident ("." Ident)*
BlockExpr = Indent Expr* Dedent