@canvasengine/compiler 2.0.0-beta.15 → 2.0.0-beta.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/grammar.pegjs +18 -2
- package/package.json +1 -1
- package/tests/compiler.spec.ts +60 -0
package/grammar.pegjs
CHANGED
|
@@ -254,7 +254,7 @@ textElement
|
|
|
254
254
|
}
|
|
255
255
|
|
|
256
256
|
forLoop
|
|
257
|
-
= _ "@for" _ "(" _ variableName:(tupleDestructuring / identifier) _ "of" _ iterable:
|
|
257
|
+
= _ "@for" _ "(" _ variableName:(tupleDestructuring / identifier) _ "of" _ iterable:iterable _ ")" _ "{" _ content:content _ "}" _ {
|
|
258
258
|
return `loop(${iterable}, ${variableName} => ${content})`;
|
|
259
259
|
}
|
|
260
260
|
|
|
@@ -283,7 +283,23 @@ variableName
|
|
|
283
283
|
= [a-zA-Z_][a-zA-Z0-9_]* { return text(); }
|
|
284
284
|
|
|
285
285
|
iterable
|
|
286
|
-
=
|
|
286
|
+
= id:identifier "(" _ args:functionArgs? _ ")" { // Direct function call
|
|
287
|
+
return `${id}(${args || ''})`;
|
|
288
|
+
}
|
|
289
|
+
/ first:identifier "." rest:dotFunctionChain { // Dot notation possibly with function call
|
|
290
|
+
return `${first}.${rest}`;
|
|
291
|
+
}
|
|
292
|
+
/ id:identifier { return id; }
|
|
293
|
+
|
|
294
|
+
dotFunctionChain
|
|
295
|
+
= segment:identifier "(" _ args:functionArgs? _ ")" rest:("." dotFunctionChain)? {
|
|
296
|
+
const restStr = rest ? `.${rest[1]}` : '';
|
|
297
|
+
return `${segment}(${args || ''})${restStr}`;
|
|
298
|
+
}
|
|
299
|
+
/ segment:identifier rest:("." dotFunctionChain)? {
|
|
300
|
+
const restStr = rest ? `.${rest[1]}` : '';
|
|
301
|
+
return `${segment}${restStr}`;
|
|
302
|
+
}
|
|
287
303
|
|
|
288
304
|
condition
|
|
289
305
|
= functionCall
|
package/package.json
CHANGED
package/tests/compiler.spec.ts
CHANGED
|
@@ -316,6 +316,66 @@ describe("Loop", () => {
|
|
|
316
316
|
);
|
|
317
317
|
});
|
|
318
318
|
|
|
319
|
+
test("should compile loop with object", () => {
|
|
320
|
+
const input = `
|
|
321
|
+
@for (sprite of sprites.items) {
|
|
322
|
+
<Sprite />
|
|
323
|
+
}
|
|
324
|
+
`;
|
|
325
|
+
const output = parser.parse(input);
|
|
326
|
+
expect(output.replace(/\s+/g, "")).toBe(
|
|
327
|
+
`loop(sprites.items,sprite=>h(Sprite))`.replace(/\s+/g, "")
|
|
328
|
+
);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
test("should compile loop with deep object", () => {
|
|
332
|
+
const input = `
|
|
333
|
+
@for (sprite of sprites.items.items) {
|
|
334
|
+
<Sprite />
|
|
335
|
+
}
|
|
336
|
+
`;
|
|
337
|
+
const output = parser.parse(input);
|
|
338
|
+
expect(output.replace(/\s+/g, "")).toBe(
|
|
339
|
+
`loop(sprites.items.items,sprite=>h(Sprite))`.replace(/\s+/g, "")
|
|
340
|
+
);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
test("should compile loop with function", () => {
|
|
344
|
+
const input = `
|
|
345
|
+
@for (sprite of sprites()) {
|
|
346
|
+
<Sprite />
|
|
347
|
+
}
|
|
348
|
+
`;
|
|
349
|
+
const output = parser.parse(input);
|
|
350
|
+
expect(output.replace(/\s+/g, "")).toBe(
|
|
351
|
+
`loop(sprites(),sprite=>h(Sprite))`.replace(/\s+/g, "")
|
|
352
|
+
);
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
test("should compile loop with function and params", () => {
|
|
356
|
+
const input = `
|
|
357
|
+
@for (sprite of sprites(x, y)) {
|
|
358
|
+
<Sprite />
|
|
359
|
+
}
|
|
360
|
+
`;
|
|
361
|
+
const output = parser.parse(input);
|
|
362
|
+
expect(output.replace(/\s+/g, "")).toBe(
|
|
363
|
+
`loop(sprites(x,y),sprite=>h(Sprite))`.replace(/\s+/g, "")
|
|
364
|
+
);
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
test("should compile loop with object and function and params", () => {
|
|
368
|
+
const input = `
|
|
369
|
+
@for (sprite of sprites.items(x, y)) {
|
|
370
|
+
<Sprite />
|
|
371
|
+
}
|
|
372
|
+
`;
|
|
373
|
+
const output = parser.parse(input);
|
|
374
|
+
expect(output.replace(/\s+/g, "")).toBe(
|
|
375
|
+
`loop(sprites.items(x,y),sprite=>h(Sprite))`.replace(/\s+/g, "")
|
|
376
|
+
);
|
|
377
|
+
});
|
|
378
|
+
|
|
319
379
|
test("should compile loop with destructuring", () => {
|
|
320
380
|
const input = `
|
|
321
381
|
@for ((sprite, index) of sprites) {
|