-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.ebnf
210 lines (171 loc) · 6.85 KB
/
syntax.ebnf
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
(*
EBNF Syntax Definition for the Elm programming language, version 0.15
Author: Jeff Smits
Reproduced based on the existing language by Evan Czaplicki
*)
(* Start symbol *)
Start = Module
(* Lexical primitives *)
lower = "a" | "b" | "c" | "d" | "e" | "f" | "g"
| "h" | "i" | "j" | "k" | "l" | "m" | "n"
| "o" | "p" | "q" | "r" | "s" | "t" | "u"
| "v" | "w" | "x" | "y" | "z"
upper = "A" | "B" | "C" | "D" | "E" | "F" | "G"
| "H" | "I" | "J" | "K" | "L" | "M" | "N"
| "O" | "P" | "Q" | "R" | "S" | "T" | "U"
| "V" | "W" | "X" | "Y" | "Z"
number = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
id-char = lower | upper | number | "_" | "'"
anything = ? any single character ?
nothing = ""
newline = ? newline character ? | ? carriage return character ?
(* Lexical syntax *)
(* Assume greedy matching for repetition *)
ID = lower, {id-char}
UPPERID = upper, {id-char}
INT = ["-"], number, {number}
FLOAT = {number}, ".", number, {number}
PREFIXTUPLEOP = "(,", {","}, ")"
BinOp = BinOpChar, {BinOpChar}
| CONSTROP
| ". "
NonBinOp = "." | "=" | "->" | "<-" | "--"
BINOP = BinOp - NonBinOp
BinOpChar = "~" | "!" | "@" | "#" | "$" | "%" | "^" | "&" | "*" | "-"
| "+" | "=" | "<" | ">" | "/" | "?" | "\" | "|" | "." | "_"
CONSTROP = ":", ConstrOpChar, {ConstrOpChar}
ConstrOpChar = BinOpChar | ":"
STRING = '"', {StringChar}, '"'
| '"""', {HereDocChar}, '"""'
HereDocChar = anything - '"'
| '"', ? not followed by '""' ?
StringChar = anything - newline - '"'
| "\", '"'
| "\", ? not followed by '"' ?
CHAR = "'" CharChar "'"
CharChar = anything - newline - "'"
LAYOUT = " " | newline | BlockComment
CommentChar = "{", ? not followed by "-" ?
BlockComment = "{-" {InsideComment} "-}"
InsideComment = anything - "{"
| CommentChar
| BlockComment
LAYOUT = "--", anything - newline - BinOpChar, { anything - newline } NewLineEOF
NewLineEOF = newline
| ? End of file ?
(*
Some influence from SDF can be seen in the follow restrictions and exceptions used.
Most system should be able to reproduce this, it's a basic lookahead
*)
(*
From here on out, consider every comma (,) as denotation of
optional layout (,{LAYOUT},) unless specified otherwise:
*)
bind = ? No layout allowed between the things left and right of this sort ?
(* Common *)
Literal (* Int *) = INT
(* Float *) | FLOAT
(* String *) | STRING
(* Char *) | CHAR
Ref (* VarRef *) = ID
(* OpRef *) | "(", BINOP, ")"
BinOp (* BinOp *) = BINOP
(* InfixFun *) | "`", bind, ID-CF, bind, "`"
(* InfixTag *) | "`", bind, UPPERID-CF, bind, "`"
UpperIds = UPPERID, bind, {".", bind, UPPERID}
(* Types *)
Type (* bracket *) = "(", Type, ")"
(* TVar *) | ID
(* TTag *) | UPPERID, {Type}
(* TFun *) | Type, "->", Type (* right associative *)
(* TUnit *) | "(", ")"
(* TTuple *) | "(", Type, ",", Type, {",", Type}, ")"
(* TRecord *) | "{", {",", TRecBind}, "}"
(* TRecExt *) | "{", ID, "|", {",", TRecBind}, "}"
TRecBind (* TRecBind *) = ID, ":", Type
(* Patterns *)
Pattern (* bracket *) = "(", Pattern, ")"
(* PVar *) | ID
(* PADT *) | UPPERID, {Pattern}
(* PWld *) | "_"
(* PUnit *) | "(", ")"
(* PTuple *) | "(", Pattern, ",", Pattern, {",", Pattern}, ")"
(* PList *) | "[", [ Pattern, {",", Pattern} ], "]"
(* PBinOp *) | Pattern, CONSTROP, Pattern (* left associative *)
(* PRecord *) | "{", {",", Pattern}, "}"
| Literal
(* Definitions *)
Def (* AnnotatedDef *) = TypeDef, ValueDef
| ValueDef
TypeDef (* NameAnno *) = ID, ":", Type
(* OpAnno *) | "(", BINOP, ")", ":", Type
ValueDef (* PrefixOpDef *) = "(", BINOP, ")", {Pattern}, "=", Expr
(* InfixOpDef *) | Pattern, BINOP, {Pattern}, "=", Expr
(* NameDef *) | ID, {Pattern}, "=", Expr
(* Modules *)
Module (* Module *) = "module", UpperIds, Exports, "where"
, {Import}, TopLevelStatement, {TopLevelStatement}
(* Main *) | {Import}, TopLevelStatement, {TopLevelStatement}
Exports (* ExportAll *) = ""
(* Exports *) | "(", [ Export, {",", Export} ], ")"
Export (* ADT *) = UPPERID, "(", [ ADTExport, {",", ADTExport} ], ")"
(* ADTNone *) | UPPERID
(* ADTAll *) | UPPERID, "(", "..", ")"
| Ref
ADTExport (* All *) = ".."
| ID
Import (* Import *) = "import", UpperIds, ModAlias, Exposing
ModAlias (* ModAlias *) = "as", UPPERID
(* NoModAlias *) | ""
Exposing (* NoExposing *) = ""
(* Exposing *) | "[", "exposing", "(", [ Export {", " Export} ], ")"
TopLevelStatement (* AliasDef *) = "type", "alias", UPPERID, {ID}, "=" Type
(* ADTDef *) | "type", UPPERID, {ID}, "=", ADTDef, {"|", ADTDef}
(* Port *) | "port", TypeDef, "port", ValueDef
| Def
ADTDef (* ADTCons *) = UPPERID, Type, {Type}
(* NOTE: You can do a two-phase parse where you first find these to gain a
precedence information on operators *)
TopLevelStatement (* Fixity *) = "infix", bind, Fixity, INT, BinOp
Fixity (* Left *) = "l"
(* Right *) | "r"
(* Expressions *)
RecBind (* RecBind *) = ID, "=", Expr
MwIfBranch (* MwIfBr *) = "|", Expr, "->", Expr
CaseBranch.CaseBr = Pattern, "->", Expr
RecAdd (* RecAdd *) = ID, "=", Expr
RecUpd (* RecUpd *) = ID, "<-", Expr
Expr (* Unit *) = "(", ")"
(* Tuple *) | "(", Expr, ",", Expr, {",", Expr}, ")"
(* List *) | "[", [Expr, {",", Expr}], "]"
(* Range *) | "[", Expr, "..", Expr, "]"
(* Rec *) | "{", [RecBind, {",", RecBind}], "}"
(* ADT *) | UPPERID
(* PrefTOp *) | PREFIXTUPLEOP
(* RecRem *) | "{", ID, "-", ID, "}"
(* RecAdd *) | "{", ID, "|", RecAdd, "}"
(* RecRen *) | "{", ID, "-", ID, "|", RecAdd, "}"
(* RecUpd *) | "{", ID, "|", RecUpd, {",", RecUpd}, "}"
(* QualRef *) | UpperIds, bind, ".", bind, ID
(* RecGet *) | Expr, bind, ".", bind, ID
(* Getter *) | " .", bind, ID
(* bracket *) | "(", Expr, ")"
(* If *) | "if", Expr, "then", Expr, "else", Expr
(* MwIf *) | "if", MwIfBranch, {MwIfBranch}
(* Case *) | "case", Expr, "of", CaseBranch, {CaseBranch}
(* Lam *) | "\", Pattern, {Pattern}, "->", Expr
(* App *) | Expr, Expr (* left associative *)
(* Let *) | "let", Def, {Def}, "in", Expr
(* EBinOp *) | Expr, BinOp, Expr (* left associative *)
| Literal
| Ref
(* General precedence:
Expr.App >
Expr.EBinOp > {
Expr.If
Expr.MwIf
Expr.Case
Expr.Lam
Expr.Let
}
*)