-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclassy.sweet.js
More file actions
298 lines (255 loc) · 9.88 KB
/
Copy pathclassy.sweet.js
File metadata and controls
298 lines (255 loc) · 9.88 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* sweet.js macros for classy.js
* adapted from sweet.js docs
* https://github.com/mozilla/sweet.js/issues/169
* https://github.com/mozilla/sweet.js/issues/278
**/
let __TO_STR__ = macro
{
case { _ $tok } => { return [makeValue(unwrapSyntax(#{ $tok }), #{ $tok })]; }
}
// DEFAULT_PARAMS_ initializes function/method parameters to given values
macro DEFAULT_PARAMS_
{
case { $default #$numargs:lit# $param:ident = $value:expr , $rest ... } => {
var numargs = unwrapSyntax(#{ $numargs });
letstx $numargsNext = [makeValue(parseInt(numargs, 10)+1, #{$default})];
return #{
if ($numargsNext > arguments.length) $param = $value;
DEFAULT_PARAMS_ #$numargsNext# $rest ...
}
}
case { _ #$numargs:lit# $param:ident = $value:expr $rest ... } => {
var numargs = unwrapSyntax(#{ $numargs });
letstx $numargsNext = [makeValue(parseInt(numargs, 10)+1, #{$default})];
return #{
if ($numargsNext > arguments.length) $param = $value;
$rest ...
}
}
case { $default #$numargs:lit# $param:ident , $rest ... } => {
var numargs = unwrapSyntax(#{ $numargs });
letstx $numargsNext = [makeValue(parseInt(numargs, 10)+1, #{$default})];
return #{
DEFAULT_PARAMS_ #$numargsNext# $rest ...
}
}
case { _ #$numargs:lit# $[...]$param:ident $rest ... } => {
return #{
var $param = Array.prototype.slice.call(arguments, $numargs);
$rest ...
}
}
// rest ...
case { _ #$numargs:lit# $rest ... } => {
return #{
$rest ...
}
}
case { _ $rest ... } => {
return #{
$rest ...
}
}
}
// WITH_DEFAULTS_ (adapted from https://github.com/jlongster/es6-macros)
macro WITH_DEFAULTS_ {
rule { ($args ...) [[ ]] $body $rest ... } => {
($args ...) $body $rest ...
}
rule { [[$args ...]] {macro $macro $macrobody $body ...} $rest ... } => {
WITH_DEFAULTS_ ( ) [[$args ...]] {macro $macro $macrobody DEFAULT_PARAMS_ #0# $args ... $body ...} $rest ...
}
rule { [[$args ...]] {$body ...} $rest ... } => {
WITH_DEFAULTS_ ( ) [[$args ...]] {DEFAULT_PARAMS_ #0# $args ... $body ...} $rest ...
}
rule { ($args ...) [[$param:ident = $val:expr $resta ...]] $body $rest ... } => {
WITH_DEFAULTS_ ($args ... $param) [[$resta ...]] $body $rest ...
}
rule { ($args ...) [[$param:ident $resta ...]] $body $rest ... } => {
WITH_DEFAULTS_ ($args ... $param) [[$resta ...]] $body $rest ...
}
rule { ($args ...) [[, $[...]$param:ident $resta ...]] $body $rest ... } => {
WITH_DEFAULTS_ ($args ...) [[$resta ...]] $body $rest ...
}
rule { ($args ...) [[, $resta ...]] $body $rest ... } => {
WITH_DEFAULTS_ ($args ... ,) [[$resta ...]] $body $rest ...
}
rule { $rest ... } => {
$rest ...
}
}
// PARSE_STATIC_DEFS_ parses the static class method/properties definitions
macro PARSE_STATIC_DEFS_
{
// static method definition
case { _ $method:ident ( $args ... ) { $body ... } $rest ... } => {
return #{
$method: function WITH_DEFAULTS_ [[ $args ... ]] {
$body ...
},
PARSE_STATIC_DEFS_ $rest ...
}
}
// static property definition no initial value (null by default)
case { _ $prop:ident ; $rest ... } => {
return #{
$prop: null,
PARSE_STATIC_DEFS_ $rest ...
}
}
// static property definition with initial value
case { _ $prop:ident = $val:expr ; $rest ... } => {
return #{
$prop: $val,
PARSE_STATIC_DEFS_ $rest ...
}
}
// rest ...
case { _ $rest ... } => {
return #{ $rest ... }
}
}
// PARSE_CLASS_DEFS_ parses the class method/property definitions recursively
macro PARSE_CLASS_DEFS_
{
// class method definition
case { _ $method:ident ( $args ... ) { $body ... } $rest ... __static__ $(:) { $static ... } } => {
// create custom "super" macro, related to current method name
var __super__ = [makeIdent("super", #{ $method })];
return withSyntax($__super__ = __super__) #{
$method: function WITH_DEFAULTS_ [[ $args ... ]] {
// parse current method-related super calls with custom macro
macro $__super__ {
// same super method no args
rule { ( ) } => {
this.$superv(__TO_STR__ $method)
}
// same super method with args
rule { ( $sargs $[...] ) } => {
this.$superv(__TO_STR__ $method, [$sargs $[...]])
}
// another super method no args
rule { . $supermethod:ident ( ) } => {
this.$superv(__TO_STR__ $supermethod)
}
// another super method with args
rule { . $supermethod:ident ( $sargs $[...] ) } => {
this.$superv(__TO_STR__ $supermethod, [$sargs $[...]] )
}
// rest ...
rule { $resta $[...] } => {
$resta $[...]
}
} $body ...
},
PARSE_CLASS_DEFS_ $rest ... __static__:{ $static ... }
}
}
// class property definition no initial value (null by default)
case { _ $prop:ident ; $rest ... __static__ $(:) { $static ... } } => {
return #{
$prop: null,
PARSE_CLASS_DEFS_ $rest ... __static__:{ $static ... }
}
}
// class property definition with initial value
case { _ $prop:ident = $val:expr ; $rest ... __static__ $(:) { $static ... } } => {
return #{
$prop: $val,
PARSE_CLASS_DEFS_ $rest ... __static__:{ $static ... }
}
}
// static method definition
case { _ static $method:ident ( $args ... ) { $body ... } $rest ... __static__ $(:) { $static ... } } => {
return #{
PARSE_CLASS_DEFS_ $rest ... __static__:{ $static ... $method ( $args ... ) { $body ... } }
}
}
// static property definition no initial value
case { _ static $prop:ident ; $rest ... __static__ $(:) { $static ... } } => {
return #{
PARSE_CLASS_DEFS_ $rest ... __static__:{ $static ... $prop ; }
}
}
// static property definition with initial value
case { _ static $prop:ident = $val:expr ; $rest ... __static__ $(:) { $static ... } } => {
return #{
PARSE_CLASS_DEFS_ $rest ... __static__:{ $static ... $prop = $val ; }
}
}
// block static definitions
case { _ static $(:) { $static_defs ... } $rest ... __static__ $(:) { $static ... } } => {
return #{
PARSE_CLASS_DEFS_ $rest ... __static__:{ $static ... $static_defs ... }
}
}
// static empty...
case { _ $rest ... __static__ $(:) { }} => {
return #{ PARSE_CLASS_DEFS_ $rest ... }
}
// static ...
case { _ $rest ... __static__ $(:) { $static ... }} => {
return #{
__static__: {
PARSE_STATIC_DEFS_ $static ...
}, PARSE_CLASS_DEFS_ $rest ...
}
}
// rest...
case { _ $rest ... } => {
return #{ $rest ... }
}
}
// main Class macro here
let Class = macro
{
// Define the Classy.Class (procedural/case) macro here...
// use "let Class = macro .. " to avoid expanding the "Class" identifier inside macro body
// Class definition without any qualifiers
case { $Classy $className:ident { $class_definitions ... } } => {
// get the Classy runtime identifier ( break default hygiene )
letstx $ClassyRuntime = [makeIdent('Classy', #{ $Classy })];
return #{
var $className = $ClassyRuntime.Class(
{ Extends: Object },
{ PARSE_CLASS_DEFS_ $class_definitions ... __static__:{ }}
);
}
}
// Class definition with "extends" qualifier
case { $Classy $className:ident extends $superClass:ident { $class_definitions ... } } => {
// get the Classy runtime identifier ( break default hygiene )
letstx $ClassyRuntime = [makeIdent('Classy', #{ $Classy })];
return #{
var $className = $ClassyRuntime.Class(
{ Extends: $superClass },
{ PARSE_CLASS_DEFS_ $class_definitions ... __static__:{ }}
);
}
}
// Class definition with "implements" qualifier
case { $Classy $className:ident implements $implements:ident (,) ... { $class_definitions ... } } => {
// get the Classy runtime identifier ( break default hygiene )
letstx $ClassyRuntime = [makeIdent('Classy', #{ $Classy })];
return #{
var $className = $ClassyRuntime.Class(
{ Extends: Object, Implements: [ $($implements) (,) ... ] },
{ PARSE_CLASS_DEFS_ $class_definitions ... __static__:{ }}
);
}
}
// full Class definition "all qualifiers"
case { $Classy $className:ident extends $superClass:ident implements $implements:ident (,) ... { $class_definitions ... } } => {
// get the Classy runtime identifier ( break default hygiene )
letstx $ClassyRuntime = [makeIdent('Classy', #{ $Classy })];
return #{
var $className = $ClassyRuntime.Class(
{ Extends: $superClass, Implements: [ $($implements) (,) ... ] },
{ PARSE_CLASS_DEFS_ $class_definitions ... __static__:{ }}
);
}
}
}
// export it
export Class