-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrammar.txt
More file actions
247 lines (193 loc) · 4.93 KB
/
grammar.txt
File metadata and controls
247 lines (193 loc) · 4.93 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# Program --------------------------------------------------------------
program :
| statement*
# Statements -----------------------------------------------------------
statement :
| statement_type NEWLINE
statement_type :
| import
| jump
| label
| continue
| expr
import :
| "import" IMPORT_PATH IMPORT_IDENT
| "import" "from" IMPORT_PATH ":" IMPORT_IDENT ("," IMPORT_IDENT)*
jump :
| "jump" VAR_IDENT
label :
| ":" VAR_IDENT ":"
# Expressions ----------------------------------------------------------
expr :
| basic_expr
| simple_expr
| compound_expr
# Basic Expressions ----------------------------------------------------
#
# Basic expressions can be used anywhere.
basic_expr :
| "(" basic_expr ")"
| atom
| sum
| assignment
atom :
| "nil"
| "true" | "false"
| INT
| FLOAT
| string
| format_string
| tuple
| IDENT
string :
| QUOTE CHAR* QUOTE
format_string :
| "$" QUOTE format_string_part* QUOTE
format_string_part :
| "{" expr "}"
| CHAR*
tuple_expr :
| basic_expr
| inline_expr
tuple :
| "(" ")"
| "(" tuple_expr "," ")"
| "(" tuple_expr ("," tuple_expr)+ [","] ")"
sum :
| expr ("+" | "-") product
| product
product :
| expr ("*" | "/" | "//" | "%") power
| power
power :
| expr ("^" power)
| expr
assignment :
| IDENT "=" expr
# Simple Expressions ---------------------------------------------------
#
# Simple expressions are non-compound expressions that may not be valid
# in certain places.
simple_expr :
| "(" simple_expr ")"
| break
break :
| "break"
| "break" expr
# Compound Expressions -------------------------------------------------
#
# Compound expressions contain either an indented block of statements
# or an inline expression on the same line. Inline compound expressions
# can be used in all the same places as basic expressions.
compound_expr :
| block
| if
| loop
| func
| call
| type
| inline_expr
inline_expr :
| inline_block
| inline_if
| inline_loop
| inline_func
| inline_type
# A suite is an indented block of statements, often referred to as
# a block. The name suite is used here to distinguish from block
# expressions.
suite :
| NEWLINE INDENT statement+ DEDENT
block :
| "block" "->" suite
inline_block :
| "block" "->" (jump | basic_expr | simple_expr | inline_expr)
if :
| "if" cond "->" suite (else_if | inline_else_if)
| "if" cond "->" suite [(else | inline_else)]
else_if :
| "else if" cond "->" suite (else_if | inline_else_if)
| "else if" cond "->" suite [(else | inline_else)]
else :
| "else" "->" suite
inline_if :
| "if" cond "->" expr inline_else_if
| "if" cond "->" expr [inline_else]
inline_else_if :
| "else if" cond "->" expr inline_else_if
| "else if" cond "->" expr [inline_else]
inline_else :
| "else" "->" expr
match :
| "match" cond "->" NEWLINE INDENT (match_arm)+ [default_match_arm]
| "match" cond "->" NEWLINE INDENT (match_arm)* default_match_arm
match_arm :
| expr "->" (suite | expr)
default_match_arm :
| ":" "->" (suite | expr)
loop :
| "loop" "->" suite
| "loop" cond "->" suite
| "loop" IDENT "<-" expr "->" suite
inline_loop :
| "loop" "->" expr
| "loop" cond "->" expr
| "loop" IDENT "<-" expr "->" expr
cond :
| basic_expr
| call
| inline_block
func :
| params "=>" suite
inline_func :
| params "=>" expr
params :
| "(" [VAR_IDENT ("," VAR_IDENT)* [","]] ")"
call :
| ( expr ) "(" [args] ")"
args :
| tuple_expr ("," tuple_expr)* [","]
type :
| params "=>" suite
inline_type :
| params "=>" expr
# Comments -------------------------------------------------------------
comment :
| "#" COMMENT_CHAR*
# Character classes ----------------------------------------------------
CHAR : ".*"
COMMENT_CHAR : "[^\n]*"
QUOTE : "[\"']"
IDENT:
| VAR_IDENT
| SPECIAL_IDENT
| TYPE_IDENT
| TYPE_FUNC_IDENT
# Identifiers
VAR_IDENT : "[a-z]|[a-z][a-z0-9_]*[a-z0-9]"
CONST_IDENT : "[A-Z]|[A-Z][A-Z0-9_]*[A-Z0-9]"
TYPE_IDENT : "[A-Z]|[A-Z][A-Za-z0-9]*[A-Za-z0-9]"
TYPE_FUNC_IDENT : "@" VAR_IDENT
SPECIAL_IDENT : "$" VAR_IDENT
# Imports
IMPORT_PATH :
| VAR_IDENT ("." VAR_IDENT)*
IMPORT_IDENT :
| VAR_IDENT ["as" VAR_IDENT]
# Numbers --------------------------------------------------------------
NUM : INT | FLOAT
INT : (INT_02 | INT_08 | INT_10 | INT_16)
INT_02 : "[+-]?0b_?{ BINARY }"
INT_08 : "[+-]?0o_?{ OCTAL }"
INT_10 : "[+-]?({ ZERO }|{ NATURAL })"
INT_16 : "[+-]?0x_{ HEX }"
FLOAT :
| "{ DECIMAL }\.{ DECIMAL }"
| "{ DECIMAL }(\.{ DECIMAL })?(e|E)[+-]?{ DECIMAL }"
# Numeric character sets allowing for underscores between digits
ZERO : "0(_?0+)*"
BINARY : "[0-1](_?[0-1]+)*"
OCTAL : "[0-7](_?[0-7]+)*"
DECIMAL : "[0-9](_?[0-9]+)*"
NATURAL : "[1-9](_?[0-9]+)*"
HEX : "[A-Za-z0-9](_?[A-Za-z0-9]+)*"